• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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;
18 
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;
35 
36 /**
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.
40  */
41 public class AppListPreference extends ListPreference {
42     private Drawable[] mEntryDrawables;
43 
44     public class AppArrayAdapter extends ArrayAdapter<CharSequence> {
45         private Drawable[] mImageDrawables = null;
46         private int mSelectedIndex = 0;
47 
AppArrayAdapter(Context context, int textViewResourceId, CharSequence[] objects, Drawable[] imageDrawables, int selectedIndex)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;
53         }
54 
55         @Override
getView(int position, View convertView, ViewGroup parent)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);
63             }
64             ImageView imageView = (ImageView)view.findViewById(R.id.app_image);
65             imageView.setImageDrawable(mImageDrawables[position]);
66             return view;
67         }
68     }
69 
AppListPreference(Context context, AttributeSet attrs)70     public AppListPreference(Context context, AttributeSet attrs) {
71         super(context, attrs);
72     }
73 
setPackageNames(String[] packageNames, String defaultPackageName)74     public void setPackageNames(String[] packageNames, String defaultPackageName) {
75         // Look up all package names in PackageManager. Skip ones we can't find.
76         int foundPackages = 0;
77         PackageManager pm = getContext().getPackageManager();
78         ApplicationInfo[] appInfos = new ApplicationInfo[packageNames.length];
79         for (int i = 0; i < packageNames.length; i++) {
80             try {
81                 appInfos[i] = pm.getApplicationInfo(packageNames[i], 0);
82                 foundPackages++;
83             } catch (NameNotFoundException e) {
84                 // Leave appInfos[i] uninitialized; it will be skipped in the list.
85             }
86         }
87 
88         // Show the label and icon for each application package.
89         CharSequence[] applicationNames = new CharSequence[foundPackages];
90         mEntryDrawables = new Drawable[foundPackages];
91         int index = 0;
92         int selectedIndex = -1;
93         for (ApplicationInfo appInfo : appInfos) {
94             if (appInfo != null) {
95                 applicationNames[index] = appInfo.loadLabel(pm);
96                 mEntryDrawables[index] = appInfo.loadIcon(pm);
97                 if (defaultPackageName != null &&
98                         appInfo.packageName.contentEquals(defaultPackageName)) {
99                     selectedIndex = index;
100                 }
101                 index++;
102             }
103         }
104         setEntries(applicationNames);
105         setEntryValues(packageNames);
106         if (selectedIndex != -1) {
107             setValueIndex(selectedIndex);
108         }
109     }
110 
111     @Override
onPrepareDialogBuilder(Builder builder)112     protected void onPrepareDialogBuilder(Builder builder) {
113         int selectedIndex = findIndexOfValue(getValue());
114         ListAdapter adapter = new AppArrayAdapter(getContext(),
115             R.layout.app_preference_item, getEntries(), mEntryDrawables, selectedIndex);
116         builder.setAdapter(adapter, this);
117         super.onPrepareDialogBuilder(builder);
118     }
119 }
120