blob: 408039aec4b465538e7856ac95c331bc9d365c19 [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);
62 mGadgetManager.bindGadgetId(mGadgetId, intent.getComponent());
63 setResultData(RESULT_OK);
64 finish();
65 }
66
67 @Override
68 public List<ListItem> makeListItems() {
The Android Open Source Projectb9f58512009-02-13 12:57:53 -080069 List<GadgetProviderInfo> installed = mGadgetManager.getInstalledProviders();
The Android Open Source Project1feaa852009-02-10 15:44:05 -080070 PackageManager pm = getPackageManager();
71
72 Drawable defaultIcon = null;
73 IconResizer resizer = new IconResizer();
74
75 ArrayList<ListItem> result = new ArrayList();
76 final int N = installed.size();
77 for (int i=0; i<N; i++) {
The Android Open Source Projectb9f58512009-02-13 12:57:53 -080078 GadgetProviderInfo info = installed.get(i);
The Android Open Source Project1feaa852009-02-10 15:44:05 -080079
80 LauncherActivity.ListItem item = new LauncherActivity.ListItem();
81 item.packageName = info.provider.getPackageName();
82 item.className = info.provider.getClassName();
83
84 item.label = info.label;
85 if (info.icon != 0) {
86 Drawable d = pm.getDrawable( item.packageName, info.icon, null);
87 if (d != null) {
88 item.icon = resizer.createIconThumbnail(d);
89 } else {
90 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
91 + " for package: " + item.packageName);
92 }
93 }
94 if (item.icon == null) {
95 // (including error case above)
96 if (defaultIcon == null) {
97 // TODO: Load standard icon.
98 }
99 item.icon = defaultIcon;
100 }
101
102 result.add(item);
103 }
The Android Open Source Projectb9f58512009-02-13 12:57:53 -0800104
105 Collections.sort(result, new Comparator<ListItem>() {
106 Collator mCollator = Collator.getInstance();
107 public int compare(ListItem lhs, ListItem rhs) {
108 return mCollator.compare(lhs.label, rhs.label);
109 }
110 });
The Android Open Source Project1feaa852009-02-10 15:44:05 -0800111 return result;
112 }
The Android Open Source Projectb9f58512009-02-13 12:57:53 -0800113
The Android Open Source Project1feaa852009-02-10 15:44:05 -0800114 void setResultData(int code) {
115 Intent result = new Intent();
116 result.putExtra(GadgetManager.EXTRA_GADGET_ID, mGadgetId);
117 setResult(code, result);
118 }
119}
120