Progress towards split storage categories.

Cleans up StorageVolumePreferenceCategory creation, and uses new
isPrimary() API instead of passing around hacky "i == 0" derived
value.

Bug: 7003520
Change-Id: I9eb7164035b5368ee795e35b22e750aebbf3b159
This commit is contained in:
Jeff Sharkey
2012-09-06 22:46:21 -07:00
parent 88220b7204
commit 96c2f8c508
4 changed files with 69 additions and 75 deletions

View File

@@ -23,7 +23,6 @@ import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.res.Resources;
import android.hardware.usb.UsbManager; import android.hardware.usb.UsbManager;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
@@ -46,30 +45,32 @@ import android.widget.Toast;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment; import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.Utils; import com.android.settings.Utils;
import com.google.common.collect.Lists;
import java.util.ArrayList;
/**
* Panel showing storage usage on disk for known {@link StorageVolume} returned
* by {@link StorageManager}. Calculates and displays usage of data types.
*/
public class Memory extends SettingsPreferenceFragment { public class Memory extends SettingsPreferenceFragment {
private static final String TAG = "MemorySettings"; private static final String TAG = "MemorySettings";
private static final int DLG_CONFIRM_UNMOUNT = 1; private static final int DLG_CONFIRM_UNMOUNT = 1;
private static final int DLG_ERROR_UNMOUNT = 2; private static final int DLG_ERROR_UNMOUNT = 2;
private Resources mResources;
// The mountToggle Preference that has last been clicked. // The mountToggle Preference that has last been clicked.
// Assumes no two successive unmount event on 2 different volumes are performed before the first // Assumes no two successive unmount event on 2 different volumes are performed before the first
// one's preference is disabled // one's preference is disabled
private static Preference mLastClickedMountToggle; private static Preference sLastClickedMountToggle;
private static String mClickedMountPoint; private static String sClickedMountPoint;
// Access using getMountService() // Access using getMountService()
private IMountService mMountService = null; private IMountService mMountService = null;
private StorageManager mStorageManager = null; private StorageManager mStorageManager = null;
private UsbManager mUsbManager = null; private UsbManager mUsbManager = null;
private StorageVolumePreferenceCategory mInternalStorageVolumePreferenceCategory; private ArrayList<StorageVolumePreferenceCategory> mCategories = Lists.newArrayList();
private StorageVolumePreferenceCategory[] mStorageVolumePreferenceCategories;
@Override @Override
public void onCreate(Bundle icicle) { public void onCreate(Bundle icicle) {
@@ -84,32 +85,29 @@ public class Memory extends SettingsPreferenceFragment {
addPreferencesFromResource(R.xml.device_info_memory); addPreferencesFromResource(R.xml.device_info_memory);
mResources = getResources();
if (!Environment.isExternalStorageEmulated()) { if (!Environment.isExternalStorageEmulated()) {
// External storage is separate from internal storage; need to // External storage is separate from internal storage; need to
// show internal storage as a separate item. // show internal storage as a separate item.
mInternalStorageVolumePreferenceCategory = new StorageVolumePreferenceCategory( addCategoryForVolume(null);
getActivity(), mResources, null, mStorageManager, false);
getPreferenceScreen().addPreference(mInternalStorageVolumePreferenceCategory);
mInternalStorageVolumePreferenceCategory.init();
} }
StorageVolume[] storageVolumes = mStorageManager.getVolumeList(); final StorageVolume[] storageVolumes = mStorageManager.getVolumeList();
int length = storageVolumes.length; for (StorageVolume volume : storageVolumes) {
mStorageVolumePreferenceCategories = new StorageVolumePreferenceCategory[length]; addCategoryForVolume(volume);
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();
} }
setHasOptionsMenu(true); setHasOptionsMenu(true);
} }
private void addCategoryForVolume(StorageVolume volume) {
final StorageVolumePreferenceCategory category = new StorageVolumePreferenceCategory(
getActivity(), volume, mStorageManager);
// TODO: if primary emulated storage, then insert split items
mCategories.add(category);
getPreferenceScreen().addPreference(category);
category.init();
}
private boolean isMassStorageEnabled() { private boolean isMassStorageEnabled() {
// mass storage is enabled if primary volume supports it // mass storage is enabled if primary volume supports it
final StorageVolume[] storageVolumes = mStorageManager.getVolumeList(); final StorageVolume[] storageVolumes = mStorageManager.getVolumeList();
@@ -128,11 +126,8 @@ public class Memory extends SettingsPreferenceFragment {
intentFilter.addAction(UsbManager.ACTION_USB_STATE); intentFilter.addAction(UsbManager.ACTION_USB_STATE);
getActivity().registerReceiver(mMediaScannerReceiver, intentFilter); getActivity().registerReceiver(mMediaScannerReceiver, intentFilter);
if (mInternalStorageVolumePreferenceCategory != null) { for (StorageVolumePreferenceCategory category : mCategories) {
mInternalStorageVolumePreferenceCategory.onResume(); category.onResume();
}
for (int i = 0; i < mStorageVolumePreferenceCategories.length; i++) {
mStorageVolumePreferenceCategories[i].onResume();
} }
} }
@@ -141,10 +136,10 @@ public class Memory extends SettingsPreferenceFragment {
public void onStorageStateChanged(String path, String oldState, String newState) { public void onStorageStateChanged(String path, String oldState, String newState) {
Log.i(TAG, "Received storage state changed notification that " + path + Log.i(TAG, "Received storage state changed notification that " + path +
" changed state from " + oldState + " to " + newState); " changed state from " + oldState + " to " + newState);
for (int i = 0; i < mStorageVolumePreferenceCategories.length; i++) { for (StorageVolumePreferenceCategory category : mCategories) {
StorageVolumePreferenceCategory svpc = mStorageVolumePreferenceCategories[i]; final StorageVolume volume = category.getStorageVolume();
if (path.equals(svpc.getStorageVolume().getPath())) { if (volume != null && path.equals(volume.getPath())) {
svpc.onStorageStateChanged(); category.onStorageStateChanged();
break; break;
} }
} }
@@ -155,11 +150,8 @@ public class Memory extends SettingsPreferenceFragment {
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
getActivity().unregisterReceiver(mMediaScannerReceiver); getActivity().unregisterReceiver(mMediaScannerReceiver);
if (mInternalStorageVolumePreferenceCategory != null) { for (StorageVolumePreferenceCategory category : mCategories) {
mInternalStorageVolumePreferenceCategory.onPause(); category.onPause();
}
for (int i = 0; i < mStorageVolumePreferenceCategories.length; i++) {
mStorageVolumePreferenceCategories[i].onPause();
} }
} }
@@ -214,9 +206,8 @@ public class Memory extends SettingsPreferenceFragment {
@Override @Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
for (int i = 0; i < mStorageVolumePreferenceCategories.length; i++) { for (StorageVolumePreferenceCategory category : mCategories) {
StorageVolumePreferenceCategory svpc = mStorageVolumePreferenceCategories[i]; Intent intent = category.intentForClick(preference);
Intent intent = svpc.intentForClick(preference);
if (intent != null) { if (intent != null) {
// Don't go across app boundary if monkey is running // Don't go across app boundary if monkey is running
if (!Utils.isMonkeyRunning()) { if (!Utils.isMonkeyRunning()) {
@@ -225,11 +216,11 @@ public class Memory extends SettingsPreferenceFragment {
return true; return true;
} }
if (svpc.mountToggleClicked(preference)) { final StorageVolume volume = category.getStorageVolume();
mLastClickedMountToggle = preference; if (volume != null && category.mountToggleClicked(preference)) {
final StorageVolume storageVolume = svpc.getStorageVolume(); sLastClickedMountToggle = preference;
mClickedMountPoint = storageVolume.getPath(); sClickedMountPoint = volume.getPath();
String state = mStorageManager.getVolumeState(storageVolume.getPath()); String state = mStorageManager.getVolumeState(volume.getPath());
if (Environment.MEDIA_MOUNTED.equals(state) || if (Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
unmount(); unmount();
@@ -250,13 +241,12 @@ public class Memory extends SettingsPreferenceFragment {
if (action.equals(UsbManager.ACTION_USB_STATE)) { if (action.equals(UsbManager.ACTION_USB_STATE)) {
boolean isUsbConnected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false); boolean isUsbConnected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
String usbFunction = mUsbManager.getDefaultFunction(); String usbFunction = mUsbManager.getDefaultFunction();
for (int i = 0; i < mStorageVolumePreferenceCategories.length; i++) { for (StorageVolumePreferenceCategory category : mCategories) {
mStorageVolumePreferenceCategories[i].onUsbStateChanged(isUsbConnected, usbFunction); category.onUsbStateChanged(isUsbConnected, usbFunction);
} }
} else if (action.equals(Intent.ACTION_MEDIA_SCANNER_FINISHED)) { } else if (action.equals(Intent.ACTION_MEDIA_SCANNER_FINISHED)) {
// mInternalStorageVolumePreferenceCategory is not affected by the media scanner for (StorageVolumePreferenceCategory category : mCategories) {
for (int i = 0; i < mStorageVolumePreferenceCategories.length; i++) { category.onMediaScannerFinished();
mStorageVolumePreferenceCategories[i].onMediaScannerFinished();
} }
} }
} }
@@ -290,10 +280,10 @@ public class Memory extends SettingsPreferenceFragment {
Toast.makeText(getActivity(), R.string.unmount_inform_text, Toast.LENGTH_SHORT).show(); Toast.makeText(getActivity(), R.string.unmount_inform_text, Toast.LENGTH_SHORT).show();
IMountService mountService = getMountService(); IMountService mountService = getMountService();
try { try {
mLastClickedMountToggle.setEnabled(false); sLastClickedMountToggle.setEnabled(false);
mLastClickedMountToggle.setTitle(mResources.getString(R.string.sd_ejecting_title)); sLastClickedMountToggle.setTitle(getString(R.string.sd_ejecting_title));
mLastClickedMountToggle.setSummary(mResources.getString(R.string.sd_ejecting_summary)); sLastClickedMountToggle.setSummary(getString(R.string.sd_ejecting_summary));
mountService.unmountVolume(mClickedMountPoint, true, false); mountService.unmountVolume(sClickedMountPoint, true, false);
} catch (RemoteException e) { } catch (RemoteException e) {
// Informative dialog to user that unmount failed. // Informative dialog to user that unmount failed.
showDialogInner(DLG_ERROR_UNMOUNT); showDialogInner(DLG_ERROR_UNMOUNT);
@@ -307,7 +297,7 @@ public class Memory extends SettingsPreferenceFragment {
private boolean hasAppsAccessingStorage() throws RemoteException { private boolean hasAppsAccessingStorage() throws RemoteException {
IMountService mountService = getMountService(); IMountService mountService = getMountService();
int stUsers[] = mountService.getStorageUsers(mClickedMountPoint); int stUsers[] = mountService.getStorageUsers(sClickedMountPoint);
if (stUsers != null && stUsers.length > 0) { if (stUsers != null && stUsers.length > 0) {
return true; return true;
} }
@@ -345,7 +335,7 @@ public class Memory extends SettingsPreferenceFragment {
IMountService mountService = getMountService(); IMountService mountService = getMountService();
try { try {
if (mountService != null) { if (mountService != null) {
mountService.mountVolume(mClickedMountPoint); mountService.mountVolume(sClickedMountPoint);
} else { } else {
Log.e(TAG, "Mount service is null, can't mount"); Log.e(TAG, "Mount service is null, can't mount");
} }

View File

@@ -195,7 +195,7 @@ public class MiscFilesHandler extends ListActivity {
final StorageVolume storageVolume = activity.getIntent().getParcelableExtra( final StorageVolume storageVolume = activity.getIntent().getParcelableExtra(
StorageVolume.EXTRA_STORAGE_VOLUME); StorageVolume.EXTRA_STORAGE_VOLUME);
StorageMeasurement mMeasurement = StorageMeasurement.getInstance( StorageMeasurement mMeasurement = StorageMeasurement.getInstance(
activity, storageVolume, new UserHandle(UserHandle.USER_CURRENT), false); activity, storageVolume, UserHandle.CURRENT);
if (mMeasurement == null) return; if (mMeasurement == null) return;
mData = (ArrayList<StorageMeasurement.FileInfo>) mMeasurement.mFileInfoForMisc; mData = (ArrayList<StorageMeasurement.FileInfo>) mMeasurement.mFileInfoForMisc;
if (mData != null) { if (mData != null) {

View File

@@ -115,12 +115,11 @@ public class StorageMeasurement {
public void updateExact(StorageMeasurement meas, Bundle bundle); public void updateExact(StorageMeasurement meas, Bundle bundle);
} }
private StorageMeasurement( private StorageMeasurement(Context context, StorageVolume volume, UserHandle user) {
Context context, StorageVolume storageVolume, UserHandle user, boolean isPrimary) { mStorageVolume = volume;
mStorageVolume = storageVolume;
mUser = Preconditions.checkNotNull(user); mUser = Preconditions.checkNotNull(user);
mIsInternal = storageVolume == null; mIsInternal = volume == null;
mIsPrimary = !mIsInternal && isPrimary; mIsPrimary = volume != null ? volume.isPrimary() : false;
// Start the thread that will measure the disk usage. // Start the thread that will measure the disk usage.
final HandlerThread handlerThread = new HandlerThread("MemoryMeasurement"); final HandlerThread handlerThread = new HandlerThread("MemoryMeasurement");
@@ -139,14 +138,14 @@ public class StorageMeasurement {
* @param isPrimary true when this storage volume is the primary volume * @param isPrimary true when this storage volume is the primary volume
*/ */
public static StorageMeasurement getInstance( public static StorageMeasurement getInstance(
Context context, StorageVolume storageVolume, UserHandle user, boolean isPrimary) { Context context, StorageVolume storageVolume, UserHandle user) {
final Pair<StorageVolume, UserHandle> key = new Pair<StorageVolume, UserHandle>( final Pair<StorageVolume, UserHandle> key = new Pair<StorageVolume, UserHandle>(
storageVolume, user); storageVolume, user);
synchronized (sInstances) { synchronized (sInstances) {
StorageMeasurement value = sInstances.get(key); StorageMeasurement value = sInstances.get(key);
if (value == null) { if (value == null) {
value = new StorageMeasurement( value = new StorageMeasurement(
context.getApplicationContext(), storageVolume, user, isPrimary); context.getApplicationContext(), storageVolume, user);
sInstances.put(key, value); sInstances.put(key, value);
} }
return value; return value;

View File

@@ -166,16 +166,20 @@ public class StorageVolumePreferenceCategory extends PreferenceCategory
} }
}; };
public StorageVolumePreferenceCategory(Context context, Resources resources, public StorageVolumePreferenceCategory(
StorageVolume storageVolume, StorageManager storageManager, boolean isPrimary) { Context context, StorageVolume volume, StorageManager storageManager) {
super(context); super(context);
mResources = resources; mResources = context.getResources();
mStorageVolume = storageVolume;
mStorageVolume = volume;
mStorageManager = storageManager; mStorageManager = storageManager;
setTitle(storageVolume != null ? storageVolume.getDescription(context)
: resources.getText(R.string.internal_storage)); final boolean isPrimary = volume != null ? volume.isPrimary() : false;
mLocalMeasure = StorageMeasurement.getInstance(
context, storageVolume, new UserHandle(UserHandle.USER_CURRENT), isPrimary); setTitle(volume != null ? volume.getDescription(context)
: context.getText(R.string.internal_storage));
mLocalMeasure = StorageMeasurement.getInstance(context, volume, UserHandle.CURRENT);
mAllMeasures.add(mLocalMeasure); mAllMeasures.add(mLocalMeasure);
// Cannot format emulated storage // Cannot format emulated storage
@@ -233,7 +237,7 @@ public class StorageVolumePreferenceCategory extends PreferenceCategory
final String key = buildUserKey(user); final String key = buildUserKey(user);
final StorageMeasurement measure = StorageMeasurement.getInstance( final StorageMeasurement measure = StorageMeasurement.getInstance(
context, mStorageVolume, user, true); context, mStorageVolume, user);
measure.setIncludeAppCodeSize(false); measure.setIncludeAppCodeSize(false);
mAllMeasures.add(measure); mAllMeasures.add(measure);
@@ -448,6 +452,7 @@ public class StorageVolumePreferenceCategory extends PreferenceCategory
mUsbFunction = usbFunction; mUsbFunction = usbFunction;
measure(); measure();
} }
public void onMediaScannerFinished() { public void onMediaScannerFinished() {
measure(); measure();
} }