1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.wallpaper; 18 19 import android.content.ComponentName; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.pm.PackageManager; 23 import android.content.pm.ResolveInfo; 24 import android.os.Bundle; 25 import android.support.v7.preference.Preference; 26 import android.support.v7.preference.PreferenceScreen; 27 28 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 29 import com.android.settings.R; 30 import com.android.settings.SettingsPreferenceFragment; 31 import com.android.settings.search.BaseSearchIndexProvider; 32 import com.android.settings.search.Indexable; 33 import com.android.settings.search.SearchIndexableRaw; 34 35 import java.util.ArrayList; 36 import java.util.List; 37 38 public class WallpaperTypeSettings extends SettingsPreferenceFragment implements Indexable { 39 40 @Override 41 public int getMetricsCategory() { 42 return MetricsEvent.WALLPAPER_TYPE; 43 } 44 45 @Override 46 protected int getHelpResource() { 47 return R.string.help_uri_wallpaper; 48 } 49 50 @Override 51 public void onCreate(Bundle savedInstanceState) { 52 super.onCreate(savedInstanceState); 53 54 addPreferencesFromResource(R.xml.wallpaper_settings); 55 populateWallpaperTypes(); 56 } 57 58 private void populateWallpaperTypes() { 59 // Search for activities that satisfy the ACTION_SET_WALLPAPER action 60 final Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER); 61 final PackageManager pm = getPackageManager(); 62 final List<ResolveInfo> rList = pm.queryIntentActivities(intent, 63 PackageManager.MATCH_DEFAULT_ONLY); 64 65 final PreferenceScreen parent = getPreferenceScreen(); 66 parent.setOrderingAsAdded(false); 67 // Add Preference items for each of the matching activities 68 for (ResolveInfo info : rList) { 69 Preference pref = new Preference(getPrefContext()); 70 Intent prefIntent = new Intent(intent); 71 prefIntent.setComponent(new ComponentName( 72 info.activityInfo.packageName, info.activityInfo.name)); 73 pref.setIntent(prefIntent); 74 CharSequence label = info.loadLabel(pm); 75 if (label == null) label = info.activityInfo.packageName; 76 pref.setTitle(label); 77 pref.setIcon(info.loadIcon(pm)); 78 parent.addPreference(pref); 79 } 80 } 81 82 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 83 new BaseSearchIndexProvider() { 84 @Override 85 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) { 86 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>(); 87 88 final Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER); 89 final PackageManager pm = context.getPackageManager(); 90 final List<ResolveInfo> rList = pm.queryIntentActivities(intent, 91 PackageManager.MATCH_DEFAULT_ONLY); 92 93 // Add indexable data for each of the matching activities 94 for (ResolveInfo info : rList) { 95 CharSequence label = info.loadLabel(pm); 96 if (label == null) label = info.activityInfo.packageName; 97 98 SearchIndexableRaw data = new SearchIndexableRaw(context); 99 data.title = label.toString(); 100 data.screenTitle = context.getResources().getString( 101 R.string.wallpaper_settings_fragment_title); 102 data.intentAction = Intent.ACTION_SET_WALLPAPER; 103 data.intentTargetPackage = info.activityInfo.packageName; 104 data.intentTargetClass = info.activityInfo.name; 105 result.add(data); 106 } 107 108 return result; 109 } 110 }; 111 } 112