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