2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings;
19 import android.appwidget.AppWidgetManager;
20 import android.appwidget.AppWidgetProviderInfo;
21 import android.content.Context;
22 import android.content.DialogInterface;
23 import android.content.Intent;
24 import android.content.pm.PackageManager;
25 import android.content.pm.PackageManager.NameNotFoundException;
26 import android.content.res.Resources;
27 import android.graphics.drawable.Drawable;
28 import android.os.Bundle;
29 import android.util.DisplayMetrics;
30 import android.util.Log;
32 import com.android.settings.ActivityPicker.PickAdapter;
34 import java.util.List;
37 * Displays a list of {@link AppWidgetProviderInfo} widgets, along with any
38 * injected special widgets specified through
39 * {@link AppWidgetManager#EXTRA_CUSTOM_INFO} and
40 * {@link AppWidgetManager#EXTRA_CUSTOM_EXTRAS}.
42 * When an installed {@link AppWidgetProviderInfo} is selected, this activity
43 * will bind it to the given {@link AppWidgetManager#EXTRA_APPWIDGET_ID},
44 * otherwise it will return the requested extras.
46 public class AppWidgetPickActivity extends ActivityPicker
47 implements AppWidgetLoader.ItemConstructor<PickAdapter.Item>{
48 private static final String TAG = "AppWidgetPickActivity";
49 static final boolean LOGD = false;
51 List<PickAdapter.Item> mItems;
54 * The allocated {@link AppWidgetManager#EXTRA_APPWIDGET_ID} that this
55 * activity is binding.
57 private int mAppWidgetId;
58 private AppWidgetLoader<PickAdapter.Item> mAppWidgetLoader;
59 private AppWidgetManager mAppWidgetManager;
60 private PackageManager mPackageManager;
63 public void onCreate(Bundle icicle) {
64 mPackageManager = getPackageManager();
65 mAppWidgetManager = AppWidgetManager.getInstance(this);
66 mAppWidgetLoader = new AppWidgetLoader<PickAdapter.Item>
67 (this, mAppWidgetManager, this);
69 super.onCreate(icicle);
71 // Set default return data
72 setResultData(RESULT_CANCELED, null);
74 // Read the appWidgetId passed our direction, otherwise bail if not found
75 final Intent intent = getIntent();
76 if (intent.hasExtra(AppWidgetManager.EXTRA_APPWIDGET_ID)) {
77 mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
78 AppWidgetManager.INVALID_APPWIDGET_ID);
85 * Build and return list of items to be shown in dialog. This will mix both
86 * installed {@link AppWidgetProviderInfo} and those provided through
87 * {@link AppWidgetManager#EXTRA_CUSTOM_INFO}, sorting them alphabetically.
90 protected List<PickAdapter.Item> getItems() {
91 mItems = mAppWidgetLoader.getItems(getIntent());
96 public PickAdapter.Item createItem(Context context, AppWidgetProviderInfo info, Bundle extras) {
97 CharSequence label = info.label;
100 if (info.icon != 0) {
102 final Resources res = context.getResources();
103 final int density = res.getDisplayMetrics().densityDpi;
106 case DisplayMetrics.DENSITY_MEDIUM:
107 iconDensity = DisplayMetrics.DENSITY_LOW;
108 case DisplayMetrics.DENSITY_TV:
109 iconDensity = DisplayMetrics.DENSITY_MEDIUM;
110 case DisplayMetrics.DENSITY_HIGH:
111 iconDensity = DisplayMetrics.DENSITY_MEDIUM;
112 case DisplayMetrics.DENSITY_XHIGH:
113 iconDensity = DisplayMetrics.DENSITY_HIGH;
114 case DisplayMetrics.DENSITY_XXHIGH:
115 iconDensity = DisplayMetrics.DENSITY_XHIGH;
117 // The density is some abnormal value. Return some other
118 // abnormal value that is a reasonable scaling of it.
119 iconDensity = (int)((density*0.75f)+.5f);
121 Resources packageResources = mPackageManager.
122 getResourcesForApplication(info.provider.getPackageName());
123 icon = packageResources.getDrawableForDensity(info.icon, iconDensity);
124 } catch (NameNotFoundException e) {
125 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
126 + " for provider: " + info.provider);
129 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
130 + " for provider: " + info.provider);
134 PickAdapter.Item item = new PickAdapter.Item(context, label, icon);
135 item.packageName = info.provider.getPackageName();
136 item.className = info.provider.getClassName();
137 item.extras = extras;
145 public void onClick(DialogInterface dialog, int which) {
146 Intent intent = getIntentForPosition(which);
147 PickAdapter.Item item = mItems.get(which);
150 if (item.extras != null) {
151 // If these extras are present it's because this entry is custom.
152 // Don't try to bind it, just pass it back to the app.
153 setResultData(RESULT_OK, intent);
156 Bundle options = null;
157 if (intent.getExtras() != null) {
158 options = intent.getExtras().getBundle(
159 AppWidgetManager.EXTRA_APPWIDGET_OPTIONS);
161 mAppWidgetManager.bindAppWidgetId(mAppWidgetId, intent.getComponent(), options);
163 } catch (IllegalArgumentException e) {
164 // This is thrown if they're already bound, or otherwise somehow
165 // bogus. Set the result to canceled, and exit. The app *should*
166 // clean up at this point. We could pass the error along, but
167 // it's not clear that that's useful -- the widget will simply not
169 result = RESULT_CANCELED;
171 setResultData(result, null);
179 * Convenience method for setting the result code and intent. This method
180 * correctly injects the {@link AppWidgetManager#EXTRA_APPWIDGET_ID} that
181 * most hosts expect returned.
183 void setResultData(int code, Intent intent) {
184 Intent result = intent != null ? intent : new Intent();
185 result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
186 setResult(code, result);