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 com.android.settings.deviceinfo.StorageSettings.TAG; 20 21 import android.app.settings.SettingsEnums; 22 import android.content.Intent; 23 import android.content.pm.PackageManager; 24 import android.content.pm.UserInfo; 25 import android.os.Bundle; 26 import android.os.UserManager; 27 import android.os.storage.DiskInfo; 28 import android.os.storage.StorageManager; 29 import android.os.storage.VolumeInfo; 30 import android.text.TextUtils; 31 import android.util.Log; 32 import android.view.View; 33 import android.widget.Toast; 34 35 import com.android.settings.R; 36 import com.android.settings.overlay.FeatureFactory; 37 import com.android.settings.password.ChooseLockSettingsHelper; 38 39 import java.util.Objects; 40 41 public class StorageWizardMigrateConfirm extends StorageWizardBase { 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 new ChooseLockSettingsHelper(this).launchConfirmationActivityForAnyUser( 107 REQUEST_CREDENTIAL, null, null, description, user.id); 108 return; 109 } 110 } 111 } 112 113 // We only expect exceptions from StorageManagerService#setPrimaryStorageUuid 114 int moveId; 115 try { 116 moveId = getPackageManager().movePrimaryStorage(mVolume); 117 } catch (IllegalArgumentException e) { 118 StorageManager sm = (StorageManager) getSystemService(STORAGE_SERVICE); 119 120 if (Objects.equals(mVolume.getFsUuid(), sm.getPrimaryStorageVolume().getUuid())) { 121 final Intent intent = new Intent(this, StorageWizardReady.class); 122 intent.putExtra(DiskInfo.EXTRA_DISK_ID, 123 getIntent().getStringExtra(DiskInfo.EXTRA_DISK_ID)); 124 startActivity(intent); 125 finishAffinity(); 126 127 return; 128 } else { 129 throw e; 130 } 131 } catch (IllegalStateException e) { 132 Toast.makeText(this, getString(R.string.another_migration_already_in_progress), 133 Toast.LENGTH_LONG).show(); 134 finishAffinity(); 135 136 return; 137 } 138 139 FeatureFactory.getFactory(this).getMetricsFeatureProvider().action(this, 140 SettingsEnums.ACTION_STORAGE_MIGRATE_NOW); 141 142 final Intent intent = new Intent(this, StorageWizardMigrateProgress.class); 143 intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, mVolume.getId()); 144 intent.putExtra(PackageManager.EXTRA_MOVE_ID, moveId); 145 startActivity(intent); 146 finishAffinity(); 147 } 148 149 @Override onActivityResult(int requestCode, int resultCode, Intent data)150 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 151 if (requestCode == REQUEST_CREDENTIAL) { 152 if (resultCode == RESULT_OK) { 153 // Credentials confirmed, so storage should be unlocked; let's 154 // go look for the next locked user. 155 onNavigateNext(null); 156 } else { 157 // User wasn't able to confirm credentials, so we're okay 158 // landing back at the wizard page again, where they read 159 // instructions again and tap "Next" to try again. 160 Log.w(TAG, "Failed to confirm credentials"); 161 } 162 } else { 163 super.onActivityResult(requestCode, resultCode, data); 164 } 165 } 166 } 167