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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
/*
* Copyright (C) 2008 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.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Environment;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.storage.IMountService;
import android.os.storage.StorageEventListener;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;
public class Memory extends SettingsPreferenceFragment {
private static final String TAG = "MemorySettings";
private static final int DLG_CONFIRM_UNMOUNT = 1;
private static final int DLG_ERROR_UNMOUNT = 2;
private static final int MENU_ID_USB = Menu.FIRST;
private Resources mResources;
// The mountToggle Preference that has last been clicked.
// Assumes no two successive unmount event on 2 different volumes are performed before the first
// one's preference is disabled
private Preference mLastClickedMountToggle;
private String mClickedMountPoint;
// Access using getMountService()
private IMountService mMountService = null;
private StorageManager mStorageManager = null;
private StorageVolumePreferenceCategory mInternalStorageVolumePreferenceCategory;
private StorageVolumePreferenceCategory[] mStorageVolumePreferenceCategories;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
if (mStorageManager == null) {
mStorageManager = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
mStorageManager.registerListener(mStorageListener);
}
addPreferencesFromResource(R.xml.device_info_memory);
mResources = getResources();
if (!Environment.isExternalStorageEmulated()) {
// External storage is separate from internal storage; need to
// show internal storage as a separate item.
mInternalStorageVolumePreferenceCategory = new StorageVolumePreferenceCategory(
getActivity(), mResources, null, mStorageManager, false);
getPreferenceScreen().addPreference(mInternalStorageVolumePreferenceCategory);
mInternalStorageVolumePreferenceCategory.init();
}
StorageVolume[] storageVolumes = mStorageManager.getVolumeList();
// mass storage is enabled if primary volume supports it
boolean massStorageEnabled = (storageVolumes.length > 0
&& storageVolumes[0].allowMassStorage());
int length = storageVolumes.length;
mStorageVolumePreferenceCategories = new StorageVolumePreferenceCategory[length];
for (int i = 0; i < length; i++) {
StorageVolume storageVolume = storageVolumes[i];
boolean isPrimary = i == 0;
mStorageVolumePreferenceCategories[i] = new StorageVolumePreferenceCategory(
getActivity(), mResources, storageVolume, mStorageManager, isPrimary);
getPreferenceScreen().addPreference(mStorageVolumePreferenceCategories[i]);
mStorageVolumePreferenceCategories[i].init();
}
// only show options menu if we are not using the legacy USB mass storage support
setHasOptionsMenu(!massStorageEnabled);
}
@Override
public void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_SCANNER_STARTED);
intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
intentFilter.addDataScheme("file");
getActivity().registerReceiver(mMediaScannerReceiver, intentFilter);
if (mInternalStorageVolumePreferenceCategory != null) {
mInternalStorageVolumePreferenceCategory.onResume();
}
for (int i = 0; i < mStorageVolumePreferenceCategories.length; i++) {
mStorageVolumePreferenceCategories[i].onResume();
}
}
StorageEventListener mStorageListener = new StorageEventListener() {
@Override
public void onStorageStateChanged(String path, String oldState, String newState) {
Log.i(TAG, "Received storage state changed notification that " + path +
" changed state from " + oldState + " to " + newState);
for (int i = 0; i < mStorageVolumePreferenceCategories.length; i++) {
StorageVolumePreferenceCategory svpc = mStorageVolumePreferenceCategories[i];
if (path.equals(svpc.getStorageVolume().getPath())) {
svpc.onStorageStateChanged();
break;
}
}
}
};
@Override
public void onPause() {
super.onPause();
getActivity().unregisterReceiver(mMediaScannerReceiver);
if (mInternalStorageVolumePreferenceCategory != null) {
mInternalStorageVolumePreferenceCategory.onPause();
}
for (int i = 0; i < mStorageVolumePreferenceCategories.length; i++) {
mStorageVolumePreferenceCategories[i].onPause();
}
}
@Override
public void onDestroy() {
if (mStorageManager != null && mStorageListener != null) {
mStorageManager.unregisterListener(mStorageListener);
}
super.onDestroy();
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.add(Menu.NONE, MENU_ID_USB, 0, R.string.storage_menu_usb)
//.setIcon(com.android.internal.R.drawable.stat_sys_data_usb)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_ID_USB:
if (getActivity() instanceof PreferenceActivity) {
((PreferenceActivity) getActivity()).startPreferencePanel(
UsbSettings.class.getCanonicalName(),
null,
R.string.storage_title_usb, null,
this, 0);
} else {
startFragment(this, UsbSettings.class.getCanonicalName(), -1, null);
}
return true;
}
return super.onOptionsItemSelected(item);
}
private synchronized IMountService getMountService() {
if (mMountService == null) {
IBinder service = ServiceManager.getService("mount");
if (service != null) {
mMountService = IMountService.Stub.asInterface(service);
} else {
Log.e(TAG, "Can't get mount service");
}
}
return mMountService;
}
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
for (int i = 0; i < mStorageVolumePreferenceCategories.length; i++) {
StorageVolumePreferenceCategory svpc = mStorageVolumePreferenceCategories[i];
Intent intent = svpc.intentForClick(preference);
if (intent != null) {
startActivity(intent);
return true;
}
if (svpc.mountToggleClicked(preference)) {
mLastClickedMountToggle = preference;
final StorageVolume storageVolume = svpc.getStorageVolume();
mClickedMountPoint = storageVolume.getPath();
String state = mStorageManager.getVolumeState(storageVolume.getPath());
if (Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
unmount();
} else {
mount();
}
return true;
}
}
return false;
}
private final BroadcastReceiver mMediaScannerReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// mInternalStorageVolumePreferenceCategory is not affected by the media scanner
for (int i = 0; i < mStorageVolumePreferenceCategories.length; i++) {
mStorageVolumePreferenceCategories[i].onMediaScannerFinished();
}
}
};
@Override
public Dialog onCreateDialog(int id) {
switch (id) {
case DLG_CONFIRM_UNMOUNT:
return new AlertDialog.Builder(getActivity())
.setTitle(R.string.dlg_confirm_unmount_title)
.setPositiveButton(R.string.dlg_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
doUnmount();
}})
.setNegativeButton(R.string.cancel, null)
.setMessage(R.string.dlg_confirm_unmount_text)
.create();
case DLG_ERROR_UNMOUNT:
return new AlertDialog.Builder(getActivity())
.setTitle(R.string.dlg_error_unmount_title)
.setNeutralButton(R.string.dlg_ok, null)
.setMessage(R.string.dlg_error_unmount_text)
.create();
}
return null;
}
private void doUnmount() {
// Present a toast here
Toast.makeText(getActivity(), R.string.unmount_inform_text, Toast.LENGTH_SHORT).show();
IMountService mountService = getMountService();
try {
mLastClickedMountToggle.setEnabled(false);
mLastClickedMountToggle.setTitle(mResources.getString(R.string.sd_ejecting_title));
mLastClickedMountToggle.setSummary(mResources.getString(R.string.sd_ejecting_summary));
mountService.unmountVolume(mClickedMountPoint, true);
} catch (RemoteException e) {
// Informative dialog to user that unmount failed.
showDialogInner(DLG_ERROR_UNMOUNT);
}
}
private void showDialogInner(int id) {
removeDialog(id);
showDialog(id);
}
private boolean hasAppsAccessingStorage() throws RemoteException {
IMountService mountService = getMountService();
int stUsers[] = mountService.getStorageUsers(mClickedMountPoint);
if (stUsers != null && stUsers.length > 0) {
return true;
}
// TODO FIXME Parameterize with mountPoint and uncomment.
// On HC-MR2, no apps can be installed on sd and the emulated internal storage is not
// removable: application cannot interfere with unmount
/*
ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
List<ApplicationInfo> list = am.getRunningExternalApplications();
if (list != null && list.size() > 0) {
return true;
}
*/
// Better safe than sorry. Assume the storage is used to ask for confirmation.
return true;
}
private void unmount() {
// Check if external media is in use.
try {
if (hasAppsAccessingStorage()) {
// Present dialog to user
showDialogInner(DLG_CONFIRM_UNMOUNT);
} else {
doUnmount();
}
} catch (RemoteException e) {
// Very unlikely. But present an error dialog anyway
Log.e(TAG, "Is MountService running?");
showDialogInner(DLG_ERROR_UNMOUNT);
}
}
private void mount() {
IMountService mountService = getMountService();
try {
if (mountService != null) {
mountService.mountVolume(mClickedMountPoint);
} else {
Log.e(TAG, "Mount service is null, can't mount");
}
} catch (RemoteException ex) {
// Not much can be done
}
}
}
|