public override void OnDataChanged (DataEventBuffer eventBuffer)
		{
			var events = FreezableUtils.FreezeIterable (eventBuffer);
			eventBuffer.Close ();

			var google_api_client = new GoogleApiClientBuilder (this)
				.AddApi (WearableClass.API)
				.Build ();

			var connectionResult = google_api_client.BlockingConnect (Constants.CONNECT_TIMEOUT_MS,
				                       TimeUnit.Milliseconds);

			if (!connectionResult.IsSuccess) {
				Log.Error (TAG, "QuizListenerService failed to connect to GoogleApiClient.");
				return;
			}

			foreach (var ev in events) {
				var e = ((Java.Lang.Object)ev).JavaCast<IDataEvent> ();
				if (e.Type == DataEvent.TypeChanged) {
					var dataItem = e.DataItem;
					var dataMap = DataMapItem.FromDataItem (dataItem).DataMap;
					if (dataMap.GetBoolean (Constants.QUESTION_WAS_ANSWERED)
					    || dataMap.GetBoolean (Constants.QUESTION_WAS_DELETED)) {
						continue;
					}

					string question = dataMap.GetString (Constants.QUESTION);
					int questionIndex = dataMap.GetInt (Constants.QUESTION_INDEX);
					int questionNum = questionIndex + 1;
					string[] answers = dataMap.GetStringArray (Constants.ANSWERS);
					int correctAnswerIndex = dataMap.GetInt (Constants.CORRECT_ANSWER_INDEX);
					Intent deleteOperation = new Intent (this, typeof(DeleteQuestionService));
					deleteOperation.SetData (dataItem.Uri);
					PendingIntent deleteIntent = PendingIntent.GetService (this, 0,
						                             deleteOperation, PendingIntentFlags.UpdateCurrent);
					//first page of notification contains question as Big Text.
					var bigTextStyle = new Notification.BigTextStyle ()
						.SetBigContentTitle (GetString (Resource.String.question, questionNum))
						.BigText (question);
					var builder = new Notification.Builder (this)
						.SetStyle (bigTextStyle)
						.SetSmallIcon (Resource.Drawable.ic_launcher)
						.SetLocalOnly (true)
						.SetDeleteIntent (deleteIntent);

					//add answers as actions
					var wearableOptions = new Notification.WearableExtender ();
					for (int i = 0; i < answers.Length; i++) {
						Notification answerPage = new Notification.Builder (this)
							.SetContentTitle (question)
							.SetContentText (answers [i])
							.Extend (new Notification.WearableExtender ()
								.SetContentAction (i))
							.Build ();

						bool correct = (i == correctAnswerIndex);
						var updateOperation = new Intent (this, typeof(UpdateQuestionService));
						//Give each intent a unique action.
						updateOperation.SetAction ("question_" + questionIndex + "_answer_" + i);
						updateOperation.SetData (dataItem.Uri);
						updateOperation.PutExtra (UpdateQuestionService.EXTRA_QUESTION_INDEX, questionIndex);
						updateOperation.PutExtra (UpdateQuestionService.EXTRA_QUESTION_CORRECT, correct);
						var updateIntent = PendingIntent.GetService (this, 0, updateOperation,
							                   PendingIntentFlags.UpdateCurrent);
						Notification.Action action = new Notification.Action.Builder (
							                             (int)question_num_to_drawable_id.Get (i), (string)null, updateIntent)
							.Build ();
						wearableOptions.AddAction (action).AddPage (answerPage);
					}
					builder.Extend (wearableOptions);
					Notification notification = builder.Build ();
					((NotificationManager)GetSystemService (NotificationService))
						.Notify (questionIndex, notification);
				} else if (e.Type == DataEvent.TypeDeleted) {
					Android.Net.Uri uri = e.DataItem.Uri;
					//URIs are in the form of "/question/0", "/question/1" etc.
					//We use the question index as the notification id.
					int notificationId = Java.Lang.Integer.ParseInt (uri.LastPathSegment);
					((NotificationManager)GetSystemService (NotificationService))
						.Cancel (notificationId);
				}

				((NotificationManager)GetSystemService (NotificationService))
					.Cancel (QUIZ_REPORT_NOTIF_ID);
			}
			google_api_client.Disconnect ();
		}
        /// <summary>
        /// Puts a local notification to show calendar card
        /// </summary>
        /// <param name="dataItem">Data item.</param>
        private void UpdateNotificationForDataItem(IDataItem dataItem)
        {
            if (Log.IsLoggable(Constants.TAG, LogPriority.Verbose))
            {
                Log.Verbose(Constants.TAG, "Updating notification for IDataItem");
            }

            DataMapItem mapDataItem = DataMapItem.FromDataItem(dataItem);
            DataMap     data        = mapDataItem.DataMap;

            String description = data.GetString(Constants.DESCRIPTION);

            if (TextUtils.IsEmpty(description))
            {
                description = "";
            }
            else
            {
                // Add a space between the description and the time of the event
                description += " ";
            }

            String contentText;

            if (data.GetBoolean(Constants.ALL_DAY))
            {
                contentText = GetString(Resource.String.desc_all_day, description);
            }
            else
            {
                String startTime = DateFormat.GetTimeFormat(this).Format(new Date(data.GetLong(Constants.BEGIN)));
                String endTime   = DateFormat.GetTimeFormat(this).Format(new Date(data.GetLong(Constants.END)));
                contentText = GetString(Resource.String.desc_time_period, description, startTime, endTime);
            }

            Intent deleteOperation = new Intent(this, typeof(DeleteService));
            // Use a unique identifier for the delete action.

            String deleteAction = "action_delete" + dataItem.Uri.ToString() + sNotificationId;

            deleteOperation.SetAction(deleteAction);
            deleteOperation.SetData(dataItem.Uri);
            PendingIntent deleteIntent       = PendingIntent.GetService(this, 0, deleteOperation, PendingIntentFlags.OneShot);
            PendingIntent silentDeleteIntent = PendingIntent.GetService(this, 1, deleteOperation.PutExtra(Constants.EXTRA_SILENT, true), PendingIntentFlags.OneShot);

            Notification.Builder notificationBuilder = new Notification.Builder(this)
                                                       .SetContentTitle(data.GetString(Constants.TITLE))
                                                       .SetContentText(contentText)
                                                       .SetSmallIcon(Resource.Drawable.ic_launcher)
                                                       .AddAction(Resource.Drawable.ic_menu_delete, GetText(Resource.String.delete), deleteIntent)
                                                       .SetDeleteIntent(silentDeleteIntent)
                                                       .SetLocalOnly(true)
                                                       .SetPriority((int)NotificationPriority.Min);

            // Set the event owner's profile picture as the notification background
            Asset asset = data.GetAsset(Constants.PROFILE_PIC);

            if (asset != null)
            {
                if (mGoogleApiClient.IsConnected)
                {
                    IDataApiGetFdForAssetResult assetFdResult = WearableClass.DataApi.GetFdForAsset(mGoogleApiClient, asset).Await().JavaCast <IDataApiGetFdForAssetResult>();
                    if (assetFdResult.Status.IsSuccess)
                    {
                        Bitmap profilePic = BitmapFactory.DecodeStream(assetFdResult.InputStream);
                        notificationBuilder.Extend(new Notification.WearableExtender().SetBackground(profilePic));
                    }
                    else if (Log.IsLoggable(Constants.TAG, LogPriority.Debug))
                    {
                        Log.Debug(Constants.TAG, "asset fetch failed with StatusCode: " + assetFdResult.Status.StatusCode);
                    }
                }
                else
                {
                    Log.Error(Constants.TAG, "Failed to set notification background - Client disconnected from Google Play Services");
                }

                Notification card = notificationBuilder.Build();

                (GetSystemService(Context.NotificationService).JavaCast <NotificationManager>()).Notify(sNotificationId, card);

                sNotificationIdByDataItemUri.Add(dataItem.Uri, sNotificationId++);
            }
        }
		/// <summary>
		/// Puts a local notification to show calendar card
		/// </summary>
		/// <param name="dataItem">Data item.</param>
		private async Task UpdateNotificationForDataItem(IDataItem dataItem)
		{
			if (Log.IsLoggable (Constants.TAG, LogPriority.Verbose))
				Log.Verbose (Constants.TAG, "Updating notification for IDataItem");

			DataMapItem mapDataItem = DataMapItem.FromDataItem (dataItem);
			DataMap data = mapDataItem.DataMap;

			String description = data.GetString (Constants.DESCRIPTION);
			if (TextUtils.IsEmpty (description)) {
				description = "";
			} else {
				// Add a space between the description and the time of the event
				description += " ";
			}

			String contentText;
			if (data.GetBoolean(Constants.ALL_DAY)) {
				contentText = GetString (Resource.String.desc_all_day, description);
			}
			else 
			{
				String startTime = DateFormat.GetTimeFormat (this).Format (new Date (data.GetLong (Constants.BEGIN)));
				String endTime = DateFormat.GetTimeFormat (this).Format (new Date (data.GetLong (Constants.END)));
				contentText = GetString (Resource.String.desc_time_period, description, startTime, endTime);
			}

			Intent deleteOperation = new Intent (this, typeof(DeleteService));
			// Use a unique identifier for the delete action.

			String deleteAction = "action_delete" + dataItem.Uri.ToString () + sNotificationId;
			deleteOperation.SetAction (deleteAction);
			deleteOperation.SetData (dataItem.Uri);
			PendingIntent deleteIntent = PendingIntent.GetService (this, 0, deleteOperation, PendingIntentFlags.OneShot);
			PendingIntent silentDeleteIntent = PendingIntent.GetService (this, 1, deleteOperation.PutExtra (Constants.EXTRA_SILENT, true), PendingIntentFlags.OneShot);

			Notification.Builder notificationBuilder = new Notification.Builder (this)
				.SetContentTitle (data.GetString (Constants.TITLE))
				.SetContentText (contentText)
				.SetSmallIcon (Resource.Drawable.ic_launcher)
				.AddAction (Resource.Drawable.ic_menu_delete, GetText (Resource.String.delete), deleteIntent)
				.SetDeleteIntent (silentDeleteIntent)
				.SetLocalOnly (true)
				.SetPriority ((int)NotificationPriority.Min);

			// Set the event owner's profile picture as the notification background
			Asset asset = data.GetAsset (Constants.PROFILE_PIC);
			if (asset != null) {
				if (mGoogleApiClient.IsConnected) {
                    var assetFdResult = await WearableClass.DataApi.GetFdForAssetAsync (mGoogleApiClient, asset);
					if (assetFdResult.Status.IsSuccess) {
						Bitmap profilePic = BitmapFactory.DecodeStream (assetFdResult.InputStream);
						notificationBuilder.Extend (new Notification.WearableExtender ().SetBackground (profilePic));
					} else if (Log.IsLoggable (Constants.TAG, LogPriority.Debug)) {
						Log.Debug (Constants.TAG, "asset fetch failed with StatusCode: " + assetFdResult.Status.StatusCode);
					}
				} else {
					Log.Error (Constants.TAG, "Failed to set notification background - Client disconnected from Google Play Services");
				}

				Notification card = notificationBuilder.Build ();

				(GetSystemService (Context.NotificationService).JavaCast<NotificationManager>()).Notify (sNotificationId, card);

				sNotificationIdByDataItemUri.Add (dataItem.Uri, sNotificationId++);
			}
		}
Exemplo n.º 4
0
        public void ProcessRxData(int airdropPhase)
        {
            int _ap = airdropPhase;

            if (_ap == 3)
            {
                ARModel.GetInstance.AirdropPhase = 3;
                var valuesForActivity = new Bundle();
                valuesForActivity.Equals("message");
                string groupkey = "group_key";
                var    intent   = new Intent(currentActivity, typeof(MainActivity));
                intent.PutExtras(valuesForActivity);
                var pendingIntent = PendingIntent.GetActivity(currentActivity, 0, intent, PendingIntentFlags.OneShot);
                var builder       = new Notification.Builder(currentActivity)
                                    .SetAutoCancel(true)
                                    .SetContentIntent(pendingIntent)
                                    .SetContentTitle("Welcome to CHADCS")
                                                                              //.SetSmallIcon(Resource.Drawable.greeen)
                                                                              //.SetLargeIcon(Resource.Drawable.red)
                                    .SetContentText("AirDrop Light is Green") //message is the one recieved from the notification
                                    .SetGroup(groupkey)                       //creates groups
                                    .SetShowWhen(true)
                                    .SetLocalOnly(false)
                                    .SetVisibility(NotificationVisibility.Public)
                                    .SetPriority((int)NotificationPriority.High);
                //                .SetDefaults(NotificationDefaults.Sound | NotificationDefaults.Vibrate);

                //intent for voice input or text selection
                var wear_intent         = new Intent(Intent.ActionView);
                var wear_pending_intent = PendingIntent.GetActivity(currentActivity, 0, wear_intent, 0);



                // Create the reply action and add the remote input
                var action = new Notification.Action.Builder(Resource.Drawable.icon, "Ok Light", wear_pending_intent)
                             .Build();

                //add it to the notification builder
                Notification notification = builder.Extend(new Notification.WearableExtender()).Build();

                int notification_id = 1;
                if (notification_id < 9)
                {
                    notification_id += 1;
                }
                else
                {
                    notification_id = 0;
                }

                //var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
                notifyManager.Notify(notification_id + 2, notification);
            }
            else if (_ap == 2)
            {
                //ARModel.GetInstance.AirdropPhase = 3;
                var valuesForActivity = new Bundle();
                valuesForActivity.Equals("message");
                string groupkey = "group_key";
                var    intent   = new Intent(currentActivity, typeof(MainActivity));
                intent.PutExtras(valuesForActivity);
                var pendingIntent = PendingIntent.GetActivity(currentActivity, 0, intent, PendingIntentFlags.OneShot);
                var builder       = new Notification.Builder(currentActivity)
                                    .SetAutoCancel(true)
                                    .SetContentIntent(pendingIntent)
                                    .SetContentTitle("Welcome to CHADCS")
                                                                               //.SetSmallIcon(Resource.Drawable.yellow)
                                                                               //.SetLargeIcon(Resource.Drawable.red)
                                    .SetContentText("AirDrop Light is Yellow") //message is the one recieved from the notification
                                    .SetGroup(groupkey)                        //creates groups
                                    .SetShowWhen(true)
                                    .SetLocalOnly(false)
                                    .SetVisibility(NotificationVisibility.Public)
                                    .SetPriority((int)NotificationPriority.High);
                //                .SetDefaults(NotificationDefaults.Sound | NotificationDefaults.Vibrate);

                //intent for voice input or text selection
                var wear_intent         = new Intent(Intent.ActionView);
                var wear_pending_intent = PendingIntent.GetActivity(currentActivity, 0, wear_intent, 0);



                // Create the reply action and add the remote input
                var action = new Notification.Action.Builder(Resource.Drawable.icon, "Ok Light", wear_pending_intent)
                             .Build();

                //add it to the notification builder
                Notification notification = builder.Extend(new Notification.WearableExtender()).Build();

                int notification_id = 1;
                if (notification_id < 9)
                {
                    notification_id += 1;
                }
                else
                {
                    notification_id = 0;
                }

                //var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
                notifyManager.Notify(notification_id + 2, notification);
            }

            else if (_ap == 1)
            {
                //ARModel.GetInstance.AirdropPhase = 3;
                var valuesForActivity = new Bundle();
                valuesForActivity.Equals("message");
                string groupkey = "group_key";
                var    intent   = new Intent(currentActivity, typeof(MainActivity));
                intent.PutExtras(valuesForActivity);
                var pendingIntent = PendingIntent.GetActivity(currentActivity, 0, intent, PendingIntentFlags.OneShot);
                var builder       = new Notification.Builder(currentActivity)
                                    .SetAutoCancel(true)
                                    .SetContentIntent(pendingIntent)
                                    .SetContentTitle("Welcome to CHADCS")
                                                                            //.SetSmallIcon(Resource.Drawable.red)
                                                                            //.SetLargeIcon(Resource.Drawable.red)
                                    .SetContentText("AirDrop Light is Red") //message is the one recieved from the notification
                                    .SetGroup(groupkey)                     //creates groups
                                    .SetShowWhen(true)
                                    .SetLocalOnly(false)
                                    .SetVisibility(NotificationVisibility.Public)
                                    .SetPriority((int)NotificationPriority.High);
                //                .SetDefaults(NotificationDefaults.Sound | NotificationDefaults.Vibrate);

                //intent for voice input or text selection
                var wear_intent         = new Intent(Intent.ActionView);
                var wear_pending_intent = PendingIntent.GetActivity(currentActivity, 0, wear_intent, 0);



                // Create the reply action and add the remote input
                var action = new Notification.Action.Builder(Resource.Drawable.icon, "Ok Light", wear_pending_intent)
                             .Build();

                //add it to the notification builder
                Notification notification = builder.Extend(new Notification.WearableExtender()).Build();

                int notification_id = 1;
                if (notification_id < 9)
                {
                    notification_id += 1;
                }
                else
                {
                    notification_id = 0;
                }

                //var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
                notifyManager.Notify(notification_id + 2, notification);
            }
        }
Exemplo n.º 5
0
        public override void OnDataChanged(DataEventBuffer eventBuffer)
        {
            var events = FreezableUtils.FreezeIterable(eventBuffer);

            eventBuffer.Close();

            var google_api_client = new GoogleApiClientBuilder(this)
                                    .AddApi(WearableClass.API)
                                    .Build();

            var connectionResult = google_api_client.BlockingConnect(Constants.CONNECT_TIMEOUT_MS,
                                                                     TimeUnit.Milliseconds);

            if (!connectionResult.IsSuccess)
            {
                Log.Error(TAG, "QuizListenerService failed to connect to GoogleApiClient.");
                return;
            }

            foreach (var ev in events)
            {
                var e = ((Java.Lang.Object)ev).JavaCast <IDataEvent> ();
                if (e.Type == DataEvent.TypeChanged)
                {
                    var dataItem = e.DataItem;
                    var dataMap  = DataMapItem.FromDataItem(dataItem).DataMap;
                    if (dataMap.GetBoolean(Constants.QUESTION_WAS_ANSWERED) ||
                        dataMap.GetBoolean(Constants.QUESTION_WAS_DELETED))
                    {
                        continue;
                    }

                    string   question           = dataMap.GetString(Constants.QUESTION);
                    int      questionIndex      = dataMap.GetInt(Constants.QUESTION_INDEX);
                    int      questionNum        = questionIndex + 1;
                    string[] answers            = dataMap.GetStringArray(Constants.ANSWERS);
                    int      correctAnswerIndex = dataMap.GetInt(Constants.CORRECT_ANSWER_INDEX);
                    Intent   deleteOperation    = new Intent(this, typeof(DeleteQuestionService));
                    deleteOperation.SetData(dataItem.Uri);
                    PendingIntent deleteIntent = PendingIntent.GetService(this, 0,
                                                                          deleteOperation, PendingIntentFlags.UpdateCurrent);
                    //first page of notification contains question as Big Text.
                    var bigTextStyle = new Notification.BigTextStyle()
                                       .SetBigContentTitle(GetString(Resource.String.question, questionNum))
                                       .BigText(question);
                    var builder = new Notification.Builder(this)
                                  .SetStyle(bigTextStyle)
                                  .SetSmallIcon(Resource.Drawable.ic_launcher)
                                  .SetLocalOnly(true)
                                  .SetDeleteIntent(deleteIntent);

                    //add answers as actions
                    var wearableOptions = new Notification.WearableExtender();
                    for (int i = 0; i < answers.Length; i++)
                    {
                        Notification answerPage = new Notification.Builder(this)
                                                  .SetContentTitle(question)
                                                  .SetContentText(answers [i])
                                                  .Extend(new Notification.WearableExtender()
                                                          .SetContentAction(i))
                                                  .Build();

                        bool correct         = (i == correctAnswerIndex);
                        var  updateOperation = new Intent(this, typeof(UpdateQuestionService));
                        //Give each intent a unique action.
                        updateOperation.SetAction("question_" + questionIndex + "_answer_" + i);
                        updateOperation.SetData(dataItem.Uri);
                        updateOperation.PutExtra(UpdateQuestionService.EXTRA_QUESTION_INDEX, questionIndex);
                        updateOperation.PutExtra(UpdateQuestionService.EXTRA_QUESTION_CORRECT, correct);
                        var updateIntent = PendingIntent.GetService(this, 0, updateOperation,
                                                                    PendingIntentFlags.UpdateCurrent);
                        Notification.Action action = new Notification.Action.Builder(
                            (int)question_num_to_drawable_id.Get(i), (string)null, updateIntent)
                                                     .Build();
                        wearableOptions.AddAction(action).AddPage(answerPage);
                    }
                    builder.Extend(wearableOptions);
                    Notification notification = builder.Build();
                    ((NotificationManager)GetSystemService(NotificationService))
                    .Notify(questionIndex, notification);
                }
                else if (e.Type == DataEvent.TypeDeleted)
                {
                    Android.Net.Uri uri = e.DataItem.Uri;
                    //URIs are in the form of "/question/0", "/question/1" etc.
                    //We use the question index as the notification id.
                    int notificationId = Java.Lang.Integer.ParseInt(uri.LastPathSegment);
                    ((NotificationManager)GetSystemService(NotificationService))
                    .Cancel(notificationId);
                }

                ((NotificationManager)GetSystemService(NotificationService))
                .Cancel(QUIZ_REPORT_NOTIF_ID);
            }
            google_api_client.Disconnect();
        }