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