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 android.app.settings.SettingsEnums;
20 import android.content.Intent;
21 import android.content.pm.PackageManager;
22 import android.content.pm.UserInfo;
23 import android.os.Bundle;
24 import android.os.UserManager;
25 import android.os.storage.DiskInfo;
26 import android.os.storage.StorageManager;
27 import android.os.storage.VolumeInfo;
28 import android.text.TextUtils;
29 import android.util.Log;
30 import android.view.View;
31 import android.widget.Toast;
32 
33 import com.android.settings.R;
34 import com.android.settings.overlay.FeatureFactory;
35 import com.android.settings.password.ChooseLockSettingsHelper;
36 
37 import java.util.Objects;
38 
39 public class StorageWizardMigrateConfirm extends StorageWizardBase {
40     private static final String TAG = "StorageWizardMigrateConfirm";
41 
42     private static final int REQUEST_CREDENTIAL = 100;
43 
44     private MigrateEstimateTask mEstimate;
45 
46     @Override
onCreate(Bundle savedInstanceState)47     protected void onCreate(Bundle savedInstanceState) {
48         super.onCreate(savedInstanceState);
49         setContentView(R.layout.storage_wizard_generic);
50 
51         // When called with just disk, find the first private volume
52         if (mVolume == null) {
53             mVolume = findFirstVolume(VolumeInfo.TYPE_PRIVATE);
54         }
55 
56         final VolumeInfo sourceVol = getPackageManager().getPrimaryStorageCurrentVolume();
57         if (sourceVol == null || mVolume == null) {
58             Log.d(TAG, "Missing either source or target volume");
59             finish();
60             return;
61         }
62 
63         setIcon(R.drawable.ic_swap_horiz);
64         setHeaderText(R.string.storage_wizard_migrate_v2_title, getDiskShortDescription());
65         setBodyText(R.string.memory_calculating_size);
66         setAuxChecklist();
67 
68         mEstimate = new MigrateEstimateTask(this) {
69             @Override
70             public void onPostExecute(String size, String time) {
71                 setBodyText(R.string.storage_wizard_migrate_v2_body,
72                         getDiskDescription(), size, time);
73             }
74         };
75 
76         mEstimate.copyFrom(getIntent());
77         mEstimate.execute();
78 
79         setBackButtonText(R.string.storage_wizard_migrate_v2_later);
80         setNextButtonText(R.string.storage_wizard_migrate_v2_now);
81     }
82 
83     @Override
onNavigateBack(View view)84     public void onNavigateBack(View view) {
85         FeatureFactory.getFactory(this).getMetricsFeatureProvider().action(this,
86                 SettingsEnums.ACTION_STORAGE_MIGRATE_LATER);
87 
88         if (mDisk != null) {
89             final Intent intent = new Intent(this, StorageWizardReady.class);
90             intent.putExtra(EXTRA_MIGRATE_SKIP, true);
91             startActivity(intent);
92         } else {
93             finishAffinity();
94         }
95     }
96 
97     @Override
onNavigateNext(View view)98     public void onNavigateNext(View view) {
99         // Ensure that all users are unlocked so that we can move their data
100         if (StorageManager.isFileEncryptedNativeOrEmulated()) {
101             for (UserInfo user : getSystemService(UserManager.class).getUsers()) {
102                 if (!StorageManager.isUserKeyUnlocked(user.id)) {
103                     Log.d(TAG, "User " + user.id + " is currently locked; requesting unlock");
104                     final CharSequence description = TextUtils.expandTemplate(
105                             getText(R.string.storage_wizard_move_unlock), user.name);
106                     final ChooseLockSettingsHelper.Builder builder =
107                             new ChooseLockSettingsHelper.Builder(this);
108                     builder.setRequestCode(REQUEST_CREDENTIAL)
109                             .setDescription(description)
110                             .setUserId(user.id)
111                             .setAllowAnyUserId(true)
112                             .setForceVerifyPath(true)
113                             .show();
114                     return;
115                 }
116             }
117         }
118 
119         // We only expect exceptions from StorageManagerService#setPrimaryStorageUuid
120         int moveId;
121         try {
122             moveId = getPackageManager().movePrimaryStorage(mVolume);
123         } catch (IllegalArgumentException e) {
124             StorageManager sm = (StorageManager) getSystemService(STORAGE_SERVICE);
125 
126             if (Objects.equals(mVolume.getFsUuid(), sm.getPrimaryStorageVolume().getUuid())) {
127                 final Intent intent = new Intent(this, StorageWizardReady.class);
128                 intent.putExtra(DiskInfo.EXTRA_DISK_ID,
129                         getIntent().getStringExtra(DiskInfo.EXTRA_DISK_ID));
130                 startActivity(intent);
131                 finishAffinity();
132 
133                 return;
134             } else {
135                 throw e;
136             }
137         } catch (IllegalStateException e) {
138             Toast.makeText(this, getString(R.string.another_migration_already_in_progress),
139                     Toast.LENGTH_LONG).show();
140             finishAffinity();
141 
142             return;
143         }
144 
145         FeatureFactory.getFactory(this).getMetricsFeatureProvider().action(this,
146                 SettingsEnums.ACTION_STORAGE_MIGRATE_NOW);
147 
148         final Intent intent = new Intent(this, StorageWizardMigrateProgress.class);
149         intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, mVolume.getId());
150         intent.putExtra(PackageManager.EXTRA_MOVE_ID, moveId);
151         startActivity(intent);
152         finishAffinity();
153     }
154 
155     @Override
onActivityResult(int requestCode, int resultCode, Intent data)156     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
157         if (requestCode == REQUEST_CREDENTIAL) {
158             if (resultCode == RESULT_OK) {
159                 // Credentials confirmed, so storage should be unlocked; let's
160                 // go look for the next locked user.
161                 onNavigateNext(null);
162             } else {
163                 // User wasn't able to confirm credentials, so we're okay
164                 // landing back at the wizard page again, where they read
165                 // instructions again and tap "Next" to try again.
166                 Log.w(TAG, "Failed to confirm credentials");
167             }
168         } else {
169             super.onActivityResult(requestCode, resultCode, data);
170         }
171     }
172 }
173