Example #1
0
            /// <summary>
            ///		<see cref="MyWatchFaceEngine"/>のインスタンスが生成された時に実行します。
            /// </summary>
            /// <param name="holder">ディスプレイ表面を表すオブジェクト</param>
            public override void OnCreate(ISurfaceHolder holder)
            {
                // TODO : ここでは主に、以下の処理を行います。
                // ・リソースから画像の読み込み
                // ・Paintなどのグラフィックスオブジェクトを生成
                // ・時刻を格納するオブジェクトの作成
                // ・システムのUI(インジケーターやOK Googleの表示など)の設定

                // システムのUIの配置方法を設定します。
                SetWatchFaceStyle(
                    new WatchFaceStyle.Builder(owner)
                    #region ウォッチフェイスのスタイルの設定

                    // ユーザーからのタップイベントを有効にするかどうか設定します。
                    //   true  : 有効
                    //   false : 無効(デフォルト)
                    //.SetAcceptsTapEvents( true )

                    // 通知が来た時の通知カードの高さを設定します。
                    //   WatchFaceStyle.PeekModeShort    : 通知カードをウィンドウの下部に小さく表示します。(デフォルト)
                    //   WatchFaceStyle.PeekModeVariable : 通知カードをウィンドウの全面に表示します。
                    .SetCardPeekMode(WatchFaceStyle.PeekModeShort)

                    // 通知カードの背景の表示方法を設定します。
                    //   WatchFaceStyle.BackgroundVisibilityInterruptive : 電話の着信など一部の通知のみ、背景を用事します。(デフォルト)
                    //   WatchFaceStyle.BackgroundVisibilityPersistent   : 通知カードの種類にかかわらず、その背景を常に表示します。
                    .SetBackgroundVisibility(WatchFaceStyle.BackgroundVisibilityInterruptive)

                    // アンビエントモード時に通知カードを表示するかどうかを設定します。
                    //   WatchFaceStyle.AmbientPeekModeVisible : 通知カードを表示します。(デフォルト)
                    //   WatchFaceStyle.AmbientPeekModeHidden  : 通知カードを表示しません。
                    //.SetAmbientPeekMode( WatchFaceStyle.AmbientPeekModeHidden )

                    // システムUIのデジタル時計を表示するするかどうかを設定します。(使用している例として、デフォルトで用意されている「シンプル」があります。)
                    //   true  : 表示
                    //   false : 非表示(デフォルト)
                    .SetShowSystemUiTime(false)

                    // ステータスアイコンなどに背景を付けるかどうかを設定します。
                    //   デフォルト                               : ステータスアイコンなどに背景を表示しません。
                    //   WatchFaceStyle.ProtectStatusBar        : ステータスアイコンに背景を表示します。
                    //   WatchFaceStyle.ProtectHotwordIndicator : 「OK Google」に背景を表示します。
                    //   WatchFaceStyle.ProtectWholeScreen      : ウォッチフェイスの背景を少し暗めにします。
                    // ※パラメーターは論理和で組み合わせることができます。
                    //.SetViewProtectionMode( WatchFaceStyle.ProtectStatusBar | WatchFaceStyle.ProtectHotwordIndicator )

                    // 通知カードを透明にするかどうかを設定します。
                    //   WatchFaceStyle.PeekOpacityModeOpaque      : 不透明(デフォルト)
                    //   WatchFaceStyle.PeekOpacityModeTranslucent : 透明
                    //.SetPeekOpacityMode( WatchFaceStyle.PeekOpacityModeTranslucent )

                    // ステータスアイコンや「OK Google」の位置を設定します。
                    //   GravityFlags.Top | GravityFlags.Left   : 左上(角形のデフォルト)
                    //   GravityFlags.Top | GravityFlags.Center : 上部の中央(丸形のデフォルト)
                    // 注 : GravityFlagsは列挙体なので、int型にキャストします。
                    //.SetStatusBarGravity( ( int )( GravityFlags.Top | GravityFlags.Center ) )
                    //.SetHotwordIndicatorGravity( ( int )( GravityFlags.Top | GravityFlags.Center ) )

                    #endregion
                    // 設定したスタイル情報をビルドします。このメソッドは最後に呼び出します。
                    .Build()
                    );
                // ベースクラスのOnCreateメソッドを実行します。
                base.OnCreate(holder);

                #region 最新のAndroid SDKにおける、Android.Content.Res.Resources.GetColorメソッドについて

                /*
                 *      Android.Content.Res.Resources.GetColorメソッドは、Android SDK Level 23以降で非推奨(Deprecated)となっています。
                 *      代わりの方法として、Android.Support.V4.Content.ContextCompat.GetColorメソッドを使用します。
                 *
                 *      [CanvasWatchFaceServiceオブジェクト].Resources.GetColor( Resource.Color.[リソース名] );
                 *      ↓
                 *      ContextCompat.GetColor( [CanvasWatchFaceServiceオブジェクト], Resource.Color.[リソース名] );
                 *      ※CanvasWatchFaceServiceクラスはContextクラスを継承しています。
                 *
                 *      なお、ContextCompat.GetColorの戻り値はColor型でなく、ARGB値を格納したint型となります。
                 *      Chronoir_net.Chronica.WatchfaceExtension.WatchfaceUtility.ConvertARGBToColor( int )メソッドで、Color型に変換することができます。
                 */
                #endregion

                var resources = owner.Resources;

                // 背景用のグラフィックスオブジェクトを生成します。
                backgroundPaint = new Paint();
                // リソースから背景色を読み込みます。
                backgroundPaint.Color = WatchfaceUtility.ConvertARGBToColor(ContextCompat.GetColor(owner, Resource.Color.background));

                // 時針用のPaintグラフィックスオブジェクトを生成します。
                var hourHandPaint = new Paint();
                hourHandPaint.Color = WatchfaceUtility.ConvertARGBToColor(ContextCompat.GetColor(owner, Resource.Color.analog_hands));
                // 時針の幅を設定します。
                hourHandPaint.StrokeWidth = resources.GetDimension(Resource.Dimension.hour_hand_stroke);
                // アンチエイリアスを有効にします。
                hourHandPaint.AntiAlias = true;
                // 線端の形は丸形を指定します。
                hourHandPaint.StrokeCap = Paint.Cap.Round;
                // 時針オブジェクトを初期化します。
                hourHand = new HourAnalogHandStroke(hourHandPaint);

                // 分針用のPaintグラフィックスオブジェクトを生成します。
                var minuteHandPaint = new Paint();
                minuteHandPaint.Color       = WatchfaceUtility.ConvertARGBToColor(ContextCompat.GetColor(owner, Resource.Color.analog_hands));
                minuteHandPaint.StrokeWidth = resources.GetDimension(Resource.Dimension.minute_hand_stroke);
                minuteHandPaint.AntiAlias   = true;
                minuteHandPaint.StrokeCap   = Paint.Cap.Round;
                minuteHand = new MinuteAnalogHandStroke(minuteHandPaint);

                // 秒針用のPaintグラフィックスオブジェクトを生成します。
                var secondHandPaint = new Paint();
                secondHandPaint.Color       = WatchfaceUtility.ConvertARGBToColor(ContextCompat.GetColor(owner, Resource.Color.analog_sec_hand));
                secondHandPaint.StrokeWidth = resources.GetDimension(Resource.Dimension.second_hand_stroke);
                secondHandPaint.AntiAlias   = true;
                secondHandPaint.StrokeCap   = Paint.Cap.Round;
                secondHand = new SecondAnalogHandStroke(secondHandPaint);

                // 時刻を格納するオブジェクトを生成します。
                // Time ( Android )
                //nowTime = new Time();
                // Calendar ( Java )
                nowTime = Java.Util.Calendar.GetInstance(Java.Util.TimeZone.Default);
                // DateTime ( C# )
                // DateTime構造体は値型なので、オブジェクトの生成はは不要です。
            }
            /// <summary>
            ///		Invoked when a <see cref="MyWatchFaceEngine"/> object is created.
            /// </summary>
            /// <param name="holder">Object representing display surface</param>
            public override void OnCreate(ISurfaceHolder holder)
            {
                // TODO: Here, mainly perform the following processing.
                // * Loads images from resources
                // * Generates graphics objects such as Paint etc.
                // * Generates an object that stores time
                // * Sets the system UI (status icon, notification card and "Ok Google" etc.)

                // Sets the display method of the system UI.
                SetWatchFaceStyle(
                    new WatchFaceStyle.Builder(owner)
                    #region Setting watch face style

                    // Sets whether to enable tap event from user.
                    //   true  : Enabled
                    //   false : Disabled(default)
                    //.SetAcceptsTapEvents( true )

                    // Sets the height of the notification card when receiving the notification.
                    //   WatchFaceStyle.PeekModeShort    : Displays the notification card small at the bottom of the window.(default)
                    //   WatchFaceStyle.PeekModeVariable : Displays the notification card on the front of the window.
                    .SetCardPeekMode(WatchFaceStyle.PeekModeShort)

                    // Sets the display method of the notification card background.
                    //   WatchFaceStyle.BackgroundVisibilityInterruptive : Displays the notification card's background, only when some important notification such as incoming calls.(default)
                    //   WatchFaceStyle.BackgroundVisibilityPersistent   : Displays the notification card's background regardless of the notification type.
                    .SetBackgroundVisibility(WatchFaceStyle.BackgroundVisibilityInterruptive)

                    // Sets whether or not to display notification cards in ambient mode.
                    //   WatchFaceStyle.AmbientPeekModeVisible : Visible(default)
                    //   WatchFaceStyle.AmbientPeekModeHidden  : Hidden
                    //.SetAmbientPeekMode( WatchFaceStyle.AmbientPeekModeHidden )

                    // Sets whether to display the digital clock of the system UI. (As an example of use, there is "Simple" which is provided by default.)
                    //   true  : Visible
                    //   false : Hidden(default)
                    .SetShowSystemUiTime(false)

                    // Sets whether to add a background to the status icon etc.
                    //   Default                                : Nothing
                    //   WatchFaceStyle.ProtectStatusBar        : Displays the background on the status icon.
                    //   WatchFaceStyle.ProtectHotwordIndicator : Displays the background on the "OK Google".
                    //   WatchFaceStyle.ProtectWholeScreen      : Makes the background of the watch face a little darker.
                    // Note: Parameters can be combined by logical OR.
                    //.SetViewProtectionMode( WatchFaceStyle.ProtectStatusBar | WatchFaceStyle.ProtectHotwordIndicator )

                    // Sets whether the notification card is transparent.
                    //   WatchFaceStyle.PeekOpacityModeOpaque      : Opacity(default)
                    //   WatchFaceStyle.PeekOpacityModeTranslucent : Transparent
                    //.SetPeekOpacityMode( WatchFaceStyle.PeekOpacityModeTranslucent )

                    // Sets the status icon and "OK Google" position.
                    //   GravityFlags.Top | GravityFlags.Left   : Left-Top(Square form's default)
                    //   GravityFlags.Top | GravityFlags.Center : Center-Top(Round form's default)
                    // Note: Parameters can be combined by logical OR.
                    // Note: Casts GravityFlags's value to int type, because it is an enumeration.
                    //.SetStatusBarGravity( ( int )( GravityFlags.Top | GravityFlags.Center ) )
                    //.SetHotwordIndicatorGravity( ( int )( GravityFlags.Top | GravityFlags.Center ) )

                    #endregion
                    // Builds the set style information. This method is called last.
                    .Build()
                    );

                base.OnCreate(holder);

                #region About method that getting color values from resources in the latest Android SDK

                /*
                 *      Android.Content.Res.Resources.GetColor method is deprecated as Android SDK Level 23 or later.
                 *      As an alternative, use the Android.Support.V4.Content.ContextCompat.GetColor method.
                 *
                 *      [CanvasWatchFaceService object].Resources.GetColor( Resource.Color.[Resource name] );
                 *      ↓
                 *      ContextCompat.GetColor( [CanvasWatchFaceService object], Resource.Color.[Resource name] );
                 *
                 *      Note: CanvasWatchFaceService class inherits Context class.
                 *
                 *      However, the return value of ContextCompat.GetColor method, because it is not a color type
                 *      but an int type storing an ARGB value, cannot be directly set to [Paint object].Color.
                 *      -> Using the Chronoir_net.Chronica.WatchfaceExtension.WatchfaceUtility.ConvertARGBToColor( int ) method,
                 *         can convert the integer value storing the ARGB value to Color type.
                 */
                #endregion

                var resources = owner.Resources;

                // Creates a graphics object for the background.
                backgroundPaint = new Paint();
                // Reads background color from resource.
                backgroundPaint.Color = WatchfaceUtility.ConvertARGBToColor(ContextCompat.GetColor(owner, Resource.Color.background));

                // Creates a graphics object for the hour hand.
                var hourHandPaint = new Paint();
                hourHandPaint.Color = WatchfaceUtility.ConvertARGBToColor(ContextCompat.GetColor(owner, Resource.Color.analog_hands));
                // Sets the hour hand width.
                hourHandPaint.StrokeWidth = resources.GetDimension(Resource.Dimension.hour_hand_stroke);
                // Enable anti-aliasing.
                hourHandPaint.AntiAlias = true;
                // Specifies the shape of the line end in a round shape.
                hourHandPaint.StrokeCap = Paint.Cap.Round;
                // Creates the hour hand object.
                hourHand = new HourAnalogHandStroke(hourHandPaint);

                // Creates a graphics object for the minute hand.
                var minuteHandPaint = new Paint();
                minuteHandPaint.Color       = WatchfaceUtility.ConvertARGBToColor(ContextCompat.GetColor(owner, Resource.Color.analog_hands));
                minuteHandPaint.StrokeWidth = resources.GetDimension(Resource.Dimension.minute_hand_stroke);
                minuteHandPaint.AntiAlias   = true;
                minuteHandPaint.StrokeCap   = Paint.Cap.Round;
                minuteHand = new MinuteAnalogHandStroke(minuteHandPaint);

                // Creates a graphics object for the second hand.
                var secondHandPaint = new Paint();
                secondHandPaint.Color       = WatchfaceUtility.ConvertARGBToColor(ContextCompat.GetColor(owner, Resource.Color.analog_sec_hand));
                secondHandPaint.StrokeWidth = resources.GetDimension(Resource.Dimension.second_hand_stroke);
                secondHandPaint.AntiAlias   = true;
                secondHandPaint.StrokeCap   = Paint.Cap.Round;
                secondHand = new SecondAnalogHandStroke(secondHandPaint);

                // Creates an object that stores time.
                // Time ( Android )
                //nowTime = new Time();
                // Calendar ( Java )
                nowTime = Java.Util.Calendar.GetInstance(Java.Util.TimeZone.Default);
                // DateTime ( C# )
                // This object need not create, because DateTime structure is value type.
            }