示例#1
0
  private static NotificationState constructNotificationState(
      Context context, MasterSecret masterSecret, Cursor cursor) {
    NotificationState notificationState = new NotificationState();
    MessageRecord record;
    MmsSmsDatabase.Reader reader;

    if (masterSecret == null) reader = DatabaseFactory.getMmsSmsDatabase(context).readerFor(cursor);
    else reader = DatabaseFactory.getMmsSmsDatabase(context).readerFor(cursor, masterSecret);

    while ((record = reader.getNext()) != null) {
      Recipient recipient = record.getIndividualRecipient();
      Recipients recipients = record.getRecipients();
      long threadId = record.getThreadId();
      CharSequence body = record.getDisplayBody();
      Recipients threadRecipients = null;
      ListenableFutureTask<SlideDeck> slideDeck = null;
      long timestamp;

      if (SMSSecurePreferences.showSentTime(context)) timestamp = record.getDateSent();
      else timestamp = record.getDateReceived();

      if (threadId != -1) {
        threadRecipients =
            DatabaseFactory.getThreadDatabase(context).getRecipientsForThreadId(threadId);
      }

      if (SmsDatabase.Types.isDecryptInProgressType(record.getType())
          || !record.getBody().isPlaintext()) {
        body = SpanUtil.italic(context.getString(R.string.MessageNotifier_encrypted_message));
      } else if (record.isMms() && TextUtils.isEmpty(body)) {
        body = SpanUtil.italic(context.getString(R.string.MessageNotifier_media_message));
        slideDeck = ((MediaMmsMessageRecord) record).getSlideDeckFuture();
      } else if (record.isMms() && !record.isMmsNotification()) {
        String message = context.getString(R.string.MessageNotifier_media_message_with_text, body);
        int italicLength = message.length() - body.length();
        body = SpanUtil.italic(message, italicLength);
        slideDeck = ((MediaMmsMessageRecord) record).getSlideDeckFuture();
      }

      if (threadRecipients == null || !threadRecipients.isMuted()) {
        notificationState.addNotification(
            new NotificationItem(
                recipient, recipients, threadRecipients, threadId, body, timestamp, slideDeck));
      }
    }

    reader.close();
    return notificationState;
  }
示例#2
0
  public static void updateNotification(Context context, MasterSecret masterSecret, long threadId) {
    Recipients recipients =
        DatabaseFactory.getThreadDatabase(context).getRecipientsForThreadId(threadId);

    if (!SMSSecurePreferences.isNotificationsEnabled(context)
        || (recipients != null && recipients.isMuted())) {
      return;
    }

    if (visibleThread == threadId) {
      ThreadDatabase threads = DatabaseFactory.getThreadDatabase(context);
      threads.setRead(threadId);
      sendInThreadNotification(context, threads.getRecipientsForThreadId(threadId));
    } else {
      updateNotification(context, masterSecret, true, 0);
    }
  }
示例#3
0
 @TargetApi(VERSION_CODES.LOLLIPOP)
 private void setRippleColor(Recipients recipients) {
   if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
     ((RippleDrawable) (getBackground()).mutate())
         .setColor(
             ColorStateList.valueOf(recipients.getColor().toConversationColor(getContext())));
   }
 }
示例#4
0
  private static void sendInThreadNotification(Context context, Recipients recipients) {
    try {
      if (!SMSSecurePreferences.isInThreadNotifications(context)) {
        return;
      }

      Uri uri = recipients != null ? recipients.getRingtone() : null;

      if (uri == null) {
        String ringtone = SMSSecurePreferences.getNotificationRingtone(context);

        if (ringtone == null) {
          Log.w(TAG, "ringtone preference was null.");
          return;
        } else {
          uri = Uri.parse(ringtone);
        }
      }

      if (uri == null) {
        Log.w(
            TAG,
            "couldn't parse ringtone uri " + SMSSecurePreferences.getNotificationRingtone(context));
        return;
      }

      MediaPlayer player = new MediaPlayer();
      player.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
      player.setDataSource(context, uri);
      player.setLooping(false);
      player.setVolume(0.25f, 0.25f);
      player.prepare();

      final AudioManager audioManager =
          ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE));

      audioManager.requestAudioFocus(
          null, AudioManager.STREAM_NOTIFICATION, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);

      player.setOnCompletionListener(
          new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
              audioManager.abandonAudioFocus(null);
            }
          });

      player.start();
    } catch (IOException ioe) {
      Log.w("MessageNotifier", ioe);
    }
  }
示例#5
0
  public static void notifyMessageDeliveryFailed(
      Context context, Recipients recipients, long threadId) {
    if (visibleThread == threadId) {
      sendInThreadNotification(context, recipients);
    } else {
      Intent intent = new Intent(context, ConversationActivity.class);
      intent.putExtra(ConversationActivity.RECIPIENTS_EXTRA, recipients.getIds());
      intent.putExtra(ConversationActivity.THREAD_ID_EXTRA, threadId);
      intent.setData((Uri.parse("custom://" + System.currentTimeMillis())));

      FailedNotificationBuilder builder =
          new FailedNotificationBuilder(
              context, SMSSecurePreferences.getNotificationPrivacy(context), intent);
      ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE))
          .notify((int) threadId, builder.build());
    }
  }