1/*
2 * Copyright (C) 2011 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
17package com.android.settings;
18
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.Intent;
22import android.content.pm.PackageManager;
23import android.content.pm.ResolveInfo;
24import android.os.Bundle;
25import android.preference.Preference;
26import android.preference.PreferenceScreen;
27import com.android.settings.search.BaseSearchIndexProvider;
28import com.android.settings.search.Indexable;
29import com.android.settings.search.SearchIndexableRaw;
30
31import java.util.ArrayList;
32import java.util.List;
33
34public class WallpaperTypeSettings extends SettingsPreferenceFragment implements Indexable {
35
36    @Override
37    public void onCreate(Bundle savedInstanceState) {
38        super.onCreate(savedInstanceState);
39
40        addPreferencesFromResource(R.xml.wallpaper_settings);
41        populateWallpaperTypes();
42    }
43
44    private void populateWallpaperTypes() {
45        // Search for activities that satisfy the ACTION_SET_WALLPAPER action
46        final Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER);
47        final PackageManager pm = getPackageManager();
48        final List<ResolveInfo> rList = pm.queryIntentActivities(intent,
49                PackageManager.MATCH_DEFAULT_ONLY);
50
51        final PreferenceScreen parent = getPreferenceScreen();
52        parent.setOrderingAsAdded(false);
53        // Add Preference items for each of the matching activities
54        for (ResolveInfo info : rList) {
55            Preference pref = new Preference(getActivity());
56            Intent prefIntent = new Intent(intent);
57            prefIntent.setComponent(new ComponentName(
58                    info.activityInfo.packageName, info.activityInfo.name));
59            pref.setIntent(prefIntent);
60            CharSequence label = info.loadLabel(pm);
61            if (label == null) label = info.activityInfo.packageName;
62            pref.setTitle(label);
63            parent.addPreference(pref);
64        }
65    }
66
67    public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
68        new BaseSearchIndexProvider() {
69            @Override
70            public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
71                final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
72
73                final Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER);
74                final PackageManager pm = context.getPackageManager();
75                final List<ResolveInfo> rList = pm.queryIntentActivities(intent,
76                        PackageManager.MATCH_DEFAULT_ONLY);
77
78                // Add indexable data for each of the matching activities
79                for (ResolveInfo info : rList) {
80                    CharSequence label = info.loadLabel(pm);
81                    if (label == null) label = info.activityInfo.packageName;
82
83                    SearchIndexableRaw data = new SearchIndexableRaw(context);
84                    data.title = label.toString();
85                    data.screenTitle = context.getResources().getString(
86                            R.string.wallpaper_settings_fragment_title);
87                    data.intentAction = Intent.ACTION_SET_WALLPAPER;
88                    data.intentTargetPackage = info.activityInfo.packageName;
89                    data.intentTargetClass = info.activityInfo.name;
90                    result.add(data);
91                }
92
93                return result;
94            }
95        };
96}
97