1/*
2 * Copyright (C) 2017 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 android.content.Context;
20import android.content.Intent;
21import android.os.storage.VolumeInfo;
22import android.view.Menu;
23import android.view.MenuInflater;
24import android.view.MenuItem;
25
26import com.android.settings.R;
27import com.android.settings.applications.PackageManagerWrapper;
28import com.android.settingslib.core.lifecycle.Lifecycle;
29import com.android.settingslib.core.lifecycle.LifecycleObserver;
30import com.android.settingslib.core.lifecycle.events.OnCreateOptionsMenu;
31import com.android.settingslib.core.lifecycle.events.OnOptionsItemSelected;
32import com.android.settingslib.core.lifecycle.events.OnPrepareOptionsMenu;
33
34import java.util.Objects;
35
36/**
37 * Handles the option menu on the Storage settings.
38 */
39public class PrivateVolumeOptionMenuController implements LifecycleObserver, OnCreateOptionsMenu,
40        OnPrepareOptionsMenu, OnOptionsItemSelected {
41    private static final int OPTIONS_MENU_MIGRATE_DATA = 100;
42
43    private Context mContext;
44    private VolumeInfo mVolumeInfo;
45    private PackageManagerWrapper mPm;
46
47    public PrivateVolumeOptionMenuController(
48            Context context, VolumeInfo volumeInfo, PackageManagerWrapper packageManager) {
49        mContext = context;
50        mVolumeInfo = volumeInfo;
51        mPm = packageManager;
52    }
53
54    @Override
55    public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
56        menu.add(Menu.NONE, OPTIONS_MENU_MIGRATE_DATA, 0, R.string.storage_menu_migrate);
57    }
58
59    @Override
60    public void onPrepareOptionsMenu(Menu menu) {
61        if (mVolumeInfo == null) {
62            return;
63        }
64
65        // Only offer to migrate when not current storage
66        final VolumeInfo privateVol = mPm.getPrimaryStorageCurrentVolume();
67        final MenuItem migrate = menu.findItem(OPTIONS_MENU_MIGRATE_DATA);
68        if (migrate != null) {
69            migrate.setVisible((privateVol != null)
70                    && (privateVol.getType() == VolumeInfo.TYPE_PRIVATE)
71                    && !Objects.equals(mVolumeInfo, privateVol));
72        }
73    }
74
75    @Override
76    public boolean onOptionsItemSelected(MenuItem menuItem) {
77        if (menuItem.getItemId() == OPTIONS_MENU_MIGRATE_DATA) {
78            final Intent intent = new Intent(mContext, StorageWizardMigrateConfirm.class);
79            intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, mVolumeInfo.getId());
80            mContext.startActivity(intent);
81            return true;
82        }
83        return false;
84    }
85}
86