2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings;
19 import android.app.Activity;
20 import android.app.AlertDialog.Builder;
21 import android.content.Context;
22 import android.content.pm.ApplicationInfo;
23 import android.content.pm.PackageManager;
24 import android.content.pm.PackageManager.NameNotFoundException;
25 import android.graphics.drawable.Drawable;
26 import android.preference.ListPreference;
27 import android.util.AttributeSet;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.view.ViewGroup;
31 import android.widget.ArrayAdapter;
32 import android.widget.CheckedTextView;
33 import android.widget.ImageView;
34 import android.widget.ListAdapter;
37 * Extends ListPreference to allow us to show the icons for a given list of applications. We do this
38 * because the names of applications are very similar and the user may not be able to determine what
39 * app they are selecting without an icon.
41 public class AppListPreference extends ListPreference {
42 private Drawable[] mEntryDrawables;
44 public class AppArrayAdapter extends ArrayAdapter<CharSequence> {
45 private Drawable[] mImageDrawables = null;
46 private int mSelectedIndex = 0;
48 public AppArrayAdapter(Context context, int textViewResourceId,
49 CharSequence[] objects, Drawable[] imageDrawables, int selectedIndex) {
50 super(context, textViewResourceId, objects);
51 mSelectedIndex = selectedIndex;
52 mImageDrawables = imageDrawables;
56 public View getView(int position, View convertView, ViewGroup parent) {
57 LayoutInflater inflater = ((Activity)getContext()).getLayoutInflater();
58 View view = inflater.inflate(R.layout.app_preference_item, parent, false);
59 CheckedTextView checkedTextView = (CheckedTextView)view.findViewById(R.id.app_label);
60 checkedTextView.setText(getItem(position));
61 if (position == mSelectedIndex) {
62 checkedTextView.setChecked(true);
64 ImageView imageView = (ImageView)view.findViewById(R.id.app_image);
65 imageView.setImageDrawable(mImageDrawables[position]);
70 public AppListPreference(Context context, AttributeSet attrs,
71 int defStyleAttr, int defStyleRes) {
72 super(context, attrs, defStyleAttr, defStyleRes);
75 public AppListPreference(Context context, AttributeSet attrs) {
76 super(context, attrs);
79 public void setPackageNames(String[] packageNames, String defaultPackageName) {
80 // Look up all package names in PackageManager. Skip ones we can't find.
81 int foundPackages = 0;
82 PackageManager pm = getContext().getPackageManager();
83 ApplicationInfo[] appInfos = new ApplicationInfo[packageNames.length];
84 for (int i = 0; i < packageNames.length; i++) {
86 appInfos[i] = pm.getApplicationInfo(packageNames[i], 0);
88 } catch (NameNotFoundException e) {
89 // Leave appInfos[i] uninitialized; it will be skipped in the list.
93 // Show the label and icon for each application package.
94 CharSequence[] applicationNames = new CharSequence[foundPackages];
95 mEntryDrawables = new Drawable[foundPackages];
97 int selectedIndex = -1;
98 for (ApplicationInfo appInfo : appInfos) {
99 if (appInfo != null) {
100 applicationNames[index] = appInfo.loadLabel(pm);
101 mEntryDrawables[index] = appInfo.loadIcon(pm);
102 if (defaultPackageName != null &&
103 appInfo.packageName.contentEquals(defaultPackageName)) {
104 selectedIndex = index;
109 setEntries(applicationNames);
110 setEntryValues(packageNames);
111 if (selectedIndex != -1) {
112 setValueIndex(selectedIndex);
119 protected void onPrepareDialogBuilder(Builder builder) {
120 int selectedIndex = findIndexOfValue(getValue());
121 ListAdapter adapter = new AppArrayAdapter(getContext(),
122 R.layout.app_preference_item, getEntries(), mEntryDrawables, selectedIndex);
123 builder.setAdapter(adapter, this);
124 super.onPrepareDialogBuilder(builder);