Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.15 KB | None | 0 0
  1. public class MessagingIntentService extends IntentService {
  2.     private static final String TAG = "MessagingIntentService";
  3.     public static final String ACTION_REPLY =
  4.             "com.example.android.wearable.wear.wearnotifications.handlers.action.REPLY";
  5.     public static final String EXTRA_REPLY =
  6.             "com.example.android.wearable.wear.wearnotifications.handlers.extra.REPLY";
  7.     public MessagingIntentService() {
  8.         super("MessagingIntentService");
  9.     }
  10.     @Override
  11.     protected void onHandleIntent(Intent intent) {
  12.         Log.d(TAG, "onHandleIntent(): " + intent);
  13.         if (intent != null) {
  14.             final String action = intent.getAction();
  15.             if (ACTION_REPLY.equals(action)) {
  16.                 handleActionReply(getMessage(intent));
  17.             }
  18.         }
  19.     }
  20.     private void handleActionReply(CharSequence replyCharSequence) {
  21.         Log.d(TAG, "handleActionReply(): " + replyCharSequence);
  22.         if (replyCharSequence != null) {
  23.             NotificationCompat.Builder notificationCompatBuilder =
  24.                     GlobalNotificationBuilder.getNotificationCompatBuilderInstance();
  25.             if (notificationCompatBuilder == null) {
  26.                 notificationCompatBuilder = recreateBuilderWithMessagingStyle();
  27.             }
  28.             Notification notification = notificationCompatBuilder.build();
  29.             MessagingStyle messagingStyle =
  30.                     NotificationCompat.MessagingStyle.extractMessagingStyleFromNotification(
  31.                             notification);
  32.             messagingStyle.addMessage(replyCharSequence, System.currentTimeMillis(), (Person) null);
  33.             notification = notificationCompatBuilder.setStyle(messagingStyle).build();
  34.             NotificationManagerCompat notificationManagerCompat =
  35.                     NotificationManagerCompat.from(getApplicationContext());
  36.             notificationManagerCompat.notify(StandaloneMainActivity.NOTIFICATION_ID, notification);
  37.         }
  38.     }
  39.     private CharSequence getMessage(Intent intent) {
  40.         Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
  41.         if (remoteInput != null) {
  42.             return remoteInput.getCharSequence(EXTRA_REPLY);
  43.         }
  44.         return null;
  45.     }
  46.     private NotificationCompat.Builder recreateBuilderWithMessagingStyle() {
  47.         MockDatabase.MessagingStyleCommsAppData messagingStyleCommsAppData =
  48.                 MockDatabase.getMessagingStyleData(getApplicationContext());
  49.         String notificationChannelId = messagingStyleCommsAppData.getChannelId();
  50.         String contentTitle = messagingStyleCommsAppData.getContentTitle();
  51.         MessagingStyle messagingStyle =
  52.                 new NotificationCompat.MessagingStyle(messagingStyleCommsAppData.getMe())
  53.                         .setConversationTitle(contentTitle);
  54.         for (MessagingStyle.Message message : messagingStyleCommsAppData.getMessages()) {
  55.             messagingStyle.addMessage(message);
  56.         }
  57.         messagingStyle.setGroupConversation(messagingStyleCommsAppData.isGroupConversation());
  58.         Intent notifyIntent = new Intent(this, MessagingMainActivity.class);
  59.         PendingIntent mainPendingIntent =
  60.                 PendingIntent.getActivity(this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
  61.         String replyLabel = getString(R.string.reply_label);
  62.         RemoteInput remoteInput =
  63.                 new RemoteInput.Builder(MessagingIntentService.EXTRA_REPLY)
  64.                         .setLabel(replyLabel)
  65.                         .setChoices(messagingStyleCommsAppData.getReplyChoicesBasedOnLastMessage())
  66.                         .build();
  67.         Intent replyIntent = new Intent(this, MessagingIntentService.class);
  68.         replyIntent.setAction(MessagingIntentService.ACTION_REPLY);
  69.         PendingIntent replyActionPendingIntent = PendingIntent.getService(this, 0, replyIntent, 0);
  70.         final NotificationCompat.Action.WearableExtender inlineActionForWear2 =
  71.                 new NotificationCompat.Action.WearableExtender()
  72.                         .setHintDisplayActionInline(true)
  73.                         .setHintLaunchesActivity(false);
  74.         NotificationCompat.Action replyAction =
  75.                 new NotificationCompat.Action.Builder(
  76.                                 R.drawable.ic_reply_white_18dp,
  77.                                 replyLabel,
  78.                                 replyActionPendingIntent)
  79.                         .addRemoteInput(remoteInput)
  80.                         .setShowsUserInterface(false)
  81.                         .setAllowGeneratedReplies(true)
  82.                         .setSemanticAction(Action.SEMANTIC_ACTION_REPLY)
  83.                         .extend(inlineActionForWear2)
  84.                         .build();
  85.         NotificationCompat.Builder notificationCompatBuilder =
  86.                 new NotificationCompat.Builder(getApplicationContext(), notificationChannelId);
  87.         GlobalNotificationBuilder.setNotificationCompatBuilderInstance(notificationCompatBuilder);
  88.         notificationCompatBuilder
  89.                 .setStyle(messagingStyle)
  90.                 .setContentTitle(contentTitle)
  91.                 .setContentText(messagingStyleCommsAppData.getContentText())
  92.                 .setSmallIcon(R.drawable.ic_launcher)
  93.                 .setLargeIcon(
  94.                         BitmapFactory.decodeResource(
  95.                                 getResources(), R.drawable.ic_person_black_48dp))
  96.                 .setContentIntent(mainPendingIntent)
  97.                 .setDefaults(NotificationCompat.DEFAULT_ALL)
  98.                 .setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
  99.                 .setSubText(Integer.toString(messagingStyleCommsAppData.getNumberOfNewMessages()))
  100.                 .addAction(replyAction)
  101.                 .setCategory(Notification.CATEGORY_MESSAGE)
  102.                 .setPriority(messagingStyleCommsAppData.getPriority())
  103.                 .setVisibility(messagingStyleCommsAppData.getChannelLockscreenVisibility());
  104.         for (Person person : messagingStyleCommsAppData.getParticipants()) {
  105.             notificationCompatBuilder.addPerson(person.getUri());
  106.         }
  107.         return notificationCompatBuilder;
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement