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 public 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).addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT); 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 @Override 83 public boolean onPreferenceTreeClick(Preference preference) { 84 if (preference.getIntent() == null) { 85 return super.onPreferenceTreeClick(preference); 86 } 87 startActivity(preference.getIntent()); 88 finish(); 89 return true; 90 } 91 92 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 93 new BaseSearchIndexProvider() { 94 @Override 95 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) { 96 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>(); 97 98 final Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER); 99 final PackageManager pm = context.getPackageManager(); 100 final List<ResolveInfo> rList = pm.queryIntentActivities(intent, 101 PackageManager.MATCH_DEFAULT_ONLY); 102 103 // Add indexable data for package that is in config_wallpaper_picker_package 104 final String wallpaperPickerPackage = 105 context.getString(R.string.config_wallpaper_picker_package); 106 for (ResolveInfo info : rList) { 107 if (!wallpaperPickerPackage.equals(info.activityInfo.packageName)) { 108 continue; 109 } 110 CharSequence label = info.loadLabel(pm); 111 if (label == null) label = info.activityInfo.packageName; 112 113 SearchIndexableRaw data = new SearchIndexableRaw(context); 114 data.title = label.toString(); 115 data.key = "wallpaper_type_settings"; 116 data.screenTitle = context.getResources().getString( 117 R.string.wallpaper_settings_fragment_title); 118 data.intentAction = Intent.ACTION_SET_WALLPAPER; 119 data.intentTargetPackage = info.activityInfo.packageName; 120 data.intentTargetClass = info.activityInfo.name; 121 data.keywords = context.getString(R.string.keywords_wallpaper); 122 result.add(data); 123 } 124 125 return result; 126 } 127 }; 128 } 129