コード例 #1
0
  /**
   * Called when an action mode item is clicked.
   *
   * @param mode The action mode containing the item that is clicked.
   * @param item The item that is clicked.
   * @return Returns true if the method handles the item clicked.
   */
  @Override
  public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
    switch (item.getItemId()) {
      case R.id.mark_read_unread_button:
        for (int i = 0; i < adapter.getItemCount(); i++) {
          if (adapter.isItemChecked(i)) {
            Message[] smses = adapter.getItem(i).getMessages();
            for (Message message : smses) {
              message.setUnread(
                  item.getTitle()
                      .equals(getResources().getString(R.string.conversations_action_mark_unread)));
              database.insertMessage(message);
            }
          }
        }
        adapter.refresh();
        mode.finish();
        return true;
      case R.id.delete_button:
        List<Long> databaseIds = new ArrayList<>();
        for (int i = 0; i < adapter.getItemCount(); i++) {
          if (adapter.isItemChecked(i)) {
            for (Message message : adapter.getItem(i).getMessages()) {
              if (message.getDatabaseId() != null) {
                databaseIds.add(message.getDatabaseId());
              }
            }
          }
        }

        Long[] databaseIdsArray = new Long[databaseIds.size()];
        databaseIds.toArray(databaseIdsArray);
        deleteMessages(databaseIdsArray);
        mode.finish();
        return true;
      default:
        return false;
    }
  }
コード例 #2
0
  public void showNotifications(List<String> contacts) {
    if (!(ActivityMonitor.getInstance().getCurrentActivity() instanceof ConversationsActivity)) {
      Conversation[] conversations =
          Database.getInstance(applicationContext).getConversations(preferences.getDid());
      for (Conversation conversation : conversations) {
        if (!conversation.isUnread()
            || !contacts.contains(conversation.getContact())
            || (ActivityMonitor.getInstance().getCurrentActivity() instanceof ConversationActivity
                && ((ConversationActivity) ActivityMonitor.getInstance().getCurrentActivity())
                    .getContact()
                    .equals(conversation.getContact()))) {
          continue;
        }

        String smsContact = Utils.getContactName(applicationContext, conversation.getContact());
        if (smsContact == null) {
          smsContact = Utils.getFormattedPhoneNumber(conversation.getContact());
        }

        String allSmses = "";
        String mostRecentSms = "";
        boolean initial = true;
        for (Message message : conversation.getMessages()) {
          if (message.getType() == Message.Type.INCOMING && message.isUnread()) {
            if (initial) {
              allSmses = message.getText();
              mostRecentSms = message.getText();
              initial = false;
            } else {
              allSmses = message.getText() + "\n" + allSmses;
            }
          } else {
            break;
          }
        }

        NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(applicationContext);

        notificationBuilder.setContentTitle(smsContact);
        notificationBuilder.setContentText(mostRecentSms);
        notificationBuilder.setSmallIcon(R.drawable.ic_chat_white_24dp);
        notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
        notificationBuilder.setSound(
            Uri.parse(Preferences.getInstance(applicationContext).getNotificationSound()));
        notificationBuilder.setLights(0xFFAA0000, 1000, 5000);
        if (Preferences.getInstance(applicationContext).getNotificationVibrateEnabled()) {
          notificationBuilder.setVibrate(new long[] {0, 250, 250, 250});
        } else {
          notificationBuilder.setVibrate(new long[] {0});
        }
        notificationBuilder.setColor(0xFFAA0000);
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(allSmses));

        Bitmap largeIconBitmap;
        try {
          largeIconBitmap =
              MediaStore.Images.Media.getBitmap(
                  applicationContext.getContentResolver(),
                  Uri.parse(
                      Utils.getContactPhotoUri(applicationContext, conversation.getContact())));
          largeIconBitmap = Bitmap.createScaledBitmap(largeIconBitmap, 256, 256, false);
          largeIconBitmap = Utils.applyCircularMask(largeIconBitmap);
          notificationBuilder.setLargeIcon(largeIconBitmap);
        } catch (Exception ignored) {

        }

        Intent intent = new Intent(applicationContext, ConversationActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(
            applicationContext.getString(R.string.conversation_extra_contact),
            conversation.getContact());
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(applicationContext);
        stackBuilder.addParentStack(ConversationActivity.class);
        stackBuilder.addNextIntent(intent);
        notificationBuilder.setContentIntent(
            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT));

        Intent replyIntent = new Intent(applicationContext, ConversationQuickReplyActivity.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
          replyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
        } else {
          //noinspection deprecation
          replyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        }
        replyIntent.putExtra(
            applicationContext.getString(R.string.conversation_extra_contact),
            conversation.getContact());
        PendingIntent replyPendingIntent =
            PendingIntent.getActivity(
                applicationContext, 0, replyIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        NotificationCompat.Action.Builder replyAction =
            new NotificationCompat.Action.Builder(
                R.drawable.ic_reply_white_24dp,
                applicationContext.getString(R.string.notifications_button_reply),
                replyPendingIntent);
        notificationBuilder.addAction(replyAction.build());

        Intent markAsReadIntent = new Intent(applicationContext, MarkAsReadReceiver.class);
        markAsReadIntent.putExtra(
            applicationContext.getString(R.string.conversation_extra_contact),
            conversation.getContact());
        PendingIntent markAsReadPendingIntent =
            PendingIntent.getBroadcast(
                applicationContext, 0, markAsReadIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        NotificationCompat.Action.Builder markAsReadAction =
            new NotificationCompat.Action.Builder(
                R.drawable.ic_drafts_white_24dp,
                applicationContext.getString(R.string.notifications_button_mark_read),
                markAsReadPendingIntent);
        notificationBuilder.addAction(markAsReadAction.build());

        int id;
        if (notificationIds.get(conversation.getContact()) != null) {
          id = notificationIds.get(conversation.getContact());
        } else {
          id = notificationIdCount++;
          notificationIds.put(conversation.getContact(), id);
        }
        NotificationManager notificationManager =
            (NotificationManager) applicationContext.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(id, notificationBuilder.build());
      }
    }
  }