Move the CryptKeeper to Settings.

Change-Id: I59e46366e1911ae7fd37f07719f1e7ba0825733c
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 17c2fcd..66bb1e4 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -48,6 +48,7 @@
     <uses-permission android:name="android.permission.READ_SYNC_STATS" />
     <uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
     <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
+    <uses-permission android:name="android.permission.STATUS_BAR" />
 
     <application android:label="@string/settings_label"
             android:icon="@mipmap/ic_launcher_settings"
@@ -1061,6 +1062,18 @@
         <activity android:name="com.android.settings.accounts.ChooseAccountActivity"
             android:label="@string/header_add_an_account"
             android:theme="@android:style/Theme.Holo.DialogWhenLarge"/>
+            
+        <activity android:name=".CryptKeeper"
+                  android:immersive="true"
+                  android:launchMode="singleTop"
+                  android:theme="@android:style/Theme.Holo.NoActionBar"
+                  android:windowSoftInputMode="stateAlwaysHidden">
+            <intent-filter android:priority="10">
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.HOME" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
+        </activity>
 
         <receiver android:name=".widget.SettingsAppWidgetProvider"
                 android:label="@string/gadget_title"
diff --git a/res/layout-xlarge-land/crypt_keeper.xml b/res/layout-xlarge-land/crypt_keeper.xml
new file mode 100644
index 0000000..892cf52
--- /dev/null
+++ b/res/layout-xlarge-land/crypt_keeper.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+**
+** Copyright 2011, The Android Open Source Project
+**
+** Licensed under the Apache License, Version 2.0 (the "License")
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+**
+**     http://www.apache.org/licenses/LICENSE-2.0
+**
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+*/
+-->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:orientation="vertical"
+>
+    <include layout="@layout/crypt_keeper_content" />
+    
+</LinearLayout>
\ No newline at end of file
diff --git a/res/layout-xlarge/crypt_keeper.xml b/res/layout-xlarge/crypt_keeper.xml
new file mode 100644
index 0000000..7cd82c5
--- /dev/null
+++ b/res/layout-xlarge/crypt_keeper.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+**
+** Copyright 2011, The Android Open Source Project
+**
+** Licensed under the Apache License, Version 2.0 (the "License")
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+**
+**     http://www.apache.org/licenses/LICENSE-2.0
+**
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+*/
+-->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:orientation="horizontal"
+>
+    <include layout="@layout/crypt_keeper_content" />
+    
+</LinearLayout>
\ No newline at end of file
diff --git a/res/layout/crypt_keeper_content.xml b/res/layout/crypt_keeper_content.xml
new file mode 100644
index 0000000..fa337e8
--- /dev/null
+++ b/res/layout/crypt_keeper_content.xml
@@ -0,0 +1,65 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+**
+** Copyright 2011, The Android Open Source Project
+**
+** Licensed under the Apache License, Version 2.0 (the "License")
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+**
+**     http://www.apache.org/licenses/LICENSE-2.0
+**
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+*/
+-->
+
+<merge xmlns:android="http://schemas.android.com/apk/res/android">
+   <LinearLayout
+           android:layout_height="match_parent"
+           android:layout_weight="1"
+           android:layout_width="0dip"
+           android:orientation="vertical"
+           android:gravity="center_vertical"
+               >
+       <include layout="@layout/crypt_keeper_status" />
+   </LinearLayout>
+   
+   <!-- right side: password -->
+   <LinearLayout
+       android:layout_width="0dip"
+       android:layout_height="match_parent"
+       android:orientation="vertical"
+       android:layout_weight="1"
+       android:gravity="center">
+
+       <!-- Password entry field -->
+       <EditText android:id="@+id/passwordEntry"
+           android:layout_height="wrap_content"
+           android:layout_width="450dip"
+           android:singleLine="true"
+           android:textStyle="normal"
+           android:inputType="textPassword"
+           android:gravity="center"
+           android:layout_gravity="center"
+           android:textSize="24sp"
+           android:layout_marginTop="120dip"
+           android:layout_marginBottom="5dip"
+           android:textAppearance="?android:attr/textAppearanceMedium"
+           android:textColor="#ffffffff"
+           android:editable="false"
+        />
+
+       <com.android.internal.widget.PasswordEntryKeyboardView android:id="@+id/keyboard"
+           android:layout_width="450dip"
+           android:layout_height="230dip"
+           android:background="#00000000"
+           android:keyBackground="@*android:drawable/btn_keyboard_key_fulltrans"
+           android:visibility="visible"
+       />
+       
+   </LinearLayout>
+</merge>
\ No newline at end of file
diff --git a/res/layout/crypt_keeper_status.xml b/res/layout/crypt_keeper_status.xml
new file mode 100644
index 0000000..ba2584f
--- /dev/null
+++ b/res/layout/crypt_keeper_status.xml
@@ -0,0 +1,102 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+**
+** Copyright 2011, The Android Open Source Project
+**
+** Licensed under the Apache License, Version 2.0 (the "License")
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+**
+**     http://www.apache.org/licenses/LICENSE-2.0
+**
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+*/
+-->
+
+<!-- Status to show on the left side of lock screen -->
+<LinearLayout
+        xmlns:android="http://schemas.android.com/apk/res/android"
+        android:orientation="vertical"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginLeft="140dip"
+        android:layout_marginTop="20dip"
+        android:gravity="left"
+        >
+
+    <com.android.internal.widget.DigitalClock android:id="@+id/time"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_alignParentTop="true"
+        android:layout_alignParentLeft="true"
+        android:layout_marginTop="8dip"
+        android:layout_marginBottom="8dip"
+        android:layout_marginLeft="-10dip"
+        >
+
+        <!-- Because we can't have multi-tone fonts, we render two TextViews, one on
+        top of the other. Hence the redundant layout... -->
+        <TextView android:id="@*android:id/timeDisplayBackground"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:singleLine="true"
+            android:ellipsize="none"
+            android:textSize="120sp"
+            android:textAppearance="?android:attr/textAppearanceMedium"
+            android:textColor="@color/crypt_keeper_clock_background"
+            android:layout_marginBottom="6dip"
+            />
+
+        <TextView android:id="@*android:id/timeDisplayForeground"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:singleLine="true"
+            android:ellipsize="none"
+            android:textSize="120sp"
+            android:textAppearance="?android:attr/textAppearanceMedium"
+            android:textColor="@color/crypt_keeper_clock_foreground"
+            android:layout_alignLeft="@*android:id/timeDisplayBackground"
+            android:layout_alignTop="@*android:id/timeDisplayBackground"
+            android:layout_marginBottom="6dip"
+            />
+
+        <TextView android:id="@*android:id/am_pm"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_toRightOf="@*android:id/timeDisplayBackground"
+            android:layout_alignBaseline="@*android:id/timeDisplayBackground"
+            android:singleLine="true"
+            android:ellipsize="none"
+            android:textSize="30sp"
+            android:layout_marginLeft="8dip"
+            android:textAppearance="?android:attr/textAppearanceMedium"
+            android:textColor="@color/crypt_keeper_clock_am_pm"
+            />
+
+    </com.android.internal.widget.DigitalClock>
+
+    <TextView
+        android:id="@+id/date"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_below="@id/time"
+        android:layout_marginTop="10dip"
+        android:textAppearance="?android:attr/textAppearanceMedium"
+        android:textSize="22sp"
+        />
+
+    <TextView
+        android:id="@+id/status"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginTop="10dip"
+        android:textSize="22sp"
+        android:textAppearance="?android:attr/textAppearanceMedium"
+        android:text="@string/enter_password"
+        android:drawableLeft="@*android:drawable/ic_lock_idle_lock"
+        />
+</LinearLayout>
diff --git a/res/values/colors.xml b/res/values/colors.xml
index 8a2b68c..0b8beb6 100644
--- a/res/values/colors.xml
+++ b/res/values/colors.xml
@@ -22,5 +22,9 @@
     <color name="memory_media_usage">#F33</color>
     <color name="memory_apps_usage">#3F3</color>
     <color name="memory_used">#FFF</color>
+    
+    <color name="crypt_keeper_clock_background">#ff9a9a9a</color>
+    <color name="crypt_keeper_clock_foreground">#ff666666</color>
+    <color name="crypt_keeper_clock_am_pm">#ff9a9a9a</color>
 </resources>
          
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 7012440..86f4e85 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -3112,4 +3112,9 @@
     <!-- Messaage shown in dialog when you can't manually sync -->
     <string name="cant_sync_dialog_message">Sync for this item is currently disabled. To change your preference, temporarily turn on background data and automatic sync.</string>
 
+    <!-- This is displayed to the user when the device needs to be decrypted -->
+    <string name="enter_password">Enter password to decrypt storage</string>
+    <!-- This is displayed when the password is entered incorrectly -->
+    <string name="try_again">Sorry, try again</string>
+
 </resources>
diff --git a/src/com/android/settings/CryptKeeper.java b/src/com/android/settings/CryptKeeper.java
new file mode 100644
index 0000000..7b07c64
--- /dev/null
+++ b/src/com/android/settings/CryptKeeper.java
@@ -0,0 +1,116 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings;
+
+import com.android.internal.widget.PasswordEntryKeyboardHelper;
+import com.android.internal.widget.PasswordEntryKeyboardView;
+
+import android.app.Activity;
+import android.app.StatusBarManager;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.pm.PackageManager;
+import android.inputmethodservice.KeyboardView;
+import android.os.Bundle;
+import android.os.IBinder;
+import android.os.ServiceManager;
+import android.os.SystemProperties;
+import android.os.storage.IMountService;
+import android.text.format.DateFormat;
+import android.util.Log;
+import android.view.KeyEvent;
+import android.view.inputmethod.EditorInfo;
+import android.widget.TextView;
+
+import java.util.Date;
+
+public class CryptKeeper extends Activity implements TextView.OnEditorActionListener {
+    private static final String DECRYPT_STATE = "trigger_restart_framework";
+    
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.crypt_keeper);
+        
+        String state = SystemProperties.get("vold.decrypt");
+        if ("".equals(state) || DECRYPT_STATE.equals(state)) {
+            PackageManager pm = getPackageManager();
+            ComponentName name = new ComponentName(this, CryptKeeper.class);
+            pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);
+            return;
+        }
+        
+        TextView passwordEntry = (TextView) findViewById(R.id.passwordEntry);
+        passwordEntry.setOnEditorActionListener(this);
+        
+        KeyboardView keyboardView = (PasswordEntryKeyboardView) findViewById(R.id.keyboard);
+        
+        PasswordEntryKeyboardHelper keyboardHelper = new PasswordEntryKeyboardHelper(this,
+                keyboardView, passwordEntry, false);
+        keyboardHelper.setKeyboardMode(PasswordEntryKeyboardHelper.KEYBOARD_MODE_ALPHA);
+
+
+        passwordEntry.setCompoundDrawablesWithIntrinsicBounds(android.R.drawable.ic_lock_idle_lock,
+                0, 0, 0);
+
+        String dateFormatString = getString(com.android.internal.R.string.full_wday_month_day_no_year);
+        TextView date = (TextView) findViewById(R.id.date);
+        date.setText(DateFormat.format(dateFormatString, new Date()));
+
+        // Disable the status bar
+        StatusBarManager sbm = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
+        sbm.disable(StatusBarManager.DISABLE_EXPAND | StatusBarManager.DISABLE_NOTIFICATION_ICONS
+                | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
+                | StatusBarManager.DISABLE_SYSTEM_INFO | StatusBarManager.DISABLE_NAVIGATION);
+    }
+
+    private IMountService getMountService() {
+        IBinder service = ServiceManager.getService("mount");
+        if (service != null) {
+            return IMountService.Stub.asInterface(service);
+        }
+        return null;
+    }
+
+    @Override
+    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
+        if (actionId == EditorInfo.IME_NULL) {
+            // Get the password
+            String password = v.getText().toString();
+
+            // Now that we have the password clear the password field.
+            v.setText(null);
+
+            IMountService service = getMountService();
+            try {
+                service.decryptStorage(password);
+
+                // For now the only way to get here is for the password to be
+                // wrong.
+
+                TextView tv = (TextView) findViewById(R.id.status);
+                tv.setText(R.string.try_again);
+
+            } catch (Exception e) {
+                Log.e("CryptKeeper", "Error while decrypting...", e);
+            }
+            
+            return true;
+        }
+        return false;
+    }
+}
\ No newline at end of file