Merge "Hook up the storage item preferences to actual data."
This commit is contained in:
@@ -81,6 +81,8 @@ public class StorageDashboardFragment extends DashboardFragment {
|
||||
final long usedBytes = mTotalSize - mVolume.getPath().getFreeSpace();
|
||||
mSummaryController.updateBytes(usedBytes, mTotalSize);
|
||||
mPreferenceController.setVolume(mVolume);
|
||||
mPreferenceController.setSystemSize(systemSize);
|
||||
mPreferenceController.startMeasurement();
|
||||
|
||||
// Initialize the footer preference to go to the smart storage management.
|
||||
final FooterPreference pref = mFooterPreferenceMixin.createFooterPreference();
|
||||
@@ -119,8 +121,8 @@ public class StorageDashboardFragment extends DashboardFragment {
|
||||
controllers.add(mSummaryController);
|
||||
|
||||
StorageManager sm = context.getSystemService(StorageManager.class);
|
||||
mPreferenceController = new StorageItemPreferenceController(context, this, mVolume,
|
||||
new StorageManagerVolumeProvider(sm));
|
||||
mPreferenceController = new StorageItemPreferenceController(context, getLifecycle(), this,
|
||||
mVolume, new StorageManagerVolumeProvider(sm));
|
||||
controllers.add(mPreferenceController);
|
||||
controllers.add(new ManageStoragePreferenceController(context));
|
||||
return controllers;
|
||||
|
@@ -21,11 +21,14 @@ import android.content.ActivityNotFoundException;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.os.UserHandle;
|
||||
import android.os.UserManager;
|
||||
import android.os.storage.VolumeInfo;
|
||||
import android.provider.DocumentsContract;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.settings.R;
|
||||
@@ -33,24 +36,56 @@ import com.android.settings.Settings;
|
||||
import com.android.settings.Utils;
|
||||
import com.android.settings.applications.ManageApplications;
|
||||
import com.android.settings.core.PreferenceController;
|
||||
import com.android.settings.core.lifecycle.Lifecycle;
|
||||
import com.android.settings.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settings.core.lifecycle.events.OnDestroy;
|
||||
import com.android.settings.deviceinfo.StorageItemPreference;
|
||||
import com.android.settingslib.deviceinfo.StorageMeasurement;
|
||||
import com.android.settingslib.deviceinfo.StorageVolumeProvider;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
|
||||
/**
|
||||
* StorageItemPreferenceController handles the storage line items which summarize the storage
|
||||
* categorization breakdown.
|
||||
*/
|
||||
public class StorageItemPreferenceController extends PreferenceController {
|
||||
public class StorageItemPreferenceController extends PreferenceController
|
||||
implements StorageMeasurement.MeasurementReceiver, LifecycleObserver, OnDestroy {
|
||||
private static final String TAG = "StorageItemPreference";
|
||||
|
||||
@VisibleForTesting
|
||||
static final String PHOTO_KEY = "pref_photos_videos";
|
||||
@VisibleForTesting
|
||||
static final String AUDIO_KEY = "pref_music_audio";
|
||||
@VisibleForTesting
|
||||
static final String GAME_KEY = "pref_games";
|
||||
@VisibleForTesting
|
||||
static final String OTHER_APPS_KEY = "pref_other_apps";
|
||||
@VisibleForTesting
|
||||
static final String SYSTEM_KEY = "pref_system";
|
||||
@VisibleForTesting
|
||||
static final String FILES_KEY = "pref_files";
|
||||
|
||||
private final Fragment mFragment;
|
||||
private final StorageVolumeProvider mSvp;
|
||||
private VolumeInfo mVolume;
|
||||
private final int mUserId;
|
||||
private StorageMeasurement mMeasure;
|
||||
private long mSystemSize;
|
||||
private long mUsedSize;
|
||||
|
||||
private StorageItemPreferenceAlternate mPhotoPreference;
|
||||
private StorageItemPreferenceAlternate mAudioPreference;
|
||||
private StorageItemPreferenceAlternate mGamePreference;
|
||||
private StorageItemPreferenceAlternate mAppPreference;
|
||||
private StorageItemPreferenceAlternate mFilePreference;
|
||||
private StorageItemPreferenceAlternate mSystemPreference;
|
||||
|
||||
private static final String AUTHORITY_MEDIA = "com.android.providers.media.documents";
|
||||
|
||||
public StorageItemPreferenceController(Context context, Fragment hostFragment,
|
||||
VolumeInfo volume, StorageVolumeProvider svp) {
|
||||
public StorageItemPreferenceController(Context context, Lifecycle lifecycle,
|
||||
Fragment hostFragment, VolumeInfo volume, StorageVolumeProvider svp) {
|
||||
super(context);
|
||||
mFragment = hostFragment;
|
||||
mVolume = volume;
|
||||
@@ -58,6 +93,10 @@ public class StorageItemPreferenceController extends PreferenceController {
|
||||
|
||||
UserManager um = mContext.getSystemService(UserManager.class);
|
||||
mUserId = um.getUserHandle();
|
||||
|
||||
if (lifecycle != null) {
|
||||
lifecycle.addObserver(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -75,15 +114,15 @@ public class StorageItemPreferenceController extends PreferenceController {
|
||||
// After the intermediate views are built, swap them in.
|
||||
Intent intent = null;
|
||||
switch (preference.getKey()) {
|
||||
case "pref_photos_videos":
|
||||
case PHOTO_KEY:
|
||||
intent = getPhotosIntent();
|
||||
break;
|
||||
case "pref_music_audio":
|
||||
case AUDIO_KEY:
|
||||
intent = getAudioIntent();
|
||||
break;
|
||||
case "pref_games":
|
||||
case GAME_KEY:
|
||||
// TODO: Once app categorization is added, make this section.
|
||||
case "pref_other_apps":
|
||||
case OTHER_APPS_KEY:
|
||||
// Because we are likely constructed with a null volume, this is theoretically
|
||||
// possible.
|
||||
if (mVolume == null) {
|
||||
@@ -91,7 +130,7 @@ public class StorageItemPreferenceController extends PreferenceController {
|
||||
}
|
||||
intent = getAppsIntent();
|
||||
break;
|
||||
case "pref_files":
|
||||
case FILES_KEY:
|
||||
intent = getFilesIntent();
|
||||
break;
|
||||
}
|
||||
@@ -118,6 +157,81 @@ public class StorageItemPreferenceController extends PreferenceController {
|
||||
mVolume = volume;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDetailsChanged(StorageMeasurement.MeasurementDetails details) {
|
||||
final long imagesSize = totalValues(details, mUserId,
|
||||
Environment.DIRECTORY_DCIM,
|
||||
Environment.DIRECTORY_PICTURES,
|
||||
Environment.DIRECTORY_MOVIES);
|
||||
if (mPhotoPreference != null) {
|
||||
mPhotoPreference.setStorageSize(imagesSize);
|
||||
}
|
||||
|
||||
final long audioSize = totalValues(details, mUserId,
|
||||
Environment.DIRECTORY_MUSIC,
|
||||
Environment.DIRECTORY_ALARMS,
|
||||
Environment.DIRECTORY_NOTIFICATIONS,
|
||||
Environment.DIRECTORY_RINGTONES,
|
||||
Environment.DIRECTORY_PODCASTS);
|
||||
if (mAudioPreference != null) {
|
||||
mAudioPreference.setStorageSize(audioSize);
|
||||
}
|
||||
|
||||
if (mGamePreference != null) {
|
||||
mGamePreference.setStorageSize(0);
|
||||
}
|
||||
|
||||
final long appSize = details.appsSize.get(mUserId);
|
||||
if (mAppPreference != null) {
|
||||
mAppPreference.setStorageSize(appSize);
|
||||
}
|
||||
|
||||
if (mSystemPreference != null) {
|
||||
mSystemPreference.setStorageSize(mSystemSize);
|
||||
}
|
||||
|
||||
final long downloadsSize = totalValues(details, mUserId, Environment.DIRECTORY_DOWNLOADS);
|
||||
final long miscSize = details.miscSize.get(mUserId);
|
||||
if (mFilePreference != null) {
|
||||
mFilePreference.setStorageSize(downloadsSize + miscSize);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
if (mMeasure != null) {
|
||||
mMeasure.onDestroy();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
mPhotoPreference = (StorageItemPreferenceAlternate) screen.findPreference(PHOTO_KEY);
|
||||
mAudioPreference = (StorageItemPreferenceAlternate) screen.findPreference(AUDIO_KEY);
|
||||
mGamePreference = (StorageItemPreferenceAlternate) screen.findPreference(GAME_KEY);
|
||||
mAppPreference = (StorageItemPreferenceAlternate) screen.findPreference(OTHER_APPS_KEY);
|
||||
mSystemPreference = (StorageItemPreferenceAlternate) screen.findPreference(SYSTEM_KEY);
|
||||
mFilePreference = (StorageItemPreferenceAlternate) screen.findPreference(FILES_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Begins an asynchronous storage measurement task for the preferences.
|
||||
*/
|
||||
public void startMeasurement() {
|
||||
//TODO: When the GID-based measurement system is completed, swap in the GID impl.
|
||||
mMeasure = new StorageMeasurement(mContext, mVolume, mSvp.findEmulatedForPrivate(mVolume));
|
||||
mMeasure.setReceiver(this);
|
||||
mMeasure.forceMeasure();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the system size for the system size preference.
|
||||
* @param systemSize the size of the system in bytes
|
||||
*/
|
||||
public void setSystemSize(long systemSize) {
|
||||
mSystemSize = systemSize;
|
||||
}
|
||||
|
||||
private Intent getPhotosIntent() {
|
||||
Intent intent = new Intent(DocumentsContract.ACTION_BROWSE);
|
||||
intent.setData(DocumentsContract.buildRootUri(AUTHORITY_MEDIA, "images_root"));
|
||||
@@ -160,4 +274,20 @@ public class StorageItemPreferenceController extends PreferenceController {
|
||||
Log.w(TAG, "No activity found for " + intent);
|
||||
}
|
||||
}
|
||||
|
||||
private static long totalValues(StorageMeasurement.MeasurementDetails details, int userId,
|
||||
String... keys) {
|
||||
long total = 0;
|
||||
HashMap<String, Long> map = details.mediaSize.get(userId);
|
||||
if (map != null) {
|
||||
for (String key : keys) {
|
||||
if (map.containsKey(key)) {
|
||||
total += map.get(key);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Log.w(TAG, "MeasurementDetails mediaSize array does not have key for user " + userId);
|
||||
}
|
||||
return total;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user