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
17package com.android.settings.deviceinfo;
18
19import static android.content.Intent.EXTRA_TITLE;
20import static android.content.pm.PackageManager.EXTRA_MOVE_ID;
21import static com.android.settings.deviceinfo.StorageSettings.TAG;
22
23import android.content.pm.PackageManager;
24import android.content.pm.PackageManager.MoveCallback;
25import android.os.Bundle;
26import android.os.Handler;
27import android.util.Log;
28import android.view.View;
29import android.widget.Toast;
30
31import com.android.settings.R;
32
33public class StorageWizardMoveProgress extends StorageWizardBase {
34    private int mMoveId;
35
36    @Override
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        setIllustrationInternal(true);
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
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
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_DOESNT_EXIST:
90                return getString(R.string.does_not_exist);
91            case PackageManager.MOVE_FAILED_FORWARD_LOCKED:
92                return getString(R.string.app_forward_locked);
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