Merge "WallpaperTypeSettings use DashboardFragment"

This commit is contained in:
TreeHugger Robot
2018-04-17 20:55:26 +00:00
committed by Android (Google) Code Review
5 changed files with 237 additions and 113 deletions

View File

@@ -0,0 +1,122 @@
/*
* Copyright (C) 2018 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.wallpaper;
import android.app.Fragment;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.text.TextUtils;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart;
import java.util.List;
import java.util.stream.Collectors;
public class WallpaperTypePreferenceController extends BasePreferenceController
implements LifecycleObserver, OnStart {
private Fragment mParentFragment;
private PreferenceScreen mScreen;
public WallpaperTypePreferenceController(Context context, String key) {
super(context, key);
}
public void setParentFragment(Fragment parentFragment) {
mParentFragment = parentFragment;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mScreen = screen;
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public boolean handlePreferenceTreeClick(Preference preference) {
if (preference.getIntent() == null) {
return super.handlePreferenceTreeClick(preference);
}
mParentFragment.startActivity(preference.getIntent());
mParentFragment.getActivity().finish();
return true;
}
@Override
public void onStart() {
populateWallpaperTypes();
}
private void populateWallpaperTypes() {
// Search for activities that satisfy the ACTION_SET_WALLPAPER action
final Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER);
final PackageManager pm = mContext.getPackageManager();
final List<ResolveInfo> rList = pm.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
removeUselessExistingPreference(rList);
mScreen.setOrderingAsAdded(false);
// Add Preference items for each of the matching activities
for (ResolveInfo info : rList) {
final String packageName = info.activityInfo.packageName;
Preference pref = mScreen.findPreference(packageName);
if (pref == null) {
pref = new Preference(mScreen.getContext());
}
final Intent prefIntent = new Intent(intent).addFlags(
Intent.FLAG_ACTIVITY_FORWARD_RESULT);
prefIntent.setComponent(new ComponentName(packageName, info.activityInfo.name));
pref.setIntent(prefIntent);
pref.setKey(packageName);
CharSequence label = info.loadLabel(pm);
if (label == null) {
label = packageName;
}
pref.setTitle(label);
pref.setIcon(info.loadIcon(pm));
mScreen.addPreference(pref);
}
}
private void removeUselessExistingPreference(List<ResolveInfo> rList) {
final int count = mScreen.getPreferenceCount();
if (count <= 0) {
return;
}
for (int i = count - 1; i >= 0; i--) {
final Preference pref = mScreen.getPreference(i);
final List<ResolveInfo> result = rList.stream().filter(
rInfo -> TextUtils.equals(pref.getKey(),
rInfo.activityInfo.packageName)).collect(Collectors.toList());
if (result.isEmpty()) {
mScreen.removePreference(pref);
}
}
}
}

View File

@@ -16,20 +16,15 @@
package com.android.settings.wallpaper;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.search.Indexable;
import com.android.settings.search.SearchIndexableRaw;
import com.android.settingslib.search.SearchIndexable;
@@ -37,7 +32,8 @@ import java.util.ArrayList;
import java.util.List;
@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
public class WallpaperTypeSettings extends SettingsPreferenceFragment implements Indexable {
public class WallpaperTypeSettings extends DashboardFragment {
private static final String TAG = "WallpaperTypeSettings";
@Override
public int getMetricsCategory() {
@@ -50,52 +46,26 @@ public class WallpaperTypeSettings extends SettingsPreferenceFragment implements
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.wallpaper_settings);
populateWallpaperTypes();
}
private void populateWallpaperTypes() {
// Search for activities that satisfy the ACTION_SET_WALLPAPER action
final Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER);
final PackageManager pm = getPackageManager();
final List<ResolveInfo> rList = pm.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
final PreferenceScreen parent = getPreferenceScreen();
parent.setOrderingAsAdded(false);
// Add Preference items for each of the matching activities
for (ResolveInfo info : rList) {
Preference pref = new Preference(getPrefContext());
Intent prefIntent = new Intent(intent).addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
prefIntent.setComponent(new ComponentName(
info.activityInfo.packageName, info.activityInfo.name));
pref.setIntent(prefIntent);
CharSequence label = info.loadLabel(pm);
if (label == null) label = info.activityInfo.packageName;
pref.setTitle(label);
pref.setIcon(info.loadIcon(pm));
parent.addPreference(pref);
}
protected String getLogTag() {
return TAG;
}
@Override
public boolean onPreferenceTreeClick(Preference preference) {
if (preference.getIntent() == null) {
return super.onPreferenceTreeClick(preference);
}
startActivity(preference.getIntent());
finish();
return true;
protected int getPreferenceScreenResId() {
return R.xml.wallpaper_settings;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
use(WallpaperTypePreferenceController.class).setParentFragment(this);
}
public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider() {
@Override
public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
final List<SearchIndexableRaw> result = new ArrayList<>();
final Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER);
final PackageManager pm = context.getPackageManager();
@@ -110,9 +80,10 @@ public class WallpaperTypeSettings extends SettingsPreferenceFragment implements
continue;
}
CharSequence label = info.loadLabel(pm);
if (label == null) label = info.activityInfo.packageName;
SearchIndexableRaw data = new SearchIndexableRaw(context);
if (label == null) {
label = info.activityInfo.packageName;
}
final SearchIndexableRaw data = new SearchIndexableRaw(context);
data.title = label.toString();
data.key = "wallpaper_type_settings";
data.screenTitle = context.getResources().getString(