Fix notification behavior.

1) Don't add a notification item to the notification bar if the thread the
   message is for is active and visible.

2) Only sound the notification ringtone at 1/4th volume if the thread the
   message is for is active and visible.

3) Auto-clear the notification in the notification bar when a thread becomes
   visible from a screen-off situation.

4) Make notification updates asynchronous.
This commit is contained in:
Moxie Marlinspike
2013-02-03 18:41:34 -08:00
parent 288e2b5572
commit 209711ae40
7 changed files with 150 additions and 118 deletions

View File

@@ -25,6 +25,8 @@ import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
@@ -43,6 +45,7 @@ import org.thoughtcrime.securesms.recipients.RecipientFactory;
import org.thoughtcrime.securesms.recipients.RecipientFormattingException;
import org.thoughtcrime.securesms.recipients.Recipients;
import java.io.IOException;
import java.util.LinkedList;
/**
@@ -56,6 +59,12 @@ public class MessageNotifier {
public static final int NOTIFICATION_ID = 1338;
private volatile static long visibleThread = -1;
public static void setVisibleThread(long threadId) {
visibleThread = threadId;
}
private static Bitmap buildContactPhoto(Recipients recipients) {
Recipient recipient = recipients.getPrimaryRecipient();
@@ -176,12 +185,41 @@ public class MessageNotifier {
manager.notify(NOTIFICATION_ID, builder.build());
}
private static void flashNotification(Context context, NotificationManager manager) {
sendNotification(context, manager, buildPendingIntent(context, null, null),
null, "(1) New Messages", "(1) New Messages", null, true);
private static void sendInThreadNotification(Context context) {
try {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
String ringtone = sp.getString(ApplicationPreferencesActivity.RINGTONE_PREF, null);
if (ringtone == null)
return;
Uri uri = Uri.parse(ringtone);
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);
}
}
public static void updateNotification(Context context, boolean signal) {
private static void updateNotification(Context context, boolean signal) {
NotificationManager manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(NOTIFICATION_ID);
@@ -190,8 +228,9 @@ public class MessageNotifier {
try {
c = DatabaseFactory.getMmsSmsDatabase(context).getUnread();
if ((c == null && signal) || (!c.moveToFirst() && signal)) {flashNotification(context, manager); return;}
else if (c == null || !c.moveToFirst()) return;
if (c == null || !c.moveToFirst()) {
return;
}
Recipients recipients = getMostRecentRecipients(context, c);
String ticker = buildTickerMessage(context, c.getCount(), recipients);
@@ -208,6 +247,19 @@ public class MessageNotifier {
}
}
public static void updateNotification(final Context context) {
updateNotification(context, false);
}
public static void updateNotification(Context context, long threadId) {
if (visibleThread == threadId) {
DatabaseFactory.getThreadDatabase(context).setRead(threadId);
sendInThreadNotification(context);
} else {
updateNotification(context, true);
}
}
private static String[] parseBlinkPattern(String blinkPattern, String blinkPatternCustom) {
if (blinkPattern.equals("custom"))
blinkPattern = blinkPatternCustom;

View File

@@ -37,12 +37,12 @@ public class MmsReceiver {
this.context = context;
}
private void scheduleDownload(NotificationInd pdu, long messageId) {
private void scheduleDownload(NotificationInd pdu, long messageId, long threadId) {
Intent intent = new Intent(SendReceiveService.DOWNLOAD_MMS_ACTION, null, context, SendReceiveService.class);
intent.putExtra("content_location", new String(pdu.getContentLocation()));
intent.putExtra("message_id", messageId);
intent.putExtra("transaction_id", pdu.getTransactionId());
intent.putExtra("thread_id", DatabaseFactory.getMmsDatabase(context).getThreadIdForMessage(messageId));
intent.putExtra("thread_id", threadId);
context.startService(intent);
}
@@ -61,8 +61,11 @@ public class MmsReceiver {
database = DatabaseFactory.getMmsDatabase(context);
long messageId = database.insertMessageReceived((NotificationInd)pdu);
MessageNotifier.updateNotification(context, true);
scheduleDownload((NotificationInd)pdu, messageId);
long threadId = database.getThreadIdForMessage(messageId);
MessageNotifier.updateNotification(context, threadId);
scheduleDownload((NotificationInd)pdu, messageId, threadId);
Log.w("MmsReceiverService", "Inserted received notification...");
}
}

View File

@@ -86,11 +86,13 @@ public class SmsReceiver {
}
}
private void storeSecureMessage(MasterSecret masterSecret, SmsMessage message, String messageBody) {
private long storeSecureMessage(MasterSecret masterSecret, SmsMessage message, String messageBody) {
long messageId = DatabaseFactory.getSmsDatabase(context).insertSecureMessageReceived(message, messageBody);
Log.w("SmsReceiver", "Inserted secure message received: " + messageId);
if (masterSecret != null)
DecryptingQueue.scheduleDecryption(context, masterSecret, messageId, message.getDisplayOriginatingAddress(), messageBody);
return messageId;
}
private long storeStandardMessage(MasterSecret masterSecret, SmsMessage message, String messageBody) {
@@ -99,7 +101,7 @@ public class SmsReceiver {
else return DatabaseFactory.getSmsDatabase(context).insertMessageReceived(message, messageBody);
}
private void storeKeyExchangeMessage(MasterSecret masterSecret, SmsMessage message, String messageBody) {
private long storeKeyExchangeMessage(MasterSecret masterSecret, SmsMessage message, String messageBody) {
if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean(ApplicationPreferencesActivity.AUTO_KEY_EXCHANGE_PREF, true)) {
try {
Recipient recipient = new Recipient(null, message.getDisplayOriginatingAddress(), null, null);
@@ -118,7 +120,7 @@ public class SmsReceiver {
long threadId = DatabaseFactory.getSmsDatabase(context).getThreadIdForMessage(messageId);
processor.processKeyExchangeMessage(keyExchangeMessage, threadId);
return;
return messageId;
}
} catch (InvalidVersionException e) {
Log.w("SmsReceiver", e);
@@ -127,19 +129,17 @@ public class SmsReceiver {
}
}
storeStandardMessage(masterSecret, message, messageBody);
return storeStandardMessage(masterSecret, message, messageBody);
}
private boolean storeMessage(MasterSecret masterSecret, SmsMessage message, String messageBody) {
private long storeMessage(MasterSecret masterSecret, SmsMessage message, String messageBody) {
if (messageBody.startsWith(Prefix.ASYMMETRIC_ENCRYPT)) {
storeSecureMessage(masterSecret, message, messageBody);
return storeSecureMessage(masterSecret, message, messageBody);
} else if (messageBody.startsWith(Prefix.KEY_EXCHANGE)) {
storeKeyExchangeMessage(masterSecret, message, messageBody);
return storeKeyExchangeMessage(masterSecret, message, messageBody);
} else {
storeStandardMessage(masterSecret, message, messageBody);
return storeStandardMessage(masterSecret, message, messageBody);
}
return true;
}
private SmsMessage[] parseMessages(Bundle bundle) {
@@ -158,8 +158,10 @@ public class SmsReceiver {
String message = assembleMessageFragments(messages);
if (message != null) {
storeMessage(masterSecret, messages[0], message);
MessageNotifier.updateNotification(context, true);
long messageId = storeMessage(masterSecret, messages[0], message);
long threadId = DatabaseFactory.getSmsDatabase(context).getThreadIdForMessage(messageId);
MessageNotifier.updateNotification(context, threadId);
}
}