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.support.v7.preference.Preference;
26import android.support.v7.preference.PreferenceScreen;
27
28import com.android.internal.logging.MetricsProto.MetricsEvent;
29import com.android.settings.search.BaseSearchIndexProvider;
30import com.android.settings.search.Indexable;
31import com.android.settings.search.SearchIndexableRaw;
32
33import java.util.ArrayList;
34import java.util.List;
35
36public class WallpaperTypeSettings extends SettingsPreferenceFragment implements Indexable {
37
38    @Override
39    protected int getMetricsCategory() {
40        return MetricsEvent.WALLPAPER_TYPE;
41    }
42
43    @Override
44    protected int getHelpResource() {
45        return R.string.help_uri_wallpaper;
46    }
47
48    @Override
49    public void onCreate(Bundle savedInstanceState) {
50        super.onCreate(savedInstanceState);
51
52        addPreferencesFromResource(R.xml.wallpaper_settings);
53        populateWallpaperTypes();
54    }
55
56    private void populateWallpaperTypes() {
57        // Search for activities that satisfy the ACTION_SET_WALLPAPER action
58        final Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER);
59        final PackageManager pm = getPackageManager();
60        final List<ResolveInfo> rList = pm.queryIntentActivities(intent,
61                PackageManager.MATCH_DEFAULT_ONLY);
62
63        final PreferenceScreen parent = getPreferenceScreen();
64        parent.setOrderingAsAdded(false);
65        // Add Preference items for each of the matching activities
66        for (ResolveInfo info : rList) {
67            Preference pref = new Preference(getPrefContext());
68            pref.setLayoutResource(R.layout.preference_wallpaper_type);
69            Intent prefIntent = new Intent(intent);
70            prefIntent.setComponent(new ComponentName(
71                    info.activityInfo.packageName, info.activityInfo.name));
72            pref.setIntent(prefIntent);
73            CharSequence label = info.loadLabel(pm);
74            if (label == null) label = info.activityInfo.packageName;
75            pref.setTitle(label);
76            pref.setIcon(info.loadIcon(pm));
77            parent.addPreference(pref);
78        }
79    }
80
81    public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
82        new BaseSearchIndexProvider() {
83            @Override
84            public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
85                final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
86
87                final Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER);
88                final PackageManager pm = context.getPackageManager();
89                final List<ResolveInfo> rList = pm.queryIntentActivities(intent,
90                        PackageManager.MATCH_DEFAULT_ONLY);
91
92                // Add indexable data for each of the matching activities
93                for (ResolveInfo info : rList) {
94                    CharSequence label = info.loadLabel(pm);
95                    if (label == null) label = info.activityInfo.packageName;
96
97                    SearchIndexableRaw data = new SearchIndexableRaw(context);
98                    data.title = label.toString();
99                    data.screenTitle = context.getResources().getString(
100                            R.string.wallpaper_settings_fragment_title);
101                    data.intentAction = Intent.ACTION_SET_WALLPAPER;
102                    data.intentTargetPackage = info.activityInfo.packageName;
103                    data.intentTargetClass = info.activityInfo.name;
104                    result.add(data);
105                }
106
107                return result;
108            }
109        };
110}
111