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_TITLE;
20 import static android.content.pm.PackageManager.EXTRA_MOVE_ID;
21 
22 import static com.android.settings.deviceinfo.StorageSettings.TAG;
23 
24 import android.content.pm.PackageManager;
25 import android.content.pm.PackageManager.MoveCallback;
26 import android.os.Bundle;
27 import android.os.Handler;
28 import android.util.Log;
29 import android.view.View;
30 import android.widget.Toast;
31 
32 import com.android.settings.R;
33 
34 public class StorageWizardMoveProgress extends StorageWizardBase {
35     private int mMoveId;
36 
37     @Override
onCreate(Bundle savedInstanceState)38     protected void onCreate(Bundle savedInstanceState) {
39         super.onCreate(savedInstanceState);
40         if (mVolume == null) {
41             finish();
42             return;
43         }
44         setContentView(R.layout.storage_wizard_progress);
45 
46         mMoveId = getIntent().getIntExtra(EXTRA_MOVE_ID, -1);
47         final String appName = getIntent().getStringExtra(EXTRA_TITLE);
48         final String volumeName = mStorage.getBestVolumeDescription(mVolume);
49 
50         setIcon(R.drawable.ic_swap_horiz);
51         setHeaderText(R.string.storage_wizard_move_progress_title, appName);
52         setBodyText(R.string.storage_wizard_move_progress_body, volumeName, appName);
53         setBackButtonVisibility(View.INVISIBLE);
54         setNextButtonVisibility(View.INVISIBLE);
55         // Register for updates and push through current status
56         getPackageManager().registerMoveCallback(mCallback, new Handler());
57         mCallback.onStatusChanged(mMoveId, getPackageManager().getMoveStatus(mMoveId), -1);
58     }
59 
60     @Override
onDestroy()61     protected void onDestroy() {
62         super.onDestroy();
63         getPackageManager().unregisterMoveCallback(mCallback);
64     }
65 
66     private final MoveCallback mCallback = new MoveCallback() {
67         @Override
68         public void onStatusChanged(int moveId, int status, long estMillis) {
69             if (mMoveId != moveId) return;
70 
71             if (PackageManager.isMoveStatusFinished(status)) {
72                 Log.d(TAG, "Finished with status " + status);
73                 if (status != PackageManager.MOVE_SUCCEEDED) {
74                     Toast.makeText(StorageWizardMoveProgress.this, moveStatusToMessage(status),
75                             Toast.LENGTH_LONG).show();
76                 }
77                 finishAffinity();
78 
79             } else {
80                 setCurrentProgress(status);
81             }
82         }
83     };
84 
moveStatusToMessage(int returnCode)85     private CharSequence moveStatusToMessage(int returnCode) {
86         switch (returnCode) {
87             case PackageManager.MOVE_FAILED_INSUFFICIENT_STORAGE:
88                 return getString(R.string.insufficient_storage);
89             case PackageManager.MOVE_FAILED_DEVICE_ADMIN:
90                 return getString(R.string.move_error_device_admin);
91             case PackageManager.MOVE_FAILED_DOESNT_EXIST:
92                 return getString(R.string.does_not_exist);
93             case PackageManager.MOVE_FAILED_INVALID_LOCATION:
94                 return getString(R.string.invalid_location);
95             case PackageManager.MOVE_FAILED_SYSTEM_PACKAGE:
96                 return getString(R.string.system_package);
97             case PackageManager.MOVE_FAILED_INTERNAL_ERROR:
98             default:
99                 return getString(R.string.insufficient_storage);
100         }
101     }
102 }
103