OSDN Git Service

am 4fedc0fd: (-s ours) Import translations. DO NOT MERGE
[android-x86/packages-apps-Settings.git] / src / com / android / settings / AppListPreference.java
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
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
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
70     public AppListPreference(Context context, AttributeSet attrs,
71             int defStyleAttr, int defStyleRes) {
72         super(context, attrs, defStyleAttr, defStyleRes);
73     }
74
75     public AppListPreference(Context context, AttributeSet attrs) {
76         super(context, attrs);
77     }
78
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++) {
85             try {
86                 appInfos[i] = pm.getApplicationInfo(packageNames[i], 0);
87                 foundPackages++;
88             } catch (NameNotFoundException e) {
89                 // Leave appInfos[i] uninitialized; it will be skipped in the list.
90             }
91         }
92
93         // Show the label and icon for each application package.
94         CharSequence[] applicationNames = new CharSequence[foundPackages];
95         mEntryDrawables = new Drawable[foundPackages];
96         int index = 0;
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;
105                 }
106                 index++;
107             }
108         }
109         setEntries(applicationNames);
110         setEntryValues(packageNames);
111         if (selectedIndex != -1) {
112             setValueIndex(selectedIndex);
113         } else {
114             setValue(null);
115         }
116     }
117
118     @Override
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);
125     }
126 }