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 | 67419a1 | 2017-11-14 16:55:22 -0800 | [diff] [blame] | 19 | import static android.app.Activity.RESULT_CANCELED; |
| 20 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 21 | import android.appwidget.AppWidgetHost; |
| 22 | import android.appwidget.AppWidgetHostView; |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 23 | import android.appwidget.AppWidgetManager; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 24 | import android.appwidget.AppWidgetProviderInfo; |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 25 | import android.content.ActivityNotFoundException; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 26 | import android.content.Context; |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 27 | import android.content.Intent; |
| 28 | import android.os.Handler; |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 29 | import android.util.SparseArray; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 30 | import android.view.LayoutInflater; |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 31 | import android.widget.Toast; |
| 32 | |
| 33 | import com.android.launcher3.config.FeatureFlags; |
Sunny Goyal | 29947f0 | 2017-12-18 13:49:44 -0800 | [diff] [blame] | 34 | import com.android.launcher3.widget.DeferredAppWidgetHostView; |
| 35 | import com.android.launcher3.widget.LauncherAppWidgetHostView; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 36 | |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 37 | import java.util.ArrayList; |
Samuel Fufa | 44043ec | 2019-08-26 15:00:30 -0700 | [diff] [blame^] | 38 | import java.util.function.IntConsumer; |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 39 | |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 40 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 41 | /** |
| 42 | * Specific {@link AppWidgetHost} that creates our {@link LauncherAppWidgetHostView} |
| 43 | * which correctly captures all long-press events. This ensures that users can |
| 44 | * always pick up and move widgets. |
| 45 | */ |
| 46 | public class LauncherAppWidgetHost extends AppWidgetHost { |
Winson Chung | a3f78e3 | 2012-06-14 11:59:51 -0700 | [diff] [blame] | 47 | |
Sunny Goyal | 67419a1 | 2017-11-14 16:55:22 -0800 | [diff] [blame] | 48 | private static final int FLAG_LISTENING = 1; |
| 49 | private static final int FLAG_RESUMED = 1 << 1; |
| 50 | private static final int FLAG_LISTEN_IF_RESUMED = 1 << 2; |
| 51 | |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 52 | public static final int APPWIDGET_HOST_ID = 1024; |
| 53 | |
| 54 | private final ArrayList<ProviderChangedListener> mProviderChangeListeners = new ArrayList<>(); |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 55 | private final SparseArray<LauncherAppWidgetHostView> mViews = new SparseArray<>(); |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 56 | |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 57 | private final Context mContext; |
Sunny Goyal | 67419a1 | 2017-11-14 16:55:22 -0800 | [diff] [blame] | 58 | private int mFlags = FLAG_RESUMED; |
Winson Chung | a3f78e3 | 2012-06-14 11:59:51 -0700 | [diff] [blame] | 59 | |
Samuel Fufa | 44043ec | 2019-08-26 15:00:30 -0700 | [diff] [blame^] | 60 | private IntConsumer mAppWidgetRemovedCallback = null; |
| 61 | |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 62 | public LauncherAppWidgetHost(Context context) { |
Samuel Fufa | 44043ec | 2019-08-26 15:00:30 -0700 | [diff] [blame^] | 63 | this(context, null); |
| 64 | } |
| 65 | |
| 66 | public LauncherAppWidgetHost(Context context, |
| 67 | IntConsumer appWidgetRemovedCallback) { |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 68 | super(context, APPWIDGET_HOST_ID); |
| 69 | mContext = context; |
Samuel Fufa | 44043ec | 2019-08-26 15:00:30 -0700 | [diff] [blame^] | 70 | mAppWidgetRemovedCallback = appWidgetRemovedCallback; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 71 | } |
Adam Cohen | 9415d87 | 2010-09-13 14:49:43 -0700 | [diff] [blame] | 72 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 73 | @Override |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 74 | protected LauncherAppWidgetHostView onCreateView(Context context, int appWidgetId, |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 75 | AppWidgetProviderInfo appWidget) { |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 76 | LauncherAppWidgetHostView view = new LauncherAppWidgetHostView(context); |
| 77 | mViews.put(appWidgetId, view); |
| 78 | return view; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 79 | } |
Patrick Dubroy | 2313eff | 2011-01-11 20:01:31 -0800 | [diff] [blame] | 80 | |
| 81 | @Override |
Adam Cohen | 084c318 | 2014-06-16 15:22:56 -0700 | [diff] [blame] | 82 | public void startListening() { |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 83 | if (FeatureFlags.GO_DISABLE_WIDGETS) { |
| 84 | return; |
| 85 | } |
Sunny Goyal | 67419a1 | 2017-11-14 16:55:22 -0800 | [diff] [blame] | 86 | mFlags |= FLAG_LISTENING; |
Adam Cohen | 084c318 | 2014-06-16 15:22:56 -0700 | [diff] [blame] | 87 | try { |
| 88 | super.startListening(); |
| 89 | } catch (Exception e) { |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 90 | if (!Utilities.isBinderSizeError(e)) { |
Adam Cohen | 084c318 | 2014-06-16 15:22:56 -0700 | [diff] [blame] | 91 | throw new RuntimeException(e); |
| 92 | } |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 93 | // We're willing to let this slide. The exception is being caused by the list of |
| 94 | // RemoteViews which is being passed back. The startListening relationship will |
| 95 | // have been established by this point, and we will end up populating the |
| 96 | // widgets upon bind anyway. See issue 14255011 for more context. |
Adam Cohen | 084c318 | 2014-06-16 15:22:56 -0700 | [diff] [blame] | 97 | } |
Sunny Goyal | 29947f0 | 2017-12-18 13:49:44 -0800 | [diff] [blame] | 98 | |
| 99 | // We go in reverse order and inflate any deferred widget |
| 100 | for (int i = mViews.size() - 1; i >= 0; i--) { |
| 101 | LauncherAppWidgetHostView view = mViews.valueAt(i); |
| 102 | if (view instanceof DeferredAppWidgetHostView) { |
| 103 | view.reInflate(); |
| 104 | } |
| 105 | } |
Adam Cohen | 084c318 | 2014-06-16 15:22:56 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 108 | @Override |
| 109 | public void stopListening() { |
| 110 | if (FeatureFlags.GO_DISABLE_WIDGETS) { |
| 111 | return; |
| 112 | } |
Sunny Goyal | 67419a1 | 2017-11-14 16:55:22 -0800 | [diff] [blame] | 113 | mFlags &= ~FLAG_LISTENING; |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 114 | super.stopListening(); |
| 115 | } |
| 116 | |
Winson Chung | ef52876 | 2019-09-06 12:05:52 -0700 | [diff] [blame] | 117 | public boolean isListening() { |
| 118 | return (mFlags & FLAG_LISTENING) != 0; |
| 119 | } |
| 120 | |
Sunny Goyal | 67419a1 | 2017-11-14 16:55:22 -0800 | [diff] [blame] | 121 | /** |
| 122 | * Updates the resumed state of the host. |
| 123 | * When a host is not resumed, it defers calls to startListening until host is resumed again. |
| 124 | * But if the host was already listening, it will not call stopListening. |
| 125 | * |
| 126 | * @see #setListenIfResumed(boolean) |
| 127 | */ |
| 128 | public void setResumed(boolean isResumed) { |
| 129 | if (isResumed == ((mFlags & FLAG_RESUMED) != 0)) { |
| 130 | return; |
| 131 | } |
| 132 | if (isResumed) { |
| 133 | mFlags |= FLAG_RESUMED; |
| 134 | // Start listening if we were supposed to start listening on resume |
| 135 | if ((mFlags & FLAG_LISTEN_IF_RESUMED) != 0 && (mFlags & FLAG_LISTENING) == 0) { |
| 136 | startListening(); |
| 137 | } |
| 138 | } else { |
| 139 | mFlags &= ~FLAG_RESUMED; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Updates the listening state of the host. If the host is not resumed, startListening is |
| 145 | * deferred until next resume. |
| 146 | * |
| 147 | * @see #setResumed(boolean) |
| 148 | */ |
| 149 | public void setListenIfResumed(boolean listenIfResumed) { |
Sunny Goyal | 67419a1 | 2017-11-14 16:55:22 -0800 | [diff] [blame] | 150 | if (listenIfResumed == ((mFlags & FLAG_LISTEN_IF_RESUMED) != 0)) { |
| 151 | return; |
| 152 | } |
| 153 | if (listenIfResumed) { |
| 154 | mFlags |= FLAG_LISTEN_IF_RESUMED; |
| 155 | if ((mFlags & FLAG_RESUMED) != 0) { |
| 156 | // If we are resumed, start listening immediately. Note we do not check for |
| 157 | // duplicate calls before calling startListening as startListening is safe to call |
| 158 | // multiple times. |
| 159 | startListening(); |
| 160 | } |
| 161 | } else { |
| 162 | mFlags &= ~FLAG_LISTEN_IF_RESUMED; |
| 163 | stopListening(); |
| 164 | } |
| 165 | } |
| 166 | |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 167 | @Override |
| 168 | public int allocateAppWidgetId() { |
| 169 | if (FeatureFlags.GO_DISABLE_WIDGETS) { |
| 170 | return AppWidgetManager.INVALID_APPWIDGET_ID; |
| 171 | } |
| 172 | |
| 173 | return super.allocateAppWidgetId(); |
| 174 | } |
| 175 | |
| 176 | public void addProviderChangeListener(ProviderChangedListener callback) { |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 177 | mProviderChangeListeners.add(callback); |
| 178 | } |
| 179 | |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 180 | public void removeProviderChangeListener(ProviderChangedListener callback) { |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 181 | mProviderChangeListeners.remove(callback); |
| 182 | } |
| 183 | |
Winson Chung | a3f78e3 | 2012-06-14 11:59:51 -0700 | [diff] [blame] | 184 | protected void onProvidersChanged() { |
Sunny Goyal | 7e2a360 | 2015-04-20 18:19:25 -0700 | [diff] [blame] | 185 | if (!mProviderChangeListeners.isEmpty()) { |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 186 | for (ProviderChangedListener callback : new ArrayList<>(mProviderChangeListeners)) { |
| 187 | callback.notifyWidgetProvidersChanged(); |
Sunny Goyal | 7e2a360 | 2015-04-20 18:19:25 -0700 | [diff] [blame] | 188 | } |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 189 | } |
Winson Chung | a3f78e3 | 2012-06-14 11:59:51 -0700 | [diff] [blame] | 190 | } |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 191 | |
| 192 | public AppWidgetHostView createView(Context context, int appWidgetId, |
| 193 | LauncherAppWidgetProviderInfo appWidget) { |
Sunny Goyal | 952e63d | 2017-08-16 04:59:08 -0700 | [diff] [blame] | 194 | if (appWidget.isCustomWidget()) { |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 195 | LauncherAppWidgetHostView lahv = new LauncherAppWidgetHostView(context); |
| 196 | LayoutInflater inflater = (LayoutInflater) |
| 197 | context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 198 | inflater.inflate(appWidget.initialLayout, lahv); |
| 199 | lahv.setAppWidget(0, appWidget); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 200 | return lahv; |
Sunny Goyal | 29947f0 | 2017-12-18 13:49:44 -0800 | [diff] [blame] | 201 | } else if ((mFlags & FLAG_LISTENING) == 0) { |
| 202 | DeferredAppWidgetHostView view = new DeferredAppWidgetHostView(context); |
| 203 | view.setAppWidget(appWidgetId, appWidget); |
| 204 | mViews.put(appWidgetId, view); |
| 205 | return view; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 206 | } else { |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 207 | try { |
| 208 | return super.createView(context, appWidgetId, appWidget); |
| 209 | } catch (Exception e) { |
| 210 | if (!Utilities.isBinderSizeError(e)) { |
| 211 | throw new RuntimeException(e); |
| 212 | } |
| 213 | |
| 214 | // If the exception was thrown while fetching the remote views, let the view stay. |
| 215 | // This will ensure that if the widget posts a valid update later, the view |
| 216 | // will update. |
| 217 | LauncherAppWidgetHostView view = mViews.get(appWidgetId); |
| 218 | if (view == null) { |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 219 | view = onCreateView(mContext, appWidgetId, appWidget); |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 220 | } |
| 221 | view.setAppWidget(appWidgetId, appWidget); |
| 222 | view.switchToErrorView(); |
Samuel Fufa | 44043ec | 2019-08-26 15:00:30 -0700 | [diff] [blame^] | 223 | return view; |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 224 | } |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Called when the AppWidget provider for a AppWidget has been upgraded to a new apk. |
| 230 | */ |
| 231 | @Override |
| 232 | protected void onProviderChanged(int appWidgetId, AppWidgetProviderInfo appWidget) { |
| 233 | LauncherAppWidgetProviderInfo info = LauncherAppWidgetProviderInfo.fromProviderInfo( |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 234 | mContext, appWidget); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 235 | super.onProviderChanged(appWidgetId, info); |
Sunny Goyal | ec88204 | 2015-10-05 10:36:54 -0700 | [diff] [blame] | 236 | // The super method updates the dimensions of the providerInfo. Update the |
| 237 | // launcher spans accordingly. |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 238 | info.initSpans(mContext); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 239 | } |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 240 | |
Samuel Fufa | 44043ec | 2019-08-26 15:00:30 -0700 | [diff] [blame^] | 241 | //TODO: make this override when SDK is updated |
| 242 | //@Override |
| 243 | protected void onAppWidgetRemoved(int appWidgetId) { |
| 244 | if (mAppWidgetRemovedCallback == null) { |
| 245 | return; |
| 246 | } |
| 247 | mAppWidgetRemovedCallback.accept(appWidgetId); |
| 248 | } |
| 249 | |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 250 | @Override |
| 251 | public void deleteAppWidgetId(int appWidgetId) { |
| 252 | super.deleteAppWidgetId(appWidgetId); |
| 253 | mViews.remove(appWidgetId); |
| 254 | } |
| 255 | |
| 256 | @Override |
Sunny Goyal | c11fac3 | 2018-02-27 19:20:15 -0800 | [diff] [blame] | 257 | public void clearViews() { |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 258 | super.clearViews(); |
| 259 | mViews.clear(); |
| 260 | } |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 261 | |
| 262 | public void startBindFlow(BaseActivity activity, |
| 263 | int appWidgetId, AppWidgetProviderInfo info, int requestCode) { |
| 264 | |
| 265 | if (FeatureFlags.GO_DISABLE_WIDGETS) { |
| 266 | sendActionCancelled(activity, requestCode); |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND) |
| 271 | .putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId) |
| 272 | .putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.provider) |
| 273 | .putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE, info.getProfile()); |
| 274 | // TODO: we need to make sure that this accounts for the options bundle. |
| 275 | // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options); |
| 276 | activity.startActivityForResult(intent, requestCode); |
| 277 | } |
| 278 | |
| 279 | |
| 280 | public void startConfigActivity(BaseActivity activity, int widgetId, int requestCode) { |
| 281 | if (FeatureFlags.GO_DISABLE_WIDGETS) { |
| 282 | sendActionCancelled(activity, requestCode); |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | try { |
| 287 | startAppWidgetConfigureActivityForResult(activity, widgetId, 0, requestCode, null); |
| 288 | } catch (ActivityNotFoundException | SecurityException e) { |
| 289 | Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); |
| 290 | sendActionCancelled(activity, requestCode); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | private void sendActionCancelled(final BaseActivity activity, final int requestCode) { |
Sunny Goyal | 67419a1 | 2017-11-14 16:55:22 -0800 | [diff] [blame] | 295 | new Handler().post(() -> activity.onActivityResult(requestCode, RESULT_CANCELED, null)); |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Listener for getting notifications on provider changes. |
| 300 | */ |
| 301 | public interface ProviderChangedListener { |
| 302 | |
| 303 | void notifyWidgetProvidersChanged(); |
| 304 | } |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 305 | } |