2 * Copyright (C) 2011 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 static android.provider.Settings.Secure.DREAM_COMPONENT;
21 import android.app.AlertDialog;
22 import android.content.Context;
23 import android.content.ComponentName;
24 import android.content.ContentResolver;
25 import android.content.DialogInterface;
26 import android.content.Intent;
27 import android.content.pm.ActivityInfo;
28 import android.content.pm.PackageManager;
29 import android.content.pm.ResolveInfo;
30 import android.content.res.Resources;
31 import android.preference.Preference;
32 import android.provider.Settings;
33 import android.util.AttributeSet;
34 import android.util.Log;
35 import android.view.Gravity;
36 import android.view.LayoutInflater;
37 import android.view.View;
38 import android.view.ViewGroup;
39 import android.widget.ArrayAdapter;
40 import android.widget.ListView;
41 import android.widget.BaseAdapter;
42 import android.widget.ImageView;
43 import android.widget.ListAdapter;
44 import android.widget.TextView;
46 import java.util.ArrayList;
47 import java.util.Collections;
48 import java.util.Comparator;
49 import java.util.List;
51 public class DreamComponentPreference extends Preference {
52 private static final String TAG = "DreamComponentPreference";
54 private static final boolean SHOW_DOCK_APPS_TOO = true;
56 private final PackageManager pm;
57 private final ContentResolver resolver;
59 public DreamComponentPreference(Context context, AttributeSet attrs) {
60 super(context, attrs);
61 pm = getContext().getPackageManager();
62 resolver = getContext().getContentResolver();
64 refreshFromSettings();
67 private void refreshFromSettings() {
68 String component = Settings.Secure.getString(resolver, DREAM_COMPONENT);
69 if (component == null) {
70 component = getContext().getResources().getString(
71 com.android.internal.R.string.config_defaultDreamComponent);
73 if (component != null) {
74 ComponentName cn = ComponentName.unflattenFromString(component);
76 setSummary(pm.getActivityInfo(cn, 0).loadLabel(pm));
77 } catch (PackageManager.NameNotFoundException ex) {
78 setSummary("(unknown)");
83 final static Comparator<ResolveInfo> sResolveInfoComparator = new Comparator<ResolveInfo>() {
85 public int compare(ResolveInfo a, ResolveInfo b) {
86 int cmp = a.activityInfo.applicationInfo.packageName.compareTo(
87 b.activityInfo.applicationInfo.packageName);
89 cmp = a.activityInfo.name.compareTo(b.activityInfo.name);
95 public class DreamListAdapter extends BaseAdapter implements ListAdapter {
96 private ArrayList<ResolveInfo> results;
97 private final LayoutInflater inflater;
99 public DreamListAdapter(Context context) {
100 Intent choosy = new Intent(Intent.ACTION_MAIN)
101 .addCategory("android.intent.category.DREAM");
103 inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
105 results = new ArrayList<ResolveInfo>(pm.queryIntentActivities(choosy, 0));
108 Collections.sort(results, sResolveInfoComparator);
110 if (SHOW_DOCK_APPS_TOO) {
111 choosy = new Intent(Intent.ACTION_MAIN)
112 .addCategory(Intent.CATEGORY_DESK_DOCK);
114 List<ResolveInfo> dockApps = pm.queryIntentActivities(choosy, 0);
115 for (ResolveInfo app : dockApps) {
116 // do not insert duplicate packages
117 int pos = Collections.binarySearch(results, app, sResolveInfoComparator);
119 results.add(-1-pos, app);
126 public int getCount() {
127 return results.size();
131 public Object getItem(int position) {
132 return results.get(position);
136 public long getItemId (int position) {
137 return (long) position;
141 public View getView(int position, View convertView, ViewGroup parent) {
142 View row = (convertView != null)
144 : inflater.inflate(R.layout.dream_picker_row, parent, false);
145 ResolveInfo ri = results.get(position);
146 ((TextView)row.findViewById(R.id.title)).setText(ri.loadLabel(pm));
147 ((ImageView)row.findViewById(R.id.icon)).setImageDrawable(ri.loadIcon(pm));
153 protected void onClick() {
154 final DreamListAdapter list = new DreamListAdapter(getContext());
155 AlertDialog alert = new AlertDialog.Builder(getContext())
158 new DialogInterface.OnClickListener() {
160 public void onClick(DialogInterface dialog, int which) {
161 ResolveInfo ri = (ResolveInfo)list.getItem(which);
162 ActivityInfo act = ri.activityInfo;
163 ComponentName cn = new ComponentName(
164 act.applicationInfo.packageName,
166 Intent intent = new Intent(Intent.ACTION_MAIN).setComponent(cn);
168 setSummary(ri.loadLabel(pm));
169 //getContext().startActivity(intent);
171 Settings.Secure.putString(resolver, DREAM_COMPONENT, cn.flattenToString());