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;
2139b467482d1bf256a111c757e9b7621c6f523271Jason Monkimport android.content.Intent;
22ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Moralesimport android.content.pm.ActivityInfo;
23ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Moralesimport android.os.AsyncTask;
24b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasaniimport android.os.Bundle;
257dbbe131683c231f6820bca4d67853649a4836f8Sudheer Shankaimport android.os.UserHandle;
262c53933e8d35ba50d0791a29e46fa3156e9cab7cJulia Reynoldsimport android.os.UserManager;
27c23357e9af9abd587cbd71f3d89384b3641e21daAndrew Scullimport android.service.oemlock.OemLockManager;
2839b467482d1bf256a111c757e9b7621c6f523271Jason Monkimport android.service.persistentdata.PersistentDataBlockManager;
29b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasaniimport android.view.LayoutInflater;
30b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasaniimport android.view.View;
31b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasaniimport android.view.ViewGroup;
32b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasaniimport android.widget.Button;
33ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynoldsimport android.widget.TextView;
34b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani
35265d3c2a0c36251bf8a9f571d7239b6dd404d942Tamas Berghammerimport com.android.internal.logging.nano.MetricsProto.MetricsEvent;
367dbbe131683c231f6820bca4d67853649a4836f8Sudheer Shankaimport com.android.settingslib.RestrictedLockUtils;
377dbbe131683c231f6820bca4d67853649a4836f8Sudheer Shanka
387dbbe131683c231f6820bca4d67853649a4836f8Sudheer Shankaimport static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
3939b467482d1bf256a111c757e9b7621c6f523271Jason Monk
40b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani/**
41b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani * Confirm and execute a reset of the device to a clean "just out of the box"
42b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani * state.  Multiple confirmations are required: first, a general "are you sure
43b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani * you want to do this?" prompt, followed by a keyguard pattern trace if the user
44b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING
45b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani * ON THE PHONE" prompt.  If at any time the phone is allowed to go to sleep, is
46b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani * locked, et cetera, then the confirmation sequence is abandoned.
47b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani *
48b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani * This is the confirmation screen.
49b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani */
500708d9e119da4c4d9424c0bc54fa458d01856bd7Udam Sainipublic class MasterClearConfirm extends OptionsMenuFragment {
51b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani
52b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    private View mContentView;
53b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    private boolean mEraseSdCard;
54072f28661d067c16499a0dd02987918d924613f9qingxi    private boolean mEraseEsims;
55b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani
56b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    /**
57b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani     * The user has gone through the multiple confirmation, so now we go ahead
58b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani     * and invoke the Checkin Service to reset the device to its factory-default
59b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani     * state (rebooting in the process).
60b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani     */
61b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    private Button.OnClickListener mFinalClickListener = new Button.OnClickListener() {
62b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani
63b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani        public void onClick(View v) {
64b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani            if (Utils.isMonkeyRunning()) {
65b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani                return;
66b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani            }
67b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani
68ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales            final PersistentDataBlockManager pdbManager = (PersistentDataBlockManager)
697ab8929cc6ecbd1ba14c0d6ecb393b31f9a6fcdaAndres Morales                    getActivity().getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
70c23357e9af9abd587cbd71f3d89384b3641e21daAndrew Scull            final OemLockManager oemLockManager = (OemLockManager)
71c23357e9af9abd587cbd71f3d89384b3641e21daAndrew Scull                    getActivity().getSystemService(Context.OEM_LOCK_SERVICE);
727ab8929cc6ecbd1ba14c0d6ecb393b31f9a6fcdaAndres Morales
73c23357e9af9abd587cbd71f3d89384b3641e21daAndrew Scull            if (pdbManager != null && !oemLockManager.isOemUnlockAllowed() &&
74679f7ad07e4e2a4b4c6645d05bca92790394c2fdUdam Saini                    Utils.isDeviceProvisioned(getActivity())) {
75c23357e9af9abd587cbd71f3d89384b3641e21daAndrew Scull                // if OEM unlock is allowed, the persistent data block will be wiped during FR
76c23357e9af9abd587cbd71f3d89384b3641e21daAndrew Scull                // process. If disabled, it will be wiped here, unless the device is still being
77c23357e9af9abd587cbd71f3d89384b3641e21daAndrew Scull                // provisioned, in which case the persistent data block will be preserved.
78e6bf2a5609b9aabbd790c3c3680172f4b50a3f8eAndres Morales                new AsyncTask<Void, Void, Void>() {
79c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                    int mOldOrientation;
80c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                    ProgressDialog mProgressDialog;
81c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales
82e6bf2a5609b9aabbd790c3c3680172f4b50a3f8eAndres Morales                    @Override
83e6bf2a5609b9aabbd790c3c3680172f4b50a3f8eAndres Morales                    protected Void doInBackground(Void... params) {
84e6bf2a5609b9aabbd790c3c3680172f4b50a3f8eAndres Morales                        pdbManager.wipe();
85e6bf2a5609b9aabbd790c3c3680172f4b50a3f8eAndres Morales                        return null;
86e6bf2a5609b9aabbd790c3c3680172f4b50a3f8eAndres Morales                    }
877ab8929cc6ecbd1ba14c0d6ecb393b31f9a6fcdaAndres Morales
88e6bf2a5609b9aabbd790c3c3680172f4b50a3f8eAndres Morales                    @Override
89e6bf2a5609b9aabbd790c3c3680172f4b50a3f8eAndres Morales                    protected void onPostExecute(Void aVoid) {
90c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                        mProgressDialog.hide();
918a38801bcf2eed131d11b1fec297c2c5217d0436Julia Reynolds                        if (getActivity() != null) {
928a38801bcf2eed131d11b1fec297c2c5217d0436Julia Reynolds                            getActivity().setRequestedOrientation(mOldOrientation);
938a38801bcf2eed131d11b1fec297c2c5217d0436Julia Reynolds                            doMasterClear();
948a38801bcf2eed131d11b1fec297c2c5217d0436Julia Reynolds                        }
95e6bf2a5609b9aabbd790c3c3680172f4b50a3f8eAndres Morales                    }
96c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales
97c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                    @Override
98c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                    protected void onPreExecute() {
99c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                        mProgressDialog = getProgressDialog();
100c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                        mProgressDialog.show();
101c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales
102c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                        // need to prevent orientation changes as we're about to go into
103c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                        // a long IO request, so we won't be able to access inflate resources on flash
104c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                        mOldOrientation = getActivity().getRequestedOrientation();
105c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                        getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
106c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                    }
107e6bf2a5609b9aabbd790c3c3680172f4b50a3f8eAndres Morales                }.execute();
108b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani            } else {
109ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales                doMasterClear();
110b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani            }
111b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani        }
112ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales
113ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales        private ProgressDialog getProgressDialog() {
114ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales            final ProgressDialog progressDialog = new ProgressDialog(getActivity());
115ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales            progressDialog.setIndeterminate(true);
116ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales            progressDialog.setCancelable(false);
117ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales            progressDialog.setTitle(
118ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales                    getActivity().getString(R.string.master_clear_progress_title));
119ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales            progressDialog.setMessage(
120ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales                    getActivity().getString(R.string.master_clear_progress_text));
121ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales            return progressDialog;
122ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales        }
123b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    };
124b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani
125ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales    private void doMasterClear() {
1263393daccd9b08c29928e7d71b6f6e1c7388c60b3Lenka Trochtova        Intent intent = new Intent(Intent.ACTION_FACTORY_RESET);
1271e99f0ce412db497475f7e220090b9c522be6e79Christopher Tate        intent.setPackage("android");
128ccbdc57ee768f291d062f3578953f5a53e37eaeeRubin Xu        intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
129ccbdc57ee768f291d062f3578953f5a53e37eaeeRubin Xu        intent.putExtra(Intent.EXTRA_REASON, "MasterClearConfirm");
130ccbdc57ee768f291d062f3578953f5a53e37eaeeRubin Xu        intent.putExtra(Intent.EXTRA_WIPE_EXTERNAL_STORAGE, mEraseSdCard);
131072f28661d067c16499a0dd02987918d924613f9qingxi        intent.putExtra(Intent.EXTRA_WIPE_ESIMS, mEraseEsims);
132ccbdc57ee768f291d062f3578953f5a53e37eaeeRubin Xu        getActivity().sendBroadcast(intent);
133ccbdc57ee768f291d062f3578953f5a53e37eaeeRubin Xu        // Intent handling is asynchronous -- assume it will happen soon.
134ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales    }
135ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales
136b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    /**
137b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani     * Configure the UI for the final confirmation interaction
138b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani     */
139b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    private void establishFinalConfirmationState() {
140ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales        mContentView.findViewById(R.id.execute_master_clear)
141ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales                .setOnClickListener(mFinalClickListener);
142b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    }
143b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani
144b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    @Override
145b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    public View onCreateView(LayoutInflater inflater, ViewGroup container,
146b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani            Bundle savedInstanceState) {
1477dbbe131683c231f6820bca4d67853649a4836f8Sudheer Shanka        final EnforcedAdmin admin = RestrictedLockUtils.checkIfRestrictionEnforced(
1487dbbe131683c231f6820bca4d67853649a4836f8Sudheer Shanka                getActivity(), UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId());
1497dbbe131683c231f6820bca4d67853649a4836f8Sudheer Shanka        if (RestrictedLockUtils.hasBaseUserRestriction(getActivity(),
1507dbbe131683c231f6820bca4d67853649a4836f8Sudheer Shanka                UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId())) {
1512c53933e8d35ba50d0791a29e46fa3156e9cab7cJulia Reynolds            return inflater.inflate(R.layout.master_clear_disallowed_screen, null);
1527dbbe131683c231f6820bca4d67853649a4836f8Sudheer Shanka        } else if (admin != null) {
1537dbbe131683c231f6820bca4d67853649a4836f8Sudheer Shanka            View view = inflater.inflate(R.layout.admin_support_details_empty_view, null);
1547dbbe131683c231f6820bca4d67853649a4836f8Sudheer Shanka            ShowAdminSupportDetailsDialog.setAdminSupportDetails(getActivity(), view, admin, false);
1557dbbe131683c231f6820bca4d67853649a4836f8Sudheer Shanka            view.setVisibility(View.VISIBLE);
1567dbbe131683c231f6820bca4d67853649a4836f8Sudheer Shanka            return view;
1572c53933e8d35ba50d0791a29e46fa3156e9cab7cJulia Reynolds        }
158b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani        mContentView = inflater.inflate(R.layout.master_clear_confirm, null);
159b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani        establishFinalConfirmationState();
160ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds        setAccessibilityTitle();
161b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani        return mContentView;
162b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    }
163b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani
164ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds    private void setAccessibilityTitle() {
165ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds        CharSequence currentTitle = getActivity().getTitle();
166ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds        TextView confirmationMessage =
167ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds                (TextView) mContentView.findViewById(R.id.master_clear_confirm);
168ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds        if (confirmationMessage != null) {
169ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds            String accessibileText = new StringBuilder(currentTitle).append(",").append(
170ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds                    confirmationMessage.getText()).toString();
171ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds            getActivity().setTitle(Utils.createAccessibleSequence(currentTitle, accessibileText));
172ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds        }
173ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds    }
174ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds
175b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    @Override
176b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    public void onCreate(Bundle savedInstanceState) {
177b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani        super.onCreate(savedInstanceState);
178b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani
179b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani        Bundle args = getArguments();
180c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales        mEraseSdCard = args != null
181c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                && args.getBoolean(MasterClear.ERASE_EXTERNAL_EXTRA);
182072f28661d067c16499a0dd02987918d924613f9qingxi        mEraseEsims = args != null
183072f28661d067c16499a0dd02987918d924613f9qingxi                && args.getBoolean(MasterClear.ERASE_ESIMS_EXTRA);
184b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    }
1858a963babe2e36b7a41f77b8d2598c97658196e58Chris Wren
1868a963babe2e36b7a41f77b8d2598c97658196e58Chris Wren    @Override
1876507613ebcd22e4691c2af92a5c161bd327db336Fan Zhang    public int getMetricsCategory() {
1889d1bfd1e8de6e46137a9571507c03526880d6a46Chris Wren        return MetricsEvent.MASTER_CLEAR_CONFIRM;
1898a963babe2e36b7a41f77b8d2598c97658196e58Chris Wren    }
190b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani}
191