Home | History | Annotate | Download | only in deviceinfo
      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.content.Intent;
     20 import android.content.pm.PackageManager;
     21 import android.os.Bundle;
     22 import android.os.storage.DiskInfo;
     23 import android.os.storage.StorageManager;
     24 import android.os.storage.VolumeInfo;
     25 import android.util.Log;
     26 
     27 import android.widget.Toast;
     28 import com.android.settings.R;
     29 
     30 import java.util.Objects;
     31 
     32 import static com.android.settings.deviceinfo.StorageSettings.TAG;
     33 
     34 public class StorageWizardMigrateConfirm extends StorageWizardBase {
     35     private MigrateEstimateTask mEstimate;
     36 
     37     @Override
     38     protected void onCreate(Bundle savedInstanceState) {
     39         super.onCreate(savedInstanceState);
     40         setContentView(R.layout.storage_wizard_generic);
     41 
     42         // When called with just disk, find the first private volume
     43         if (mVolume == null) {
     44             mVolume = findFirstVolume(VolumeInfo.TYPE_PRIVATE);
     45         }
     46 
     47         final VolumeInfo sourceVol = getPackageManager().getPrimaryStorageCurrentVolume();
     48         if (sourceVol == null || mVolume == null) {
     49             Log.d(TAG, "Missing either source or target volume");
     50             finish();
     51             return;
     52         }
     53 
     54         final String sourceDescrip = mStorage.getBestVolumeDescription(sourceVol);
     55         final String targetDescrip = mStorage.getBestVolumeDescription(mVolume);
     56 
     57         setIllustrationType(ILLUSTRATION_INTERNAL);
     58         setHeaderText(R.string.storage_wizard_migrate_confirm_title, targetDescrip);
     59         setBodyText(R.string.memory_calculating_size);
     60         setSecondaryBodyText(R.string.storage_wizard_migrate_details, targetDescrip);
     61 
     62         mEstimate = new MigrateEstimateTask(this) {
     63             @Override
     64             public void onPostExecute(String size, String time) {
     65                 setBodyText(R.string.storage_wizard_migrate_confirm_body, time, size,
     66                         sourceDescrip);
     67             }
     68         };
     69 
     70         mEstimate.copyFrom(getIntent());
     71         mEstimate.execute();
     72 
     73         getNextButton().setText(R.string.storage_wizard_migrate_confirm_next);
     74     }
     75 
     76     @Override
     77     public void onNavigateNext() {
     78         int moveId;
     79 
     80         // We only expect exceptions from MountService#setPrimaryStorageUuid
     81         try {
     82             moveId = getPackageManager().movePrimaryStorage(mVolume);
     83         } catch (IllegalArgumentException e) {
     84             StorageManager sm = (StorageManager) getSystemService(STORAGE_SERVICE);
     85 
     86             if (Objects.equals(mVolume.getFsUuid(), sm.getPrimaryStorageVolume().getUuid())) {
     87                 final Intent intent = new Intent(this, StorageWizardReady.class);
     88                 intent.putExtra(DiskInfo.EXTRA_DISK_ID,
     89                         getIntent().getStringExtra(DiskInfo.EXTRA_DISK_ID));
     90                 startActivity(intent);
     91                 finishAffinity();
     92 
     93                 return;
     94             } else {
     95                 throw e;
     96             }
     97         } catch (IllegalStateException e) {
     98             Toast.makeText(this, getString(R.string.another_migration_already_in_progress),
     99                     Toast.LENGTH_LONG).show();
    100             finishAffinity();
    101 
    102             return;
    103         }
    104 
    105         final Intent intent = new Intent(this, StorageWizardMigrateProgress.class);
    106         intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, mVolume.getId());
    107         intent.putExtra(PackageManager.EXTRA_MOVE_ID, moveId);
    108         startActivity(intent);
    109         finishAffinity();
    110     }
    111 }
    112