1/*
2 * Copyright (C) 2015 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
17package com.android.settings.deviceinfo;
18
19import static android.content.Intent.EXTRA_PACKAGE_NAME;
20import static android.content.Intent.EXTRA_TITLE;
21import static android.content.pm.PackageManager.EXTRA_MOVE_ID;
22import static android.os.storage.VolumeInfo.EXTRA_VOLUME_ID;
23
24import static com.android.settings.deviceinfo.StorageSettings.TAG;
25
26import android.content.Intent;
27import android.content.pm.ApplicationInfo;
28import android.content.pm.PackageManager.NameNotFoundException;
29import android.content.pm.UserInfo;
30import android.os.Bundle;
31import android.os.UserManager;
32import android.os.storage.StorageManager;
33import android.text.TextUtils;
34import android.util.Log;
35import android.view.View;
36
37import com.android.internal.util.Preconditions;
38import com.android.settings.R;
39import com.android.settings.password.ChooseLockSettingsHelper;
40
41public class StorageWizardMoveConfirm extends StorageWizardBase {
42    private static final int REQUEST_CREDENTIAL = 100;
43
44    private String mPackageName;
45    private ApplicationInfo mApp;
46
47    @Override
48    protected void onCreate(Bundle savedInstanceState) {
49        super.onCreate(savedInstanceState);
50        if (mVolume == null) {
51            finish();
52            return;
53        }
54        setContentView(R.layout.storage_wizard_generic);
55
56        try {
57            mPackageName = getIntent().getStringExtra(EXTRA_PACKAGE_NAME);
58            mApp = getPackageManager().getApplicationInfo(mPackageName, 0);
59        } catch (NameNotFoundException e) {
60            finish();
61            return;
62        }
63
64        // Sanity check that target volume is candidate
65        Preconditions.checkState(
66                getPackageManager().getPackageCandidateVolumes(mApp).contains(mVolume));
67
68        final String appName = getPackageManager().getApplicationLabel(mApp).toString();
69        final String volumeName = mStorage.getBestVolumeDescription(mVolume);
70
71        setIcon(R.drawable.ic_swap_horiz);
72        setHeaderText(R.string.storage_wizard_move_confirm_title, appName);
73        setBodyText(R.string.storage_wizard_move_confirm_body, appName, volumeName);
74
75        setNextButtonText(R.string.move_app);
76    }
77
78    @Override
79    public void onNavigateNext(View view) {
80        // Ensure that all users are unlocked so that we can move their data
81        if (StorageManager.isFileEncryptedNativeOrEmulated()) {
82            for (UserInfo user : getSystemService(UserManager.class).getUsers()) {
83                if (!StorageManager.isUserKeyUnlocked(user.id)) {
84                    Log.d(TAG, "User " + user.id + " is currently locked; requesting unlock");
85                    final CharSequence description = TextUtils.expandTemplate(
86                            getText(R.string.storage_wizard_move_unlock), user.name);
87                    new ChooseLockSettingsHelper(this).launchConfirmationActivityForAnyUser(
88                            REQUEST_CREDENTIAL, null, null, description, user.id);
89                    return;
90                }
91            }
92        }
93
94        // Kick off move before we transition
95        final String appName = getPackageManager().getApplicationLabel(mApp).toString();
96        final int moveId = getPackageManager().movePackage(mPackageName, mVolume);
97
98        final Intent intent = new Intent(this, StorageWizardMoveProgress.class);
99        intent.putExtra(EXTRA_MOVE_ID, moveId);
100        intent.putExtra(EXTRA_TITLE, appName);
101        intent.putExtra(EXTRA_VOLUME_ID, mVolume.getId());
102        startActivity(intent);
103        finishAffinity();
104    }
105
106    @Override
107    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
108        if (requestCode == REQUEST_CREDENTIAL) {
109            if (resultCode == RESULT_OK) {
110                // Credentials confirmed, so storage should be unlocked; let's
111                // go look for the next locked user.
112                onNavigateNext(null);
113            } else {
114                // User wasn't able to confirm credentials, so we're okay
115                // landing back at the wizard page again, where they read
116                // instructions again and tap "Next" to try again.
117                Log.w(TAG, "Failed to confirm credentials");
118            }
119        } else {
120            super.onActivityResult(requestCode, resultCode, data);
121        }
122    }
123}
124