OSDN Git Service

3bc0eb4994c98ff1466c3e0ad6acb2a992ec7141
[android-x86/packages-apps-Settings.git] / src / com / android / settings / DreamComponentPreference.java
1 /*
2  * Copyright (C) 2011 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 static android.provider.Settings.Secure.DREAM_COMPONENT;
20
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;
45
46 import java.util.ArrayList;
47 import java.util.List;
48
49 public class DreamComponentPreference extends Preference {
50     private static final String TAG = "DreamComponentPreference";
51     
52     private final PackageManager pm;
53     private final ContentResolver resolver;
54
55     public DreamComponentPreference(Context context, AttributeSet attrs) {
56         super(context, attrs);
57         pm = getContext().getPackageManager();
58         resolver = getContext().getContentResolver();
59
60         refreshFromSettings();
61     }
62
63     private void refreshFromSettings() {
64         String component = Settings.Secure.getString(resolver, DREAM_COMPONENT);
65         if (component != null) {
66             ComponentName cn = ComponentName.unflattenFromString(component);
67             try {
68                 setSummary(pm.getActivityInfo(cn, 0).loadLabel(pm));
69             } catch (PackageManager.NameNotFoundException ex) {
70                 setSummary("(unknown)");
71             }
72         }
73     }
74
75     public class DreamListAdapter extends BaseAdapter implements ListAdapter {
76         private ArrayList<ResolveInfo> results;
77         private final LayoutInflater inflater;
78
79         public DreamListAdapter(Context context) {
80             Intent choosy = new Intent(Intent.ACTION_MAIN)
81                         .addCategory("android.intent.category.DREAM");
82
83             inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
84             results = new ArrayList<ResolveInfo>(pm.queryIntentActivities(choosy, 0));
85         }
86
87         @Override
88         public int getCount() {
89             return results.size();
90         }
91
92         @Override
93         public Object getItem(int position) {
94             return results.get(position);
95         }
96
97         @Override
98         public long getItemId (int position) {
99             return (long) position;
100         }
101
102         @Override
103         public View getView(int position, View convertView, ViewGroup parent) {
104             View row = (convertView != null) 
105                 ? convertView
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));
110             return row;
111         }
112     }
113
114     @Override
115     protected void onClick() {
116         final DreamListAdapter list = new DreamListAdapter(getContext());
117         AlertDialog alert = new AlertDialog.Builder(getContext())
118             .setAdapter(
119                 list,
120                 new DialogInterface.OnClickListener() {
121                     @Override
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,
127                             act.name);
128                         Intent intent = new Intent(Intent.ACTION_MAIN).setComponent(cn);
129                         
130                         setSummary(ri.loadLabel(pm));
131                         //getContext().startActivity(intent);
132                         
133                         Settings.Secure.putString(resolver, DREAM_COMPONENT, cn.flattenToString());
134                     }
135                 })
136             .create();
137         alert.show();
138     }
139 }