OSDN Git Service

Force condition provider unsubscribe when exiting zen mode.
authorJohn Spurlock <jspurlock@google.com>
Sun, 27 Apr 2014 20:42:29 +0000 (16:42 -0400)
committerJohn Spurlock <jspurlock@google.com>
Sun, 27 Apr 2014 21:41:51 +0000 (17:41 -0400)
Bug:13743109
Change-Id: I3c205067498a86e2862a0c545bc38e41682693d5

core/java/android/service/notification/ConditionProviderService.java
services/core/java/com/android/server/notification/ConditionProviders.java
services/core/java/com/android/server/notification/NotificationManagerService.java
services/core/java/com/android/server/notification/ZenModeHelper.java

index 70d474e..d6ef8f5 100644 (file)
@@ -69,8 +69,13 @@ public abstract class ConditionProviderService extends Service {
         return mNoMan;
     }
 
-    public final void notifyConditions(Condition[] conditions) {
-        if (!isBound()) return;
+    public final void notifyCondition(Condition condition) {
+        if (condition == null) return;
+        notifyConditions(new Condition[]{ condition });
+    }
+
+    public final void notifyConditions(Condition... conditions) {
+        if (!isBound() || conditions == null) return;
         try {
             getNotificationInterface().notifyConditions(getPackageName(), mProvider, conditions);
         } catch (android.os.RemoteException ex) {
index 2c5d69c..29af433 100644 (file)
@@ -23,6 +23,7 @@ import android.os.IBinder;
 import android.os.IInterface;
 import android.os.RemoteException;
 import android.provider.Settings;
+import android.provider.Settings.Global;
 import android.service.notification.Condition;
 import android.service.notification.ConditionProviderService;
 import android.service.notification.IConditionListener;
@@ -51,6 +52,7 @@ public class ConditionProviders extends ManagedServices {
             UserProfiles userProfiles, ZenModeHelper zenModeHelper) {
         super(context, handler, new Object(), userProfiles);
         mZenModeHelper = zenModeHelper;
+        mZenModeHelper.addCallback(new ZenModeHelperCallback());
     }
 
     @Override
@@ -235,4 +237,21 @@ public class ConditionProviders extends ManagedServices {
             }
         }
     }
+
+    private class ZenModeHelperCallback extends ZenModeHelper.Callback {
+        @Override
+        void onZenModeChanged() {
+            final int mode = mZenModeHelper.getZenMode();
+            if (mode == Global.ZEN_MODE_OFF) {
+                synchronized (mMutex) {
+                    if (mCurrentConditionId != null) {
+                        if (DEBUG) Slog.d(TAG, "Zen mode off, forcing unsubscribe from "
+                                + mCurrentConditionId);
+                        unsubscribeLocked(mCurrentConditionId);
+                        mCurrentConditionId = null;
+                    }
+                }
+            }
+        }
+    }
 }
index 96e3a9f..6e4eb56 100644 (file)
@@ -840,7 +840,7 @@ public class NotificationManagerService extends SystemService {
 
         mHandler = new WorkerHandler();
         mZenModeHelper = new ZenModeHelper(getContext(), mHandler);
-        mZenModeHelper.setCallback(new ZenModeHelper.Callback() {
+        mZenModeHelper.addCallback(new ZenModeHelper.Callback() {
             @Override
             public void onConfigChanged() {
                 savePolicyFile();
index b06e0cb..137730a 100644 (file)
@@ -46,6 +46,7 @@ import org.xmlpull.v1.XmlSerializer;
 
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Calendar;
 import java.util.Date;
@@ -69,8 +70,8 @@ public class ZenModeHelper {
     private final SettingsObserver mSettingsObserver;
     private final AppOpsManager mAppOps;
     private final ZenModeConfig mDefaultConfig;
+    private final ArrayList<Callback> mCallbacks = new ArrayList<Callback>();
 
-    private Callback mCallback;
     private int mZenMode;
     private ZenModeConfig mConfig;
 
@@ -115,8 +116,8 @@ public class ZenModeHelper {
         return new ZenModeConfig();
     }
 
-    public void setCallback(Callback callback) {
-        mCallback = callback;
+    public void addCallback(Callback callback) {
+        mCallbacks.add(callback);
     }
 
     public boolean shouldIntercept(String pkg, Notification n) {
@@ -132,6 +133,10 @@ public class ZenModeHelper {
         return false;
     }
 
+    public int getZenMode() {
+        return mZenMode;
+    }
+
     public void setZenMode(int zenModeValue) {
         Global.putInt(mContext.getContentResolver(), Global.ZEN_MODE, zenModeValue);
     }
@@ -161,6 +166,7 @@ public class ZenModeHelper {
         mAppOps.setRestriction(AppOpsManager.OP_VIBRATE, AudioManager.USE_DEFAULT_STREAM_TYPE,
                 zen ? AppOpsManager.MODE_IGNORED : AppOpsManager.MODE_ALLOWED,
                 exceptionPackages);
+        dispatchOnZenModeChanged();
     }
 
     public boolean allowDisable(int what, IBinder token, String pkg) {
@@ -197,7 +203,7 @@ public class ZenModeHelper {
         if (config.equals(mConfig)) return true;
         mConfig = config;
         Slog.d(TAG, "mConfig=" + mConfig);
-        if (mCallback != null) mCallback.onConfigChanged();
+        dispatchOnConfigChanged();
         final String val = Integer.toString(mConfig.hashCode());
         Global.putString(mContext.getContentResolver(), Global.ZEN_MODE_CONFIG_ETAG, val);
         updateAlarms();
@@ -205,6 +211,18 @@ public class ZenModeHelper {
         return true;
     }
 
+    private void dispatchOnConfigChanged() {
+        for (Callback callback : mCallbacks) {
+            callback.onConfigChanged();
+        }
+    }
+
+    private void dispatchOnZenModeChanged() {
+        for (Callback callback : mCallbacks) {
+            callback.onZenModeChanged();
+        }
+    }
+
     private boolean isCall(String pkg, Notification n) {
         return CALL_PACKAGES.contains(pkg);
     }
@@ -310,7 +328,8 @@ public class ZenModeHelper {
         }
     }
 
-    public interface Callback {
-        void onConfigChanged();
+    public static class Callback {
+        void onConfigChanged() {}
+        void onZenModeChanged() {}
     }
 }