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.List;
49 public class DreamComponentPreference extends Preference {
50 private static final String TAG = "DreamComponentPreference";
52 private final PackageManager pm;
53 private final ContentResolver resolver;
55 public DreamComponentPreference(Context context, AttributeSet attrs) {
56 super(context, attrs);
57 pm = getContext().getPackageManager();
58 resolver = getContext().getContentResolver();
60 refreshFromSettings();
63 private void refreshFromSettings() {
64 String component = Settings.Secure.getString(resolver, DREAM_COMPONENT);
65 if (component != null) {
66 ComponentName cn = ComponentName.unflattenFromString(component);
68 setSummary(pm.getActivityInfo(cn, 0).loadLabel(pm));
69 } catch (PackageManager.NameNotFoundException ex) {
70 setSummary("(unknown)");
75 public class DreamListAdapter extends BaseAdapter implements ListAdapter {
76 private ArrayList<ResolveInfo> results;
77 private final LayoutInflater inflater;
79 public DreamListAdapter(Context context) {
80 Intent choosy = new Intent(Intent.ACTION_MAIN)
81 .addCategory("android.intent.category.DREAM");
83 inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
84 results = new ArrayList<ResolveInfo>(pm.queryIntentActivities(choosy, 0));
88 public int getCount() {
89 return results.size();
93 public Object getItem(int position) {
94 return results.get(position);
98 public long getItemId (int position) {
99 return (long) position;
103 public View getView(int position, View convertView, ViewGroup parent) {
104 View row = (convertView != null)
106 : inflater.inflate(R.layout.dream_picker_row, parent, false);
107 ResolveInfo ri = results.get(position);
108 ((TextView)row.findViewById(R.id.title)).setText(ri.loadLabel(pm));
109 ((ImageView)row.findViewById(R.id.icon)).setImageDrawable(ri.loadIcon(pm));
115 protected void onClick() {
116 final DreamListAdapter list = new DreamListAdapter(getContext());
117 AlertDialog alert = new AlertDialog.Builder(getContext())
120 new DialogInterface.OnClickListener() {
122 public void onClick(DialogInterface dialog, int which) {
123 ResolveInfo ri = (ResolveInfo)list.getItem(which);
124 ActivityInfo act = ri.activityInfo;
125 ComponentName cn = new ComponentName(
126 act.applicationInfo.packageName,
128 Intent intent = new Intent(Intent.ACTION_MAIN).setComponent(cn);
130 setSummary(ri.loadLabel(pm));
131 //getContext().startActivity(intent);
133 Settings.Secure.putString(resolver, DREAM_COMPONENT, cn.flattenToString());