[go: nahoru, domu]

14a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate/*
24a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate * Copyright (C) 2011 The Android Open Source Project
34a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate *
44a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate * Licensed under the Apache License, Version 2.0 (the "License");
54a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate * you may not use this file except in compliance with the License.
64a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate * You may obtain a copy of the License at
74a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate *
84a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate *      http://www.apache.org/licenses/LICENSE-2.0
94a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate *
104a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate * Unless required by applicable law or agreed to in writing, software
114a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate * distributed under the License is distributed on an "AS IS" BASIS,
124a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate * See the License for the specific language governing permissions and
144a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate * limitations under the License.
154a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate */
164a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
174a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tatepackage com.android.backupconfirm;
184a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
194a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tateimport android.app.Activity;
204a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tateimport android.app.backup.FullBackup;
214a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tateimport android.app.backup.IBackupManager;
224a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tateimport android.app.backup.IFullBackupRestoreObserver;
234a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tateimport android.content.Context;
244a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tateimport android.content.Intent;
254a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tateimport android.os.Bundle;
264a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tateimport android.os.Handler;
274a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tateimport android.os.Message;
284a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tateimport android.os.RemoteException;
294a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tateimport android.os.ServiceManager;
3032418be49e5b61c2e9281528cb8fb67939e301e8Christopher Tateimport android.os.storage.IMountService;
3132d06732cdb7ee653a58e49a4dab13a780513db5Paul Lawrenceimport android.os.storage.StorageManager;
3214d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tateimport android.text.Editable;
3314d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tateimport android.text.TextWatcher;
344a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tateimport android.util.Slog;
354a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tateimport android.view.View;
364a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tateimport android.widget.Button;
374a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tateimport android.widget.TextView;
384a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tateimport android.widget.Toast;
394a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
404a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate/**
414a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate * Confirm with the user that a requested full backup/restore operation is legitimate.
424a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate * Any attempt to perform a full backup/restore will launch this UI and wait for a
434a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate * designated timeout interval (nominally 30 seconds) for the user to confirm.  If the
444a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate * user fails to respond within the timeout period, or explicitly refuses the operation
454a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate * within the UI presented here, no data will be transferred off the device.
464a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate *
474a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate * Note that the fully scoped name of this class is baked into the backup manager service.
484a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate *
494a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate * @hide
504a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate */
514a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tatepublic class BackupRestoreConfirmation extends Activity {
524a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate    static final String TAG = "BackupRestoreConfirmation";
534a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate    static final boolean DEBUG = true;
544a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
553851fa9c8d180ca636e69b25f84fdcc294150009Christopher Tate    static final String DID_ACKNOWLEDGE = "did_acknowledge";
563851fa9c8d180ca636e69b25f84fdcc294150009Christopher Tate
574a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate    static final int MSG_START_BACKUP = 1;
584a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate    static final int MSG_BACKUP_PACKAGE = 2;
594a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate    static final int MSG_END_BACKUP = 3;
604a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate    static final int MSG_START_RESTORE = 11;
614a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate    static final int MSG_RESTORE_PACKAGE = 12;
624a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate    static final int MSG_END_RESTORE = 13;
634a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate    static final int MSG_TIMEOUT = 100;
644a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
654a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate    Handler mHandler;
664a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate    IBackupManager mBackupManager;
6732418be49e5b61c2e9281528cb8fb67939e301e8Christopher Tate    IMountService mMountService;
684a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate    FullObserver mObserver;
694a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate    int mToken;
7032418be49e5b61c2e9281528cb8fb67939e301e8Christopher Tate    boolean mIsEncrypted;
71ec5d4a0f9765f1645ab2e28ad2ef3cad247a042bChristopher Tate    boolean mDidAcknowledge;
724a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
734a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate    TextView mStatusView;
74728a1c4d5ed3b808172013a7f5bb5065d1e964f6Christopher Tate    TextView mCurPassword;
75728a1c4d5ed3b808172013a7f5bb5065d1e964f6Christopher Tate    TextView mEncPassword;
764a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate    Button mAllowButton;
774a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate    Button mDenyButton;
784a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
794a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate    // Handler for dealing with observer callbacks on the main thread
804a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate    class ObserverHandler extends Handler {
814a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        Context mContext;
824a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        ObserverHandler(Context context) {
834a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate            mContext = context;
84ec5d4a0f9765f1645ab2e28ad2ef3cad247a042bChristopher Tate            mDidAcknowledge = false;
854a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        }
864a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
874a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        @Override
884a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        public void handleMessage(Message msg) {
894a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate            switch (msg.what) {
904a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                case MSG_START_BACKUP: {
9128b591c2a416d7f5955371970272bcaa2b1b303eChristopher Tate                    Toast.makeText(mContext, R.string.toast_backup_started, Toast.LENGTH_LONG).show();
924a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                }
934a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                break;
944a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
954a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                case MSG_BACKUP_PACKAGE: {
964a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                    String name = (String) msg.obj;
974a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                    mStatusView.setText(name);
984a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                }
994a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                break;
1004a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
1014a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                case MSG_END_BACKUP: {
10228b591c2a416d7f5955371970272bcaa2b1b303eChristopher Tate                    Toast.makeText(mContext, R.string.toast_backup_ended, Toast.LENGTH_LONG).show();
103dc92c82b4180e8067f1acd00a7db7935afce00ffChristopher Tate                    finish();
1044a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                }
1054a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                break;
1064a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
1074a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                case MSG_START_RESTORE: {
10828b591c2a416d7f5955371970272bcaa2b1b303eChristopher Tate                    Toast.makeText(mContext, R.string.toast_restore_started, Toast.LENGTH_LONG).show();
1094a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                }
1104a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                break;
1114a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
1124a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                case MSG_RESTORE_PACKAGE: {
11375a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate                    String name = (String) msg.obj;
11475a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate                    mStatusView.setText(name);
1154a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                }
1164a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                break;
1174a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
1184a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                case MSG_END_RESTORE: {
11928b591c2a416d7f5955371970272bcaa2b1b303eChristopher Tate                    Toast.makeText(mContext, R.string.toast_restore_ended, Toast.LENGTH_SHORT).show();
120dc92c82b4180e8067f1acd00a7db7935afce00ffChristopher Tate                    finish();
1214a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                }
1224a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                break;
1234a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
1244a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                case MSG_TIMEOUT: {
12528b591c2a416d7f5955371970272bcaa2b1b303eChristopher Tate                    Toast.makeText(mContext, R.string.toast_timeout, Toast.LENGTH_LONG).show();
1264a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                }
1274a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                break;
1284a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate            }
1294a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        }
1304a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate    }
1314a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
1324a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate    @Override
1334a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate    public void onCreate(Bundle icicle) {
1344a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        super.onCreate(icicle);
1354a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
1364a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        final Intent intent = getIntent();
1374a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        final String action = intent.getAction();
1384a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
1392efd2dbbac9eac89620683696c6076463c3a1cd6Christopher Tate        final int layoutId;
140c58cf7dd02ad227a68d62a0204152cee62c13182Christopher Tate        final int titleId;
1414a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        if (action.equals(FullBackup.FULL_BACKUP_INTENT_ACTION)) {
1424a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate            layoutId = R.layout.confirm_backup;
143c58cf7dd02ad227a68d62a0204152cee62c13182Christopher Tate            titleId = R.string.backup_confirm_title;
1444a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        } else if (action.equals(FullBackup.FULL_RESTORE_INTENT_ACTION)) {
1454a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate            layoutId = R.layout.confirm_restore;
146c58cf7dd02ad227a68d62a0204152cee62c13182Christopher Tate            titleId = R.string.restore_confirm_title;
1474a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        } else {
1484a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate            Slog.w(TAG, "Backup/restore confirmation activity launched with invalid action!");
1494a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate            finish();
1504a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate            return;
1514a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        }
1524a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
1534a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        mToken = intent.getIntExtra(FullBackup.CONF_TOKEN_INTENT_EXTRA, -1);
1544a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        if (mToken < 0) {
1554a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate            Slog.e(TAG, "Backup/restore confirmation requested but no token passed!");
1564a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate            finish();
1574a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate            return;
1584a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        }
1594a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
1604a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        mBackupManager = IBackupManager.Stub.asInterface(ServiceManager.getService(Context.BACKUP_SERVICE));
16132418be49e5b61c2e9281528cb8fb67939e301e8Christopher Tate        mMountService = IMountService.Stub.asInterface(ServiceManager.getService("mount"));
1624a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
1634a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        mHandler = new ObserverHandler(getApplicationContext());
1643851fa9c8d180ca636e69b25f84fdcc294150009Christopher Tate        final Object oldObserver = getLastNonConfigurationInstance();
1653851fa9c8d180ca636e69b25f84fdcc294150009Christopher Tate        if (oldObserver == null) {
1663851fa9c8d180ca636e69b25f84fdcc294150009Christopher Tate            mObserver = new FullObserver(mHandler);
1673851fa9c8d180ca636e69b25f84fdcc294150009Christopher Tate        } else {
1683851fa9c8d180ca636e69b25f84fdcc294150009Christopher Tate            mObserver = (FullObserver) oldObserver;
1693851fa9c8d180ca636e69b25f84fdcc294150009Christopher Tate            mObserver.setHandler(mHandler);
1703851fa9c8d180ca636e69b25f84fdcc294150009Christopher Tate        }
1714a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
172c58cf7dd02ad227a68d62a0204152cee62c13182Christopher Tate        setTitle(titleId);
1734a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        setContentView(layoutId);
1744a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
1754a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        // Same resource IDs for each layout variant (backup / restore)
1764a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        mStatusView = (TextView) findViewById(R.id.package_name);
1774a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        mAllowButton = (Button) findViewById(R.id.button_allow);
1784a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        mDenyButton = (Button) findViewById(R.id.button_deny);
1794a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
180728a1c4d5ed3b808172013a7f5bb5065d1e964f6Christopher Tate        mCurPassword = (TextView) findViewById(R.id.password);
181728a1c4d5ed3b808172013a7f5bb5065d1e964f6Christopher Tate        mEncPassword = (TextView) findViewById(R.id.enc_password);
182728a1c4d5ed3b808172013a7f5bb5065d1e964f6Christopher Tate        TextView curPwDesc = (TextView) findViewById(R.id.password_desc);
183728a1c4d5ed3b808172013a7f5bb5065d1e964f6Christopher Tate
1844a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        mAllowButton.setOnClickListener(new View.OnClickListener() {
1854a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate            @Override
1864a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate            public void onClick(View v) {
187ec5d4a0f9765f1645ab2e28ad2ef3cad247a042bChristopher Tate                sendAcknowledgement(mToken, true, mObserver);
1884a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                mAllowButton.setEnabled(false);
1894a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                mDenyButton.setEnabled(false);
1904a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate            }
1914a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        });
1924a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
1934a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        mDenyButton.setOnClickListener(new View.OnClickListener() {
1944a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate            @Override
1954a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate            public void onClick(View v) {
196ec5d4a0f9765f1645ab2e28ad2ef3cad247a042bChristopher Tate                sendAcknowledgement(mToken, false, mObserver);
1974a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                mAllowButton.setEnabled(false);
1984a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate                mDenyButton.setEnabled(false);
19914d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tate                finish();
2004a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate            }
2014a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        });
2023851fa9c8d180ca636e69b25f84fdcc294150009Christopher Tate
2033851fa9c8d180ca636e69b25f84fdcc294150009Christopher Tate        // if we're a relaunch we may need to adjust button enable state
2043851fa9c8d180ca636e69b25f84fdcc294150009Christopher Tate        if (icicle != null) {
2053851fa9c8d180ca636e69b25f84fdcc294150009Christopher Tate            mDidAcknowledge = icicle.getBoolean(DID_ACKNOWLEDGE, false);
2063851fa9c8d180ca636e69b25f84fdcc294150009Christopher Tate            mAllowButton.setEnabled(!mDidAcknowledge);
2073851fa9c8d180ca636e69b25f84fdcc294150009Christopher Tate            mDenyButton.setEnabled(!mDidAcknowledge);
2083851fa9c8d180ca636e69b25f84fdcc294150009Christopher Tate        }
20914d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tate
21014d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tate        // We vary the password prompt depending on whether one is predefined, and whether
21114d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tate        // the device is encrypted.
21214d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tate        mIsEncrypted = deviceIsEncrypted();
21314d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tate        if (!haveBackupPassword()) {
21414d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tate            curPwDesc.setVisibility(View.GONE);
21514d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tate            mCurPassword.setVisibility(View.GONE);
21614d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tate            if (layoutId == R.layout.confirm_backup) {
21714d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tate                TextView encPwDesc = (TextView) findViewById(R.id.enc_password_desc);
21814d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tate                if (mIsEncrypted) {
21914d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tate                    encPwDesc.setText(R.string.backup_enc_password_required);
22014d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tate                    monitorEncryptionPassword();
22114d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tate                } else {
22214d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tate                    encPwDesc.setText(R.string.backup_enc_password_optional);
22314d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tate                }
22414d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tate            }
22514d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tate        }
22614d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tate    }
22714d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tate
22814d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tate    private void monitorEncryptionPassword() {
22914d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tate        mAllowButton.setEnabled(false);
23014d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tate        mEncPassword.addTextChangedListener(new TextWatcher() {
23114d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tate            @Override
23214d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tate            public void onTextChanged(CharSequence s, int start, int before, int count) { }
23314d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tate
23414d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tate            @Override
23514d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tate            public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
23614d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tate
23714d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tate            @Override
23814d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tate            public void afterTextChanged(Editable s) {
23914d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tate                mAllowButton.setEnabled(mEncPassword.getText().length() > 0);
24014d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tate            }
24114d0e1a4e14c1180b813d4932ba4f70ca1c8e185Christopher Tate        });
2424a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate    }
2434a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
2443851fa9c8d180ca636e69b25f84fdcc294150009Christopher Tate    // Preserve the restore observer callback binder across activity relaunch
2454a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate    @Override
2463851fa9c8d180ca636e69b25f84fdcc294150009Christopher Tate    public Object onRetainNonConfigurationInstance() {
2473851fa9c8d180ca636e69b25f84fdcc294150009Christopher Tate        return mObserver;
2483851fa9c8d180ca636e69b25f84fdcc294150009Christopher Tate    }
2494a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
2503851fa9c8d180ca636e69b25f84fdcc294150009Christopher Tate    @Override
2513851fa9c8d180ca636e69b25f84fdcc294150009Christopher Tate    protected void onSaveInstanceState(Bundle outState) {
2523851fa9c8d180ca636e69b25f84fdcc294150009Christopher Tate        outState.putBoolean(DID_ACKNOWLEDGE, mDidAcknowledge);
2534a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate    }
2544a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
255ec5d4a0f9765f1645ab2e28ad2ef3cad247a042bChristopher Tate    void sendAcknowledgement(int token, boolean allow, IFullBackupRestoreObserver observer) {
256ec5d4a0f9765f1645ab2e28ad2ef3cad247a042bChristopher Tate        if (!mDidAcknowledge) {
257ec5d4a0f9765f1645ab2e28ad2ef3cad247a042bChristopher Tate            mDidAcknowledge = true;
2582efd2dbbac9eac89620683696c6076463c3a1cd6Christopher Tate
259ec5d4a0f9765f1645ab2e28ad2ef3cad247a042bChristopher Tate            try {
26032d06732cdb7ee653a58e49a4dab13a780513db5Paul Lawrence                CharSequence encPassword = mEncPassword.getText();
261728a1c4d5ed3b808172013a7f5bb5065d1e964f6Christopher Tate                mBackupManager.acknowledgeFullBackupOrRestore(mToken,
262728a1c4d5ed3b808172013a7f5bb5065d1e964f6Christopher Tate                        allow,
263728a1c4d5ed3b808172013a7f5bb5065d1e964f6Christopher Tate                        String.valueOf(mCurPassword.getText()),
26432418be49e5b61c2e9281528cb8fb67939e301e8Christopher Tate                        String.valueOf(encPassword),
265728a1c4d5ed3b808172013a7f5bb5065d1e964f6Christopher Tate                        mObserver);
266ec5d4a0f9765f1645ab2e28ad2ef3cad247a042bChristopher Tate            } catch (RemoteException e) {
267ec5d4a0f9765f1645ab2e28ad2ef3cad247a042bChristopher Tate                // TODO: bail gracefully if we can't contact the backup manager
268ec5d4a0f9765f1645ab2e28ad2ef3cad247a042bChristopher Tate            }
269ec5d4a0f9765f1645ab2e28ad2ef3cad247a042bChristopher Tate        }
270ec5d4a0f9765f1645ab2e28ad2ef3cad247a042bChristopher Tate    }
271ec5d4a0f9765f1645ab2e28ad2ef3cad247a042bChristopher Tate
27232418be49e5b61c2e9281528cb8fb67939e301e8Christopher Tate    boolean deviceIsEncrypted() {
27332418be49e5b61c2e9281528cb8fb67939e301e8Christopher Tate        try {
27432d06732cdb7ee653a58e49a4dab13a780513db5Paul Lawrence            return mMountService.getEncryptionState()
27532d06732cdb7ee653a58e49a4dab13a780513db5Paul Lawrence                     != IMountService.ENCRYPTION_STATE_NONE
27632d06732cdb7ee653a58e49a4dab13a780513db5Paul Lawrence                && mMountService.getPasswordType()
27732d06732cdb7ee653a58e49a4dab13a780513db5Paul Lawrence                     != StorageManager.CRYPT_TYPE_DEFAULT;
27832418be49e5b61c2e9281528cb8fb67939e301e8Christopher Tate        } catch (Exception e) {
27932418be49e5b61c2e9281528cb8fb67939e301e8Christopher Tate            // If we can't talk to the mount service we have a serious problem; fail
28032418be49e5b61c2e9281528cb8fb67939e301e8Christopher Tate            // "secure" i.e. assuming that the device is encrypted.
281a23b343299783e5990370579cfc7d93e62dacb8dChristopher Tate            Slog.e(TAG, "Unable to communicate with mount service: " + e.getMessage());
28232418be49e5b61c2e9281528cb8fb67939e301e8Christopher Tate            return true;
28332418be49e5b61c2e9281528cb8fb67939e301e8Christopher Tate        }
28432418be49e5b61c2e9281528cb8fb67939e301e8Christopher Tate    }
28532418be49e5b61c2e9281528cb8fb67939e301e8Christopher Tate
286728a1c4d5ed3b808172013a7f5bb5065d1e964f6Christopher Tate    boolean haveBackupPassword() {
287728a1c4d5ed3b808172013a7f5bb5065d1e964f6Christopher Tate        try {
288728a1c4d5ed3b808172013a7f5bb5065d1e964f6Christopher Tate            return mBackupManager.hasBackupPassword();
289728a1c4d5ed3b808172013a7f5bb5065d1e964f6Christopher Tate        } catch (RemoteException e) {
290728a1c4d5ed3b808172013a7f5bb5065d1e964f6Christopher Tate            return true;        // in the failure case, assume we need one
291728a1c4d5ed3b808172013a7f5bb5065d1e964f6Christopher Tate        }
292728a1c4d5ed3b808172013a7f5bb5065d1e964f6Christopher Tate    }
293728a1c4d5ed3b808172013a7f5bb5065d1e964f6Christopher Tate
2944a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate    /**
2954a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate     * The observer binder for showing backup/restore progress.  This binder just bounces
2964a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate     * the notifications onto the main thread.
2974a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate     */
2984a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate    class FullObserver extends IFullBackupRestoreObserver.Stub {
2993851fa9c8d180ca636e69b25f84fdcc294150009Christopher Tate        private Handler mHandler;
3003851fa9c8d180ca636e69b25f84fdcc294150009Christopher Tate
3013851fa9c8d180ca636e69b25f84fdcc294150009Christopher Tate        public FullObserver(Handler h) {
3023851fa9c8d180ca636e69b25f84fdcc294150009Christopher Tate            mHandler = h;
3033851fa9c8d180ca636e69b25f84fdcc294150009Christopher Tate        }
3043851fa9c8d180ca636e69b25f84fdcc294150009Christopher Tate
3053851fa9c8d180ca636e69b25f84fdcc294150009Christopher Tate        public void setHandler(Handler h) {
3063851fa9c8d180ca636e69b25f84fdcc294150009Christopher Tate            mHandler = h;
3073851fa9c8d180ca636e69b25f84fdcc294150009Christopher Tate        }
3083851fa9c8d180ca636e69b25f84fdcc294150009Christopher Tate
3094a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        //
3104a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        // IFullBackupRestoreObserver implementation
3114a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        //
3124a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        @Override
3134a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        public void onStartBackup() throws RemoteException {
3144a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate            mHandler.sendEmptyMessage(MSG_START_BACKUP);
3154a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        }
3164a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
3174a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        @Override
3184a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        public void onBackupPackage(String name) throws RemoteException {
3194a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate            mHandler.sendMessage(mHandler.obtainMessage(MSG_BACKUP_PACKAGE, name));
3204a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        }
3214a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
3224a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        @Override
3234a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        public void onEndBackup() throws RemoteException {
3244a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate            mHandler.sendEmptyMessage(MSG_END_BACKUP);
3254a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        }
3264a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
3274a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        @Override
3284a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        public void onStartRestore() throws RemoteException {
3294a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate            mHandler.sendEmptyMessage(MSG_START_RESTORE);
3304a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        }
3314a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
3324a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        @Override
3334a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        public void onRestorePackage(String name) throws RemoteException {
3344a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate            mHandler.sendMessage(mHandler.obtainMessage(MSG_RESTORE_PACKAGE, name));
3354a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        }
3364a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
3374a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        @Override
3384a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        public void onEndRestore() throws RemoteException {
3394a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate            mHandler.sendEmptyMessage(MSG_END_RESTORE);
3404a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        }
3414a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
3424a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        @Override
3434a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        public void onTimeout() throws RemoteException {
3444a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate            mHandler.sendEmptyMessage(MSG_TIMEOUT);
3454a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate        }
3464a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate    }
3474a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate}
348