Commit 318c5ae5 authored by dfalcantara's avatar dfalcantara Committed by Commit bot

Group Downloads notifications, and separate Incognito

* Download notifications are grouped together, and Incognito
  is put into its own group.  Summary notifications will come
  in a later patch; Android N doesn't seem to bundle these
  together once they're in their own group, making it hard
  to debug without Android Wear.

* New and existing group name constants are moved into
  NotificationConstants.java.

* Cleans up DownloadNotificationService's constant naming
  schemes to make it clearer what each field does.

BUG=678694,674015

Review-Url: https://codereview.chromium.org/2619433003
Cr-Commit-Position: refs/heads/master@{#442017}
parent ca86c9e8
......@@ -37,6 +37,7 @@ import org.chromium.chrome.browser.ChromeApplication;
import org.chromium.chrome.browser.init.BrowserParts;
import org.chromium.chrome.browser.init.ChromeBrowserInitializer;
import org.chromium.chrome.browser.init.EmptyBrowserParts;
import org.chromium.chrome.browser.notifications.NotificationConstants;
import org.chromium.chrome.browser.offlinepages.downloads.OfflinePageDownloadBridge;
import org.chromium.chrome.browser.util.IntentUtils;
......@@ -45,6 +46,7 @@ import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* Service responsible for creating and updating download notifications even after
......@@ -73,21 +75,25 @@ public class DownloadNotificationService extends Service {
public static final int INVALID_DOWNLOAD_PERCENTAGE = -1;
@VisibleForTesting
static final String PENDING_DOWNLOAD_NOTIFICATIONS = "PendingDownloadNotifications";
static final String NOTIFICATION_NAMESPACE = "DownloadNotificationService";
private static final String TAG = "DownloadNotification";
private static final String NEXT_DOWNLOAD_NOTIFICATION_ID = "NextDownloadNotificationId";
// Notification Id starting value, to avoid conflicts from IDs used in prior versions.
/** Notification Id starting value, to avoid conflicts from IDs used in prior versions. */
private static final int STARTING_NOTIFICATION_ID = 1000000;
private static final String AUTO_RESUMPTION_ATTEMPT_LEFT = "ResumptionAttemptLeft";
private static final int MAX_RESUMPTION_ATTEMPT_LEFT = 5;
@VisibleForTesting static final int SECONDS_PER_MINUTE = 60;
@VisibleForTesting static final int SECONDS_PER_HOUR = 60 * 60;
@VisibleForTesting static final int SECONDS_PER_DAY = 24 * 60 * 60;
@VisibleForTesting static final long SECONDS_PER_MINUTE = TimeUnit.MINUTES.toSeconds(1);
@VisibleForTesting static final long SECONDS_PER_HOUR = TimeUnit.HOURS.toSeconds(1);
@VisibleForTesting static final long SECONDS_PER_DAY = TimeUnit.DAYS.toSeconds(1);
private static final String KEY_AUTO_RESUMPTION_ATTEMPT_LEFT = "ResumptionAttemptLeft";
private static final String KEY_NEXT_DOWNLOAD_NOTIFICATION_ID = "NextDownloadNotificationId";
static final String KEY_PENDING_DOWNLOAD_NOTIFICATIONS = "PendingDownloadNotifications";
private final IBinder mBinder = new LocalBinder();
private final List<DownloadSharedPreferenceEntry> mDownloadSharedPreferenceEntries =
new ArrayList<DownloadSharedPreferenceEntry>();
private final List<String> mDownloadsInProgress = new ArrayList<String>();
private NotificationManager mNotificationManager;
private SharedPreferences mSharedPrefs;
private Context mContext;
......@@ -141,7 +147,7 @@ public class DownloadNotificationService extends Service {
onBrowserKilled();
}
mNextNotificationId = mSharedPrefs.getInt(
NEXT_DOWNLOAD_NOTIFICATION_ID, STARTING_NOTIFICATION_ID);
KEY_NEXT_DOWNLOAD_NOTIFICATION_ID, STARTING_NOTIFICATION_ID);
}
......@@ -207,7 +213,7 @@ public class DownloadNotificationService extends Service {
*/
private void updateResumptionAttemptLeft() {
SharedPreferences.Editor editor = mSharedPrefs.edit();
editor.putInt(AUTO_RESUMPTION_ATTEMPT_LEFT, mNumAutoResumptionAttemptLeft);
editor.putInt(KEY_AUTO_RESUMPTION_ATTEMPT_LEFT, mNumAutoResumptionAttemptLeft);
editor.apply();
}
......@@ -217,7 +223,7 @@ public class DownloadNotificationService extends Service {
static void clearResumptionAttemptLeft() {
SharedPreferences SharedPrefs = ContextUtils.getAppSharedPreferences();
SharedPreferences.Editor editor = SharedPrefs.edit();
editor.remove(AUTO_RESUMPTION_ATTEMPT_LEFT);
editor.remove(KEY_AUTO_RESUMPTION_ATTEMPT_LEFT);
editor.apply();
}
......@@ -546,7 +552,8 @@ public class DownloadNotificationService extends Service {
.setSmallIcon(iconId)
.setLocalOnly(true)
.setAutoCancel(true)
.setContentText(contentText);
.setContentText(contentText)
.setGroup(NotificationConstants.GROUP_DOWNLOADS);
return builder;
}
......@@ -746,7 +753,7 @@ public class DownloadNotificationService extends Service {
*/
@VisibleForTesting
void updateNotification(int id, Notification notification) {
mNotificationManager.notify(NOTIFICATION_NAMESPACE, id, notification);
mNotificationManager.notify(id, notification);
}
/**
......@@ -835,11 +842,11 @@ public class DownloadNotificationService extends Service {
* left from the shared preference.
*/
void parseDownloadSharedPrefs() {
mNumAutoResumptionAttemptLeft = mSharedPrefs.getInt(AUTO_RESUMPTION_ATTEMPT_LEFT,
mNumAutoResumptionAttemptLeft = mSharedPrefs.getInt(KEY_AUTO_RESUMPTION_ATTEMPT_LEFT,
MAX_RESUMPTION_ATTEMPT_LEFT);
if (!mSharedPrefs.contains(PENDING_DOWNLOAD_NOTIFICATIONS)) return;
if (!mSharedPrefs.contains(KEY_PENDING_DOWNLOAD_NOTIFICATIONS)) return;
Set<String> entries = DownloadManagerService.getStoredDownloadInfo(
mSharedPrefs, PENDING_DOWNLOAD_NOTIFICATIONS);
mSharedPrefs, KEY_PENDING_DOWNLOAD_NOTIFICATIONS);
for (String entryString : entries) {
DownloadSharedPreferenceEntry entry =
DownloadSharedPreferenceEntry.parseFromString(entryString);
......@@ -873,7 +880,7 @@ public class DownloadNotificationService extends Service {
entries.add(mDownloadSharedPreferenceEntries.get(i).getSharedPreferenceString());
}
DownloadManagerService.storeDownloadInfo(
mSharedPrefs, PENDING_DOWNLOAD_NOTIFICATIONS, entries);
mSharedPrefs, KEY_PENDING_DOWNLOAD_NOTIFICATIONS, entries);
}
/**
......@@ -887,7 +894,7 @@ public class DownloadNotificationService extends Service {
mNextNotificationId = mNextNotificationId == Integer.MAX_VALUE
? STARTING_NOTIFICATION_ID : mNextNotificationId + 1;
SharedPreferences.Editor editor = mSharedPrefs.edit();
editor.putInt(NEXT_DOWNLOAD_NOTIFICATION_ID, mNextNotificationId);
editor.putInt(KEY_NEXT_DOWNLOAD_NOTIFICATION_ID, mNextNotificationId);
editor.apply();
return notificationId;
}
......@@ -910,15 +917,15 @@ public class DownloadNotificationService extends Service {
int minutes = 0;
if (secondsLong >= SECONDS_PER_DAY) {
days = (int) (secondsLong / SECONDS_PER_DAY);
secondsLong -= (long) days * SECONDS_PER_DAY;
secondsLong -= days * SECONDS_PER_DAY;
}
if (secondsLong >= SECONDS_PER_HOUR) {
hours = (int) (secondsLong / SECONDS_PER_HOUR);
secondsLong -= (long) hours * SECONDS_PER_HOUR;
secondsLong -= hours * SECONDS_PER_HOUR;
}
if (secondsLong >= SECONDS_PER_MINUTE) {
minutes = (int) (secondsLong / SECONDS_PER_MINUTE);
secondsLong -= (long) minutes * SECONDS_PER_MINUTE;
secondsLong -= minutes * SECONDS_PER_MINUTE;
}
int seconds = (int) secondsLong;
......
......@@ -11,6 +11,7 @@ import android.support.v4.app.NotificationCompat;
import org.chromium.base.ContextUtils;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.notifications.NotificationConstants;
/**
* Manages the notification indicating that there are incognito tabs opened in Document mode.
......@@ -37,7 +38,8 @@ public class IncognitoNotificationManager {
.setVisibility(Notification.VISIBILITY_SECRET)
.setSmallIcon(R.drawable.incognito_statusbar)
.setShowWhen(false)
.setLocalOnly(true);
.setLocalOnly(true)
.setGroup(NotificationConstants.GROUP_INCOGNITO);
NotificationManager nm =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(INCOGNITO_TABS_OPEN_TAG, INCOGNITO_TABS_OPEN_ID, builder.build());
......
......@@ -37,6 +37,7 @@ import org.chromium.base.ContextUtils;
import org.chromium.base.VisibleForTesting;
import org.chromium.blink.mojom.MediaSessionAction;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.notifications.NotificationConstants;
import org.chromium.content_public.common.MediaMetadata;
import java.util.ArrayList;
......@@ -270,7 +271,6 @@ public class MediaNotificationManager {
*/
public static final class PlaybackListenerService extends ListenerService {
private static final int NOTIFICATION_ID = R.id.media_playback_notification;
private static final String NOTIFICATION_GROUP_NAME = "MediaPlayback";
@Override
public void onCreate() {
......@@ -310,7 +310,6 @@ public class MediaNotificationManager {
*/
public static final class PresentationListenerService extends ListenerService {
private static final int NOTIFICATION_ID = R.id.presentation_notification;
private static final String NOTIFICATION_GROUP_NAME = "MediaPresentation";
@Override
@Nullable
......@@ -324,7 +323,6 @@ public class MediaNotificationManager {
*/
public static final class CastListenerService extends ListenerService {
private static final int NOTIFICATION_ID = R.id.remote_notification;
private static final String NOTIFICATION_GROUP_NAME = "MediaRemote";
@Override
@Nullable
......@@ -399,11 +397,11 @@ public class MediaNotificationManager {
// Returns the notification group name used to prevent automatic grouping.
private String getNotificationGroupName() {
if (mMediaNotificationInfo.id == PlaybackListenerService.NOTIFICATION_ID) {
return PlaybackListenerService.NOTIFICATION_GROUP_NAME;
return NotificationConstants.GROUP_MEDIA_PLAYBACK;
} else if (mMediaNotificationInfo.id == PresentationListenerService.NOTIFICATION_ID) {
return PresentationListenerService.NOTIFICATION_GROUP_NAME;
return NotificationConstants.GROUP_MEDIA_PRESENTATION;
} else if (mMediaNotificationInfo.id == CastListenerService.NOTIFICATION_ID) {
return CastListenerService.NOTIFICATION_GROUP_NAME;
return NotificationConstants.GROUP_MEDIA_REMOTE;
}
assert false;
......
......@@ -59,4 +59,11 @@ public class NotificationConstants {
* Key for retrieving the results of user input from notification text action intents.
*/
static final String KEY_TEXT_REPLY = "key_text_reply";
// Notification groups for features that show notifications to the user.
public static final String GROUP_DOWNLOADS = "Downloads";
public static final String GROUP_INCOGNITO = "Incognito";
public static final String GROUP_MEDIA_PLAYBACK = "MediaPlayback";
public static final String GROUP_MEDIA_PRESENTATION = "MediaPresentation";
public static final String GROUP_MEDIA_REMOTE = "MediaRemote";
}
......@@ -80,7 +80,7 @@ public class DownloadNotificationServiceTest extends
super.setupService();
SharedPreferences sharedPrefs = ContextUtils.getAppSharedPreferences();
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.remove(DownloadNotificationService.PENDING_DOWNLOAD_NOTIFICATIONS);
editor.remove(DownloadNotificationService.KEY_PENDING_DOWNLOAD_NOTIFICATIONS);
editor.apply();
super.tearDown();
}
......@@ -148,7 +148,7 @@ public class DownloadNotificationServiceTest extends
SharedPreferences sharedPrefs = ContextUtils.getAppSharedPreferences();
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putStringSet(
DownloadNotificationService.PENDING_DOWNLOAD_NOTIFICATIONS, notifications);
DownloadNotificationService.KEY_PENDING_DOWNLOAD_NOTIFICATIONS, notifications);
editor.apply();
startNotificationService();
assertTrue(scheduler.mScheduled);
......@@ -195,7 +195,7 @@ public class DownloadNotificationServiceTest extends
SharedPreferences sharedPrefs = ContextUtils.getAppSharedPreferences();
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putStringSet(
DownloadNotificationService.PENDING_DOWNLOAD_NOTIFICATIONS, notifications);
DownloadNotificationService.KEY_PENDING_DOWNLOAD_NOTIFICATIONS, notifications);
editor.apply();
startNotificationService();
assertFalse(scheduler.mScheduled);
......@@ -223,15 +223,15 @@ public class DownloadNotificationServiceTest extends
ContextUtils.getAppSharedPreferences();
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putStringSet(
DownloadNotificationService.PENDING_DOWNLOAD_NOTIFICATIONS, notifications);
DownloadNotificationService.KEY_PENDING_DOWNLOAD_NOTIFICATIONS, notifications);
editor.apply();
startNotificationService();
assertTrue(getService().isPaused());
assertEquals(2, getService().getNotificationIds().size());
assertTrue(getService().getNotificationIds().contains(1));
assertTrue(getService().getNotificationIds().contains(2));
assertTrue(
sharedPrefs.contains(DownloadNotificationService.PENDING_DOWNLOAD_NOTIFICATIONS));
assertTrue(sharedPrefs.contains(
DownloadNotificationService.KEY_PENDING_DOWNLOAD_NOTIFICATIONS));
}
/**
......@@ -255,7 +255,7 @@ public class DownloadNotificationServiceTest extends
SharedPreferences sharedPrefs = ContextUtils.getAppSharedPreferences();
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putStringSet(
DownloadNotificationService.PENDING_DOWNLOAD_NOTIFICATIONS, notifications);
DownloadNotificationService.KEY_PENDING_DOWNLOAD_NOTIFICATIONS, notifications);
editor.apply();
startNotificationService();
assertEquals(2, getService().getNotificationIds().size());
......@@ -268,17 +268,17 @@ public class DownloadNotificationServiceTest extends
assertEquals(3, getService().getNotificationIds().size());
int lastNotificationId = getService().getLastAddedNotificationId();
Set<String> entries = DownloadManagerService.getStoredDownloadInfo(
sharedPrefs, DownloadNotificationService.PENDING_DOWNLOAD_NOTIFICATIONS);
sharedPrefs, DownloadNotificationService.KEY_PENDING_DOWNLOAD_NOTIFICATIONS);
assertEquals(3, entries.size());
service.notifyDownloadSuccessful(guid1, "/path/to/success", "success", 100L, false, false);
entries = DownloadManagerService.getStoredDownloadInfo(
sharedPrefs, DownloadNotificationService.PENDING_DOWNLOAD_NOTIFICATIONS);
sharedPrefs, DownloadNotificationService.KEY_PENDING_DOWNLOAD_NOTIFICATIONS);
assertEquals(2, entries.size());
service.notifyDownloadFailed(guid2, "failed");
entries = DownloadManagerService.getStoredDownloadInfo(
sharedPrefs, DownloadNotificationService.PENDING_DOWNLOAD_NOTIFICATIONS);
sharedPrefs, DownloadNotificationService.KEY_PENDING_DOWNLOAD_NOTIFICATIONS);
assertEquals(1, entries.size());
service.notifyDownloadCanceled(guid3);
......@@ -326,7 +326,7 @@ public class DownloadNotificationServiceTest extends
SharedPreferences sharedPrefs = ContextUtils.getAppSharedPreferences();
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putStringSet(
DownloadNotificationService.PENDING_DOWNLOAD_NOTIFICATIONS, notifications);
DownloadNotificationService.KEY_PENDING_DOWNLOAD_NOTIFICATIONS, notifications);
editor.apply();
startNotificationService();
DownloadNotificationService service = bindNotificationService();
......@@ -370,12 +370,12 @@ public class DownloadNotificationServiceTest extends
ContextUtils.getAppSharedPreferences();
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putStringSet(
DownloadNotificationService.PENDING_DOWNLOAD_NOTIFICATIONS, notifications);
DownloadNotificationService.KEY_PENDING_DOWNLOAD_NOTIFICATIONS, notifications);
editor.apply();
startNotificationService();
assertTrue(getService().isPaused());
assertFalse(sharedPrefs.contains(
DownloadNotificationService.PENDING_DOWNLOAD_NOTIFICATIONS));
DownloadNotificationService.KEY_PENDING_DOWNLOAD_NOTIFICATIONS));
}
@SmallTest
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment