Bug 1288821 - Refactor Java notification interface; r=snorp
authorJim Chen <nchen@mozilla.com>
Thu, 04 Aug 2016 09:17:54 -0400
changeset 308007 ca7a13d72c4132cc6375ccf067100a14ac34842c
parent 308006 d8ed6dd3fbbd4e3c3a80fef36b12b102934fc0ef
child 308008 51eca6620dc5fa304712700b78a1ef24b1cef547
push id20216
push usernchen@mozilla.com
push dateThu, 04 Aug 2016 13:18:19 +0000
treeherderfx-team@cb68418cb454 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssnorp
bugs1288821
milestone51.0a1
Bug 1288821 - Refactor Java notification interface; r=snorp Refactor the notification-related methods in GeckoAppShell. In particular, combine showPersistentAlertNotification into showAlertNotification, and move code out of AndroidBridge.
mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoAppShell.java
widget/android/AndroidAlerts.cpp
widget/android/AndroidBridge.cpp
widget/android/AndroidBridge.h
--- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoAppShell.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoAppShell.java
@@ -1008,91 +1008,100 @@ public class GeckoAppShell
     public static void setNotificationClient(NotificationClient client) {
         if (notificationClient == null) {
             notificationClient = client;
         } else {
             Log.d(LOGTAG, "Notification client already set");
         }
     }
 
-    private static PendingIntent makePersistentNotificationIntent(
-            int aNotificationID, String aType, String aPersistentData) {
-        Uri.Builder b = new Uri.Builder();
-        Uri u = b.scheme("notification-event").path(Integer.toString(aNotificationID))
-                .appendQueryParameter("type", aType)
+    private static PendingIntent makePersistentNotificationIntent(int notificationID, String type,
+                                                                  String persistentData) {
+        final Uri.Builder b = new Uri.Builder();
+        final Uri u = b.scheme("notification-event")
+                .path(Integer.toString(notificationID))
+                .appendQueryParameter("type", type)
                 .build();
-        Intent intent = GeckoService.getIntentToCreateServices(
-                getApplicationContext(), aType, aPersistentData);
+        final Intent intent = GeckoService.getIntentToCreateServices(
+                getApplicationContext(), type, persistentData);
         intent.setData(u);
-        return PendingIntent.getService(
-                getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
-    }
-
-    @WrapForJNI(stubName = "ShowPersistentAlertNotificationWrapper")
-    public static void showPersistentAlertNotification(
-            String aPersistentData,
-            String aImageUrl, String aAlertTitle, String aAlertText,
-            String aAlertCookie, String aAlertName, String aHost) {
-        int notificationID = aAlertName.hashCode();
-        PendingIntent clickIntent = makePersistentNotificationIntent(notificationID, "persistent-notification-click", aPersistentData);
-        PendingIntent closeIntent = makePersistentNotificationIntent(notificationID, "persistent-notification-close", aPersistentData);
-        notificationClient.add(notificationID, aImageUrl, aHost, aAlertTitle, aAlertText, clickIntent, closeIntent);
-    }
 
-    @WrapForJNI(stubName = "ShowAlertNotificationWrapper")
-    public static void showAlertNotification(String aImageUrl, String aAlertTitle, String aAlertText, String aAlertCookie, String aAlertName, String aHost) {
-        // The intent to launch when the user clicks the expanded notification
-        Intent notificationIntent = new Intent(GeckoApp.ACTION_ALERT_CALLBACK);
-        notificationIntent.setClassName(AppConstants.ANDROID_PACKAGE_NAME, AppConstants.MOZ_ANDROID_BROWSER_INTENT_CLASS);
-        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-
-        int notificationID = aAlertName.hashCode();
-
-        // Put the strings into the intent as an URI "alert:?name=<alertName>&app=<appName>&cookie=<cookie>"
-        Uri.Builder b = new Uri.Builder();
-        Uri dataUri = b.scheme("alert").path(Integer.toString(notificationID))
-                                       .appendQueryParameter("name", aAlertName)
-                                       .appendQueryParameter("cookie", aAlertCookie)
-                                       .build();
-        notificationIntent.setData(dataUri);
-        PendingIntent contentIntent = PendingIntent.getActivity(
-                getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
-
-        ALERT_COOKIES.put(aAlertName, aAlertCookie);
-        callObserver(aAlertName, "alertshow", aAlertCookie);
-
-        notificationClient.add(notificationID, aImageUrl, aHost, aAlertTitle, aAlertText, contentIntent, null);
+        return PendingIntent.getService(getApplicationContext(), 0, intent,
+                                        PendingIntent.FLAG_UPDATE_CURRENT);
     }
 
     @WrapForJNI
-    public static void closeNotification(String aAlertName) {
-        String alertCookie = ALERT_COOKIES.get(aAlertName);
-        if (alertCookie != null) {
-            callObserver(aAlertName, "alertfinished", alertCookie);
-            ALERT_COOKIES.remove(aAlertName);
+    public static void showAlertNotification(String imageUrl, String alertTitle, String alertText,
+                                             String alertCookie, String alertName, String host,
+                                             String persistentData) {
+        final int notificationID = alertName.hashCode();
+        final PendingIntent clickIntent, closeIntent;
+
+        if (persistentData != null) {
+            clickIntent = makePersistentNotificationIntent(
+                    notificationID, "persistent-notification-click", persistentData);
+            closeIntent = makePersistentNotificationIntent(
+                    notificationID, "persistent-notification-close", persistentData);
+
+        } else {
+            ALERT_COOKIES.put(alertName, alertCookie);
+            callObserver(alertName, "alertshow", alertCookie);
+
+            // The intent to launch when the user clicks the expanded notification
+            final Intent notificationIntent = new Intent(GeckoApp.ACTION_ALERT_CALLBACK);
+            notificationIntent.setClassName(AppConstants.ANDROID_PACKAGE_NAME,
+                                            AppConstants.MOZ_ANDROID_BROWSER_INTENT_CLASS);
+            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+
+            // Put the strings into the intent as an URI
+            // "alert:?name=<alertName>&app=<appName>&cookie=<cookie>"
+            final Uri.Builder b = new Uri.Builder();
+            final Uri dataUri = b.scheme("alert")
+                    .path(Integer.toString(notificationID))
+                    .appendQueryParameter("name", alertName)
+                    .appendQueryParameter("cookie", alertCookie)
+                    .build();
+            notificationIntent.setData(dataUri);
+
+            clickIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent,
+                                                    PendingIntent.FLAG_UPDATE_CURRENT);
+            closeIntent = null;
         }
 
-        removeObserver(aAlertName);
+        notificationClient.add(notificationID, imageUrl, host, alertTitle,
+                               alertText, clickIntent, closeIntent);
+    }
 
-        int notificationID = aAlertName.hashCode();
+    @WrapForJNI
+    public static void closeNotification(String alertName) {
+        final String alertCookie = ALERT_COOKIES.get(alertName);
+        if (alertCookie != null) {
+            callObserver(alertName, "alertfinished", alertCookie);
+            ALERT_COOKIES.remove(alertName);
+        }
+
+        removeObserver(alertName);
+
+        final int notificationID = alertName.hashCode();
         notificationClient.remove(notificationID);
     }
 
-    public static void handleNotification(String aAction, String aAlertName, String aAlertCookie) {
-        int notificationID = aAlertName.hashCode();
+    public static void handleNotification(String action, String alertName, String alertCookie) {
+        final int notificationID = alertName.hashCode();
 
-        if (GeckoApp.ACTION_ALERT_CALLBACK.equals(aAction)) {
-            callObserver(aAlertName, "alertclickcallback", aAlertCookie);
+        if (GeckoApp.ACTION_ALERT_CALLBACK.equals(action)) {
+            callObserver(alertName, "alertclickcallback", alertCookie);
 
             if (notificationClient.isOngoing(notificationID)) {
                 // When clicked, keep the notification if it displays progress
                 return;
             }
         }
-        closeNotification(aAlertName);
+
+        closeNotification(alertName);
     }
 
     @WrapForJNI(stubName = "GetDpiWrapper")
     public static int getDpi() {
         if (sDensityDpi == 0) {
             sDensityDpi = getApplicationContext().getResources().getDisplayMetrics().densityDpi;
         }
 
--- a/widget/android/AndroidAlerts.cpp
+++ b/widget/android/AndroidAlerts.cpp
@@ -70,28 +70,25 @@ AndroidAlerts::ShowPersistentNotificatio
 
     nsCOMPtr<nsIPrincipal> principal;
     rv = aAlert->GetPrincipal(getter_AddRefs(principal));
     NS_ENSURE_SUCCESS(rv, NS_OK);
 
     nsAutoString host;
     nsAlertsUtils::GetSourceHostPort(principal, host);
 
-    if (!aPersistentData.IsEmpty()) {
-        java::GeckoAppShell::ShowPersistentAlertNotificationWrapper(
-                aPersistentData, imageUrl, title, text, cookie, name, host);
-    } else {
-        if (aAlertListener) {
-            // This will remove any observers already registered for this id
-            nsAppShell::PostEvent(AndroidGeckoEvent::MakeAddObserver(name, aAlertListener));
-        }
+    if (aPersistentData.IsEmpty() && aAlertListener) {
+        // This will remove any observers already registered for this id
+        nsAppShell::PostEvent(AndroidGeckoEvent::MakeAddObserver(name, aAlertListener));
+    }
 
-        java::GeckoAppShell::ShowAlertNotificationWrapper(
-               imageUrl, title, text, cookie, name, host);
-    }
+    java::GeckoAppShell::ShowAlertNotification(
+            imageUrl, title, text, cookie, name, host,
+            !aPersistentData.IsEmpty() ? jni::StringParam(aPersistentData)
+                                       : jni::StringParam(nullptr));
     return NS_OK;
 }
 
 NS_IMETHODIMP
 AndroidAlerts::CloseAlert(const nsAString& aAlertName,
                           nsIPrincipal* aPrincipal)
 {
     java::GeckoAppShell::CloseNotification(aAlertName);
--- a/widget/android/AndroidBridge.cpp
+++ b/widget/android/AndroidBridge.cpp
@@ -438,53 +438,16 @@ AndroidBridge::GetClipboardText(nsAStrin
     auto text = Clipboard::GetClipboardTextWrapper();
 
     if (text) {
         aText = text->ToString();
     }
     return !!text;
 }
 
-void
-AndroidBridge::ShowPersistentAlertNotification(const nsAString& aPersistentData,
-                                               const nsAString& aImageUrl,
-                                               const nsAString& aAlertTitle,
-                                               const nsAString& aAlertText,
-                                               const nsAString& aAlertCookie,
-                                               const nsAString& aAlertName,
-                                               nsIPrincipal* aPrincipal)
-{
-    nsAutoString host;
-    nsAlertsUtils::GetSourceHostPort(aPrincipal, host);
-
-    GeckoAppShell::ShowPersistentAlertNotificationWrapper
-        (aPersistentData, aImageUrl, aAlertTitle, aAlertText, aAlertCookie, aAlertName, host);
-}
-
-void
-AndroidBridge::ShowAlertNotification(const nsAString& aImageUrl,
-                                     const nsAString& aAlertTitle,
-                                     const nsAString& aAlertText,
-                                     const nsAString& aAlertCookie,
-                                     nsIObserver *aAlertListener,
-                                     const nsAString& aAlertName,
-                                     nsIPrincipal* aPrincipal)
-{
-    if (aAlertListener) {
-        // This will remove any observers already registered for this id
-        nsAppShell::PostEvent(AndroidGeckoEvent::MakeAddObserver(aAlertName, aAlertListener));
-    }
-
-    nsAutoString host;
-    nsAlertsUtils::GetSourceHostPort(aPrincipal, host);
-
-    GeckoAppShell::ShowAlertNotificationWrapper
-           (aImageUrl, aAlertTitle, aAlertText, aAlertCookie, aAlertName, host);
-}
-
 int
 AndroidBridge::GetDPI()
 {
     static int sDPI = 0;
     if (sDPI)
         return sDPI;
 
     const int DEFAULT_DPI = 160;
--- a/widget/android/AndroidBridge.h
+++ b/widget/android/AndroidBridge.h
@@ -170,32 +170,16 @@ public:
     bool GetHWEncoderCapability();
     bool GetHWDecoderCapability();
 
     void GetMimeTypeFromExtensions(const nsACString& aFileExt, nsCString& aMimeType);
     void GetExtensionFromMimeType(const nsACString& aMimeType, nsACString& aFileExt);
 
     bool GetClipboardText(nsAString& aText);
 
-    void ShowPersistentAlertNotification(const nsAString& aPersistentData,
-                                         const nsAString& aImageUrl,
-                                         const nsAString& aAlertTitle,
-                                         const nsAString& aAlertText,
-                                         const nsAString& aAlertCookie,
-                                         const nsAString& aAlertName,
-                                         nsIPrincipal* aPrincipal);
-
-    void ShowAlertNotification(const nsAString& aImageUrl,
-                               const nsAString& aAlertTitle,
-                               const nsAString& aAlertText,
-                               const nsAString& aAlertCookie,
-                               nsIObserver *aAlertListener,
-                               const nsAString& aAlertName,
-                               nsIPrincipal* aPrincipal);
-
     int GetDPI();
     int GetScreenDepth();
 
     void Vibrate(const nsTArray<uint32_t>& aPattern);
 
     void GetSystemColors(AndroidSystemColors *aColors);
 
     void GetIconForExtension(const nsACString& aFileExt, uint32_t aIconSize, uint8_t * const aBuf);