The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 18 | |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame^] | 19 | import android.app.Activity; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 20 | import android.appwidget.AppWidgetHost; |
| 21 | import android.appwidget.AppWidgetHostView; |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame^] | 22 | import android.appwidget.AppWidgetManager; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 23 | import android.appwidget.AppWidgetProviderInfo; |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame^] | 24 | import android.content.ActivityNotFoundException; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 25 | import android.content.Context; |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame^] | 26 | import android.content.Intent; |
| 27 | import android.os.Handler; |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 28 | import android.util.SparseArray; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 29 | import android.view.LayoutInflater; |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame^] | 30 | import android.widget.Toast; |
| 31 | |
| 32 | import com.android.launcher3.config.FeatureFlags; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 33 | |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 34 | import java.util.ArrayList; |
| 35 | |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 36 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 37 | /** |
| 38 | * Specific {@link AppWidgetHost} that creates our {@link LauncherAppWidgetHostView} |
| 39 | * which correctly captures all long-press events. This ensures that users can |
| 40 | * always pick up and move widgets. |
| 41 | */ |
| 42 | public class LauncherAppWidgetHost extends AppWidgetHost { |
Winson Chung | a3f78e3 | 2012-06-14 11:59:51 -0700 | [diff] [blame] | 43 | |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame^] | 44 | public static final int APPWIDGET_HOST_ID = 1024; |
| 45 | |
| 46 | private final ArrayList<ProviderChangedListener> mProviderChangeListeners = new ArrayList<>(); |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 47 | private final SparseArray<LauncherAppWidgetHostView> mViews = new SparseArray<>(); |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 48 | |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame^] | 49 | private final Context mContext; |
Winson Chung | a3f78e3 | 2012-06-14 11:59:51 -0700 | [diff] [blame] | 50 | |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame^] | 51 | public LauncherAppWidgetHost(Context context) { |
| 52 | super(context, APPWIDGET_HOST_ID); |
| 53 | mContext = context; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 54 | } |
Adam Cohen | 9415d87 | 2010-09-13 14:49:43 -0700 | [diff] [blame] | 55 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 56 | @Override |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 57 | protected LauncherAppWidgetHostView onCreateView(Context context, int appWidgetId, |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 58 | AppWidgetProviderInfo appWidget) { |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 59 | LauncherAppWidgetHostView view = new LauncherAppWidgetHostView(context); |
| 60 | mViews.put(appWidgetId, view); |
| 61 | return view; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 62 | } |
Patrick Dubroy | 2313eff | 2011-01-11 20:01:31 -0800 | [diff] [blame] | 63 | |
| 64 | @Override |
Adam Cohen | 084c318 | 2014-06-16 15:22:56 -0700 | [diff] [blame] | 65 | public void startListening() { |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame^] | 66 | if (FeatureFlags.GO_DISABLE_WIDGETS) { |
| 67 | return; |
| 68 | } |
| 69 | |
Adam Cohen | 084c318 | 2014-06-16 15:22:56 -0700 | [diff] [blame] | 70 | try { |
| 71 | super.startListening(); |
| 72 | } catch (Exception e) { |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 73 | if (!Utilities.isBinderSizeError(e)) { |
Adam Cohen | 084c318 | 2014-06-16 15:22:56 -0700 | [diff] [blame] | 74 | throw new RuntimeException(e); |
| 75 | } |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 76 | // We're willing to let this slide. The exception is being caused by the list of |
| 77 | // RemoteViews which is being passed back. The startListening relationship will |
| 78 | // have been established by this point, and we will end up populating the |
| 79 | // widgets upon bind anyway. See issue 14255011 for more context. |
Adam Cohen | 084c318 | 2014-06-16 15:22:56 -0700 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame^] | 83 | @Override |
| 84 | public void stopListening() { |
| 85 | if (FeatureFlags.GO_DISABLE_WIDGETS) { |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | super.stopListening(); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public int allocateAppWidgetId() { |
| 94 | if (FeatureFlags.GO_DISABLE_WIDGETS) { |
| 95 | return AppWidgetManager.INVALID_APPWIDGET_ID; |
| 96 | } |
| 97 | |
| 98 | return super.allocateAppWidgetId(); |
| 99 | } |
| 100 | |
| 101 | public void addProviderChangeListener(ProviderChangedListener callback) { |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 102 | mProviderChangeListeners.add(callback); |
| 103 | } |
| 104 | |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame^] | 105 | public void removeProviderChangeListener(ProviderChangedListener callback) { |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 106 | mProviderChangeListeners.remove(callback); |
| 107 | } |
| 108 | |
Winson Chung | a3f78e3 | 2012-06-14 11:59:51 -0700 | [diff] [blame] | 109 | protected void onProvidersChanged() { |
Sunny Goyal | 7e2a360 | 2015-04-20 18:19:25 -0700 | [diff] [blame] | 110 | if (!mProviderChangeListeners.isEmpty()) { |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame^] | 111 | for (ProviderChangedListener callback : new ArrayList<>(mProviderChangeListeners)) { |
| 112 | callback.notifyWidgetProvidersChanged(); |
Sunny Goyal | 7e2a360 | 2015-04-20 18:19:25 -0700 | [diff] [blame] | 113 | } |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 114 | } |
Winson Chung | a3f78e3 | 2012-06-14 11:59:51 -0700 | [diff] [blame] | 115 | } |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 116 | |
| 117 | public AppWidgetHostView createView(Context context, int appWidgetId, |
| 118 | LauncherAppWidgetProviderInfo appWidget) { |
| 119 | if (appWidget.isCustomWidget) { |
| 120 | LauncherAppWidgetHostView lahv = new LauncherAppWidgetHostView(context); |
| 121 | LayoutInflater inflater = (LayoutInflater) |
| 122 | context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 123 | inflater.inflate(appWidget.initialLayout, lahv); |
| 124 | lahv.setAppWidget(0, appWidget); |
| 125 | lahv.updateLastInflationOrientation(); |
| 126 | return lahv; |
| 127 | } else { |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 128 | try { |
| 129 | return super.createView(context, appWidgetId, appWidget); |
| 130 | } catch (Exception e) { |
| 131 | if (!Utilities.isBinderSizeError(e)) { |
| 132 | throw new RuntimeException(e); |
| 133 | } |
| 134 | |
| 135 | // If the exception was thrown while fetching the remote views, let the view stay. |
| 136 | // This will ensure that if the widget posts a valid update later, the view |
| 137 | // will update. |
| 138 | LauncherAppWidgetHostView view = mViews.get(appWidgetId); |
| 139 | if (view == null) { |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame^] | 140 | view = onCreateView(mContext, appWidgetId, appWidget); |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 141 | } |
| 142 | view.setAppWidget(appWidgetId, appWidget); |
| 143 | view.switchToErrorView(); |
| 144 | return view; |
| 145 | } |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Called when the AppWidget provider for a AppWidget has been upgraded to a new apk. |
| 151 | */ |
| 152 | @Override |
| 153 | protected void onProviderChanged(int appWidgetId, AppWidgetProviderInfo appWidget) { |
| 154 | LauncherAppWidgetProviderInfo info = LauncherAppWidgetProviderInfo.fromProviderInfo( |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame^] | 155 | mContext, appWidget); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 156 | super.onProviderChanged(appWidgetId, info); |
Sunny Goyal | ec88204 | 2015-10-05 10:36:54 -0700 | [diff] [blame] | 157 | // The super method updates the dimensions of the providerInfo. Update the |
| 158 | // launcher spans accordingly. |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame^] | 159 | info.initSpans(mContext); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 160 | } |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 161 | |
| 162 | @Override |
| 163 | public void deleteAppWidgetId(int appWidgetId) { |
| 164 | super.deleteAppWidgetId(appWidgetId); |
| 165 | mViews.remove(appWidgetId); |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | protected void clearViews() { |
| 170 | super.clearViews(); |
| 171 | mViews.clear(); |
| 172 | } |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame^] | 173 | |
| 174 | public void startBindFlow(BaseActivity activity, |
| 175 | int appWidgetId, AppWidgetProviderInfo info, int requestCode) { |
| 176 | |
| 177 | if (FeatureFlags.GO_DISABLE_WIDGETS) { |
| 178 | sendActionCancelled(activity, requestCode); |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND) |
| 183 | .putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId) |
| 184 | .putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.provider) |
| 185 | .putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE, info.getProfile()); |
| 186 | // TODO: we need to make sure that this accounts for the options bundle. |
| 187 | // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options); |
| 188 | activity.startActivityForResult(intent, requestCode); |
| 189 | } |
| 190 | |
| 191 | |
| 192 | public void startConfigActivity(BaseActivity activity, int widgetId, int requestCode) { |
| 193 | if (FeatureFlags.GO_DISABLE_WIDGETS) { |
| 194 | sendActionCancelled(activity, requestCode); |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | try { |
| 199 | startAppWidgetConfigureActivityForResult(activity, widgetId, 0, requestCode, null); |
| 200 | } catch (ActivityNotFoundException | SecurityException e) { |
| 201 | Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); |
| 202 | sendActionCancelled(activity, requestCode); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | private void sendActionCancelled(final BaseActivity activity, final int requestCode) { |
| 207 | new Handler().post(new Runnable() { |
| 208 | @Override |
| 209 | public void run() { |
| 210 | activity.onActivityResult(requestCode, Activity.RESULT_CANCELED, null); |
| 211 | } |
| 212 | }); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Listener for getting notifications on provider changes. |
| 217 | */ |
| 218 | public interface ProviderChangedListener { |
| 219 | |
| 220 | void notifyWidgetProvidersChanged(); |
| 221 | } |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 222 | } |