blob: 09e0fc70c557f29d47a2246fda6c7ef10d00ccc5 [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;
23import android.gadget.GadgetInfo;
24import 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
31import java.util.List;
32import java.util.ArrayList;
33
34public class GadgetPickActivity extends LauncherActivity
35{
36 private static final String TAG = "GadgetPickActivity";
37
38 GadgetManager mGadgetManager;
39 int mGadgetId;
40 int mHostId;
41
42 public GadgetPickActivity() {
43 mGadgetManager = GadgetManager.getInstance(this);
44 }
45
46 @Override
47 public void onCreate(Bundle icicle) {
48 super.onCreate(icicle);
49
50 Bundle extras = getIntent().getExtras();
51 mHostId = extras.getInt(GadgetManager.EXTRA_HOST_ID);
52 mGadgetId = extras.getInt(GadgetManager.EXTRA_GADGET_ID);
53
54 setResultData(RESULT_CANCELED);
55 }
56
57 @Override
58 public void onListItemClick(ListView l, View v, int position, long id)
59 {
60 Intent intent = intentForPosition(position);
61 mGadgetManager.bindGadgetId(mGadgetId, intent.getComponent());
62 setResultData(RESULT_OK);
63 finish();
64 }
65
66 @Override
67 public List<ListItem> makeListItems() {
68 List<GadgetInfo> installed = mGadgetManager.getInstalledProviders();
69 PackageManager pm = getPackageManager();
70
71 Drawable defaultIcon = null;
72 IconResizer resizer = new IconResizer();
73
74 ArrayList<ListItem> result = new ArrayList();
75 final int N = installed.size();
76 for (int i=0; i<N; i++) {
77 GadgetInfo info = installed.get(i);
78
79 LauncherActivity.ListItem item = new LauncherActivity.ListItem();
80 item.packageName = info.provider.getPackageName();
81 item.className = info.provider.getClassName();
82
83 item.label = info.label;
84 if (info.icon != 0) {
85 Drawable d = pm.getDrawable( item.packageName, info.icon, null);
86 if (d != null) {
87 item.icon = resizer.createIconThumbnail(d);
88 } else {
89 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
90 + " for package: " + item.packageName);
91 }
92 }
93 if (item.icon == null) {
94 // (including error case above)
95 if (defaultIcon == null) {
96 // TODO: Load standard icon.
97 }
98 item.icon = defaultIcon;
99 }
100
101 result.add(item);
102 }
103 return result;
104 }
105
106 void setResultData(int code) {
107 Intent result = new Intent();
108 result.putExtra(GadgetManager.EXTRA_GADGET_ID, mGadgetId);
109 setResult(code, result);
110 }
111}
112