blob: 49d027ba49241d628756dc9d173322b63158e003 [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
Doris Ling72489722017-11-16 11:03:40 -080019import android.annotation.Nullable;
Jason parksf8217302011-01-26 13:11:42 -060020import android.app.Activity;
Jason parksf8217302011-01-26 13:11:42 -060021import android.app.StatusBarManager;
Fan Zhang31b21002019-01-16 13:49:47 -080022import android.app.settings.SettingsEnums;
Jason parksf8217302011-01-26 13:11:42 -060023import android.content.Context;
24import android.content.Intent;
25import android.os.Bundle;
26import android.os.Handler;
27import android.os.IBinder;
28import android.os.ServiceManager;
Paul Lawrence0f11e152014-08-20 07:43:32 -070029import android.os.UserHandle;
Sudheer Shankaee2d5922016-11-09 15:47:53 -080030import android.os.storage.IStorageManager;
Paul Lawrencec04420c2015-05-18 13:25:01 -070031import android.provider.Settings;
Jason parksf8217302011-01-26 13:11:42 -060032import android.util.Log;
33import android.view.LayoutInflater;
34import android.view.View;
35import android.view.ViewGroup;
36import android.widget.Button;
37
Paul Lawrence0f11e152014-08-20 07:43:32 -070038import com.android.internal.widget.LockPatternUtils;
Doris Ling72489722017-11-16 11:03:40 -080039import com.android.settings.core.InstrumentedFragment;
Paul Lawrence0f11e152014-08-20 07:43:32 -070040
Rich Canningsb27c4302019-02-19 13:15:30 -080041import java.util.Arrays;
Elliott Hughesed6a6ca2014-09-26 12:29:12 -070042import java.util.Locale;
43
Doris Ling72489722017-11-16 11:03:40 -080044public class CryptKeeperConfirm extends InstrumentedFragment {
Jason parksf8217302011-01-26 13:11:42 -060045
Elliott Hughesed6a6ca2014-09-26 12:29:12 -070046 private static final String TAG = "CryptKeeperConfirm";
47
Chris Wren8a963ba2015-03-20 10:29:14 -040048 @Override
Fan Zhang65076132016-08-08 10:25:13 -070049 public int getMetricsCategory() {
Fan Zhang31b21002019-01-16 13:49:47 -080050 return SettingsEnums.CRYPT_KEEPER_CONFIRM;
Chris Wren8a963ba2015-03-20 10:29:14 -040051 }
52
Jason parksf8217302011-01-26 13:11:42 -060053 public static class Blank extends Activity {
54 private Handler mHandler = new Handler();
55
56 @Override
57 public void onCreate(Bundle savedInstanceState) {
58 super.onCreate(savedInstanceState);
59
Andy Stadler13d62042011-01-31 19:21:37 -080060 setContentView(R.layout.crypt_keeper_blank);
61
Jason parksf8217302011-01-26 13:11:42 -060062 if (Utils.isMonkeyRunning()) {
63 finish();
64 }
65
66 StatusBarManager sbm = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
67 sbm.disable(StatusBarManager.DISABLE_EXPAND
68 | StatusBarManager.DISABLE_NOTIFICATION_ICONS
69 | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
70 | StatusBarManager.DISABLE_SYSTEM_INFO
Daniel Sandler4d2bfd12011-10-12 15:34:23 -040071 | StatusBarManager.DISABLE_HOME
Chris Wren0481dc72013-10-07 01:11:08 -040072 | StatusBarManager.DISABLE_SEARCH
Daniel Sandler4d2bfd12011-10-12 15:34:23 -040073 | StatusBarManager.DISABLE_RECENT
Andy Stadler13d62042011-01-31 19:21:37 -080074 | StatusBarManager.DISABLE_BACK);
Jason parksf8217302011-01-26 13:11:42 -060075
76 // Post a delayed message in 700 milliseconds to enable encryption.
77 // NOTE: The animation on this activity is set for 500 milliseconds
78 // I am giving it a little extra time to complete.
79 mHandler.postDelayed(new Runnable() {
80 public void run() {
81 IBinder service = ServiceManager.getService("mount");
82 if (service == null) {
Jason parks00046d62011-06-13 17:38:45 -050083 Log.e("CryptKeeper", "Failed to find the mount service");
84 finish();
Jason parksf8217302011-01-26 13:11:42 -060085 return;
86 }
87
Sudheer Shankaee2d5922016-11-09 15:47:53 -080088 IStorageManager storageManager = IStorageManager.Stub.asInterface(service);
Jason parksf8217302011-01-26 13:11:42 -060089 try {
90 Bundle args = getIntent().getExtras();
Rich Canningsb27c4302019-02-19 13:15:30 -080091 // TODO(b/120484642): Update vold to accept a password as a byte array
92 byte[] passwordBytes = args.getByteArray("password");
93 String password = passwordBytes != null ? new String(passwordBytes) : null;
94 Arrays.fill(passwordBytes, (byte) 0);
95 storageManager.encryptStorage(args.getInt("type", -1),
96 password);
Jason parksf8217302011-01-26 13:11:42 -060097 } catch (Exception e) {
98 Log.e("CryptKeeper", "Error while encrypting...", e);
99 }
100 }
101 }, 700);
102 }
103 }
104
105 private View mContentView;
106 private Button mFinalButton;
107 private Button.OnClickListener mFinalClickListener = new Button.OnClickListener() {
108
109 public void onClick(View v) {
110 if (Utils.isMonkeyRunning()) {
111 return;
112 }
113
Paul Lawrence0f11e152014-08-20 07:43:32 -0700114 /* WARNING - nasty hack!
115 Settings for the lock screen are not available to the crypto
116 screen (CryptKeeper) at boot. We duplicate the ones that
117 CryptKeeper needs to the crypto key/value store when they are
118 modified (see LockPatternUtils).
119 However, prior to encryption, the crypto key/value store is not
120 persisted across reboots, thus modified settings are lost to
121 CryptKeeper.
122 In order to make sure CryptKeeper had the correct settings after
123 first encrypting, we thus need to rewrite them, which ensures the
124 crypto key/value store is up to date. On encryption, this store
125 is then persisted, and the settings will be there on future
126 reboots.
127 */
Elliott Hughesed6a6ca2014-09-26 12:29:12 -0700128
129 // 1. The owner info.
Paul Lawrence0f11e152014-08-20 07:43:32 -0700130 LockPatternUtils utils = new LockPatternUtils(getActivity());
Adrian Roos54375882015-04-16 17:11:22 -0700131 utils.setVisiblePatternEnabled(
Xiaohui Chenb7c8fbf2015-08-27 15:46:32 -0700132 utils.isVisiblePatternEnabled(UserHandle.USER_SYSTEM),
133 UserHandle.USER_SYSTEM);
134 if (utils.isOwnerInfoEnabled(UserHandle.USER_SYSTEM)) {
135 utils.setOwnerInfo(utils.getOwnerInfo(UserHandle.USER_SYSTEM),
136 UserHandle.USER_SYSTEM);
Paul Lawrence0f11e152014-08-20 07:43:32 -0700137 }
Paul Lawrencec04420c2015-05-18 13:25:01 -0700138 int value = Settings.System.getInt(getContext().getContentResolver(),
139 Settings.System.TEXT_SHOW_PASSWORD,
140 1);
Xiaohui Chenb7c8fbf2015-08-27 15:46:32 -0700141 utils.setVisiblePasswordEnabled(value != 0, UserHandle.USER_SYSTEM);
Paul Lawrencec04420c2015-05-18 13:25:01 -0700142
Jason parksf8217302011-01-26 13:11:42 -0600143 Intent intent = new Intent(getActivity(), Blank.class);
144 intent.putExtras(getArguments());
Jason parksf8217302011-01-26 13:11:42 -0600145 startActivity(intent);
Elliott Hughesed6a6ca2014-09-26 12:29:12 -0700146
147 // 2. The system locale.
148 try {
149 IBinder service = ServiceManager.getService("mount");
Sudheer Shankaee2d5922016-11-09 15:47:53 -0800150 IStorageManager storageManager = IStorageManager.Stub.asInterface(service);
151 storageManager.setField("SystemLocale", Locale.getDefault().toLanguageTag());
Elliott Hughesed6a6ca2014-09-26 12:29:12 -0700152 } catch (Exception e) {
153 Log.e(TAG, "Error storing locale for decryption UI", e);
154 }
Jason parksf8217302011-01-26 13:11:42 -0600155 }
156 };
157
158 private void establishFinalConfirmationState() {
159 mFinalButton = (Button) mContentView.findViewById(R.id.execute_encrypt);
160 mFinalButton.setOnClickListener(mFinalClickListener);
161 }
162
163 @Override
Doris Ling72489722017-11-16 11:03:40 -0800164 public void onCreate(@Nullable Bundle savedInstanceState) {
165 super.onCreate(savedInstanceState);
166 getActivity().setTitle(R.string.crypt_keeper_confirm_title);
167 }
168
169 @Override
Jason parksf8217302011-01-26 13:11:42 -0600170 public View onCreateView(LayoutInflater inflater, ViewGroup container,
171 Bundle savedInstanceState) {
172 mContentView = inflater.inflate(R.layout.crypt_keeper_confirm, null);
173 establishFinalConfirmationState();
174 return mContentView;
175 }
176}