PublicVolumeSettings.java revision 2597625fd9704ff9eab94987d332378f806dae83
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 com.android.settings.deviceinfo.StorageSettings.TAG; 20 21import android.content.Context; 22import android.content.Intent; 23import android.net.Uri; 24import android.os.Bundle; 25import android.os.storage.DiskInfo; 26import android.os.storage.StorageEventListener; 27import android.os.storage.StorageManager; 28import android.os.storage.VolumeInfo; 29import android.os.storage.VolumeRecord; 30import android.preference.Preference; 31import android.preference.PreferenceScreen; 32import android.provider.DocumentsContract; 33import android.text.TextUtils; 34import android.text.format.Formatter; 35import android.text.format.Formatter.BytesResult; 36import android.util.Log; 37 38import com.android.internal.logging.MetricsLogger; 39import com.android.internal.util.Preconditions; 40import com.android.settings.R; 41import com.android.settings.SettingsPreferenceFragment; 42import com.android.settings.deviceinfo.StorageSettings.MountTask; 43import com.android.settings.deviceinfo.StorageSettings.UnmountTask; 44 45import java.io.File; 46import java.util.Objects; 47 48/** 49 * Panel showing summary and actions for a {@link VolumeInfo#TYPE_PUBLIC} 50 * storage volume. 51 */ 52public class PublicVolumeSettings extends SettingsPreferenceFragment { 53 // TODO: disable unmount when providing over MTP/PTP 54 55 private StorageManager mStorageManager; 56 57 private String mVolumeId; 58 private VolumeInfo mVolume; 59 private DiskInfo mDisk; 60 61 private int mNextOrder = 0; 62 63 private StorageSummaryPreference mSummary; 64 65 private Preference mMount; 66 private Preference mUnmount; 67 private Preference mFormatPublic; 68 private Preference mFormatPrivate; 69 70 @Override 71 protected int getMetricsCategory() { 72 return MetricsLogger.DEVICEINFO_STORAGE; 73 } 74 75 @Override 76 public void onCreate(Bundle icicle) { 77 super.onCreate(icicle); 78 79 final Context context = getActivity(); 80 81 mStorageManager = context.getSystemService(StorageManager.class); 82 83 if (DocumentsContract.ACTION_DOCUMENT_ROOT_SETTINGS.equals( 84 getActivity().getIntent().getAction())) { 85 final Uri rootUri = getActivity().getIntent().getData(); 86 final String fsUuid = DocumentsContract.getRootId(rootUri); 87 mVolume = mStorageManager.findVolumeByUuid(fsUuid); 88 } else { 89 final String volId = getArguments().getString(VolumeInfo.EXTRA_VOLUME_ID); 90 mVolume = mStorageManager.findVolumeById(volId); 91 } 92 93 Preconditions.checkNotNull(mVolume); 94 Preconditions.checkState(mVolume.getType() == VolumeInfo.TYPE_PUBLIC); 95 96 mDisk = mStorageManager.findDiskById(mVolume.getDiskId()); 97 Preconditions.checkNotNull(mDisk); 98 99 mVolumeId = mVolume.getId(); 100 101 addPreferencesFromResource(R.xml.device_info_storage_volume); 102 103 mSummary = new StorageSummaryPreference(context); 104 105 mMount = buildAction(R.string.storage_menu_mount); 106 mUnmount = buildAction(R.string.storage_menu_unmount); 107 mFormatPublic = buildAction(R.string.storage_menu_format); 108 mFormatPrivate = buildAction(R.string.storage_menu_format_private); 109 } 110 111 public void update() { 112 getActivity().setTitle(mStorageManager.getBestVolumeDescription(mVolume)); 113 114 final Context context = getActivity(); 115 final PreferenceScreen screen = getPreferenceScreen(); 116 117 screen.removeAll(); 118 119 if (!mVolume.isMountedReadable()) { 120 Log.d(TAG, "Leaving details fragment due to state " + mVolume.getState()); 121 finish(); 122 return; 123 } 124 125 if (mVolume.isMountedReadable()) { 126 screen.addPreference(mSummary); 127 128 final File file = mVolume.getPath(); 129 final long totalBytes = file.getTotalSpace(); 130 final long freeBytes = file.getFreeSpace(); 131 final long usedBytes = totalBytes - freeBytes; 132 133 final BytesResult result = Formatter.formatBytes(getResources(), usedBytes, 0); 134 mSummary.setTitle(TextUtils.expandTemplate(getText(R.string.storage_size_large), 135 result.value, result.units)); 136 mSummary.setSummary(getString(R.string.storage_volume_used, 137 Formatter.formatFileSize(context, totalBytes))); 138 mSummary.setPercent((int) ((usedBytes * 100) / totalBytes)); 139 } 140 141 if (mVolume.getState() == VolumeInfo.STATE_UNMOUNTED) { 142 screen.addPreference(mMount); 143 } 144 if (mVolume.isMountedReadable()) { 145 screen.addPreference(mUnmount); 146 } 147 screen.addPreference(mFormatPublic); 148 if (mDisk.isAdoptable()) { 149 screen.addPreference(mFormatPrivate); 150 } 151 } 152 153 private Preference buildAction(int titleRes) { 154 final Preference pref = new Preference(getActivity()); 155 pref.setTitle(titleRes); 156 pref.setOrder(mNextOrder++); 157 return pref; 158 } 159 160 @Override 161 public void onResume() { 162 super.onResume(); 163 164 // Refresh to verify that we haven't been formatted away 165 mVolume = mStorageManager.findVolumeById(mVolumeId); 166 if (mVolume == null) { 167 getActivity().finish(); 168 return; 169 } 170 171 mStorageManager.registerListener(mStorageListener); 172 update(); 173 } 174 175 @Override 176 public void onPause() { 177 super.onPause(); 178 mStorageManager.unregisterListener(mStorageListener); 179 } 180 181 @Override 182 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference pref) { 183 final Context context = getActivity(); 184 if (pref == mMount) { 185 new MountTask(context, mVolume).execute(); 186 } else if (pref == mUnmount) { 187 new UnmountTask(context, mVolume).execute(); 188 } else if (pref == mFormatPublic) { 189 final Intent intent = new Intent(context, StorageWizardFormatConfirm.class); 190 intent.putExtra(DiskInfo.EXTRA_DISK_ID, mDisk.getId()); 191 intent.putExtra(StorageWizardFormatConfirm.EXTRA_FORMAT_PRIVATE, false); 192 startActivity(intent); 193 } else if (pref == mFormatPrivate) { 194 final Intent intent = new Intent(context, StorageWizardFormatConfirm.class); 195 intent.putExtra(DiskInfo.EXTRA_DISK_ID, mDisk.getId()); 196 intent.putExtra(StorageWizardFormatConfirm.EXTRA_FORMAT_PRIVATE, true); 197 startActivity(intent); 198 } 199 200 return super.onPreferenceTreeClick(preferenceScreen, pref); 201 } 202 203 private final StorageEventListener mStorageListener = new StorageEventListener() { 204 @Override 205 public void onVolumeStateChanged(VolumeInfo vol, int oldState, int newState) { 206 if (Objects.equals(mVolume.getId(), vol.getId())) { 207 mVolume = vol; 208 update(); 209 } 210 } 211 212 @Override 213 public void onVolumeRecordChanged(VolumeRecord rec) { 214 if (Objects.equals(mVolume.getFsUuid(), rec.getFsUuid())) { 215 mVolume = mStorageManager.findVolumeById(mVolumeId); 216 update(); 217 } 218 } 219 }; 220} 221