Commit 2e36b96a authored by peter's avatar peter Committed by Commit bot

Merge the GoogleServicesNotificationController to SyncNotificationController

The SyncNotificationController is the only consumer of the GSNC, so we
can simplify by merging the two classes together. In addition, remove
displayAndroidMasterSyncDisabledNotification() which had no callers.

The sync notifications do not appear to be tested. I filed
https://crbug.com/679750 to track adding some.

BUG=

Review-Url: https://codereview.chromium.org/2621023002
Cr-Commit-Position: refs/heads/master@{#442992}
parent b38f05eb
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.notifications;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import org.chromium.base.VisibleForTesting;
import org.chromium.chrome.R;
/**
* This class controls Android notifications for Google Services such as signin and sync.
*
* Users of these classes must ensure that their notification IDs are unique, and preferably
* listed in {@link NotificationConstants}.
* TODO(beverloo): Merge the display of notifications with NotificationUIManager.
*/
public class GoogleServicesNotificationController {
private static final String TAG = "GoogleServicesNotificationController";
private static final Object LOCK = new Object();
private static GoogleServicesNotificationController sInstance;
private final Context mApplicationContext;
private NotificationManagerProxy mNotificationManager;
/**
* Retrieve the singleton instance of this class.
*
* @param context the current context.
* @return the singleton instance.
*/
public static GoogleServicesNotificationController get(Context context) {
synchronized (LOCK) {
if (sInstance == null) {
sInstance = new GoogleServicesNotificationController(context);
}
return sInstance;
}
}
public static String formatMessageParts(
Context context, Integer featureNameResource, Integer messageResource) {
return context.getString(featureNameResource) + ": " + context.getString(messageResource);
}
private GoogleServicesNotificationController(Context context) {
mApplicationContext = context.getApplicationContext();
mNotificationManager = new NotificationManagerProxyImpl(
(NotificationManager) mApplicationContext.getSystemService(
Context.NOTIFICATION_SERVICE));
}
public void updateSingleNotification(int id, String message, Intent intent) {
if (message == null) {
cancelNotification(id);
} else {
showNotification(id, message, message, intent);
}
}
/**
* Shows a message in the Android status bar.
* @param id Must be a unique notification ID.
* @param tickerText Message to display in the ticker when the notification first appears.
* @param contentText Message to display in the main body of the notification.
* @param intent Intent to fire when the notification is clicked.
*/
public void showNotification(int id, String tickerText, String contentText, Intent intent) {
String title = mApplicationContext.getString(R.string.app_name);
PendingIntent contentIntent = PendingIntent.getActivity(mApplicationContext, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(mApplicationContext)
.setAutoCancel(true)
.setContentIntent(contentIntent)
.setContentTitle(title)
.setContentText(contentText)
.setSmallIcon(R.drawable.ic_chrome)
.setTicker(tickerText)
.setLocalOnly(true);
Notification notification =
new NotificationCompat.BigTextStyle(builder).bigText(contentText).build();
mNotificationManager.notify(id, notification);
}
public void cancelNotification(int id) {
mNotificationManager.cancel(id);
}
@VisibleForTesting
public void overrideNotificationManagerForTests(NotificationManagerProxy managerProxy) {
mNotificationManager = managerProxy;
}
}
......@@ -6,16 +6,20 @@ package org.chromium.chrome.browser.sync;
import android.app.Activity;
import android.app.Fragment;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import org.chromium.base.ThreadUtils;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.notifications.GoogleServicesNotificationController;
import org.chromium.chrome.browser.notifications.NotificationConstants;
import org.chromium.chrome.browser.notifications.NotificationManagerProxy;
import org.chromium.chrome.browser.notifications.NotificationManagerProxyImpl;
import org.chromium.chrome.browser.preferences.PreferencesLauncher;
import org.chromium.components.sync.AndroidSyncSettings;
......@@ -26,7 +30,7 @@ import org.chromium.components.sync.AndroidSyncSettings;
public class SyncNotificationController implements ProfileSyncService.SyncStateChangedListener {
private static final String TAG = "SyncNotificationController";
private final Context mApplicationContext;
private final GoogleServicesNotificationController mNotificationController;
private final NotificationManagerProxy mNotificationManager;
private final Class<? extends Activity> mPassphraseRequestActivity;
private final Class<? extends Fragment> mAccountManagementFragment;
private final ProfileSyncService mProfileSyncService;
......@@ -35,21 +39,15 @@ public class SyncNotificationController implements ProfileSyncService.SyncStateC
Class<? extends Activity> passphraseRequestActivity,
Class<? extends Fragment> accountManagementFragment) {
mApplicationContext = context.getApplicationContext();
mNotificationController = GoogleServicesNotificationController.get(context);
mNotificationManager = new NotificationManagerProxyImpl(
(NotificationManager) mApplicationContext.getSystemService(
Context.NOTIFICATION_SERVICE));
mProfileSyncService = ProfileSyncService.get();
assert mProfileSyncService != null;
mPassphraseRequestActivity = passphraseRequestActivity;
mAccountManagementFragment = accountManagementFragment;
}
public void displayAndroidMasterSyncDisabledNotification() {
String masterSyncDisabled =
GoogleServicesNotificationController.formatMessageParts(mApplicationContext,
R.string.sign_in_sync, R.string.sync_android_master_sync_disabled);
mNotificationController.showNotification(masterSyncDisabled.hashCode(), masterSyncDisabled,
masterSyncDisabled, new Intent(Settings.ACTION_SYNC_SETTINGS));
}
/**
* Callback for {@link ProfileSyncService.SyncStateChangedListener}.
*/
......@@ -57,17 +55,14 @@ public class SyncNotificationController implements ProfileSyncService.SyncStateC
public void syncStateChanged() {
ThreadUtils.assertOnUiThread();
int message;
Intent intent;
// Auth errors take precedence over passphrase errors.
if (!AndroidSyncSettings.isSyncEnabled(mApplicationContext)) {
mNotificationController.cancelNotification(NotificationConstants.NOTIFICATION_ID_SYNC);
mNotificationManager.cancel(NotificationConstants.NOTIFICATION_ID_SYNC);
return;
}
if (shouldSyncAuthErrorBeShown()) {
message = mProfileSyncService.getAuthError().getMessage();
intent = createSettingsIntent();
showSyncNotification(
mProfileSyncService.getAuthError().getMessage(), createSettingsIntent());
} else if (mProfileSyncService.isEngineInitialized()
&& mProfileSyncService.isPassphraseRequiredForDecryption()) {
if (mProfileSyncService.isPassphrasePrompted()) {
......@@ -77,24 +72,44 @@ public class SyncNotificationController implements ProfileSyncService.SyncStateC
case IMPLICIT_PASSPHRASE: // Falling through intentionally.
case FROZEN_IMPLICIT_PASSPHRASE: // Falling through intentionally.
case CUSTOM_PASSPHRASE:
message = R.string.sync_need_passphrase;
intent = createPasswordIntent();
showSyncNotification(R.string.sync_need_passphrase, createPasswordIntent());
break;
case KEYSTORE_PASSPHRASE: // Falling through intentionally.
default:
mNotificationController.cancelNotification(
NotificationConstants.NOTIFICATION_ID_SYNC);
mNotificationManager.cancel(NotificationConstants.NOTIFICATION_ID_SYNC);
return;
}
} else {
mNotificationController.cancelNotification(NotificationConstants.NOTIFICATION_ID_SYNC);
mNotificationManager.cancel(NotificationConstants.NOTIFICATION_ID_SYNC);
return;
}
}
/**
* Builds and shows a notification for the |message|.
*
* @param message Resource id of the message to display in the notification.
* @param intent Intent to send when the user activates the notification.
*/
private void showSyncNotification(int message, Intent intent) {
String title = mApplicationContext.getString(R.string.app_name);
String text = mApplicationContext.getString(R.string.sign_in_sync) + ": "
+ mApplicationContext.getString(message);
PendingIntent contentIntent = PendingIntent.getActivity(mApplicationContext, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(mApplicationContext)
.setAutoCancel(true)
.setContentIntent(contentIntent)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(R.drawable.ic_chrome)
.setTicker(text)
.setLocalOnly(true);
Notification notification =
new NotificationCompat.BigTextStyle(builder).bigText(text).build();
mNotificationController.updateSingleNotification(NotificationConstants.NOTIFICATION_ID_SYNC,
GoogleServicesNotificationController.formatMessageParts(
mApplicationContext, R.string.sign_in_sync, message),
intent);
mNotificationManager.notify(NotificationConstants.NOTIFICATION_ID_SYNC, notification);
}
private boolean shouldSyncAuthErrorBeShown() {
......
......@@ -542,7 +542,6 @@ chrome_java_sources = [
"java/src/org/chromium/chrome/browser/nfc/BeamProvider.java",
"java/src/org/chromium/chrome/browser/notifications/ActionInfo.java",
"java/src/org/chromium/chrome/browser/notifications/CustomNotificationBuilder.java",
"java/src/org/chromium/chrome/browser/notifications/GoogleServicesNotificationController.java",
"java/src/org/chromium/chrome/browser/notifications/NotificationBuilderBase.java",
"java/src/org/chromium/chrome/browser/notifications/NotificationConstants.java",
"java/src/org/chromium/chrome/browser/notifications/NotificationManagerProxy.java",
......
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