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 
17 package com.android.settings.deviceinfo;
18 
19 import static android.content.Intent.EXTRA_PACKAGE_NAME;
20 import static android.content.Intent.EXTRA_TITLE;
21 import static android.content.pm.PackageManager.EXTRA_MOVE_ID;
22 import static android.os.storage.VolumeInfo.EXTRA_VOLUME_ID;
23 
24 import static com.android.settings.deviceinfo.StorageSettings.TAG;
25 
26 import android.content.Intent;
27 import android.content.pm.ApplicationInfo;
28 import android.content.pm.PackageManager.NameNotFoundException;
29 import android.content.pm.UserInfo;
30 import android.os.Bundle;
31 import android.os.UserManager;
32 import android.os.storage.StorageManager;
33 import android.text.TextUtils;
34 import android.util.Log;
35 import android.view.View;
36 
37 import com.android.internal.util.Preconditions;
38 import com.android.settings.R;
39 import com.android.settings.password.ChooseLockSettingsHelper;
40 
41 public class StorageWizardMoveConfirm extends StorageWizardBase {
42     private static final int REQUEST_CREDENTIAL = 100;
43 
44     private String mPackageName;
45     private ApplicationInfo mApp;
46 
47     @Override
onCreate(Bundle savedInstanceState)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         setBackButtonVisibility(View.INVISIBLE);
77     }
78 
79     @Override
onNavigateNext(View view)80     public void onNavigateNext(View view) {
81         // Ensure that all users are unlocked so that we can move their data
82         if (StorageManager.isFileEncryptedNativeOrEmulated()) {
83             for (UserInfo user : getSystemService(UserManager.class).getUsers()) {
84                 if (!StorageManager.isUserKeyUnlocked(user.id)) {
85                     Log.d(TAG, "User " + user.id + " is currently locked; requesting unlock");
86                     final CharSequence description = TextUtils.expandTemplate(
87                             getText(R.string.storage_wizard_move_unlock), user.name);
88                     new ChooseLockSettingsHelper(this).launchConfirmationActivityForAnyUser(
89                             REQUEST_CREDENTIAL, null, null, description, user.id);
90                     return;
91                 }
92             }
93         }
94 
95         // Kick off move before we transition
96         final String appName = getPackageManager().getApplicationLabel(mApp).toString();
97         final int moveId = getPackageManager().movePackage(mPackageName, mVolume);
98 
99         final Intent intent = new Intent(this, StorageWizardMoveProgress.class);
100         intent.putExtra(EXTRA_MOVE_ID, moveId);
101         intent.putExtra(EXTRA_TITLE, appName);
102         intent.putExtra(EXTRA_VOLUME_ID, mVolume.getId());
103         startActivity(intent);
104         finishAffinity();
105     }
106 
107     @Override
onActivityResult(int requestCode, int resultCode, Intent data)108     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
109         if (requestCode == REQUEST_CREDENTIAL) {
110             if (resultCode == RESULT_OK) {
111                 // Credentials confirmed, so storage should be unlocked; let's
112                 // go look for the next locked user.
113                 onNavigateNext(null);
114             } else {
115                 // User wasn't able to confirm credentials, so we're okay
116                 // landing back at the wizard page again, where they read
117                 // instructions again and tap "Next" to try again.
118                 Log.w(TAG, "Failed to confirm credentials");
119             }
120         } else {
121             super.onActivityResult(requestCode, resultCode, data);
122         }
123     }
124 }
125