blob: 840a6a55fcbc3ef52cec799b50b09c1b157f17f9 [file] [log] [blame]
The Android Open Source Project1feaa852009-02-10 15:44:05 -08001/*
2 * Copyright (C) 2008 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
17package com.android.settings;
18
19import android.app.LauncherActivity;
20import android.content.ComponentName;
21import android.content.Intent;
22import android.content.pm.PackageManager;
The Android Open Source Projectb9f58512009-02-13 12:57:53 -080023import android.gadget.GadgetProviderInfo;
The Android Open Source Project1feaa852009-02-10 15:44:05 -080024import android.gadget.GadgetManager;
25import android.graphics.drawable.Drawable;
26import android.os.Bundle;
27import android.view.View;
28import android.widget.ListView;
29import android.util.Log;
30
The Android Open Source Projectb9f58512009-02-13 12:57:53 -080031import java.text.Collator;
The Android Open Source Project1feaa852009-02-10 15:44:05 -080032import java.util.List;
33import java.util.ArrayList;
The Android Open Source Projectb9f58512009-02-13 12:57:53 -080034import java.util.Collections;
35import java.util.Comparator;
The Android Open Source Project1feaa852009-02-10 15:44:05 -080036
37public class GadgetPickActivity extends LauncherActivity
38{
39 private static final String TAG = "GadgetPickActivity";
40
41 GadgetManager mGadgetManager;
42 int mGadgetId;
The Android Open Source Project1feaa852009-02-10 15:44:05 -080043
44 public GadgetPickActivity() {
45 mGadgetManager = GadgetManager.getInstance(this);
46 }
47
48 @Override
49 public void onCreate(Bundle icicle) {
50 super.onCreate(icicle);
51
52 Bundle extras = getIntent().getExtras();
The Android Open Source Project1feaa852009-02-10 15:44:05 -080053 mGadgetId = extras.getInt(GadgetManager.EXTRA_GADGET_ID);
54
55 setResultData(RESULT_CANCELED);
56 }
57
58 @Override
59 public void onListItemClick(ListView l, View v, int position, long id)
60 {
61 Intent intent = intentForPosition(position);
The Android Open Source Project47729682009-02-19 10:57:36 -080062 int result;
63 try {
64 mGadgetManager.bindGadgetId(mGadgetId, intent.getComponent());
65 result = RESULT_OK;
66 } catch (IllegalArgumentException e) {
67 // This is thrown if they're already bound, or otherwise somehow
68 // bogus. Set the result to canceled, and exit. The app *should*
69 // clean up at this point. We could pass the error along, but
70 // it's not clear that that's useful -- the gadget will simply not
71 // appear.
72 result = RESULT_CANCELED;
73 }
74 setResultData(result);
The Android Open Source Project1feaa852009-02-10 15:44:05 -080075 finish();
76 }
77
78 @Override
79 public List<ListItem> makeListItems() {
The Android Open Source Projectb9f58512009-02-13 12:57:53 -080080 List<GadgetProviderInfo> installed = mGadgetManager.getInstalledProviders();
The Android Open Source Project1feaa852009-02-10 15:44:05 -080081 PackageManager pm = getPackageManager();
82
83 Drawable defaultIcon = null;
84 IconResizer resizer = new IconResizer();
85
86 ArrayList<ListItem> result = new ArrayList();
87 final int N = installed.size();
88 for (int i=0; i<N; i++) {
The Android Open Source Projectb9f58512009-02-13 12:57:53 -080089 GadgetProviderInfo info = installed.get(i);
The Android Open Source Project1feaa852009-02-10 15:44:05 -080090
91 LauncherActivity.ListItem item = new LauncherActivity.ListItem();
92 item.packageName = info.provider.getPackageName();
93 item.className = info.provider.getClassName();
94
95 item.label = info.label;
96 if (info.icon != 0) {
97 Drawable d = pm.getDrawable( item.packageName, info.icon, null);
98 if (d != null) {
99 item.icon = resizer.createIconThumbnail(d);
100 } else {
101 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
102 + " for package: " + item.packageName);
103 }
104 }
105 if (item.icon == null) {
106 // (including error case above)
107 if (defaultIcon == null) {
108 // TODO: Load standard icon.
109 }
110 item.icon = defaultIcon;
111 }
112
113 result.add(item);
114 }
The Android Open Source Projectb9f58512009-02-13 12:57:53 -0800115
116 Collections.sort(result, new Comparator<ListItem>() {
117 Collator mCollator = Collator.getInstance();
118 public int compare(ListItem lhs, ListItem rhs) {
119 return mCollator.compare(lhs.label, rhs.label);
120 }
121 });
The Android Open Source Project1feaa852009-02-10 15:44:05 -0800122 return result;
123 }
The Android Open Source Projectb9f58512009-02-13 12:57:53 -0800124
The Android Open Source Project1feaa852009-02-10 15:44:05 -0800125 void setResultData(int code) {
126 Intent result = new Intent();
127 result.putExtra(GadgetManager.EXTRA_GADGET_ID, mGadgetId);
128 setResult(code, result);
129 }
130}
131