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.internal.widget.LockPatternUtils;
37 import com.android.settings.R;
38 import com.android.settings.password.ChooseLockSettingsHelper;
39 
40 public class StorageWizardMoveConfirm extends StorageWizardBase {
41     private static final String TAG = "StorageWizardMoveConfirm";
42 
43     private static final int REQUEST_CREDENTIAL = 100;
44 
45     private String mPackageName;
46     private ApplicationInfo mApp;
47 
48     @Override
onCreate(Bundle savedInstanceState)49     protected void onCreate(Bundle savedInstanceState) {
50         super.onCreate(savedInstanceState);
51         if (mVolume == null) {
52             finish();
53             return;
54         }
55         setContentView(R.layout.storage_wizard_generic);
56 
57         try {
58             mPackageName = getIntent().getStringExtra(EXTRA_PACKAGE_NAME);
59             mApp = getPackageManager().getApplicationInfo(mPackageName, 0);
60         } catch (NameNotFoundException e) {
61             finish();
62             return;
63         }
64 
65         // Check that target volume is candidate
66         Preconditions.checkState(
67                 getPackageManager().getPackageCandidateVolumes(mApp).contains(mVolume));
68 
69         final String appName = getPackageManager().getApplicationLabel(mApp).toString();
70         final String volumeName = mStorage.getBestVolumeDescription(mVolume);
71 
72         setIcon(R.drawable.ic_swap_horiz);
73         setHeaderText(R.string.storage_wizard_move_confirm_title, appName);
74         setBodyText(R.string.storage_wizard_move_confirm_body, appName, volumeName);
75 
76         setNextButtonText(R.string.move_app);
77         setBackButtonVisibility(View.INVISIBLE);
78     }
79 
80     @Override
onNavigateNext(View view)81     public void onNavigateNext(View view) {
82         // Ensure that all users are unlocked so that we can move their data
83         final LockPatternUtils lpu = new LockPatternUtils(this);
84         if (StorageManager.isFileEncrypted()) {
85             for (UserInfo user : getSystemService(UserManager.class).getUsers()) {
86                 if (StorageManager.isCeStorageUnlocked(user.id)) {
87                     continue;
88                 }
89                 if (!lpu.isSecure(user.id)) {
90                     Log.d(TAG, "Unsecured user " + user.id + " is currently locked; attempting "
91                             + "automatic unlock");
92                     lpu.unlockUserKeyIfUnsecured(user.id);
93                 } else {
94                     Log.d(TAG, "Secured user " + user.id + " is currently locked; requesting "
95                             + "manual unlock");
96                     final CharSequence description = TextUtils.expandTemplate(
97                             getText(R.string.storage_wizard_move_unlock), user.name);
98                     final ChooseLockSettingsHelper.Builder builder =
99                             new ChooseLockSettingsHelper.Builder(this);
100                     builder.setRequestCode(REQUEST_CREDENTIAL)
101                             .setDescription(description)
102                             .setUserId(user.id)
103                             .setForceVerifyPath(true)
104                             .setAllowAnyUserId(true)
105                             .show();
106                     return;
107                 }
108             }
109         }
110 
111         // Kick off move before we transition
112         final String appName = getPackageManager().getApplicationLabel(mApp).toString();
113         final int moveId = getPackageManager().movePackage(mPackageName, mVolume);
114 
115         final Intent intent = new Intent(this, StorageWizardMoveProgress.class);
116         intent.putExtra(EXTRA_MOVE_ID, moveId);
117         intent.putExtra(EXTRA_TITLE, appName);
118         intent.putExtra(EXTRA_VOLUME_ID, mVolume.getId());
119         startActivity(intent);
120         finishAffinity();
121     }
122 
123     @Override
onActivityResult(int requestCode, int resultCode, Intent data)124     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
125         if (requestCode == REQUEST_CREDENTIAL) {
126             if (resultCode == RESULT_OK) {
127                 // Credentials confirmed, so storage should be unlocked; let's
128                 // go look for the next locked user.
129                 onNavigateNext(null);
130             } else {
131                 // User wasn't able to confirm credentials, so we're okay
132                 // landing back at the wizard page again, where they read
133                 // instructions again and tap "Next" to try again.
134                 Log.w(TAG, "Failed to confirm credentials");
135             }
136         } else {
137             super.onActivityResult(requestCode, resultCode, data);
138         }
139     }
140 }
141