2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.deviceinfo;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.net.Uri;
22 import android.os.Bundle;
23 import android.os.UserManager;
24 import android.os.storage.DiskInfo;
25 import android.os.storage.StorageEventListener;
26 import android.os.storage.StorageManager;
27 import android.os.storage.VolumeInfo;
28 import android.os.storage.VolumeRecord;
29 import android.preference.Preference;
30 import android.preference.PreferenceScreen;
31 import android.provider.DocumentsContract;
32 import android.text.TextUtils;
33 import android.text.format.Formatter;
34 import android.text.format.Formatter.BytesResult;
36 import com.android.internal.logging.MetricsLogger;
37 import com.android.internal.util.Preconditions;
38 import com.android.settings.R;
39 import com.android.settings.SettingsPreferenceFragment;
40 import com.android.settings.deviceinfo.StorageSettings.MountTask;
41 import com.android.settings.deviceinfo.StorageSettings.UnmountTask;
44 import java.util.Objects;
47 * Panel showing summary and actions for a {@link VolumeInfo#TYPE_PUBLIC}
50 public class PublicVolumeSettings extends SettingsPreferenceFragment {
51 // TODO: disable unmount when providing over MTP/PTP
53 private StorageManager mStorageManager;
55 private String mVolumeId;
56 private VolumeInfo mVolume;
57 private DiskInfo mDisk;
59 private StorageSummaryPreference mSummary;
61 private Preference mMount;
62 private Preference mUnmount;
63 private Preference mFormatPublic;
64 private Preference mFormatPrivate;
66 private boolean mIsPermittedToAdopt;
68 private boolean isVolumeValid() {
69 return (mVolume != null) && (mVolume.getType() == VolumeInfo.TYPE_PUBLIC)
70 && mVolume.isMountedReadable();
74 protected int getMetricsCategory() {
75 return MetricsLogger.DEVICEINFO_STORAGE;
79 public void onCreate(Bundle icicle) {
80 super.onCreate(icicle);
82 final Context context = getActivity();
84 mIsPermittedToAdopt = UserManager.get(context).isAdminUser();
86 mStorageManager = context.getSystemService(StorageManager.class);
88 if (DocumentsContract.ACTION_DOCUMENT_ROOT_SETTINGS.equals(
89 getActivity().getIntent().getAction())) {
90 final Uri rootUri = getActivity().getIntent().getData();
91 final String fsUuid = DocumentsContract.getRootId(rootUri);
92 mVolume = mStorageManager.findVolumeByUuid(fsUuid);
94 final String volId = getArguments().getString(VolumeInfo.EXTRA_VOLUME_ID);
95 mVolume = mStorageManager.findVolumeById(volId);
98 if (!isVolumeValid()) {
99 getActivity().finish();
103 mDisk = mStorageManager.findDiskById(mVolume.getDiskId());
104 Preconditions.checkNotNull(mDisk);
106 mVolumeId = mVolume.getId();
108 addPreferencesFromResource(R.xml.device_info_storage_volume);
109 getPreferenceScreen().setOrderingAsAdded(true);
111 mSummary = new StorageSummaryPreference(context);
113 mMount = buildAction(R.string.storage_menu_mount);
114 mUnmount = buildAction(R.string.storage_menu_unmount);
115 mFormatPublic = buildAction(R.string.storage_menu_format);
116 if (mIsPermittedToAdopt) {
117 mFormatPrivate = buildAction(R.string.storage_menu_format_private);
121 public void update() {
122 if (!isVolumeValid()) {
123 getActivity().finish();
127 getActivity().setTitle(mStorageManager.getBestVolumeDescription(mVolume));
129 final Context context = getActivity();
130 final PreferenceScreen screen = getPreferenceScreen();
134 if (mVolume.isMountedReadable()) {
135 addPreference(mSummary);
137 final File file = mVolume.getPath();
138 final long totalBytes = file.getTotalSpace();
139 final long freeBytes = file.getFreeSpace();
140 final long usedBytes = totalBytes - freeBytes;
142 final BytesResult result = Formatter.formatBytes(getResources(), usedBytes, 0);
143 mSummary.setTitle(TextUtils.expandTemplate(getText(R.string.storage_size_large),
144 result.value, result.units));
145 mSummary.setSummary(getString(R.string.storage_volume_used,
146 Formatter.formatFileSize(context, totalBytes)));
147 mSummary.setPercent((int) ((usedBytes * 100) / totalBytes));
150 if (mVolume.getState() == VolumeInfo.STATE_UNMOUNTED) {
151 addPreference(mMount);
153 if (mVolume.isMountedReadable()) {
154 addPreference(mUnmount);
156 addPreference(mFormatPublic);
157 if (mDisk.isAdoptable() && mIsPermittedToAdopt) {
158 addPreference(mFormatPrivate);
162 private void addPreference(Preference pref) {
163 pref.setOrder(Preference.DEFAULT_ORDER);
164 getPreferenceScreen().addPreference(pref);
167 private Preference buildAction(int titleRes) {
168 final Preference pref = new Preference(getActivity());
169 pref.setTitle(titleRes);
174 public void onResume() {
177 // Refresh to verify that we haven't been formatted away
178 mVolume = mStorageManager.findVolumeById(mVolumeId);
179 if (!isVolumeValid()) {
180 getActivity().finish();
184 mStorageManager.registerListener(mStorageListener);
189 public void onPause() {
191 mStorageManager.unregisterListener(mStorageListener);
195 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference pref) {
196 final Context context = getActivity();
197 if (pref == mMount) {
198 new MountTask(context, mVolume).execute();
199 } else if (pref == mUnmount) {
200 new UnmountTask(context, mVolume).execute();
201 } else if (pref == mFormatPublic) {
202 final Intent intent = new Intent(context, StorageWizardFormatConfirm.class);
203 intent.putExtra(DiskInfo.EXTRA_DISK_ID, mDisk.getId());
204 intent.putExtra(StorageWizardFormatConfirm.EXTRA_FORMAT_PRIVATE, false);
205 startActivity(intent);
206 } else if (pref == mFormatPrivate) {
207 final Intent intent = new Intent(context, StorageWizardFormatConfirm.class);
208 intent.putExtra(DiskInfo.EXTRA_DISK_ID, mDisk.getId());
209 intent.putExtra(StorageWizardFormatConfirm.EXTRA_FORMAT_PRIVATE, true);
210 startActivity(intent);
213 return super.onPreferenceTreeClick(preferenceScreen, pref);
216 private final StorageEventListener mStorageListener = new StorageEventListener() {
218 public void onVolumeStateChanged(VolumeInfo vol, int oldState, int newState) {
219 if (Objects.equals(mVolume.getId(), vol.getId())) {
226 public void onVolumeRecordChanged(VolumeRecord rec) {
227 if (Objects.equals(mVolume.getFsUuid(), rec.getFsUuid())) {
228 mVolume = mStorageManager.findVolumeById(mVolumeId);