Add click behavior to the storage item preferences.

This currently imitates the previous implementation behavior.
The new mocks have a new intermediate view for some of these
behaviors, but this view isn't ready yet.
This patch should bring parity for the click functionality
for the storage line item preferences.

Bug: 33199077
Test: Settings Robo Tests

Change-Id: I4f654d30add92e4eec721c36b2801f9af9824e78
This commit is contained in:
Daniel Nishi
2017-01-09 12:33:08 -08:00
parent c8cae26b21
commit 8cc6f9f718
4 changed files with 219 additions and 63 deletions

View File

@@ -35,6 +35,7 @@ import com.android.settings.overlay.FeatureFactory;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.search.Indexable;
import com.android.settings.widget.FooterPreference;
import com.android.settingslib.deviceinfo.StorageManagerVolumeProvider;
import com.android.settingslib.drawer.CategoryKey;
import java.util.ArrayList;
@@ -48,6 +49,7 @@ public class StorageDashboardFragment extends DashboardFragment {
private long mTotalSize;
private StorageSummaryDonutPreferenceController mSummaryController;
private StorageItemPreferenceController mPreferenceController;
private boolean isVolumeValid() {
return (mVolume != null) && (mVolume.getType() == VolumeInfo.TYPE_PRIVATE)
@@ -78,6 +80,7 @@ public class StorageDashboardFragment extends DashboardFragment {
final long usedBytes = mTotalSize - mVolume.getPath().getFreeSpace();
mSummaryController.updateBytes(usedBytes, mTotalSize);
mPreferenceController.setVolume(mVolume);
// Initialize the footer preference to go to the smart storage management.
final FooterPreference pref = mFooterPreferenceMixin.createFooterPreference();
@@ -114,13 +117,12 @@ public class StorageDashboardFragment extends DashboardFragment {
final List<PreferenceController> controllers = new ArrayList<>();
mSummaryController = new StorageSummaryDonutPreferenceController(context);
controllers.add(mSummaryController);
StorageManager sm = context.getSystemService(StorageManager.class);
mPreferenceController = new StorageItemPreferenceController(context, this, mVolume,
new StorageManagerVolumeProvider(sm));
controllers.add(mPreferenceController);
controllers.add(new ManageStoragePreferenceController(context));
controllers.add(new StorageItemPreferenceController(context, "pref_photos_videos"));
controllers.add(new StorageItemPreferenceController(context, "pref_music_audio"));
controllers.add(new StorageItemPreferenceController(context, "pref_games"));
controllers.add(new StorageItemPreferenceController(context, "pref_other_apps"));
controllers.add(new StorageItemPreferenceController(context, "pref_system"));
controllers.add(new StorageItemPreferenceController(context, "pref_files"));
return controllers;
}

View File

@@ -16,23 +16,48 @@
package com.android.settings.deviceinfo.storage;
import android.app.Fragment;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.UserHandle;
import android.os.UserManager;
import android.os.storage.VolumeInfo;
import android.provider.DocumentsContract;
import android.support.v7.preference.Preference;
import android.util.Log;
import com.android.settings.R;
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.settingslib.deviceinfo.StorageVolumeProvider;
/**
* StorageItemPreferenceController handles the updating of a single storage preference line item.
* StorageItemPreferenceController handles the storage line items which summarize the storage
* categorization breakdown.
*/
public class StorageItemPreferenceController extends PreferenceController {
private static final long NOT_YET_SET = -1;
private final String mKey;
private long mStorageSize;
private static final String TAG = "StorageItemPreference";
private final Fragment mFragment;
private final StorageVolumeProvider mSvp;
private VolumeInfo mVolume;
private final int mUserId;
public StorageItemPreferenceController(Context context, String key) {
private static final String AUTHORITY_MEDIA = "com.android.providers.media.documents";
public StorageItemPreferenceController(Context context, Fragment hostFragment,
VolumeInfo volume, StorageVolumeProvider svp) {
super(context);
mKey = key;
mStorageSize = NOT_YET_SET;
mFragment = hostFragment;
mVolume = volume;
mSvp = svp;
UserManager um = mContext.getSystemService(UserManager.class);
mUserId = um.getUserHandle();
}
@Override
@@ -42,28 +67,97 @@ public class StorageItemPreferenceController extends PreferenceController {
@Override
public boolean handlePreferenceTreeClick(Preference preference) {
return false;
if (preference == null) {
return false;
}
// TODO: Currently, this reflects the existing behavior for these toggles.
// After the intermediate views are built, swap them in.
Intent intent = null;
switch (preference.getKey()) {
case "pref_photos_videos":
intent = getPhotosIntent();
break;
case "pref_music_audio":
intent = getAudioIntent();
break;
case "pref_games":
// TODO: Once app categorization is added, make this section.
case "pref_other_apps":
// Because we are likely constructed with a null volume, this is theoretically
// possible.
if (mVolume == null) {
break;
}
intent = getAppsIntent();
break;
case "pref_files":
intent = getFilesIntent();
break;
}
if (intent != null) {
intent.putExtra(Intent.EXTRA_USER_ID, mUserId);
launchIntent(intent);
return true;
}
return super.handlePreferenceTreeClick(preference);
}
@Override
public String getPreferenceKey() {
return mKey;
}
@Override
public void updateState(Preference preference) {
if (preference == null || mStorageSize == NOT_YET_SET) {
return;
}
StorageItemPreferenceAlternate summary = (StorageItemPreferenceAlternate) preference;
summary.setStorageSize(mStorageSize);
return null;
}
/**
* Sets the amount of bytes used by this storage item.
* Sets the storage volume to use for when handling taps.
*/
public void setStorageSize(long size) {
mStorageSize = size;
public void setVolume(VolumeInfo volume) {
mVolume = volume;
}
private Intent getPhotosIntent() {
Intent intent = new Intent(DocumentsContract.ACTION_BROWSE);
intent.setData(DocumentsContract.buildRootUri(AUTHORITY_MEDIA, "images_root"));
intent.addCategory(Intent.CATEGORY_DEFAULT);
return intent;
}
private Intent getAudioIntent() {
Intent intent = new Intent(DocumentsContract.ACTION_BROWSE);
intent.setData(DocumentsContract.buildRootUri(AUTHORITY_MEDIA, "audio_root"));
intent.addCategory(Intent.CATEGORY_DEFAULT);
return intent;
}
private Intent getAppsIntent() {
Bundle args = new Bundle();
args.putString(ManageApplications.EXTRA_CLASSNAME,
Settings.StorageUseActivity.class.getName());
args.putString(ManageApplications.EXTRA_VOLUME_UUID, mVolume.getFsUuid());
args.putString(ManageApplications.EXTRA_VOLUME_NAME, mVolume.getDescription());
return Utils.onBuildStartFragmentIntent(mContext,
ManageApplications.class.getName(), args, null, R.string.apps_storage, null,
false);
}
private Intent getFilesIntent() {
return mSvp.findEmulatedForPrivate(mVolume).buildBrowseIntent();
}
private void launchIntent(Intent intent) {
try {
final int userId = intent.getIntExtra(Intent.EXTRA_USER_ID, -1);
if (userId == -1) {
mFragment.startActivity(intent);
} else {
mFragment.getActivity().startActivityAsUser(intent, new UserHandle(userId));
}
} catch (ActivityNotFoundException e) {
Log.w(TAG, "No activity found for " + intent);
}
}
}