OSDN Git Service

Show desk dock apps as screen savers.
[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.Collections;
48 import java.util.Comparator;
49 import java.util.List;
50
51 public class DreamComponentPreference extends Preference {
52     private static final String TAG = "DreamComponentPreference";
53
54     private static final boolean SHOW_DOCK_APPS_TOO = true;
55     
56     private final PackageManager pm;
57     private final ContentResolver resolver;
58
59     public DreamComponentPreference(Context context, AttributeSet attrs) {
60         super(context, attrs);
61         pm = getContext().getPackageManager();
62         resolver = getContext().getContentResolver();
63
64         refreshFromSettings();
65     }
66
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);
72         }
73         if (component != null) {
74             ComponentName cn = ComponentName.unflattenFromString(component);
75             try {
76                 setSummary(pm.getActivityInfo(cn, 0).loadLabel(pm));
77             } catch (PackageManager.NameNotFoundException ex) {
78                 setSummary("(unknown)");
79             }
80         }
81     }
82
83     final static Comparator<ResolveInfo> sResolveInfoComparator = new Comparator<ResolveInfo>() {
84         @Override
85         public int compare(ResolveInfo a, ResolveInfo b) {
86             int cmp = a.activityInfo.applicationInfo.packageName.compareTo(
87                     b.activityInfo.applicationInfo.packageName);
88             if (cmp == 0) {
89                 cmp = a.activityInfo.name.compareTo(b.activityInfo.name);
90             }
91             return cmp;
92         }
93     };
94
95     public class DreamListAdapter extends BaseAdapter implements ListAdapter {
96         private ArrayList<ResolveInfo> results;
97         private final LayoutInflater inflater;
98
99         public DreamListAdapter(Context context) {
100             Intent choosy = new Intent(Intent.ACTION_MAIN)
101                         .addCategory("android.intent.category.DREAM");
102
103             inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
104
105             results = new ArrayList<ResolveInfo>(pm.queryIntentActivities(choosy, 0));
106
107             // Group by package
108             Collections.sort(results, sResolveInfoComparator);
109
110             if (SHOW_DOCK_APPS_TOO) {
111                 choosy = new Intent(Intent.ACTION_MAIN)
112                             .addCategory(Intent.CATEGORY_DESK_DOCK);
113
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);
118                     if (pos < 0) {
119                         results.add(-1-pos, app);
120                     }
121                 }
122             }
123         }
124
125         @Override
126         public int getCount() {
127             return results.size();
128         }
129
130         @Override
131         public Object getItem(int position) {
132             return results.get(position);
133         }
134
135         @Override
136         public long getItemId (int position) {
137             return (long) position;
138         }
139
140         @Override
141         public View getView(int position, View convertView, ViewGroup parent) {
142             View row = (convertView != null) 
143                 ? convertView
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));
148             return row;
149         }
150     }
151
152     @Override
153     protected void onClick() {
154         final DreamListAdapter list = new DreamListAdapter(getContext());
155         AlertDialog alert = new AlertDialog.Builder(getContext())
156             .setAdapter(
157                 list,
158                 new DialogInterface.OnClickListener() {
159                     @Override
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,
165                             act.name);
166                         Intent intent = new Intent(Intent.ACTION_MAIN).setComponent(cn);
167                         
168                         setSummary(ri.loadLabel(pm));
169                         //getContext().startActivity(intent);
170                         
171                         Settings.Secure.putString(resolver, DREAM_COMPONENT, cn.flattenToString());
172                     }
173                 })
174             .create();
175         alert.show();
176     }
177 }