MasterClearConfirm.java revision de4fc8eb2a27797adaa7afac98fa3a2a7ffd8f99
1b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani/*
2b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani * Copyright (C) 2010 The Android Open Source Project
3b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani *
4b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani * Licensed under the Apache License, Version 2.0 (the "License");
5b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani * you may not use this file except in compliance with the License.
6b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani * You may obtain a copy of the License at
7b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani *
8b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani *      http://www.apache.org/licenses/LICENSE-2.0
9b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani *
10b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani * Unless required by applicable law or agreed to in writing, software
11b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani * distributed under the License is distributed on an "AS IS" BASIS,
12b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani * See the License for the specific language governing permissions and
14b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani * limitations under the License.
15b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani */
16b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani
17b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasanipackage com.android.settings;
18b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani
19ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Moralesimport android.app.ProgressDialog;
207ab8929cc6ecbd1ba14c0d6ecb393b31f9a6fcdaAndres Moralesimport android.content.Context;
21ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Moralesimport android.content.pm.ActivityInfo;
22ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Moralesimport android.os.AsyncTask;
23de4fc8eb2a27797adaa7afac98fa3a2a7ffd8f99Russell Brennerimport android.provider.Settings;
247ab8929cc6ecbd1ba14c0d6ecb393b31f9a6fcdaAndres Moralesimport android.service.persistentdata.PersistentDataBlockManager;
25ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds
268a963babe2e36b7a41f77b8d2598c97658196e58Chris Wrenimport com.android.internal.logging.MetricsLogger;
27b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani
28b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasaniimport android.content.Intent;
29b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasaniimport android.os.Bundle;
302c53933e8d35ba50d0791a29e46fa3156e9cab7cJulia Reynoldsimport android.os.UserManager;
31b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasaniimport android.view.LayoutInflater;
32b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasaniimport android.view.View;
33b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasaniimport android.view.ViewGroup;
34b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasaniimport android.widget.Button;
35ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynoldsimport android.widget.TextView;
36b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani
37b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani/**
38b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani * Confirm and execute a reset of the device to a clean "just out of the box"
39b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani * state.  Multiple confirmations are required: first, a general "are you sure
40b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani * you want to do this?" prompt, followed by a keyguard pattern trace if the user
41b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING
42b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani * ON THE PHONE" prompt.  If at any time the phone is allowed to go to sleep, is
43b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani * locked, et cetera, then the confirmation sequence is abandoned.
44b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani *
45b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani * This is the confirmation screen.
46b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani */
478a963babe2e36b7a41f77b8d2598c97658196e58Chris Wrenpublic class MasterClearConfirm extends InstrumentedFragment {
48b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani
49b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    private View mContentView;
50b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    private boolean mEraseSdCard;
51b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani
52b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    /**
53b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani     * The user has gone through the multiple confirmation, so now we go ahead
54b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani     * and invoke the Checkin Service to reset the device to its factory-default
55b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani     * state (rebooting in the process).
56b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani     */
57b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    private Button.OnClickListener mFinalClickListener = new Button.OnClickListener() {
58b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani
59b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani        public void onClick(View v) {
60b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani            if (Utils.isMonkeyRunning()) {
61b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani                return;
62b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani            }
63b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani
64ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales            final PersistentDataBlockManager pdbManager = (PersistentDataBlockManager)
657ab8929cc6ecbd1ba14c0d6ecb393b31f9a6fcdaAndres Morales                    getActivity().getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
667ab8929cc6ecbd1ba14c0d6ecb393b31f9a6fcdaAndres Morales
67de4fc8eb2a27797adaa7afac98fa3a2a7ffd8f99Russell Brenner            if (pdbManager != null && !pdbManager.getOemUnlockEnabled() &&
68de4fc8eb2a27797adaa7afac98fa3a2a7ffd8f99Russell Brenner                    Settings.Global.getInt(getActivity().getContentResolver(),
69de4fc8eb2a27797adaa7afac98fa3a2a7ffd8f99Russell Brenner                            Settings.Global.DEVICE_PROVISIONED, 0) != 0) {
70de4fc8eb2a27797adaa7afac98fa3a2a7ffd8f99Russell Brenner                // if OEM unlock is enabled, this will be wiped during FR process. If disabled, it
71de4fc8eb2a27797adaa7afac98fa3a2a7ffd8f99Russell Brenner                // will be wiped here, unless the device is still being provisioned, in which case
72de4fc8eb2a27797adaa7afac98fa3a2a7ffd8f99Russell Brenner                // the persistent data block will be preserved.
73e6bf2a5609b9aabbd790c3c3680172f4b50a3f8eAndres Morales                new AsyncTask<Void, Void, Void>() {
74c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                    int mOldOrientation;
75c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                    ProgressDialog mProgressDialog;
76c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales
77e6bf2a5609b9aabbd790c3c3680172f4b50a3f8eAndres Morales                    @Override
78e6bf2a5609b9aabbd790c3c3680172f4b50a3f8eAndres Morales                    protected Void doInBackground(Void... params) {
79e6bf2a5609b9aabbd790c3c3680172f4b50a3f8eAndres Morales                        pdbManager.wipe();
80e6bf2a5609b9aabbd790c3c3680172f4b50a3f8eAndres Morales                        return null;
81e6bf2a5609b9aabbd790c3c3680172f4b50a3f8eAndres Morales                    }
827ab8929cc6ecbd1ba14c0d6ecb393b31f9a6fcdaAndres Morales
83e6bf2a5609b9aabbd790c3c3680172f4b50a3f8eAndres Morales                    @Override
84e6bf2a5609b9aabbd790c3c3680172f4b50a3f8eAndres Morales                    protected void onPostExecute(Void aVoid) {
85c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                        mProgressDialog.hide();
86c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                        getActivity().setRequestedOrientation(mOldOrientation);
87e6bf2a5609b9aabbd790c3c3680172f4b50a3f8eAndres Morales                        doMasterClear();
88e6bf2a5609b9aabbd790c3c3680172f4b50a3f8eAndres Morales                    }
89c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales
90c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                    @Override
91c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                    protected void onPreExecute() {
92c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                        mProgressDialog = getProgressDialog();
93c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                        mProgressDialog.show();
94c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales
95c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                        // need to prevent orientation changes as we're about to go into
96c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                        // a long IO request, so we won't be able to access inflate resources on flash
97c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                        mOldOrientation = getActivity().getRequestedOrientation();
98c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                        getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
99c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                    }
100e6bf2a5609b9aabbd790c3c3680172f4b50a3f8eAndres Morales                }.execute();
101b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani            } else {
102ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales                doMasterClear();
103b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani            }
104b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani        }
105ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales
106ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales        private ProgressDialog getProgressDialog() {
107ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales            final ProgressDialog progressDialog = new ProgressDialog(getActivity());
108ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales            progressDialog.setIndeterminate(true);
109ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales            progressDialog.setCancelable(false);
110ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales            progressDialog.setTitle(
111ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales                    getActivity().getString(R.string.master_clear_progress_title));
112ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales            progressDialog.setMessage(
113ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales                    getActivity().getString(R.string.master_clear_progress_text));
114ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales            return progressDialog;
115ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales        }
116b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    };
117b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani
118ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales    private void doMasterClear() {
119ccbdc57ee768f291d062f3578953f5a53e37eaeeRubin Xu        Intent intent = new Intent(Intent.ACTION_MASTER_CLEAR);
120ccbdc57ee768f291d062f3578953f5a53e37eaeeRubin Xu        intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
121ccbdc57ee768f291d062f3578953f5a53e37eaeeRubin Xu        intent.putExtra(Intent.EXTRA_REASON, "MasterClearConfirm");
122ccbdc57ee768f291d062f3578953f5a53e37eaeeRubin Xu        intent.putExtra(Intent.EXTRA_WIPE_EXTERNAL_STORAGE, mEraseSdCard);
123ccbdc57ee768f291d062f3578953f5a53e37eaeeRubin Xu        getActivity().sendBroadcast(intent);
124ccbdc57ee768f291d062f3578953f5a53e37eaeeRubin Xu        // Intent handling is asynchronous -- assume it will happen soon.
125ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales    }
126ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales
127b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    /**
128b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani     * Configure the UI for the final confirmation interaction
129b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani     */
130b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    private void establishFinalConfirmationState() {
131ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales        mContentView.findViewById(R.id.execute_master_clear)
132ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales                .setOnClickListener(mFinalClickListener);
133b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    }
134b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani
135b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    @Override
136b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    public View onCreateView(LayoutInflater inflater, ViewGroup container,
137b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani            Bundle savedInstanceState) {
1382c53933e8d35ba50d0791a29e46fa3156e9cab7cJulia Reynolds        if (UserManager.get(getActivity()).hasUserRestriction(
1392c53933e8d35ba50d0791a29e46fa3156e9cab7cJulia Reynolds                UserManager.DISALLOW_FACTORY_RESET)) {
1402c53933e8d35ba50d0791a29e46fa3156e9cab7cJulia Reynolds            return inflater.inflate(R.layout.master_clear_disallowed_screen, null);
1412c53933e8d35ba50d0791a29e46fa3156e9cab7cJulia Reynolds        }
142b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani        mContentView = inflater.inflate(R.layout.master_clear_confirm, null);
143b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani        establishFinalConfirmationState();
144ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds        setAccessibilityTitle();
145b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani        return mContentView;
146b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    }
147b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani
148ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds    private void setAccessibilityTitle() {
149ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds        CharSequence currentTitle = getActivity().getTitle();
150ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds        TextView confirmationMessage =
151ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds                (TextView) mContentView.findViewById(R.id.master_clear_confirm);
152ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds        if (confirmationMessage != null) {
153ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds            String accessibileText = new StringBuilder(currentTitle).append(",").append(
154ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds                    confirmationMessage.getText()).toString();
155ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds            getActivity().setTitle(Utils.createAccessibleSequence(currentTitle, accessibileText));
156ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds        }
157ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds    }
158ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds
159b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    @Override
160b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    public void onCreate(Bundle savedInstanceState) {
161b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani        super.onCreate(savedInstanceState);
162b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani
163b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani        Bundle args = getArguments();
164c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales        mEraseSdCard = args != null
165c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                && args.getBoolean(MasterClear.ERASE_EXTERNAL_EXTRA);
166b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    }
1678a963babe2e36b7a41f77b8d2598c97658196e58Chris Wren
1688a963babe2e36b7a41f77b8d2598c97658196e58Chris Wren    @Override
1698a963babe2e36b7a41f77b8d2598c97658196e58Chris Wren    protected int getMetricsCategory() {
1708a963babe2e36b7a41f77b8d2598c97658196e58Chris Wren        return MetricsLogger.MASTER_CLEAR_CONFIRM;
1718a963babe2e36b7a41f77b8d2598c97658196e58Chris Wren    }
172b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani}
173