The Android Open Source Project | 1feaa85 | 2009-02-10 15:44:05 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.settings; |
| 18 | |
| 19 | import android.app.LauncherActivity; |
| 20 | import android.content.ComponentName; |
| 21 | import android.content.Intent; |
| 22 | import android.content.pm.PackageManager; |
The Android Open Source Project | b9f5851 | 2009-02-13 12:57:53 -0800 | [diff] [blame] | 23 | import android.gadget.GadgetProviderInfo; |
The Android Open Source Project | 1feaa85 | 2009-02-10 15:44:05 -0800 | [diff] [blame] | 24 | import android.gadget.GadgetManager; |
| 25 | import android.graphics.drawable.Drawable; |
| 26 | import android.os.Bundle; |
| 27 | import android.view.View; |
| 28 | import android.widget.ListView; |
| 29 | import android.util.Log; |
| 30 | |
The Android Open Source Project | b9f5851 | 2009-02-13 12:57:53 -0800 | [diff] [blame] | 31 | import java.text.Collator; |
The Android Open Source Project | 1feaa85 | 2009-02-10 15:44:05 -0800 | [diff] [blame] | 32 | import java.util.List; |
| 33 | import java.util.ArrayList; |
The Android Open Source Project | b9f5851 | 2009-02-13 12:57:53 -0800 | [diff] [blame] | 34 | import java.util.Collections; |
| 35 | import java.util.Comparator; |
The Android Open Source Project | 1feaa85 | 2009-02-10 15:44:05 -0800 | [diff] [blame] | 36 | |
| 37 | public class GadgetPickActivity extends LauncherActivity |
| 38 | { |
| 39 | private static final String TAG = "GadgetPickActivity"; |
| 40 | |
| 41 | GadgetManager mGadgetManager; |
| 42 | int mGadgetId; |
The Android Open Source Project | 1feaa85 | 2009-02-10 15:44:05 -0800 | [diff] [blame] | 43 | |
| 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 Project | 1feaa85 | 2009-02-10 15:44:05 -0800 | [diff] [blame] | 53 | 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 Project | 4772968 | 2009-02-19 10:57:36 -0800 | [diff] [blame] | 62 | 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 Project | 1feaa85 | 2009-02-10 15:44:05 -0800 | [diff] [blame] | 75 | finish(); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public List<ListItem> makeListItems() { |
The Android Open Source Project | b9f5851 | 2009-02-13 12:57:53 -0800 | [diff] [blame] | 80 | List<GadgetProviderInfo> installed = mGadgetManager.getInstalledProviders(); |
The Android Open Source Project | 1feaa85 | 2009-02-10 15:44:05 -0800 | [diff] [blame] | 81 | 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 Project | b9f5851 | 2009-02-13 12:57:53 -0800 | [diff] [blame] | 89 | GadgetProviderInfo info = installed.get(i); |
The Android Open Source Project | 1feaa85 | 2009-02-10 15:44:05 -0800 | [diff] [blame] | 90 | |
| 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 Project | b9f5851 | 2009-02-13 12:57:53 -0800 | [diff] [blame] | 115 | |
| 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 Project | 1feaa85 | 2009-02-10 15:44:05 -0800 | [diff] [blame] | 122 | return result; |
| 123 | } |
The Android Open Source Project | b9f5851 | 2009-02-13 12:57:53 -0800 | [diff] [blame] | 124 | |
The Android Open Source Project | 1feaa85 | 2009-02-10 15:44:05 -0800 | [diff] [blame] | 125 | void setResultData(int code) { |
| 126 | Intent result = new Intent(); |
| 127 | result.putExtra(GadgetManager.EXTRA_GADGET_ID, mGadgetId); |
| 128 | setResult(code, result); |
| 129 | } |
| 130 | } |
| 131 | |