blob: 2bd62c0a1cc0f2880fbc09a672c87a3afb4c858e [file] [log] [blame]
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -08001/*
Jeffrey Sharkey173545e2009-03-24 20:43:02 -07002 * Copyright (C) 2009 The Android Open Source Project
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -08003 *
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
The Android Open Source Project80a7a1d2009-03-11 12:11:59 -070019import android.appwidget.AppWidgetManager;
20import android.appwidget.AppWidgetProviderInfo;
Michael Jurka0b2bd8d2012-10-30 15:32:27 -070021import android.content.Context;
Jeffrey Sharkey173545e2009-03-24 20:43:02 -070022import android.content.DialogInterface;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080023import android.content.Intent;
24import android.content.pm.PackageManager;
Michael Jurka16398c92012-09-24 17:30:17 -070025import android.content.pm.PackageManager.NameNotFoundException;
26import android.content.res.Resources;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080027import android.graphics.drawable.Drawable;
28import android.os.Bundle;
Michael Jurka16398c92012-09-24 17:30:17 -070029import android.util.DisplayMetrics;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080030import android.util.Log;
31
Michael Jurka0b2bd8d2012-10-30 15:32:27 -070032import com.android.settings.ActivityPicker.PickAdapter;
33
Michael Jurka16398c92012-09-24 17:30:17 -070034import java.util.List;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080035
Jeffrey Sharkey173545e2009-03-24 20:43:02 -070036/**
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}.
41 * <p>
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.
45 */
Michael Jurka0b2bd8d2012-10-30 15:32:27 -070046public class AppWidgetPickActivity extends ActivityPicker
47 implements AppWidgetLoader.ItemConstructor<PickAdapter.Item>{
The Android Open Source Project80a7a1d2009-03-11 12:11:59 -070048 private static final String TAG = "AppWidgetPickActivity";
Michael Jurka0b2bd8d2012-10-30 15:32:27 -070049 static final boolean LOGD = false;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080050
Michael Jurka16398c92012-09-24 17:30:17 -070051 List<PickAdapter.Item> mItems;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080052
Jeffrey Sharkey173545e2009-03-24 20:43:02 -070053 /**
54 * The allocated {@link AppWidgetManager#EXTRA_APPWIDGET_ID} that this
55 * activity is binding.
56 */
57 private int mAppWidgetId;
Michael Jurka0b2bd8d2012-10-30 15:32:27 -070058 private AppWidgetLoader<PickAdapter.Item> mAppWidgetLoader;
59 private AppWidgetManager mAppWidgetManager;
60 private PackageManager mPackageManager;
Jim Millerf8f62172012-09-17 21:02:25 -070061
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080062 @Override
63 public void onCreate(Bundle icicle) {
Jeffrey Sharkey173545e2009-03-24 20:43:02 -070064 mPackageManager = getPackageManager();
65 mAppWidgetManager = AppWidgetManager.getInstance(this);
Michael Jurka0b2bd8d2012-10-30 15:32:27 -070066 mAppWidgetLoader = new AppWidgetLoader<PickAdapter.Item>
67 (this, mAppWidgetManager, this);
Jeffrey Sharkey173545e2009-03-24 20:43:02 -070068
69 super.onCreate(icicle);
70
71 // Set default return data
72 setResultData(RESULT_CANCELED, null);
73
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);
79 } else {
The Android Open Source Projecte6dd1fa2009-03-18 17:39:48 -070080 finish();
81 }
Jeffrey Sharkey173545e2009-03-24 20:43:02 -070082 }
Adam Cohen8291c082012-09-11 18:41:01 -070083
Jeffrey Sharkey173545e2009-03-24 20:43:02 -070084 /**
Michael Jurka0b2bd8d2012-10-30 15:32:27 -070085 * 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.
Jeffrey Sharkey173545e2009-03-24 20:43:02 -070088 */
Michael Jurka0b2bd8d2012-10-30 15:32:27 -070089 @Override
90 protected List<PickAdapter.Item> getItems() {
91 mItems = mAppWidgetLoader.getItems(getIntent());
92 return mItems;
93 }
The Android Open Source Projecte6dd1fa2009-03-18 17:39:48 -070094
Michael Jurka0b2bd8d2012-10-30 15:32:27 -070095 @Override
96 public PickAdapter.Item createItem(Context context, AppWidgetProviderInfo info, Bundle extras) {
97 CharSequence label = info.label;
98 Drawable icon = null;
99
100 if (info.icon != 0) {
101 try {
102 final Resources res = context.getResources();
103 final int density = res.getDisplayMetrics().densityDpi;
104 int iconDensity;
105 switch (density) {
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;
116 default:
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);
The Android Open Source Projecte6dd1fa2009-03-18 17:39:48 -0700120 }
Michael Jurka0b2bd8d2012-10-30 15:32:27 -0700121 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);
The Android Open Source Projecte6dd1fa2009-03-18 17:39:48 -0700127 }
Michael Jurka0b2bd8d2012-10-30 15:32:27 -0700128 if (icon == null) {
129 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
130 + " for provider: " + info.provider);
The Android Open Source Projecte6dd1fa2009-03-18 17:39:48 -0700131 }
The Android Open Source Projecte6dd1fa2009-03-18 17:39:48 -0700132 }
133
Michael Jurka0b2bd8d2012-10-30 15:32:27 -0700134 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;
138 return item;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800139 }
Michael Jurka0b2bd8d2012-10-30 15:32:27 -0700140
Jeffrey Sharkey173545e2009-03-24 20:43:02 -0700141 /**
142 * {@inheritDoc}
143 */
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800144 @Override
Jeffrey Sharkey173545e2009-03-24 20:43:02 -0700145 public void onClick(DialogInterface dialog, int which) {
146 Intent intent = getIntentForPosition(which);
Michael Jurka0b2bd8d2012-10-30 15:32:27 -0700147 PickAdapter.Item item = mItems.get(which);
Adam Cohen85d4b5f2012-09-24 13:15:14 -0700148
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800149 int result;
Michael Jurka16398c92012-09-24 17:30:17 -0700150 if (item.extras != null) {
Adam Cohen85d4b5f2012-09-24 13:15:14 -0700151 // If these extras are present it's because this entry is custom.
The Android Open Source Projecte6dd1fa2009-03-18 17:39:48 -0700152 // Don't try to bind it, just pass it back to the app.
153 setResultData(RESULT_OK, intent);
154 } else {
155 try {
Adam Cohen85d4b5f2012-09-24 13:15:14 -0700156 Bundle options = null;
157 if (intent.getExtras() != null) {
158 options = intent.getExtras().getBundle(
159 AppWidgetManager.EXTRA_APPWIDGET_OPTIONS);
160 }
161 mAppWidgetManager.bindAppWidgetId(mAppWidgetId, intent.getComponent(), options);
The Android Open Source Projecte6dd1fa2009-03-18 17:39:48 -0700162 result = RESULT_OK;
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
168 // appear.
169 result = RESULT_CANCELED;
170 }
171 setResultData(result, null);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800172 }
Michael Jurka0b2bd8d2012-10-30 15:32:27 -0700173
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800174 finish();
175 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800176
Jeffrey Sharkey173545e2009-03-24 20:43:02 -0700177
178 /**
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.
182 */
The Android Open Source Projecte6dd1fa2009-03-18 17:39:48 -0700183 void setResultData(int code, Intent intent) {
184 Intent result = intent != null ? intent : new Intent();
The Android Open Source Project80a7a1d2009-03-11 12:11:59 -0700185 result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800186 setResult(code, result);
187 }
188}