Home | History | Annotate | Download | only in settings
      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 android.accounts.Account;
     20 import android.accounts.AccountManager;
     21 import android.accounts.AuthenticatorDescription;
     22 import android.app.Activity;
     23 import android.app.Fragment;
     24 import android.content.Context;
     25 import android.content.Intent;
     26 import android.content.pm.PackageManager;
     27 import android.content.res.Resources;
     28 import android.graphics.drawable.Drawable;
     29 import android.os.Bundle;
     30 import android.os.Environment;
     31 import android.os.SystemProperties;
     32 import android.os.UserManager;
     33 import android.preference.Preference;
     34 import android.preference.PreferenceActivity;
     35 import android.util.Log;
     36 import android.view.LayoutInflater;
     37 import android.view.View;
     38 import android.view.ViewGroup;
     39 import android.widget.Button;
     40 import android.widget.CheckBox;
     41 import android.widget.LinearLayout;
     42 import android.widget.TextView;
     43 
     44 /**
     45  * Confirm and execute a reset of the device to a clean "just out of the box"
     46  * state.  Multiple confirmations are required: first, a general "are you sure
     47  * you want to do this?" prompt, followed by a keyguard pattern trace if the user
     48  * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING
     49  * ON THE PHONE" prompt.  If at any time the phone is allowed to go to sleep, is
     50  * locked, et cetera, then the confirmation sequence is abandoned.
     51  *
     52  * This is the initial screen.
     53  */
     54 public class MasterClear extends Fragment {
     55     private static final String TAG = "MasterClear";
     56 
     57     private static final int KEYGUARD_REQUEST = 55;
     58     private static final int PIN_REQUEST = 56;
     59 
     60     static final String ERASE_EXTERNAL_EXTRA = "erase_sd";
     61 
     62     private View mContentView;
     63     private Button mInitiateButton;
     64     private View mExternalStorageContainer;
     65     private CheckBox mExternalStorage;
     66     private boolean mPinConfirmed;
     67 
     68     /**
     69      * Keyguard validation is run using the standard {@link ConfirmLockPattern}
     70      * component as a subactivity
     71      * @param request the request code to be returned once confirmation finishes
     72      * @return true if confirmation launched
     73      */
     74     private boolean runKeyguardConfirmation(int request) {
     75         Resources res = getActivity().getResources();
     76         return new ChooseLockSettingsHelper(getActivity(), this)
     77                 .launchConfirmationActivity(request,
     78                         res.getText(R.string.master_clear_gesture_prompt),
     79                         res.getText(R.string.master_clear_gesture_explanation));
     80     }
     81 
     82     private boolean runRestrictionsChallenge() {
     83         if (UserManager.get(getActivity()).hasRestrictionsChallenge()) {
     84             startActivityForResult(
     85                     new Intent(Intent.ACTION_RESTRICTIONS_CHALLENGE), PIN_REQUEST);
     86             return true;
     87         }
     88         return false;
     89     }
     90 
     91     @Override
     92     public void onActivityResult(int requestCode, int resultCode, Intent data) {
     93         super.onActivityResult(requestCode, resultCode, data);
     94 
     95         if (requestCode == PIN_REQUEST) {
     96             if (resultCode == Activity.RESULT_OK) {
     97                 mPinConfirmed = true;
     98             }
     99             return;
    100         } else if (requestCode != KEYGUARD_REQUEST) {
    101             return;
    102         }
    103 
    104         // If the user entered a valid keyguard trace, present the final
    105         // confirmation prompt; otherwise, go back to the initial state.
    106         if (resultCode == Activity.RESULT_OK) {
    107             showFinalConfirmation();
    108         } else {
    109             establishInitialState();
    110         }
    111     }
    112 
    113     private void showFinalConfirmation() {
    114         Preference preference = new Preference(getActivity());
    115         preference.setFragment(MasterClearConfirm.class.getName());
    116         preference.setTitle(R.string.master_clear_confirm_title);
    117         preference.getExtras().putBoolean(ERASE_EXTERNAL_EXTRA, mExternalStorage.isChecked());
    118         ((PreferenceActivity) getActivity()).onPreferenceStartFragment(null, preference);
    119     }
    120 
    121     /**
    122      * If the user clicks to begin the reset sequence, we next require a
    123      * keyguard confirmation if the user has currently enabled one.  If there
    124      * is no keyguard available, we simply go to the final confirmation prompt.
    125      */
    126     private final Button.OnClickListener mInitiateListener = new Button.OnClickListener() {
    127 
    128         public void onClick(View v) {
    129             mPinConfirmed = false;
    130             if (runRestrictionsChallenge()) {
    131                 return;
    132             }
    133             if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) {
    134                 showFinalConfirmation();
    135             }
    136         }
    137     };
    138 
    139     /**
    140      * In its initial state, the activity presents a button for the user to
    141      * click in order to initiate a confirmation sequence.  This method is
    142      * called from various other points in the code to reset the activity to
    143      * this base state.
    144      *
    145      * <p>Reinflating views from resources is expensive and prevents us from
    146      * caching widget pointers, so we use a single-inflate pattern:  we lazy-
    147      * inflate each view, caching all of the widget pointers we'll need at the
    148      * time, then simply reuse the inflated views directly whenever we need
    149      * to change contents.
    150      */
    151     private void establishInitialState() {
    152         mInitiateButton = (Button) mContentView.findViewById(R.id.initiate_master_clear);
    153         mInitiateButton.setOnClickListener(mInitiateListener);
    154         mExternalStorageContainer = mContentView.findViewById(R.id.erase_external_container);
    155         mExternalStorage = (CheckBox) mContentView.findViewById(R.id.erase_external);
    156 
    157         /*
    158          * If the external storage is emulated, it will be erased with a factory
    159          * reset at any rate. There is no need to have a separate option until
    160          * we have a factory reset that only erases some directories and not
    161          * others. Likewise, if it's non-removable storage, it could potentially have been
    162          * encrypted, and will also need to be wiped.
    163          */
    164         boolean isExtStorageEmulated = Environment.isExternalStorageEmulated();
    165         if (isExtStorageEmulated
    166                 || (!Environment.isExternalStorageRemovable() && isExtStorageEncrypted())) {
    167             mExternalStorageContainer.setVisibility(View.GONE);
    168 
    169             final View externalOption = mContentView.findViewById(R.id.erase_external_option_text);
    170             externalOption.setVisibility(View.GONE);
    171 
    172             final View externalAlsoErased = mContentView.findViewById(R.id.also_erases_external);
    173             externalAlsoErased.setVisibility(View.VISIBLE);
    174 
    175             // If it's not emulated, it is on a separate partition but it means we're doing
    176             // a force wipe due to encryption.
    177             mExternalStorage.setChecked(!isExtStorageEmulated);
    178         } else {
    179             mExternalStorageContainer.setOnClickListener(new View.OnClickListener() {
    180 
    181                 @Override
    182                 public void onClick(View v) {
    183                     mExternalStorage.toggle();
    184                 }
    185             });
    186         }
    187 
    188         loadAccountList();
    189     }
    190 
    191     private boolean isExtStorageEncrypted() {
    192         String state = SystemProperties.get("vold.decrypt");
    193         return !"".equals(state);
    194     }
    195 
    196     private void loadAccountList() {
    197         View accountsLabel = mContentView.findViewById(R.id.accounts_label);
    198         LinearLayout contents = (LinearLayout)mContentView.findViewById(R.id.accounts);
    199         contents.removeAllViews();
    200 
    201         Context context = getActivity();
    202 
    203         AccountManager mgr = AccountManager.get(context);
    204         Account[] accounts = mgr.getAccounts();
    205         final int N = accounts.length;
    206         if (N == 0) {
    207             accountsLabel.setVisibility(View.GONE);
    208             contents.setVisibility(View.GONE);
    209             return;
    210         }
    211 
    212         LayoutInflater inflater = (LayoutInflater)context.getSystemService(
    213                 Context.LAYOUT_INFLATER_SERVICE);
    214 
    215         AuthenticatorDescription[] descs = AccountManager.get(context).getAuthenticatorTypes();
    216         final int M = descs.length;
    217 
    218         for (int i=0; i<N; i++) {
    219             Account account = accounts[i];
    220             AuthenticatorDescription desc = null;
    221             for (int j=0; j<M; j++) {
    222                 if (account.type.equals(descs[j].type)) {
    223                     desc = descs[j];
    224                     break;
    225                 }
    226             }
    227             if (desc == null) {
    228                 Log.w(TAG, "No descriptor for account name=" + account.name
    229                         + " type=" + account.type);
    230                 continue;
    231             }
    232             Drawable icon = null;
    233             try {
    234                 if (desc.iconId != 0) {
    235                     Context authContext = context.createPackageContext(desc.packageName, 0);
    236                     icon = authContext.getResources().getDrawable(desc.iconId);
    237                 }
    238             } catch (PackageManager.NameNotFoundException e) {
    239                 Log.w(TAG, "No icon for account type " + desc.type);
    240             }
    241 
    242             TextView child = (TextView)inflater.inflate(R.layout.master_clear_account,
    243                     contents, false);
    244             child.setText(account.name);
    245             if (icon != null) {
    246                 child.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
    247             }
    248             contents.addView(child);
    249         }
    250 
    251         accountsLabel.setVisibility(View.VISIBLE);
    252         contents.setVisibility(View.VISIBLE);
    253     }
    254 
    255     @Override
    256     public View onCreateView(LayoutInflater inflater, ViewGroup container,
    257             Bundle savedInstanceState) {
    258         mContentView = inflater.inflate(R.layout.master_clear, null);
    259 
    260         establishInitialState();
    261         return mContentView;
    262     }
    263 
    264     @Override
    265     public void onResume() {
    266         super.onResume();
    267 
    268         // If this is the second step after restrictions pin challenge
    269         if (mPinConfirmed) {
    270             mPinConfirmed = false;
    271             if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) {
    272                 showFinalConfirmation();
    273             }
    274         }
    275     }
    276 }
    277