Bug 1233150 - WIP: handle blocking origin draft
authorChenxia Liu <liuche@mozilla.com>
Tue, 15 Dec 2015 17:10:53 -0800
changeset 315869 bf1df460a1b6fb1216b4042f315e5b38806153d3
parent 315868 45d68810ab5d962125318ac2263a99a52b956989
child 512095 69ffdd7f12f2adf6fb351a8c6dc64d37b45f9381
push id8474
push usercliu@mozilla.com
push dateWed, 16 Dec 2015 22:12:37 +0000
bugs1233150
milestone46.0a1
Bug 1233150 - WIP: handle blocking origin
mobile/android/base/java/org/mozilla/gecko/NotificationHandler.java
mobile/android/base/java/org/mozilla/gecko/NotificationService.java
mobile/android/base/java/org/mozilla/gecko/ServiceNotificationClient.java
mobile/android/base/java/org/mozilla/gecko/overlays/OverlayConstants.java
mobile/android/modules/Notifications.jsm
--- a/mobile/android/base/java/org/mozilla/gecko/NotificationHandler.java
+++ b/mobile/android/base/java/org/mozilla/gecko/NotificationHandler.java
@@ -14,16 +14,17 @@ import android.content.Context;
 import android.support.v4.app.NotificationCompat;
 import android.support.v4.app.NotificationManagerCompat;
 import org.mozilla.gecko.gfx.BitmapUtils;
 
 import java.util.concurrent.ConcurrentHashMap;
 
 public class NotificationHandler {
     private static String LOGTAG = "GeckoNotifHandler";
+    public static String EXTRA_ORIGIN = "notification-extra-origin";
     private final ConcurrentHashMap<Integer, Notification>
             mNotifications = new ConcurrentHashMap<Integer, Notification>();
     private final Context mContext;
     private final NotificationManagerCompat mNotificationManager;
 
     /**
      * Notification associated with this service's foreground state.
      *
@@ -51,32 +52,30 @@ public class NotificationHandler {
      * @param aAlertText     text of the notification
      * @param contentIntent  Intent used when the notification is clicked
      */
     public void add(final int notificationID, String aImageUrl, String aHost, String aAlertTitle,
                     String aAlertText, PendingIntent contentIntent, String origin) {
         // Remove the old notification with the same ID, if any
         remove(notificationID);
 
-        // TODO: Service intent to clear pref on reopen.
-        Intent prefIntent = new Intent(Intent.ACTION_VIEW);
-        prefIntent.setClassName(AppConstants.ANDROID_PACKAGE_NAME, AppConstants.MOZ_ANDROID_BROWSER_INTENT_CLASS);
-        PendingIntent buttonIntent = PendingIntent.getActivity(mContext, 0, prefIntent, PendingIntent.FLAG_UPDATE_CURRENT);
-        PendingIntent testIntent = PendingIntent.getService(mContext, 0, prefIntent, PendingIntent.FLAG_UPDATE_CURRENT);
+        Intent prefIntent = new Intent(GeckoApp.ACTION_ALERT_CALLBACK);
+        prefIntent.putExtra(EXTRA_ORIGIN, origin);
+        PendingIntent actionIntent = PendingIntent.getService(mContext, 0, prefIntent, PendingIntent.FLAG_UPDATE_CURRENT);
 
         final NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext)
                 .setContentTitle(aAlertTitle)
                 .setContentText(aAlertText)
                 .setSmallIcon(R.drawable.ic_status_logo)
                 .setContentIntent(contentIntent)
                 .setAutoCancel(true)
                 .setStyle(new NotificationCompat.InboxStyle()
                         .addLine(aAlertText)
                         .setSummaryText(aHost))
-                .addAction(R.drawable.find_close, mContext.getResources().getString(R.string.web_notification_mute), buttonIntent);
+                .addAction(R.drawable.find_close, mContext.getResources().getString(R.string.web_notification_mute), actionIntent);
 
         // Fetch icon.
         if (!aImageUrl.isEmpty()) {
             final Bitmap image = BitmapUtils.decodeUrl(aImageUrl);
             builder.setLargeIcon(image);
         }
 
         builder.setWhen(System.currentTimeMillis());
--- a/mobile/android/base/java/org/mozilla/gecko/NotificationService.java
+++ b/mobile/android/base/java/org/mozilla/gecko/NotificationService.java
@@ -5,20 +5,25 @@
 
 package org.mozilla.gecko;
 
 import android.app.IntentService;
 import android.app.Notification;
 import android.app.Service;
 import android.content.Intent;
 import android.os.Binder;
+import android.os.Bundle;
 import android.os.IBinder;
+import android.util.Log;
+import org.json.JSONException;
+import org.json.JSONObject;
 
 public class NotificationService extends IntentService {
     private static final String LOGTAG = "GeckoNotifactionService";
+    public static final String MUTE_NOTIFICATIONS = "notifications-mute";
     private final IBinder mBinder = new NotificationBinder();
     private NotificationHandler mHandler;
 
     public NotificationService(){
         super(LOGTAG);
     }
 
     @Override
@@ -37,16 +42,28 @@ public class NotificationService extends
                 }
             }
         };
     }
 
     @Override
     protected void onHandleIntent(final Intent intent) {
         // If Firefox is running, process intent immediately and block notifications from the site.
+        final Bundle extras = intent.getExtras();
+        if (extras != null) {
+            final String origin = extras.getString(NotificationHandler.EXTRA_ORIGIN);
+            final JSONObject message = new JSONObject();
+            try {
+                message.put("origin", origin);
+
+            } catch (JSONException e) {
+                Log.e(LOGTAG, "Couldn't add origin to Notifications message.");
+            }
+
+        }
         // If Firefox is not running, save a JSON file and update a pref.
     }
 
     public class NotificationBinder extends Binder {
         NotificationService getService() {
             // Return this instance of NotificationService so clients can call public methods
             return NotificationService.this;
         }
--- a/mobile/android/base/java/org/mozilla/gecko/ServiceNotificationClient.java
+++ b/mobile/android/base/java/org/mozilla/gecko/ServiceNotificationClient.java
@@ -11,17 +11,17 @@ import android.content.Intent;
 import android.content.ServiceConnection;
 import android.os.IBinder;
 import android.util.Log;
 
 /**
  * Client for posting notifications through the NotificationService.
  */
 public class ServiceNotificationClient extends NotificationClient {
-    private static final String LOGTAG = "GeckoServiceNotificationClient";
+    private static final String LOGTAG = "GeckoSvcNotifClient";
 
     private final ServiceConnection mConnection = new NotificationServiceConnection();
     private boolean mBound;
     private final Context mContext;
 
     public ServiceNotificationClient(Context context) {
         mContext = context;
     }
--- a/mobile/android/base/java/org/mozilla/gecko/overlays/OverlayConstants.java
+++ b/mobile/android/base/java/org/mozilla/gecko/overlays/OverlayConstants.java
@@ -24,17 +24,17 @@ public class OverlayConstants {
      */
     public static final String ACTION_PREPARE_SHARE = "org.mozilla.gecko.overlays.ACTION_PREPARE_SHARE";
 
     /*
      * Action for sharing a page.
      *
      * Intent parameters:
      *
-     * $EXTRA_URL: URL of page to share.    (required)
+     * $EXTRA_ORIGIN: URL of page to share.    (required)
      * $EXTRA_SHARE_METHOD: Method(s) via which to share this url/title combination. Can be either a
      *                ShareType or a ShareType[]
      * $EXTRA_TITLE: Title of page to share (optional)
      * $EXTRA_PARAMETERS: Parcelable of extra data to pass to the ShareMethod (optional)
      */
     public static final String ACTION_SHARE = "org.mozilla.gecko.overlays.ACTION_SHARE";
 
     /*
--- a/mobile/android/modules/Notifications.jsm
+++ b/mobile/android/modules/Notifications.jsm
@@ -247,16 +247,20 @@ var Notifications = {
             handler.onCancel(cookie);
           });
         }
 
         if (notification && notification._onCancel)
           notification._onCancel(id, notification._cookie);
         delete _notificationsMap[id]; // since the notification was dismissed, we no longer need to hold a reference.
         break;
+      case "notification-mute":
+        let url = data.origin;
+        let type = "desktop-notification";
+        Services.perms.addFromPrincipal(url, type, Ci.nsIPermissionManager.DENY_ACTION);
     }
   },
 
   QueryInterface: function (aIID) {
     if (!aIID.equals(Ci.nsISupports) &&
         !aIID.equals(Ci.nsIObserver) &&
         !aIID.equals(Ci.nsISupportsWeakReference))
       throw Components.results.NS_ERROR_NO_INTERFACE;