blob: d61fd983f3cfd1b734b545083ea36c94657daed5 [file] [log] [blame]
Jason parksf8217302011-01-26 13:11:42 -06001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
19import android.app.Activity;
Jason parksf8217302011-01-26 13:11:42 -060020import android.app.StatusBarManager;
21import android.content.Context;
22import android.content.Intent;
23import android.os.Bundle;
24import android.os.Handler;
25import android.os.IBinder;
26import android.os.ServiceManager;
Paul Lawrence0f11e152014-08-20 07:43:32 -070027import android.os.UserHandle;
Sudheer Shankaee2d5922016-11-09 15:47:53 -080028import android.os.storage.IStorageManager;
Paul Lawrencec04420c2015-05-18 13:25:01 -070029import android.provider.Settings;
Jason parksf8217302011-01-26 13:11:42 -060030import android.util.Log;
31import android.view.LayoutInflater;
32import android.view.View;
33import android.view.ViewGroup;
34import android.widget.Button;
35
Tamas Berghammer265d3c22016-06-22 15:34:45 +010036import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Paul Lawrence0f11e152014-08-20 07:43:32 -070037import com.android.internal.widget.LockPatternUtils;
Fan Zhang2d0b3442016-12-05 17:02:33 -080038import com.android.settings.core.InstrumentedPreferenceFragment;
Paul Lawrence0f11e152014-08-20 07:43:32 -070039
Elliott Hughesed6a6ca2014-09-26 12:29:12 -070040import java.util.Locale;
41
Fan Zhang2d0b3442016-12-05 17:02:33 -080042public class CryptKeeperConfirm extends InstrumentedPreferenceFragment {
Jason parksf8217302011-01-26 13:11:42 -060043
Elliott Hughesed6a6ca2014-09-26 12:29:12 -070044 private static final String TAG = "CryptKeeperConfirm";
45
Chris Wren8a963ba2015-03-20 10:29:14 -040046 @Override
Fan Zhang65076132016-08-08 10:25:13 -070047 public int getMetricsCategory() {
Chris Wren9d1bfd12016-01-26 18:04:01 -050048 return MetricsEvent.CRYPT_KEEPER_CONFIRM;
Chris Wren8a963ba2015-03-20 10:29:14 -040049 }
50
Jason parksf8217302011-01-26 13:11:42 -060051 public static class Blank extends Activity {
52 private Handler mHandler = new Handler();
53
54 @Override
55 public void onCreate(Bundle savedInstanceState) {
56 super.onCreate(savedInstanceState);
57
Andy Stadler13d62042011-01-31 19:21:37 -080058 setContentView(R.layout.crypt_keeper_blank);
59
Jason parksf8217302011-01-26 13:11:42 -060060 if (Utils.isMonkeyRunning()) {
61 finish();
62 }
63
64 StatusBarManager sbm = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
65 sbm.disable(StatusBarManager.DISABLE_EXPAND
66 | StatusBarManager.DISABLE_NOTIFICATION_ICONS
67 | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
68 | StatusBarManager.DISABLE_SYSTEM_INFO
Daniel Sandler4d2bfd12011-10-12 15:34:23 -040069 | StatusBarManager.DISABLE_HOME
Chris Wren0481dc72013-10-07 01:11:08 -040070 | StatusBarManager.DISABLE_SEARCH
Daniel Sandler4d2bfd12011-10-12 15:34:23 -040071 | StatusBarManager.DISABLE_RECENT
Andy Stadler13d62042011-01-31 19:21:37 -080072 | StatusBarManager.DISABLE_BACK);
Jason parksf8217302011-01-26 13:11:42 -060073
74 // Post a delayed message in 700 milliseconds to enable encryption.
75 // NOTE: The animation on this activity is set for 500 milliseconds
76 // I am giving it a little extra time to complete.
77 mHandler.postDelayed(new Runnable() {
78 public void run() {
79 IBinder service = ServiceManager.getService("mount");
80 if (service == null) {
Jason parks00046d62011-06-13 17:38:45 -050081 Log.e("CryptKeeper", "Failed to find the mount service");
82 finish();
Jason parksf8217302011-01-26 13:11:42 -060083 return;
84 }
85
Sudheer Shankaee2d5922016-11-09 15:47:53 -080086 IStorageManager storageManager = IStorageManager.Stub.asInterface(service);
Jason parksf8217302011-01-26 13:11:42 -060087 try {
88 Bundle args = getIntent().getExtras();
Sudheer Shankaee2d5922016-11-09 15:47:53 -080089 storageManager.encryptStorage(args.getInt("type", -1), args.getString("password"));
Jason parksf8217302011-01-26 13:11:42 -060090 } catch (Exception e) {
91 Log.e("CryptKeeper", "Error while encrypting...", e);
92 }
93 }
94 }, 700);
95 }
96 }
97
98 private View mContentView;
99 private Button mFinalButton;
100 private Button.OnClickListener mFinalClickListener = new Button.OnClickListener() {
101
102 public void onClick(View v) {
103 if (Utils.isMonkeyRunning()) {
104 return;
105 }
106
Paul Lawrence0f11e152014-08-20 07:43:32 -0700107 /* WARNING - nasty hack!
108 Settings for the lock screen are not available to the crypto
109 screen (CryptKeeper) at boot. We duplicate the ones that
110 CryptKeeper needs to the crypto key/value store when they are
111 modified (see LockPatternUtils).
112 However, prior to encryption, the crypto key/value store is not
113 persisted across reboots, thus modified settings are lost to
114 CryptKeeper.
115 In order to make sure CryptKeeper had the correct settings after
116 first encrypting, we thus need to rewrite them, which ensures the
117 crypto key/value store is up to date. On encryption, this store
118 is then persisted, and the settings will be there on future
119 reboots.
120 */
Elliott Hughesed6a6ca2014-09-26 12:29:12 -0700121
122 // 1. The owner info.
Paul Lawrence0f11e152014-08-20 07:43:32 -0700123 LockPatternUtils utils = new LockPatternUtils(getActivity());
Adrian Roos54375882015-04-16 17:11:22 -0700124 utils.setVisiblePatternEnabled(
Xiaohui Chenb7c8fbf2015-08-27 15:46:32 -0700125 utils.isVisiblePatternEnabled(UserHandle.USER_SYSTEM),
126 UserHandle.USER_SYSTEM);
127 if (utils.isOwnerInfoEnabled(UserHandle.USER_SYSTEM)) {
128 utils.setOwnerInfo(utils.getOwnerInfo(UserHandle.USER_SYSTEM),
129 UserHandle.USER_SYSTEM);
Paul Lawrence0f11e152014-08-20 07:43:32 -0700130 }
Paul Lawrencec04420c2015-05-18 13:25:01 -0700131 int value = Settings.System.getInt(getContext().getContentResolver(),
132 Settings.System.TEXT_SHOW_PASSWORD,
133 1);
Xiaohui Chenb7c8fbf2015-08-27 15:46:32 -0700134 utils.setVisiblePasswordEnabled(value != 0, UserHandle.USER_SYSTEM);
Paul Lawrencec04420c2015-05-18 13:25:01 -0700135
Jason parksf8217302011-01-26 13:11:42 -0600136 Intent intent = new Intent(getActivity(), Blank.class);
137 intent.putExtras(getArguments());
Jason parksf8217302011-01-26 13:11:42 -0600138 startActivity(intent);
Elliott Hughesed6a6ca2014-09-26 12:29:12 -0700139
140 // 2. The system locale.
141 try {
142 IBinder service = ServiceManager.getService("mount");
Sudheer Shankaee2d5922016-11-09 15:47:53 -0800143 IStorageManager storageManager = IStorageManager.Stub.asInterface(service);
144 storageManager.setField("SystemLocale", Locale.getDefault().toLanguageTag());
Elliott Hughesed6a6ca2014-09-26 12:29:12 -0700145 } catch (Exception e) {
146 Log.e(TAG, "Error storing locale for decryption UI", e);
147 }
Jason parksf8217302011-01-26 13:11:42 -0600148 }
149 };
150
151 private void establishFinalConfirmationState() {
152 mFinalButton = (Button) mContentView.findViewById(R.id.execute_encrypt);
153 mFinalButton.setOnClickListener(mFinalClickListener);
154 }
155
156 @Override
157 public View onCreateView(LayoutInflater inflater, ViewGroup container,
158 Bundle savedInstanceState) {
159 mContentView = inflater.inflate(R.layout.crypt_keeper_confirm, null);
160 establishFinalConfirmationState();
161 return mContentView;
162 }
163}