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.pm.PackageManager;
20 import android.content.pm.PackageManager.MoveCallback;
21 import android.os.Bundle;
22 import android.os.Handler;
23 import android.util.Log;
24 import android.view.View;
25 import android.widget.Toast;
26 
27 import com.android.settings.R;
28 
29 import static android.content.Intent.EXTRA_TITLE;
30 import static android.content.pm.PackageManager.EXTRA_MOVE_ID;
31 import static com.android.settings.deviceinfo.StorageSettings.TAG;
32 
33 public class StorageWizardMoveProgress extends StorageWizardBase {
34     private int mMoveId;
35 
36     @Override
onCreate(Bundle savedInstanceState)37     protected void onCreate(Bundle savedInstanceState) {
38         super.onCreate(savedInstanceState);
39         if (mVolume == null) {
40             finish();
41             return;
42         }
43         setContentView(R.layout.storage_wizard_progress);
44 
45         mMoveId = getIntent().getIntExtra(EXTRA_MOVE_ID, -1);
46         final String appName = getIntent().getStringExtra(EXTRA_TITLE);
47         final String volumeName = mStorage.getBestVolumeDescription(mVolume);
48 
49         setIllustrationType(ILLUSTRATION_INTERNAL);
50         setHeaderText(R.string.storage_wizard_move_progress_title, appName);
51         setBodyText(R.string.storage_wizard_move_progress_body, volumeName, appName);
52 
53         getNextButton().setVisibility(View.GONE);
54 
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_FORWARD_LOCKED:
94                 return getString(R.string.app_forward_locked);
95             case PackageManager.MOVE_FAILED_INVALID_LOCATION:
96                 return getString(R.string.invalid_location);
97             case PackageManager.MOVE_FAILED_SYSTEM_PACKAGE:
98                 return getString(R.string.system_package);
99             case PackageManager.MOVE_FAILED_INTERNAL_ERROR:
100             default:
101                 return getString(R.string.insufficient_storage);
102         }
103     }
104 }
105