• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.settings.deviceinfo;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.pm.PackageManager;
22 import android.os.storage.VolumeInfo;
23 import android.view.Menu;
24 import android.view.MenuInflater;
25 import android.view.MenuItem;
26 
27 import com.android.settings.R;
28 import com.android.settingslib.core.lifecycle.LifecycleObserver;
29 import com.android.settingslib.core.lifecycle.events.OnCreateOptionsMenu;
30 import com.android.settingslib.core.lifecycle.events.OnOptionsItemSelected;
31 import com.android.settingslib.core.lifecycle.events.OnPrepareOptionsMenu;
32 
33 import java.util.Objects;
34 
35 /**
36  * Handles the option menu on the Storage settings.
37  */
38 public class PrivateVolumeOptionMenuController implements LifecycleObserver, OnCreateOptionsMenu,
39         OnPrepareOptionsMenu, OnOptionsItemSelected {
40     private static final int OPTIONS_MENU_MIGRATE_DATA = 100;
41 
42     private Context mContext;
43     private VolumeInfo mVolumeInfo;
44     private PackageManager mPm;
45 
PrivateVolumeOptionMenuController( Context context, VolumeInfo volumeInfo, PackageManager packageManager)46     public PrivateVolumeOptionMenuController(
47             Context context, VolumeInfo volumeInfo, PackageManager packageManager) {
48         mContext = context;
49         mVolumeInfo = volumeInfo;
50         mPm = packageManager;
51     }
52 
53     @Override
onCreateOptionsMenu(final Menu menu, final MenuInflater inflater)54     public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
55         menu.add(Menu.NONE, OPTIONS_MENU_MIGRATE_DATA, 0, R.string.storage_menu_migrate);
56     }
57 
58     @Override
onPrepareOptionsMenu(Menu menu)59     public void onPrepareOptionsMenu(Menu menu) {
60         if (mVolumeInfo == null) {
61             return;
62         }
63 
64         // Only offer to migrate when not current storage
65         final VolumeInfo privateVol = mPm.getPrimaryStorageCurrentVolume();
66         final MenuItem migrate = menu.findItem(OPTIONS_MENU_MIGRATE_DATA);
67         if (migrate != null) {
68             migrate.setVisible((privateVol != null)
69                     && (privateVol.getType() == VolumeInfo.TYPE_PRIVATE)
70                     && !Objects.equals(mVolumeInfo, privateVol)
71                     && privateVol.isMountedWritable());
72         }
73     }
74 
75     @Override
onOptionsItemSelected(MenuItem menuItem)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