1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.deviceinfo;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.storage.DiskInfo;
import android.os.storage.StorageEventListener;
import android.os.storage.StorageManager;
import android.os.storage.VolumeInfo;
import android.preference.Preference;
import android.preference.PreferenceScreen;
import android.provider.DocumentsContract;
import android.text.format.Formatter;
import com.android.internal.logging.MetricsLogger;
import com.android.internal.util.Preconditions;
import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.deviceinfo.StorageSettings.FormatTask;
import com.android.settings.deviceinfo.StorageSettings.MountTask;
import com.android.settings.deviceinfo.StorageSettings.UnmountTask;
import java.io.File;
import java.util.Objects;
/**
* Panel showing summary and actions for a {@link VolumeInfo#TYPE_PUBLIC}
* storage volume.
*/
public class PublicVolumeSettings extends SettingsPreferenceFragment {
// TODO: disable unmount when providing over MTP/PTP
private StorageManager mStorageManager;
private String mVolumeId;
private VolumeInfo mVolume;
private DiskInfo mDisk;
private int mNextOrder = 0;
private UsageBarPreference mGraph;
private StorageItemPreference mTotal;
private StorageItemPreference mAvailable;
private Preference mMount;
private Preference mUnmount;
private Preference mFormat;
private Preference mFormatInternal;
private long mTotalSize;
private long mAvailSize;
@Override
protected int getMetricsCategory() {
return MetricsLogger.DEVICEINFO_STORAGE;
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
final Context context = getActivity();
mStorageManager = context.getSystemService(StorageManager.class);
if (DocumentsContract.ACTION_DOCUMENT_ROOT_SETTINGS.equals(
getActivity().getIntent().getAction())) {
final Uri rootUri = getActivity().getIntent().getData();
final String fsUuid = DocumentsContract.getRootId(rootUri);
mVolume = mStorageManager.findVolumeByUuid(fsUuid);
} else {
final String volId = getArguments().getString(VolumeInfo.EXTRA_VOLUME_ID);
mVolume = mStorageManager.findVolumeById(volId);
}
Preconditions.checkNotNull(mVolume);
Preconditions.checkState(mVolume.getType() == VolumeInfo.TYPE_PUBLIC);
mDisk = mStorageManager.findDiskById(mVolume.getDiskId());
Preconditions.checkNotNull(mDisk);
mVolumeId = mVolume.getId();
addPreferencesFromResource(R.xml.device_info_storage_volume);
mGraph = buildGraph();
mTotal = buildItem(R.string.memory_size, 0);
mAvailable = buildItem(R.string.memory_available, R.color.memory_avail);
mMount = buildAction(R.string.storage_menu_mount);
mUnmount = buildAction(R.string.storage_menu_unmount);
mFormat = buildAction(R.string.storage_menu_format);
mFormatInternal = buildAction(R.string.storage_menu_format_internal);
}
public void refresh() {
getActivity().setTitle(mStorageManager.getBestVolumeDescription(mVolume));
final Context context = getActivity();
final PreferenceScreen screen = getPreferenceScreen();
screen.removeAll();
if (mVolume.isMountedReadable()) {
screen.addPreference(mGraph);
screen.addPreference(mTotal);
screen.addPreference(mAvailable);
final File file = mVolume.getPath();
mTotalSize = file.getTotalSpace();
mAvailSize = file.getFreeSpace();
mTotal.setSummary(Formatter.formatFileSize(context, mTotalSize));
mAvailable.setSummary(Formatter.formatFileSize(context, mAvailSize));
mGraph.clear();
mGraph.addEntry(0, (mTotalSize - mAvailSize) / (float) mTotalSize,
android.graphics.Color.GRAY);
mGraph.commit();
}
if (mVolume.getState() == VolumeInfo.STATE_UNMOUNTED) {
screen.addPreference(mMount);
}
if (mVolume.isMountedReadable()) {
screen.addPreference(mUnmount);
}
screen.addPreference(mFormat);
if (mDisk.isAdoptable()) {
screen.addPreference(mFormatInternal);
}
}
private UsageBarPreference buildGraph() {
final UsageBarPreference pref = new UsageBarPreference(getActivity());
pref.setOrder(mNextOrder++);
return pref;
}
private StorageItemPreference buildItem(int titleRes, int colorRes) {
final StorageItemPreference pref = new StorageItemPreference(getActivity(), titleRes,
colorRes);
pref.setOrder(mNextOrder++);
return pref;
}
private Preference buildAction(int titleRes) {
final Preference pref = new Preference(getActivity());
pref.setTitle(titleRes);
pref.setOrder(mNextOrder++);
return pref;
}
@Override
public void onResume() {
super.onResume();
// Refresh to verify that we haven't been formatted away
mVolume = mStorageManager.findVolumeById(mVolumeId);
if (mVolume == null) {
getActivity().finish();
return;
}
mStorageManager.registerListener(mStorageListener);
refresh();
}
@Override
public void onPause() {
super.onPause();
mStorageManager.unregisterListener(mStorageListener);
}
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference pref) {
final Context context = getActivity();
if (pref == mMount) {
new MountTask(context, mVolume).execute();
} else if (pref == mUnmount) {
new UnmountTask(context, mVolume).execute();
} else if (pref == mFormat) {
new FormatTask(context, mVolume).execute();
} else if (pref == mFormatInternal) {
final Intent intent = new Intent(context, StorageWizardFormatConfirm.class);
intent.putExtra(DiskInfo.EXTRA_DISK_ID, mDisk.getId());
startActivity(intent);
}
return super.onPreferenceTreeClick(preferenceScreen, pref);
}
private final StorageEventListener mStorageListener = new StorageEventListener() {
@Override
public void onVolumeStateChanged(VolumeInfo vol, int oldState, int newState) {
if (Objects.equals(mVolume.getId(), vol.getId())) {
mVolume = vol;
refresh();
}
}
@Override
public void onVolumeMetadataChanged(VolumeInfo vol) {
if (Objects.equals(mVolume.getId(), vol.getId())) {
mVolume = vol;
refresh();
}
}
};
}
|