protected override void OnHandleIntent(Intent intent) { google_api_client.BlockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.Milliseconds); if (Log.IsLoggable(TAG, LogPriority.Verbose)) { Log.Verbose(TAG, "FindPhoneService.OnHandleEvent"); } if (google_api_client.IsConnected) { bool alarmOn = false; if (intent.Action == ACTION_TOGGLE_ALARM) { var result = WearableClass.DataApi.GetDataItems(google_api_client).Await().JavaCast <DataItemBuffer>(); if (result.Status.IsSuccess) { if (result.Count == 1) { alarmOn = DataMap.FromByteArray((result.Get(0).JavaCast <IDataItem>()).GetData()).GetBoolean(FIELD_ALARM_ON, false); } else { Log.Error(TAG, "Unexpected number of DataItems found.\n" + "\tExpected: 1\n" + "\tActual: " + result.Count); } } else if (Log.IsLoggable(TAG, LogPriority.Debug)) { Log.Debug(TAG, "OnHandleIntent: failed to get current alarm state"); } result.Close(); alarmOn = !alarmOn; string notificationText = alarmOn ? GetString(Resource.String.turn_alarm_on) : GetString(Resource.String.turn_alarm_off); MainActivity.UpdateNotification(this, notificationText); } var putDataMapRequest = PutDataMapRequest.Create(PATH_SOUND_ALARM); putDataMapRequest.DataMap.PutBoolean(FIELD_ALARM_ON, alarmOn); WearableClass.DataApi.PutDataItem(google_api_client, putDataMapRequest.AsPutDataRequest()).Await(); } else { Log.Error(TAG, "Failed to toggle alarm on phone - Client disconnected from Google Play Services"); } google_api_client.Disconnect(); }
public override void OnDataChanged(DataEventBuffer buffer) { if (Log.IsLoggable(TAG, LogPriority.Debug)) { Log.Debug(TAG, "OnDataChanged: " + buffer + " for " + PackageName); } for (int i = 0; i < buffer.Count; i++) { IDataEvent e = Android.Runtime.Extensions.JavaCast <IDataEvent>(buffer.Get(i)); if (e.Type == DataEvent.TypeDeleted) { Log.Info(TAG, e + " deleted"); } else if (e.Type == DataEvent.TypeChanged) { bool alarmOn = (bool)DataMap.FromByteArray(e.DataItem.GetData()).Get(FIELD_ALARM_ON); if (!alarmOn) { orig_volume = audio_manager.GetStreamVolume(Android.Media.Stream.Alarm); media_player.Reset(); max_volume = 0; audio_manager.SetStreamVolume(Android.Media.Stream.Alarm, max_volume, 0); media_player.SetAudioStreamType(Android.Media.Stream.Alarm); try { media_player.SetDataSource(ApplicationContext, alarm_sound); media_player.Prepare(); } catch (IOException ex) { Log.Error(TAG, "Failed to prepare media player to play alarm.", ex); } media_player.Start(); } else { audio_manager.SetStreamVolume(Android.Media.Stream.Alarm, orig_volume, 0); if (media_player.IsPlaying) { media_player.Stop(); } } } } buffer.Close(); }
/// <summary> /// Listens for DataItems added/deleted from the geofence service running on the companion /// </summary> public override void OnDataChanged(DataEventBuffer dataEvents) { if (Log.IsLoggable(Constants.TAG, LogPriority.Debug)) { Log.Debug(Constants.TAG, "OnDataChanged: " + dataEvents + " for " + PackageName); } for (int i = 0; i < dataEvents.Count; i++) { var dEvent = dataEvents.Get(i).JavaCast <IDataEvent> (); if (dEvent.Type == DataEvent.TypeDeleted) { CancelNotificationForDataItem(dEvent.DataItem); } else if (dEvent.Type == DataEvent.TypeChanged) { // The user has entered a geofence - post a notification! String geofenceId = DataMap.FromByteArray(dEvent.DataItem.GetData()).GetString(Constants.KEY_GEOFENCE_ID); PostNotificationForGeofenceId(geofenceId, dEvent.DataItem.Uri); } } }
public override void OnMessageReceived(IMessageEvent messageEvent) { string path = messageEvent.Path; if (path.Equals(Constants.QUIZ_EXITED_PATH)) { ((NotificationManager)GetSystemService(NotificationService)).CancelAll(); } if (path.Equals(Constants.QUIZ_ENDED_PATH) || path.Equals(Constants.QUIZ_EXITED_PATH)) { var dataMap = DataMap.FromByteArray(messageEvent.GetData()); int numCorrect = dataMap.GetInt(Constants.NUM_CORRECT); int numIncorrect = dataMap.GetInt(Constants.NUM_INCORRECT); int numSkipped = dataMap.GetInt(Constants.NUM_SKIPPED); var builder = new Notification.Builder(this) .SetContentTitle(GetString(Resource.String.quiz_report)) .SetSmallIcon(Resource.Drawable.ic_launcher) .SetLocalOnly(true); var quizReportText = new SpannableStringBuilder(); AppendColored(quizReportText, numCorrect.ToString(), Resource.Color.dark_green); quizReportText.Append(" " + GetString(Resource.String.correct) + "\n"); AppendColored(quizReportText, numIncorrect.ToString(), Resource.Color.dark_red); quizReportText.Append(" " + GetString(Resource.String.incorrect) + "\n"); AppendColored(quizReportText, numSkipped.ToString(), Resource.Color.dark_yellow); quizReportText.Append(" " + GetString(Resource.String.skipped) + "\n"); builder.SetContentText(quizReportText); if (!path.Equals(Constants.QUIZ_EXITED_PATH)) { builder.AddAction(Resource.Drawable.ic_launcher, GetString(Resource.String.reset_quiz), GetResetQuizPendingIntent()); } ((NotificationManager)GetSystemService(NotificationService)) .Notify(QUIZ_REPORT_NOTIF_ID, builder.Build()); } }
public override void OnMessageReceived(IMessageEvent messageEvent) { if (!messageEvent.Path.Equals(DigitalWatchFaceUtil.PathWithFeature)) { return; } var rawData = messageEvent.GetData(); // It's allowed that the message carries only some of the keys used in the config DataItem // and skips the ones that we don't want to change. var configKeysToOverwrite = DataMap.FromByteArray(rawData); if (Log.IsLoggable(Tag, LogPriority.Debug)) { Log.Debug(Tag, "Received watch face config message: " + configKeysToOverwrite); } if (googleApiClient == null) { googleApiClient = new GoogleApiClientBuilder(this) .AddConnectionCallbacks(this) .AddOnConnectionFailedListener(this) .AddApi(WearableClass.Api) .Build(); } if (!googleApiClient.IsConnected) { var connectionResult = googleApiClient.BlockingConnect(30, TimeUnit.Seconds); if (!connectionResult.IsSuccess) { Log.Error(Tag, "Failed to connect to GoogleApiClient."); return; } } DigitalWatchFaceUtil.OverwriteKeysInConfigDataMap(googleApiClient, configKeysToOverwrite); }