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 android.content.Intent;
25 import android.content.pm.ApplicationInfo;
26 import android.content.pm.PackageManager.NameNotFoundException;
27 import android.content.pm.UserInfo;
28 import android.os.Bundle;
29 import android.os.UserManager;
30 import android.os.storage.StorageManager;
31 import android.text.TextUtils;
32 import android.util.Log;
33 import android.view.View;
34 
35 import com.android.internal.util.Preconditions;
36 import com.android.settings.R;
37 import com.android.settings.password.ChooseLockSettingsHelper;
38 
39 public class StorageWizardMoveConfirm extends StorageWizardBase {
40     private static final String TAG = "StorageWizardMoveConfirm";
41 
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         // 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                     final ChooseLockSettingsHelper.Builder builder =
89                             new ChooseLockSettingsHelper.Builder(this);
90                     builder.setRequestCode(REQUEST_CREDENTIAL)
91                             .setDescription(description)
92                             .setUserId(user.id)
93                             .setForceVerifyPath(true)
94                             .setAllowAnyUserId(true)
95                             .show();
96                     return;
97                 }
98             }
99         }
100 
101         // Kick off move before we transition
102         final String appName = getPackageManager().getApplicationLabel(mApp).toString();
103         final int moveId = getPackageManager().movePackage(mPackageName, mVolume);
104 
105         final Intent intent = new Intent(this, StorageWizardMoveProgress.class);
106         intent.putExtra(EXTRA_MOVE_ID, moveId);
107         intent.putExtra(EXTRA_TITLE, appName);
108         intent.putExtra(EXTRA_VOLUME_ID, mVolume.getId());
109         startActivity(intent);
110         finishAffinity();
111     }
112 
113     @Override
onActivityResult(int requestCode, int resultCode, Intent data)114     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
115         if (requestCode == REQUEST_CREDENTIAL) {
116             if (resultCode == RESULT_OK) {
117                 // Credentials confirmed, so storage should be unlocked; let's
118                 // go look for the next locked user.
119                 onNavigateNext(null);
120             } else {
121                 // User wasn't able to confirm credentials, so we're okay
122                 // landing back at the wizard page again, where they read
123                 // instructions again and tap "Next" to try again.
124                 Log.w(TAG, "Failed to confirm credentials");
125             }
126         } else {
127             super.onActivityResult(requestCode, resultCode, data);
128         }
129     }
130 }
131