/// <summary> /// <see cref="MyWatchFaceEngine"/>クラスの新しいインスタンスを生成します。 /// </summary> /// <param name="owner"><see cref="CanvasWatchFaceService"/>クラスを継承したオブジェクトの参照</param> public MyWatchFaceEngine(CanvasWatchFaceService owner) : base(owner) { // CanvasWatchFaceServiceクラスを継承したオブジェクトの参照をセットします。 this.owner = owner; // 時刻を更新した時の処理を構成します。 updateTimeHandler = new Handler( message => { //#if DEBUG // if( Log.IsLoggable( logTag, LogPriority.Info ) ) { // Log.Info( logTag, $"Updating timer: Message = {message.What}" ); // } //#endif // Whatプロパティでメッセージを判別します。 switch (message.What) { case MessageUpdateTime: // TODO : 時刻の更新のメッセージの時の処理を入れます。 // ウォッチフェイスを再描画します。 Invalidate(); // タイマーを動作させるかどうかを判別します。 if (ShouldTimerBeRunning) { /* * Javaでは、System.currentTimeMillisメソッドで世界協定時(ミリ秒)を取得します。 * 一方C#では、DateTime.UtcNow.Ticksプロパティで世界協定時(100ナノ秒)取得し、 * TimeSpan.TicksPerMillisecondフィールドで割って、ミリ秒の値を求めます。 */ long timeMillseconds = DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond; // delayMs = 更新間隔 - ( 現在時刻(ミリ秒) % 更新間隔) -> 更新間隔との差 long delayMilliseconds = InteractiveUpdateRateMilliseconds - (timeMillseconds % InteractiveUpdateRateMilliseconds); // UpdateTimeHandlerにメッセージをセットします。 // SendEmptyMessageDelayedメソッドは指定した時間後にメッセージを発行します。 updateTimeHandler.SendEmptyMessageDelayed(MessageUpdateTime, delayMilliseconds); } break; } } ); // TimeZoneReceiverのインスタンスを生成します。 timeZoneReceiver = new ActionExecutableBroadcastReceiver( intent => { // TODO : ブロードキャストされた Intent.ActionTimezoneChanged のIntentオブジェクトを受け取った時に実行する処理を入れます。 // IntentからタイムゾーンIDを取得して、Timeオブジェクトのタイムゾーンに設定し、現在時刻を取得します。 // intent.GetStringExtra( "time-zone" )の戻り値はタイムゾーンのIDです。 // Time ( Android ) //nowTime.Clear( intent.GetStringExtra( "time-zone" ) ); //nowTime.SetToNow(); // Calendar ( Java ) nowTime = Java.Util.Calendar.GetInstance(Java.Util.TimeZone.GetTimeZone(intent.GetStringExtra("time-zone"))); // DateTime ( C# ) //nowTime = DateTime.Now; }, // Intentフィルターに「ActionTimezoneChanged」を指定します。 Intent.ActionTimezoneChanged ); }
/// <summary> /// Creates a new instance of <see cref="MyWatchFaceEngine"/> class. /// </summary> /// <param name="owner">Reference to a <see cref="CanvasWatchFaceService"/> object</param> public MyWatchFaceEngine(CanvasWatchFaceService owner) : base(owner) { // Sets a reference to an object that inherits CanvasWatchFaceService class. this.owner = owner; // Configures processing when updating time. updateTimeHandler = new Handler( message => { #if DEBUG if (Log.IsLoggable(logTag, LogPriority.Info)) { Log.Info(logTag, $"Updating timer: Message = {message.What}"); } #endif // Determines the message ID with the What property. switch (message.What) { case MessageUpdateTime: // TODO: Writes here, the processing at the time of the time update message. // Redraws the watch face. Invalidate(); // Determinea whether to activate the timer. if (ShouldTimerBeRunning) { /* * In Java, gets Universal Time Coordinated (milli-seconds) by System.currentTimeMillis method. * Meanwhile, in C#, gets Universal Time Coordinated (100 nano-seconds) by DateTime.UtcNow.Ticks property, * and divides by TimeSpan.TicksPerMillisecond field to calculate the value in milliseconds. */ long timeMillseconds = DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond; // delayMilliseconds = [Update interval] - ([Current time( milliseconds )] % [Update interval] ) -> Difference from update interval long delayMilliseconds = InteractiveUpdateRateMilliseconds - (timeMillseconds % InteractiveUpdateRateMilliseconds); // Sets the message in UpdateTimeHandler. updateTimeHandler.SendEmptyMessageDelayed(MessageUpdateTime, delayMilliseconds); } break; } } ); // Creates an instance of ActionExecutableBroadcastReceiver. timeZoneReceiver = new ActionExecutableBroadcastReceiver( intent => { // TODO: Writes here, process to be executed when receiving Intent object of broadcasted with Intent.ActionTimezoneChanged. // Gets the time zone ID from the Intent, set it as the time zone of the Time object, and get the current time. // Time ( Android ) //nowTime.Clear( intent.GetStringExtra( "time-zone" ) ); //nowTime.SetToNow(); // Calendar ( Java ) nowTime = Java.Util.Calendar.GetInstance(Java.Util.TimeZone.GetTimeZone(intent.GetStringExtra("time-zone"))); // DateTime ( C# ) //nowTime = DateTime.Now; }, // Specifies "android.intent.action.TIMEZONE_CHANGED" for the Intent filter. Intent.ActionTimezoneChanged ); }
/// <summary> /// ウォッチフェイスの表示・非表示が切り替わった時に実行します。 /// </summary> /// <param name="visible">ウォッチフェイスの表示・非表示</param> public override void OnVisibilityChanged(bool visible) { // ベースクラスのOnVisibilityChangedメソッドを実行します。 base.OnVisibilityChanged(visible); #if DEBUG if (Log.IsLoggable(logTag, LogPriority.Info)) { Log.Info(logTag, $"{nameof( OnVisibilityChanged )}: Visible = {visible}"); } #endif // ウォッチフェイスの表示・非表示を判別します。 if (visible) { if (timeZoneReceiver == null) { timeZoneReceiver = new ActionExecutableBroadcastReceiver( intent => { // Time ( Android ) //nowTime.Clear( intent.GetStringExtra( "time-zone" ) ); //nowTime.SetToNow(); // Calendar ( Java ) nowTime = Java.Util.Calendar.GetInstance(Java.Util.TimeZone.GetTimeZone(intent.GetStringExtra("time-zone"))); // DateTime ( C# ) //nowTime = DateTime.Now; }, Intent.ActionTimezoneChanged ); } // タイムゾーン用のレシーバーを登録します。 timeZoneReceiver.IsRegistered = true; // ウォッチフェイスが非表示の時にタイムゾーンが変化した場合のために、タイムゾーンを更新します。 // Time ( Android ) //nowTime.Clear( Java.Util.TimeZone.Default.ID ); //nowTime.SetToNow(); // Calendar ( Java ) nowTime = Java.Util.Calendar.GetInstance(Java.Util.TimeZone.Default); // DateTime ( C# ) //nowTime = DateTime.Now; } else { // タイムゾーン用のレシーバーを登録解除します。 timeZoneReceiver.IsRegistered = false; } // タイマーの動作を更新します。 UpdateTimer(); }
/// <summary> /// Invoked when the watch face is switched between display / non-display. /// </summary> /// <param name="visible">Display / non-display of watch face</param> public override void OnVisibilityChanged(bool visible) { base.OnVisibilityChanged(visible); #if DEBUG if (Log.IsLoggable(logTag, LogPriority.Info)) { Log.Info(logTag, $"{nameof( OnVisibilityChanged )}: Visible = {visible}"); } #endif // Determines whether the watch face is displayed or not. if (visible) { if (timeZoneReceiver == null) { timeZoneReceiver = new ActionExecutableBroadcastReceiver( intent => { // Time ( Android ) //nowTime.Clear( intent.GetStringExtra( "time-zone" ) ); //nowTime.SetToNow(); // Calendar ( Java ) nowTime = Java.Util.Calendar.GetInstance(Java.Util.TimeZone.GetTimeZone(intent.GetStringExtra("time-zone"))); // DateTime ( C# ) //nowTime = DateTime.Now; }, Intent.ActionTimezoneChanged ); } // Register the BroadcastReciever for the time zone. timeZoneReceiver.IsRegistered = true; // Updates time in case the time zone changes when the watch face is hidden. // Time ( Android ) //nowTime.Clear( Java.Util.TimeZone.Default.ID ); //nowTime.SetToNow(); // Calendar ( Java ) nowTime = Java.Util.Calendar.GetInstance(Java.Util.TimeZone.Default); // DateTime ( C# ) //nowTime = DateTime.Now; } else { // Unregister the BroadcastReciever for the time zone. timeZoneReceiver.IsRegistered = false; } UpdateTimer(); }