OSDN Git Service

Zen: follow independent ringer mode changes.
authorJohn Spurlock <jspurlock@google.com>
Mon, 28 Jul 2014 17:37:13 +0000 (13:37 -0400)
committerJohn Spurlock <jspurlock@google.com>
Tue, 29 Jul 2014 00:39:17 +0000 (20:39 -0400)
Bug:16624498
Change-Id: Ib623118970a82c8406c714687b1b516378fe7824

services/core/java/com/android/server/notification/ZenLog.java
services/core/java/com/android/server/notification/ZenModeHelper.java

index 3069ad9..64efa67 100644 (file)
@@ -57,6 +57,7 @@ public class ZenLog {
     private static final int TYPE_SUBSCRIBE = 7;
     private static final int TYPE_UNSUBSCRIBE = 8;
     private static final int TYPE_CONFIG = 9;
+    private static final int TYPE_FOLLOW_RINGER_MODE = 10;
 
     private static int sNext;
     private static int sSize;
@@ -100,6 +101,11 @@ public class ZenLog {
         append(TYPE_CONFIG, newConfig != null ? newConfig.toString() : null);
     }
 
+    public static void traceFollowRingerMode(int ringerMode, int oldZen, int newZen) {
+        append(TYPE_FOLLOW_RINGER_MODE, ringerModeToString(ringerMode) + ", "
+                + zenModeToString(oldZen) + " -> " + zenModeToString(newZen));
+    }
+
     private static String subscribeResult(IConditionProvider provider, RemoteException e) {
         return provider == null ? "no provider" : e != null ? e.getMessage() : "ok";
     }
@@ -115,6 +121,7 @@ public class ZenLog {
             case TYPE_SUBSCRIBE: return "subscribe";
             case TYPE_UNSUBSCRIBE: return "unsubscribe";
             case TYPE_CONFIG: return "config";
+            case TYPE_FOLLOW_RINGER_MODE: return "follow_ringer_mode";
             default: return "unknown";
         }
     }
index 1289cf7..c61ce03 100644 (file)
@@ -96,6 +96,7 @@ public class ZenModeHelper {
         final IntentFilter filter = new IntentFilter();
         filter.addAction(ACTION_ENTER_ZEN);
         filter.addAction(ACTION_EXIT_ZEN);
+        filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
         mContext.registerReceiver(new ZenBroadcastReceiver(), filter);
     }
 
@@ -279,6 +280,27 @@ public class ZenModeHelper {
         return true;
     }
 
+    private void handleRingerModeChanged() {
+        if (mAudioManager != null) {
+            // follow ringer mode if necessary
+            final int ringerMode = mAudioManager.getRingerMode();
+            int newZen = -1;
+            if (ringerMode == AudioManager.RINGER_MODE_SILENT) {
+                if (mZenMode != Global.ZEN_MODE_NO_INTERRUPTIONS) {
+                    newZen = Global.ZEN_MODE_NO_INTERRUPTIONS;
+                }
+            } else if ((ringerMode == AudioManager.RINGER_MODE_NORMAL
+                    || ringerMode == AudioManager.RINGER_MODE_VIBRATE)
+                    && mZenMode == Global.ZEN_MODE_NO_INTERRUPTIONS) {
+                newZen = Global.ZEN_MODE_OFF;
+            }
+            if (newZen != -1) {
+                ZenLog.traceFollowRingerMode(ringerMode, mZenMode, newZen);
+                setZenMode(newZen);
+            }
+        }
+    }
+
     private void dispatchOnConfigChanged() {
         for (Callback callback : mCallbacks) {
             callback.onConfigChanged();
@@ -376,6 +398,13 @@ public class ZenModeHelper {
         return new Date(time) + " (" + time + ")";
     }
 
+    private final Runnable mRingerModeChanged = new Runnable() {
+        @Override
+        public void run() {
+            handleRingerModeChanged();
+        }
+    };
+
     private class SettingsObserver extends ContentObserver {
         private final Uri ZEN_MODE = Global.getUriFor(Global.ZEN_MODE);
 
@@ -410,6 +439,8 @@ public class ZenModeHelper {
                 setZenMode(intent, Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS);
             } else if (ACTION_EXIT_ZEN.equals(intent.getAction())) {
                 setZenMode(intent, Global.ZEN_MODE_OFF);
+            } else if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(intent.getAction())) {
+                mHandler.post(mRingerModeChanged);
             }
         }