Disable CryptKeeper activity for secondary users

For secondary users, disable CryptKeeper activity in a broadcast receiver of
USER_INITIALIZE intent. This change has the following benefits for guest
user switching:
 - The code will be executed earlier in the user switching flow, when the
   screen is frozen by WindowManager.
 - Initialization of CryptKeeperActivity is skipped

Bug:18670536
Change-Id: I60300198b605c26ad15afe2c874d5f1be7da5087
diff --git a/src/com/android/settings/CryptKeeper.java b/src/com/android/settings/CryptKeeper.java
index f08ed5a..08bdc42 100644
--- a/src/com/android/settings/CryptKeeper.java
+++ b/src/com/android/settings/CryptKeeper.java
@@ -17,7 +17,9 @@
 package com.android.settings;
 
 import android.app.Activity;
+import android.app.ActivityManager;
 import android.app.StatusBarManager;
+import android.content.BroadcastReceiver;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
@@ -408,11 +410,7 @@
         // If we are not encrypted or encrypting, get out quickly.
         final String state = SystemProperties.get("vold.decrypt");
         if (!isDebugView() && ("".equals(state) || DECRYPT_STATE.equals(state))) {
-            // Disable the crypt keeper.
-            PackageManager pm = getPackageManager();
-            ComponentName name = new ComponentName(this, CryptKeeper.class);
-            pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
-                    PackageManager.DONT_KILL_APP);
+            disableCryptKeeperComponent(this);
             // Typically CryptKeeper is launched as the home app.  We didn't
             // want to be running, so need to finish this activity.  We can count
             // on the activity manager re-launching the new home app upon finishing
@@ -1021,4 +1019,25 @@
     public void afterTextChanged(Editable s) {
         return;
     }
+
+    private static void disableCryptKeeperComponent(Context context) {
+        PackageManager pm = context.getPackageManager();
+        ComponentName name = new ComponentName(context, CryptKeeper.class);
+        Log.d(TAG, "Disabling component " + name);
+        pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
+                PackageManager.DONT_KILL_APP);
+    }
+
+    public static class UserInitBroadcastReceiver extends BroadcastReceiver {
+
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            final String intentAction = intent.getAction();
+            // Disable CryptKeeper activity if user is not primary
+            if (Intent.ACTION_USER_INITIALIZE.equals(intentAction)
+                    && UserHandle.USER_OWNER != UserHandle.myUserId()) {
+                disableCryptKeeperComponent(context);
+            }
+        }
+    }
 }