Refactored and improved how multiple notifications are handled based on Tim's recommendations

pull/14/head
Sam Bosley 13 years ago
parent f297a3475c
commit b3b38583be

@ -273,7 +273,7 @@ public class Notifications extends BroadcastReceiver {
* @param ringTimes number of times to ring (-1 = nonstop) * @param ringTimes number of times to ring (-1 = nonstop)
*/ */
public static void showNotification(int notificationId, Intent intent, int type, String title, public static void showNotification(int notificationId, Intent intent, int type, String title,
final String text, int ringTimes) { String text, int ringTimes) {
Context context = ContextManager.getContext(); Context context = ContextManager.getContext();
if(notificationManager == null) if(notificationManager == null)
@ -304,7 +304,7 @@ public class Notifications extends BroadcastReceiver {
} }
// create notification object // create notification object
Notification notification = new Notification( final Notification notification = new Notification(
icon, text, System.currentTimeMillis()); icon, text, System.currentTimeMillis());
notification.setLatestEventInfo(context, notification.setLatestEventInfo(context,
title, title,
@ -321,7 +321,7 @@ public class Notifications extends BroadcastReceiver {
else else
notification.defaults = Notification.DEFAULT_LIGHTS; notification.defaults = Notification.DEFAULT_LIGHTS;
final AudioManager audioManager = (AudioManager)context.getSystemService( AudioManager audioManager = (AudioManager)context.getSystemService(
Context.AUDIO_SERVICE); Context.AUDIO_SERVICE);
// detect call state // detect call state
@ -333,9 +333,9 @@ public class Notifications extends BroadcastReceiver {
// if multi-ring is activated and the setting p_rmd_maxvolume allows it, set up the flags for insistent // if multi-ring is activated and the setting p_rmd_maxvolume allows it, set up the flags for insistent
// notification, and increase the volume to full volume, so the user // notification, and increase the volume to full volume, so the user
// will actually pay attention to the alarm // will actually pay attention to the alarm
final boolean maxOutVolumeForMultipleRingReminders = Preferences.getBoolean(R.string.p_rmd_maxvolume, true); boolean maxOutVolumeForMultipleRingReminders = Preferences.getBoolean(R.string.p_rmd_maxvolume, true);
// remember it to set it to the old value after the alarm // remember it to set it to the old value after the alarm
final int previousAlarmVolume = audioManager.getStreamVolume(AudioManager.STREAM_ALARM); int previousAlarmVolume = audioManager.getStreamVolume(AudioManager.STREAM_ALARM);
if (ringTimes != 1 && (type != ReminderService.TYPE_RANDOM)) { if (ringTimes != 1 && (type != ReminderService.TYPE_RANDOM)) {
notification.audioStreamType = AudioManager.STREAM_ALARM; notification.audioStreamType = AudioManager.STREAM_ALARM;
if (maxOutVolumeForMultipleRingReminders) { if (maxOutVolumeForMultipleRingReminders) {
@ -393,34 +393,56 @@ public class Notifications extends BroadcastReceiver {
if(Constants.DEBUG) if(Constants.DEBUG)
Log.w("Astrid", "Logging notification: " + text); //$NON-NLS-1$ //$NON-NLS-2$ Log.w("Astrid", "Logging notification: " + text); //$NON-NLS-1$ //$NON-NLS-2$
for(int i = 0; i < Math.max(ringTimes, 1); i++) { singleThreadVoicePool.submit(new NotificationRunnable(ringTimes, notificationId, notification, voiceReminder,
notificationManager.notify(notificationId, notification); maxOutVolumeForMultipleRingReminders, audioManager, previousAlarmVolume, text));
}
private static class NotificationRunnable implements Runnable {
private final int ringTimes;
private final int notificationId;
private final Notification notification;
private final boolean voiceReminder;
private final boolean maxOutVolumeForMultipleRingReminders;
private final AudioManager audioManager;
private final int previousAlarmVolume;
private final String text;
public NotificationRunnable(int ringTimes, int notificationId, Notification notification, boolean voiceReminder,
boolean maxOutVolume, AudioManager audioManager, int previousAlarmVolume, String text) {
this.ringTimes = ringTimes;
this.notificationId = notificationId;
this.notification = notification;
this.voiceReminder = voiceReminder;
this.maxOutVolumeForMultipleRingReminders = maxOutVolume;
this.audioManager = audioManager;
this.previousAlarmVolume = previousAlarmVolume;
this.text = text;
} }
Flags.set(Flags.REFRESH); // Forces a reload when app launches
@Override
final boolean finalVoiceReminder = voiceReminder; public void run() {
for(int i = 0; i < Math.max(ringTimes, 1); i++) {
if ((finalVoiceReminder || maxOutVolumeForMultipleRingReminders) && soundIntervalOk) { notificationManager.notify(notificationId, notification);
singleThreadVoicePool.submit(new Runnable() { AndroidUtilities.sleepDeep(500);
@Override }
public void run() { Flags.set(Flags.REFRESH); // Forces a reload when app launches
AndroidUtilities.sleepDeep(2000); if ((voiceReminder || maxOutVolumeForMultipleRingReminders)) {
for(int i = 0; i < 50; i++) { AndroidUtilities.sleepDeep(2000);
AndroidUtilities.sleepDeep(500); for(int i = 0; i < 50; i++) {
if(audioManager.getMode() != AudioManager.MODE_RINGTONE) AndroidUtilities.sleepDeep(500);
break; if(audioManager.getMode() != AudioManager.MODE_RINGTONE)
} break;
try { }
// first reset the Alarm-volume to the value before it was eventually maxed out try {
if (maxOutVolumeForMultipleRingReminders) // first reset the Alarm-volume to the value before it was eventually maxed out
audioManager.setStreamVolume(AudioManager.STREAM_ALARM, previousAlarmVolume, 0); if (maxOutVolumeForMultipleRingReminders)
if (finalVoiceReminder) audioManager.setStreamVolume(AudioManager.STREAM_ALARM, previousAlarmVolume, 0);
VoiceOutputService.getVoiceOutputInstance().queueSpeak(text); if (voiceReminder)
} catch (VerifyError e) { VoiceOutputService.getVoiceOutputInstance().queueSpeak(text);
// unavailable } catch (VerifyError e) {
} // unavailable
} }
}); }
} }
} }

Loading…
Cancel
Save