Merge "Disable zen mode UI until provisioned."
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/BaseStatusBar.java b/packages/SystemUI/src/com/android/systemui/statusbar/BaseStatusBar.java
index 5f62554..c3ed1f7 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/BaseStatusBar.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/BaseStatusBar.java
@@ -1023,6 +1023,7 @@
     }
 
     protected void setZenMode(int mode) {
+        if (!isDeviceProvisioned()) return;
         final boolean change = mZenMode != mode;
         mZenMode = mode;
         final int N = mNotificationData.size();
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
index 51c7b96..51d0669 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
@@ -346,6 +346,7 @@
     public void setZenMode(int mode) {
         super.setZenMode(mode);
         if (mModeIcon == null) return;
+        if (!isDeviceProvisioned()) return;
         final boolean limited = mode == Settings.Global.ZEN_MODE_LIMITED;
         final boolean full = mode == Settings.Global.ZEN_MODE_FULL;
         mModeIcon.setVisibility(full || limited ? View.VISIBLE : View.GONE);
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/ZenModeView.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/ZenModeView.java
index a89dacf..f5dc4d9 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/ZenModeView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/ZenModeView.java
@@ -175,6 +175,10 @@
         addView(mHintText, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
     }
 
+    private boolean isApplicable() {
+        return mAdapter != null && mAdapter.isApplicable();
+    }
+
     private void close() {
         mClosing = true;
         final int startBottom = mBottom;
@@ -217,6 +221,11 @@
     }
 
     private void updateState(boolean animate) {
+        final boolean applicable = isApplicable();
+        setVisibility(applicable ? VISIBLE : GONE);
+        if (!applicable) {
+            return;
+        }
         if (mAdapter != null && mAdapter.getMode() == Adapter.MODE_OFF && !mPeekable) {
             close();
         } else {
@@ -303,7 +312,8 @@
     public boolean onInterceptTouchEvent(MotionEvent ev) {
         final boolean rt = super.onInterceptTouchEvent(ev);
         if (DEBUG) logTouchEvent("onInterceptTouchEvent", rt, ev);
-        if (ev.getAction() == MotionEvent.ACTION_DOWN
+        if (isApplicable()
+                && ev.getAction() == MotionEvent.ACTION_DOWN
                 && ev.getY() > mCloseButton.getBottom()
                 && mPeekable) {
             return true;
@@ -326,7 +336,7 @@
     public boolean onTouchEvent(MotionEvent event) {
         boolean rt = super.onTouchEvent(event);
         if (DEBUG) logTouchEvent("onTouchEvent", rt, event);
-        if (!mPeekable) {
+        if (!isApplicable() || !mPeekable) {
             return rt;
         }
         if (event.getAction() == MotionEvent.ACTION_DOWN) {
@@ -398,7 +408,9 @@
     }
 
     public void dispatchExternalTouchEvent(MotionEvent ev) {
-        onTouchEvent(ev);
+        if (isApplicable()) {
+            onTouchEvent(ev);
+        }
     }
 
     private static void bounce(final View v, final Runnable midBounce) {
@@ -584,6 +596,7 @@
         public static final int MODE_LIMITED = 1;
         public static final int MODE_FULL = 2;
 
+        boolean isApplicable();
         int getMode();
         void setMode(int mode);
         void select(ExitCondition ec);
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/ZenModeViewAdapter.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/ZenModeViewAdapter.java
index 2078b3c..c9ac89f 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/ZenModeViewAdapter.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/ZenModeViewAdapter.java
@@ -33,27 +33,30 @@
     private final Context mContext;
     private final ContentResolver mResolver;
     private final Handler mHandler = new Handler();
+    private final SettingsObserver mObserver;
     private final List<ExitCondition> mExits = Arrays.asList(
             newExit("Until you delete this", "Until", "You delete this"));
 
     private Callbacks mCallbacks;
     private int mExitIndex;
+    private boolean mDeviceProvisioned;
+    private int mMode;
 
     public ZenModeViewAdapter(Context context) {
         mContext = context;
         mResolver = mContext.getContentResolver();
-        mResolver.registerContentObserver(
-                Settings.Global.getUriFor(Settings.Global.ZEN_MODE),
-                false, new SettingsObserver(mHandler));
+        mObserver = new SettingsObserver(mHandler);
+        mObserver.init();
+    }
+
+    @Override
+    public boolean isApplicable() {
+        return mDeviceProvisioned;
     }
 
     @Override
     public int getMode() {
-        final int v = Settings.Global.getInt(mContext.getContentResolver(),
-                Settings.Global.ZEN_MODE, Settings.Global.ZEN_MODE_OFF);
-        if (v == Settings.Global.ZEN_MODE_LIMITED) return MODE_LIMITED;
-        if (v == Settings.Global.ZEN_MODE_FULL) return MODE_FULL;
-        return MODE_OFF;
+        return mMode;
     }
 
     @Override
@@ -137,9 +140,34 @@
             super(handler);
         }
 
+        public void init() {
+            loadSettings();
+            mResolver.registerContentObserver(
+                    Settings.Global.getUriFor(Settings.Global.ZEN_MODE),
+                    false, this);
+            mResolver.registerContentObserver(
+                    Settings.Global.getUriFor(Settings.Global.DEVICE_PROVISIONED),
+                    false, this);
+        }
+
         @Override
         public void onChange(boolean selfChange) {
+            loadSettings();
             mChange.run();  // already on handler
         }
+
+        private void loadSettings() {
+            mDeviceProvisioned = Settings.Global.getInt(mResolver,
+                    Settings.Global.DEVICE_PROVISIONED, 0) != 0;
+            mMode = getMode();
+        }
+
+        private int getMode() {
+            final int v = Settings.Global.getInt(mResolver,
+                    Settings.Global.ZEN_MODE, Settings.Global.ZEN_MODE_OFF);
+            if (v == Settings.Global.ZEN_MODE_LIMITED) return MODE_LIMITED;
+            if (v == Settings.Global.ZEN_MODE_FULL) return MODE_FULL;
+            return MODE_OFF;
+        }
     }
 }