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