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;
27ff8a68010e2f7f41da6db592f3eea836e7d9274fRussell Brennerimport android.provider.Settings;
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
359d1bfd1e8de6e46137a9571507c03526880d6a46Chris Wrenimport com.android.internal.logging.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;
54b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani
55b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    /**
56b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani     * The user has gone through the multiple confirmation, so now we go ahead
57b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani     * and invoke the Checkin Service to reset the device to its factory-default
58b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani     * state (rebooting in the process).
59b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani     */
60b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    private Button.OnClickListener mFinalClickListener = new Button.OnClickListener() {
61b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani
62b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani        public void onClick(View v) {
63b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani            if (Utils.isMonkeyRunning()) {
64b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani                return;
65b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani            }
66b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani
67ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales            final PersistentDataBlockManager pdbManager = (PersistentDataBlockManager)
687ab8929cc6ecbd1ba14c0d6ecb393b31f9a6fcdaAndres Morales                    getActivity().getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
697ab8929cc6ecbd1ba14c0d6ecb393b31f9a6fcdaAndres Morales
70de4fc8eb2a27797adaa7afac98fa3a2a7ffd8f99Russell Brenner            if (pdbManager != null && !pdbManager.getOemUnlockEnabled() &&
71679f7ad07e4e2a4b4c6645d05bca92790394c2fdUdam Saini                    Utils.isDeviceProvisioned(getActivity())) {
72de4fc8eb2a27797adaa7afac98fa3a2a7ffd8f99Russell Brenner                // if OEM unlock is enabled, this will be wiped during FR process. If disabled, it
73de4fc8eb2a27797adaa7afac98fa3a2a7ffd8f99Russell Brenner                // will be wiped here, unless the device is still being provisioned, in which case
74de4fc8eb2a27797adaa7afac98fa3a2a7ffd8f99Russell Brenner                // the persistent data block will be preserved.
75e6bf2a5609b9aabbd790c3c3680172f4b50a3f8eAndres Morales                new AsyncTask<Void, Void, Void>() {
76c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                    int mOldOrientation;
77c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                    ProgressDialog mProgressDialog;
78c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales
79e6bf2a5609b9aabbd790c3c3680172f4b50a3f8eAndres Morales                    @Override
80e6bf2a5609b9aabbd790c3c3680172f4b50a3f8eAndres Morales                    protected Void doInBackground(Void... params) {
81e6bf2a5609b9aabbd790c3c3680172f4b50a3f8eAndres Morales                        pdbManager.wipe();
82e6bf2a5609b9aabbd790c3c3680172f4b50a3f8eAndres Morales                        return null;
83e6bf2a5609b9aabbd790c3c3680172f4b50a3f8eAndres Morales                    }
847ab8929cc6ecbd1ba14c0d6ecb393b31f9a6fcdaAndres Morales
85e6bf2a5609b9aabbd790c3c3680172f4b50a3f8eAndres Morales                    @Override
86e6bf2a5609b9aabbd790c3c3680172f4b50a3f8eAndres Morales                    protected void onPostExecute(Void aVoid) {
87c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                        mProgressDialog.hide();
888a38801bcf2eed131d11b1fec297c2c5217d0436Julia Reynolds                        if (getActivity() != null) {
898a38801bcf2eed131d11b1fec297c2c5217d0436Julia Reynolds                            getActivity().setRequestedOrientation(mOldOrientation);
908a38801bcf2eed131d11b1fec297c2c5217d0436Julia Reynolds                            doMasterClear();
918a38801bcf2eed131d11b1fec297c2c5217d0436Julia Reynolds                        }
92e6bf2a5609b9aabbd790c3c3680172f4b50a3f8eAndres Morales                    }
93c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales
94c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                    @Override
95c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                    protected void onPreExecute() {
96c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                        mProgressDialog = getProgressDialog();
97c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                        mProgressDialog.show();
98c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales
99c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                        // need to prevent orientation changes as we're about to go into
100c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                        // a long IO request, so we won't be able to access inflate resources on flash
101c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                        mOldOrientation = getActivity().getRequestedOrientation();
102c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                        getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
103c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                    }
104e6bf2a5609b9aabbd790c3c3680172f4b50a3f8eAndres Morales                }.execute();
105b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani            } else {
106ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales                doMasterClear();
107b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani            }
108b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani        }
109ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales
110ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales        private ProgressDialog getProgressDialog() {
111ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales            final ProgressDialog progressDialog = new ProgressDialog(getActivity());
112ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales            progressDialog.setIndeterminate(true);
113ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales            progressDialog.setCancelable(false);
114ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales            progressDialog.setTitle(
115ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales                    getActivity().getString(R.string.master_clear_progress_title));
116ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales            progressDialog.setMessage(
117ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales                    getActivity().getString(R.string.master_clear_progress_text));
118ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales            return progressDialog;
119ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales        }
120b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    };
121b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani
122ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales    private void doMasterClear() {
123ccbdc57ee768f291d062f3578953f5a53e37eaeeRubin Xu        Intent intent = new Intent(Intent.ACTION_MASTER_CLEAR);
124ccbdc57ee768f291d062f3578953f5a53e37eaeeRubin Xu        intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
125ccbdc57ee768f291d062f3578953f5a53e37eaeeRubin Xu        intent.putExtra(Intent.EXTRA_REASON, "MasterClearConfirm");
126ccbdc57ee768f291d062f3578953f5a53e37eaeeRubin Xu        intent.putExtra(Intent.EXTRA_WIPE_EXTERNAL_STORAGE, mEraseSdCard);
127ccbdc57ee768f291d062f3578953f5a53e37eaeeRubin Xu        getActivity().sendBroadcast(intent);
128ccbdc57ee768f291d062f3578953f5a53e37eaeeRubin Xu        // Intent handling is asynchronous -- assume it will happen soon.
129ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales    }
130ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales
131b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    /**
132b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani     * Configure the UI for the final confirmation interaction
133b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani     */
134b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    private void establishFinalConfirmationState() {
135ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales        mContentView.findViewById(R.id.execute_master_clear)
136ab61b0df4af1a504e845c7008224ebadb5400ce9Andres Morales                .setOnClickListener(mFinalClickListener);
137b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    }
138b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani
139b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    @Override
140b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    public View onCreateView(LayoutInflater inflater, ViewGroup container,
141b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani            Bundle savedInstanceState) {
1427dbbe131683c231f6820bca4d67853649a4836f8Sudheer Shanka        final EnforcedAdmin admin = RestrictedLockUtils.checkIfRestrictionEnforced(
1437dbbe131683c231f6820bca4d67853649a4836f8Sudheer Shanka                getActivity(), UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId());
1447dbbe131683c231f6820bca4d67853649a4836f8Sudheer Shanka        if (RestrictedLockUtils.hasBaseUserRestriction(getActivity(),
1457dbbe131683c231f6820bca4d67853649a4836f8Sudheer Shanka                UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId())) {
1462c53933e8d35ba50d0791a29e46fa3156e9cab7cJulia Reynolds            return inflater.inflate(R.layout.master_clear_disallowed_screen, null);
1477dbbe131683c231f6820bca4d67853649a4836f8Sudheer Shanka        } else if (admin != null) {
1487dbbe131683c231f6820bca4d67853649a4836f8Sudheer Shanka            View view = inflater.inflate(R.layout.admin_support_details_empty_view, null);
1497dbbe131683c231f6820bca4d67853649a4836f8Sudheer Shanka            ShowAdminSupportDetailsDialog.setAdminSupportDetails(getActivity(), view, admin, false);
1507dbbe131683c231f6820bca4d67853649a4836f8Sudheer Shanka            view.setVisibility(View.VISIBLE);
1517dbbe131683c231f6820bca4d67853649a4836f8Sudheer Shanka            return view;
1522c53933e8d35ba50d0791a29e46fa3156e9cab7cJulia Reynolds        }
153b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani        mContentView = inflater.inflate(R.layout.master_clear_confirm, null);
154b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani        establishFinalConfirmationState();
155ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds        setAccessibilityTitle();
156b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani        return mContentView;
157b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    }
158b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani
159ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds    private void setAccessibilityTitle() {
160ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds        CharSequence currentTitle = getActivity().getTitle();
161ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds        TextView confirmationMessage =
162ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds                (TextView) mContentView.findViewById(R.id.master_clear_confirm);
163ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds        if (confirmationMessage != null) {
164ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds            String accessibileText = new StringBuilder(currentTitle).append(",").append(
165ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds                    confirmationMessage.getText()).toString();
166ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds            getActivity().setTitle(Utils.createAccessibleSequence(currentTitle, accessibileText));
167ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds        }
168ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds    }
169ce25af48383b26535ec07312ab0db6928bba1fe3Julia Reynolds
170b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    @Override
171b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    public void onCreate(Bundle savedInstanceState) {
172b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani        super.onCreate(savedInstanceState);
173b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani
174b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani        Bundle args = getArguments();
175c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales        mEraseSdCard = args != null
176c17ec1be19a45dec12d601f1e6a545f558bdf293Andres Morales                && args.getBoolean(MasterClear.ERASE_EXTERNAL_EXTRA);
177b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani    }
1788a963babe2e36b7a41f77b8d2598c97658196e58Chris Wren
1798a963babe2e36b7a41f77b8d2598c97658196e58Chris Wren    @Override
1808a963babe2e36b7a41f77b8d2598c97658196e58Chris Wren    protected int getMetricsCategory() {
1819d1bfd1e8de6e46137a9571507c03526880d6a46Chris Wren        return MetricsEvent.MASTER_CLEAR_CONFIRM;
1828a963babe2e36b7a41f77b8d2598c97658196e58Chris Wren    }
183b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani}
184