1 /*
2  * Copyright (C) 2019 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.car.developeroptions.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.text.TextUtils;
25 
26 import androidx.preference.Preference;
27 import androidx.preference.PreferenceScreen;
28 
29 import com.android.car.developeroptions.core.BasePreferenceController;
30 import com.android.settingslib.core.lifecycle.LifecycleObserver;
31 import com.android.settingslib.core.lifecycle.events.OnStart;
32 
33 import java.util.List;
34 import java.util.stream.Collectors;
35 
36 public class WallpaperTypePreferenceController extends BasePreferenceController
37         implements LifecycleObserver, OnStart {
38 
39     private PreferenceScreen mScreen;
40 
WallpaperTypePreferenceController(Context context, String key)41     public WallpaperTypePreferenceController(Context context, String key) {
42         super(context, key);
43     }
44 
45     @Override
displayPreference(PreferenceScreen screen)46     public void displayPreference(PreferenceScreen screen) {
47         super.displayPreference(screen);
48         mScreen = screen;
49     }
50 
51     @Override
getAvailabilityStatus()52     public int getAvailabilityStatus() {
53         return AVAILABLE;
54     }
55 
56     @Override
handlePreferenceTreeClick(Preference preference)57     public boolean handlePreferenceTreeClick(Preference preference) {
58         if (preference.getIntent() == null) {
59             return super.handlePreferenceTreeClick(preference);
60         }
61         mContext.startActivity(preference.getIntent());
62         return true;
63     }
64 
65     @Override
onStart()66     public void onStart() {
67         populateWallpaperTypes();
68     }
69 
populateWallpaperTypes()70     private void populateWallpaperTypes() {
71         // Search for activities that satisfy the ACTION_SET_WALLPAPER action
72         final Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER);
73         final PackageManager pm = mContext.getPackageManager();
74         final List<ResolveInfo> rList = pm.queryIntentActivities(intent,
75                 PackageManager.MATCH_DEFAULT_ONLY);
76 
77         removeUselessExistingPreference(rList);
78         mScreen.setOrderingAsAdded(false);
79         // Add Preference items for each of the matching activities
80         for (ResolveInfo info : rList) {
81             final String packageName = info.activityInfo.packageName;
82             Preference pref = mScreen.findPreference(packageName);
83             if (pref == null) {
84                 pref = new Preference(mScreen.getContext());
85             }
86             final Intent prefIntent = new Intent(intent).addFlags(
87                     Intent.FLAG_ACTIVITY_FORWARD_RESULT);
88             prefIntent.setComponent(new ComponentName(packageName, info.activityInfo.name));
89             pref.setIntent(prefIntent);
90             pref.setKey(packageName);
91             CharSequence label = info.loadLabel(pm);
92             if (label == null) {
93                 label = packageName;
94             }
95             pref.setTitle(label);
96             pref.setIcon(info.loadIcon(pm));
97             mScreen.addPreference(pref);
98         }
99     }
100 
removeUselessExistingPreference(List<ResolveInfo> rList)101     private void removeUselessExistingPreference(List<ResolveInfo> rList) {
102         final int count = mScreen.getPreferenceCount();
103         if (count <= 0) {
104             return;
105         }
106         for (int i = count - 1; i >= 0; i--) {
107             final Preference pref = mScreen.getPreference(i);
108             final List<ResolveInfo> result = rList.stream().filter(
109                     rInfo -> TextUtils.equals(pref.getKey(),
110                             rInfo.activityInfo.packageName)).collect(Collectors.toList());
111             if (result.isEmpty()) {
112                 mScreen.removePreference(pref);
113             }
114         }
115     }
116 }