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.enterprise; 18 19 import android.content.Context; 20 import android.content.pm.PackageManager; 21 import android.util.IconDrawableFactory; 22 23 import androidx.preference.Preference; 24 import androidx.preference.PreferenceScreen; 25 26 import com.android.settings.SettingsPreferenceFragment; 27 import com.android.settings.applications.ApplicationFeatureProvider; 28 import com.android.settings.applications.UserAppInfo; 29 import com.android.settings.core.PreferenceControllerMixin; 30 import com.android.settingslib.core.AbstractPreferenceController; 31 import com.android.settingslib.widget.apppreference.AppPreference; 32 33 import java.util.List; 34 35 /** 36 * PreferenceController that builds a dynamic list of applications provided by 37 * {@link ApplicationListBuilder} instance. 38 */ 39 public class ApplicationListPreferenceController extends AbstractPreferenceController implements 40 PreferenceControllerMixin, ApplicationFeatureProvider.ListOfAppsCallback { 41 private final PackageManager mPm; 42 private SettingsPreferenceFragment mParent; 43 ApplicationListPreferenceController(Context context, ApplicationListBuilder builder, PackageManager packageManager, SettingsPreferenceFragment parent)44 public ApplicationListPreferenceController(Context context, ApplicationListBuilder builder, 45 PackageManager packageManager, SettingsPreferenceFragment parent) { 46 super(context); 47 mPm = packageManager; 48 mParent = parent; 49 builder.buildApplicationList(context, this); 50 } 51 52 @Override isAvailable()53 public boolean isAvailable() { 54 return true; 55 } 56 57 @Override getPreferenceKey()58 public String getPreferenceKey() { 59 return null; 60 } 61 62 @Override onListOfAppsResult(List<UserAppInfo> result)63 public void onListOfAppsResult(List<UserAppInfo> result) { 64 final PreferenceScreen screen = mParent.getPreferenceScreen(); 65 if (screen == null) { 66 return; 67 } 68 final IconDrawableFactory iconDrawableFactory = IconDrawableFactory.newInstance(mContext); 69 final Context prefContext = mParent.getPreferenceManager().getContext(); 70 for (int position = 0; position < result.size(); position++) { 71 final UserAppInfo item = result.get(position); 72 final Preference preference = new AppPreference(prefContext); 73 preference.setTitle(item.appInfo.loadLabel(mPm)); 74 preference.setIcon(iconDrawableFactory.getBadgedIcon(item.appInfo)); 75 preference.setOrder(position); 76 preference.setSelectable(false); 77 screen.addPreference(preference); 78 } 79 } 80 81 /** 82 * Simple interface for building application list within { 83 * 84 * @link ApplicationListPreferenceController} 85 */ 86 public interface ApplicationListBuilder { buildApplicationList(Context context, ApplicationFeatureProvider.ListOfAppsCallback callback)87 void buildApplicationList(Context context, 88 ApplicationFeatureProvider.ListOfAppsCallback callback); 89 } 90 } 91