• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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 
17 package com.android.settings;
18 
19 import com.android.internal.widget.LockPatternUtils;
20 
21 import android.app.Activity;
22 import android.app.AlertDialog;
23 import android.app.Fragment;
24 import android.app.admin.DevicePolicyManager;
25 import android.content.BroadcastReceiver;
26 import android.content.Context;
27 import android.content.Intent;
28 import android.content.IntentFilter;
29 import android.content.res.Resources;
30 import android.os.BatteryManager;
31 import android.os.Bundle;
32 import android.os.storage.StorageManager;
33 import android.preference.Preference;
34 import android.text.TextUtils;
35 import android.view.LayoutInflater;
36 import android.view.View;
37 import android.view.ViewGroup;
38 import android.widget.Button;
39 
40 public class CryptKeeperSettings extends Fragment {
41     private static final String TAG = "CryptKeeper";
42 
43     private static final int KEYGUARD_REQUEST = 55;
44 
45     // Minimum battery charge level (in percent) to launch encryption.  If the battery charge is
46     // lower than this, encryption should not be activated.
47     private static final int MIN_BATTERY_LEVEL = 80;
48 
49     private View mContentView;
50     private Button mInitiateButton;
51     private View mPowerWarning;
52     private View mBatteryWarning;
53     private IntentFilter mIntentFilter;
54 
55     private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
56         @Override
57         public void onReceive(Context context, Intent intent) {
58             String action = intent.getAction();
59             if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
60                 final int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
61                 final int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
62                 final int invalidCharger = intent.getIntExtra(
63                     BatteryManager.EXTRA_INVALID_CHARGER, 0);
64 
65                 final boolean levelOk = level >= MIN_BATTERY_LEVEL;
66                 final boolean pluggedOk =
67                     ((plugged & BatteryManager.BATTERY_PLUGGED_ANY) != 0) &&
68                      invalidCharger == 0;
69 
70                 // Update UI elements based on power/battery status
71                 mInitiateButton.setEnabled(levelOk && pluggedOk);
72                 mPowerWarning.setVisibility(pluggedOk ? View.GONE : View.VISIBLE );
73                 mBatteryWarning.setVisibility(levelOk ? View.GONE : View.VISIBLE);
74             }
75         }
76     };
77 
78     /**
79      * If the user clicks to begin the reset sequence, we next require a
80      * keyguard confirmation if the user has currently enabled one.  If there
81      * is no keyguard available, we prompt the user to set a password.
82      */
83     private Button.OnClickListener mInitiateListener = new Button.OnClickListener() {
84 
85         public void onClick(View v) {
86             if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) {
87                 // TODO replace (or follow) this dialog with an explicit launch into password UI
88                 new AlertDialog.Builder(getActivity())
89                     .setTitle(R.string.crypt_keeper_dialog_need_password_title)
90                     .setMessage(R.string.crypt_keeper_dialog_need_password_message)
91                     .setPositiveButton(android.R.string.ok, null)
92                     .create()
93                     .show();
94             }
95         }
96     };
97 
98     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState)99     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) {
100         mContentView = inflater.inflate(R.layout.crypt_keeper_settings, null);
101 
102         mIntentFilter = new IntentFilter();
103         mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
104 
105         mInitiateButton = (Button) mContentView.findViewById(R.id.initiate_encrypt);
106         mInitiateButton.setOnClickListener(mInitiateListener);
107         mInitiateButton.setEnabled(false);
108 
109         mPowerWarning = mContentView.findViewById(R.id.warning_unplugged);
110         mBatteryWarning = mContentView.findViewById(R.id.warning_low_charge);
111 
112         return mContentView;
113     }
114 
115     @Override
onResume()116     public void onResume() {
117         super.onResume();
118         getActivity().registerReceiver(mIntentReceiver, mIntentFilter);
119     }
120 
121     @Override
onPause()122     public void onPause() {
123         super.onPause();
124         getActivity().unregisterReceiver(mIntentReceiver);
125     }
126 
127     /**
128      * If encryption is already started, and this launched via a "start encryption" intent,
129      * then exit immediately - it's already up and running, so there's no point in "starting" it.
130      */
131     @Override
onActivityCreated(Bundle savedInstanceState)132     public void onActivityCreated(Bundle savedInstanceState) {
133         super.onActivityCreated(savedInstanceState);
134         Activity activity = getActivity();
135         Intent intent = activity.getIntent();
136         if (DevicePolicyManager.ACTION_START_ENCRYPTION.equals(intent.getAction())) {
137             DevicePolicyManager dpm = (DevicePolicyManager)
138                     activity.getSystemService(Context.DEVICE_POLICY_SERVICE);
139             if (dpm != null) {
140                 int status = dpm.getStorageEncryptionStatus();
141                 if (status != DevicePolicyManager.ENCRYPTION_STATUS_INACTIVE) {
142                     // There is nothing to do here, so simply finish() (which returns to caller)
143                     activity.finish();
144                 }
145             }
146         }
147     }
148 
149     /**
150      * Keyguard validation is run using the standard {@link ConfirmLockPattern}
151      * component as a subactivity
152      * @param request the request code to be returned once confirmation finishes
153      * @return true if confirmation launched
154      */
runKeyguardConfirmation(int request)155     private boolean runKeyguardConfirmation(int request) {
156         Resources res = getActivity().getResources();
157         ChooseLockSettingsHelper helper = new ChooseLockSettingsHelper(getActivity(), this);
158 
159         if (helper.utils().getKeyguardStoredPasswordQuality()
160                 == DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) {
161             showFinalConfirmation(StorageManager.CRYPT_TYPE_DEFAULT, "");
162             return true;
163         }
164 
165         return helper.launchConfirmationActivity(request,
166                 res.getText(R.string.master_clear_gesture_prompt),
167                 res.getText(R.string.crypt_keeper_confirm_encrypt),
168                 true);
169     }
170 
171     @Override
onActivityResult(int requestCode, int resultCode, Intent data)172     public void onActivityResult(int requestCode, int resultCode, Intent data) {
173         super.onActivityResult(requestCode, resultCode, data);
174 
175         if (requestCode != KEYGUARD_REQUEST) {
176             return;
177         }
178 
179         // If the user entered a valid keyguard trace, present the final
180         // confirmation prompt; otherwise, go back to the initial state.
181         if (resultCode == Activity.RESULT_OK && data != null) {
182             int type = data.getIntExtra(ChooseLockSettingsHelper.EXTRA_KEY_TYPE, -1);
183             String password = data.getStringExtra(ChooseLockSettingsHelper.EXTRA_KEY_PASSWORD);
184             if (!TextUtils.isEmpty(password)) {
185                 showFinalConfirmation(type, password);
186             }
187         }
188     }
189 
showFinalConfirmation(int type, String password)190     private void showFinalConfirmation(int type, String password) {
191         Preference preference = new Preference(getActivity());
192         preference.setFragment(CryptKeeperConfirm.class.getName());
193         preference.setTitle(R.string.crypt_keeper_confirm_title);
194         preference.getExtras().putInt("type", type);
195         preference.getExtras().putString("password", password);
196         ((SettingsActivity) getActivity()).onPreferenceStartFragment(null, preference);
197     }
198 }
199