blob: 9921f76f0c3f99e345c6fe7966954b21cbcd0eca [file] [log] [blame]
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001/*
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 Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070018
Sunny Goyal67419a12017-11-14 16:55:22 -080019import static android.app.Activity.RESULT_CANCELED;
20
The Android Open Source Project7376fae2009-03-11 12:11:58 -070021import android.appwidget.AppWidgetHost;
22import android.appwidget.AppWidgetHostView;
Sunny Goyal64a75aa2017-07-03 13:50:52 -070023import android.appwidget.AppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070024import android.appwidget.AppWidgetProviderInfo;
Sunny Goyal64a75aa2017-07-03 13:50:52 -070025import android.content.ActivityNotFoundException;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070026import android.content.Context;
Sunny Goyal64a75aa2017-07-03 13:50:52 -070027import android.content.Intent;
28import android.os.Handler;
Sunny Goyal712ee532016-11-04 10:19:58 -070029import android.util.SparseArray;
Sunny Goyal64a75aa2017-07-03 13:50:52 -070030import android.widget.Toast;
31
Sunny Goyal3e3d7592019-09-11 16:51:50 -070032import com.android.launcher3.model.WidgetsModel;
Sunny Goyal29947f02017-12-18 13:49:44 -080033import com.android.launcher3.widget.DeferredAppWidgetHostView;
34import com.android.launcher3.widget.LauncherAppWidgetHostView;
Samuel Fufa9151c012020-02-24 17:06:28 -080035import com.android.launcher3.widget.PendingAppWidgetHostView;
Pinyao Tingc7a6c292019-08-26 14:36:02 -070036import com.android.launcher3.widget.custom.CustomWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070037
Sunny Goyal0fc1be12014-08-11 17:05:23 -070038import java.util.ArrayList;
Samuel Fufa44043ec2019-08-26 15:00:30 -070039import java.util.function.IntConsumer;
Sunny Goyal0fc1be12014-08-11 17:05:23 -070040
Adam Cohen59400422014-03-05 18:07:04 -080041
The Android Open Source Project7376fae2009-03-11 12:11:58 -070042/**
43 * Specific {@link AppWidgetHost} that creates our {@link LauncherAppWidgetHostView}
44 * which correctly captures all long-press events. This ensures that users can
45 * always pick up and move widgets.
46 */
47public class LauncherAppWidgetHost extends AppWidgetHost {
Winson Chunga3f78e32012-06-14 11:59:51 -070048
Sunny Goyal67419a12017-11-14 16:55:22 -080049 private static final int FLAG_LISTENING = 1;
50 private static final int FLAG_RESUMED = 1 << 1;
51 private static final int FLAG_LISTEN_IF_RESUMED = 1 << 2;
52
Sunny Goyal64a75aa2017-07-03 13:50:52 -070053 public static final int APPWIDGET_HOST_ID = 1024;
54
55 private final ArrayList<ProviderChangedListener> mProviderChangeListeners = new ArrayList<>();
Sunny Goyal712ee532016-11-04 10:19:58 -070056 private final SparseArray<LauncherAppWidgetHostView> mViews = new SparseArray<>();
Samuel Fufa9151c012020-02-24 17:06:28 -080057 private final SparseArray<PendingAppWidgetHostView> mPendingViews = new SparseArray<>();
Sunny Goyal0fc1be12014-08-11 17:05:23 -070058
Sunny Goyal64a75aa2017-07-03 13:50:52 -070059 private final Context mContext;
Sunny Goyal67419a12017-11-14 16:55:22 -080060 private int mFlags = FLAG_RESUMED;
Winson Chunga3f78e32012-06-14 11:59:51 -070061
Samuel Fufa44043ec2019-08-26 15:00:30 -070062 private IntConsumer mAppWidgetRemovedCallback = null;
63
Samuel Fufa9151c012020-02-24 17:06:28 -080064
Sunny Goyal64a75aa2017-07-03 13:50:52 -070065 public LauncherAppWidgetHost(Context context) {
Samuel Fufa44043ec2019-08-26 15:00:30 -070066 this(context, null);
67 }
68
69 public LauncherAppWidgetHost(Context context,
70 IntConsumer appWidgetRemovedCallback) {
Sunny Goyal64a75aa2017-07-03 13:50:52 -070071 super(context, APPWIDGET_HOST_ID);
72 mContext = context;
Samuel Fufa44043ec2019-08-26 15:00:30 -070073 mAppWidgetRemovedCallback = appWidgetRemovedCallback;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070074 }
Adam Cohen9415d872010-09-13 14:49:43 -070075
The Android Open Source Project7376fae2009-03-11 12:11:58 -070076 @Override
Sunny Goyal712ee532016-11-04 10:19:58 -070077 protected LauncherAppWidgetHostView onCreateView(Context context, int appWidgetId,
The Android Open Source Project7376fae2009-03-11 12:11:58 -070078 AppWidgetProviderInfo appWidget) {
Samuel Fufa9151c012020-02-24 17:06:28 -080079 final LauncherAppWidgetHostView view;
80 if (mPendingViews.get(appWidgetId) != null) {
81 view = mPendingViews.get(appWidgetId);
82 mPendingViews.remove(appWidgetId);
83 } else {
84 view = new LauncherAppWidgetHostView(context);
85 }
Sunny Goyal712ee532016-11-04 10:19:58 -070086 mViews.put(appWidgetId, view);
87 return view;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070088 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -080089
90 @Override
Adam Cohen084c3182014-06-16 15:22:56 -070091 public void startListening() {
Sunny Goyal3e3d7592019-09-11 16:51:50 -070092 if (WidgetsModel.GO_DISABLE_WIDGETS) {
Sunny Goyal64a75aa2017-07-03 13:50:52 -070093 return;
94 }
Sunny Goyal67419a12017-11-14 16:55:22 -080095 mFlags |= FLAG_LISTENING;
Adam Cohen084c3182014-06-16 15:22:56 -070096 try {
97 super.startListening();
98 } catch (Exception e) {
Sunny Goyal712ee532016-11-04 10:19:58 -070099 if (!Utilities.isBinderSizeError(e)) {
Adam Cohen084c3182014-06-16 15:22:56 -0700100 throw new RuntimeException(e);
101 }
Sunny Goyal712ee532016-11-04 10:19:58 -0700102 // We're willing to let this slide. The exception is being caused by the list of
103 // RemoteViews which is being passed back. The startListening relationship will
104 // have been established by this point, and we will end up populating the
105 // widgets upon bind anyway. See issue 14255011 for more context.
Adam Cohen084c3182014-06-16 15:22:56 -0700106 }
Sunny Goyal29947f02017-12-18 13:49:44 -0800107
108 // We go in reverse order and inflate any deferred widget
109 for (int i = mViews.size() - 1; i >= 0; i--) {
110 LauncherAppWidgetHostView view = mViews.valueAt(i);
111 if (view instanceof DeferredAppWidgetHostView) {
112 view.reInflate();
113 }
114 }
Adam Cohen084c3182014-06-16 15:22:56 -0700115 }
116
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700117 @Override
118 public void stopListening() {
Sunny Goyal3e3d7592019-09-11 16:51:50 -0700119 if (WidgetsModel.GO_DISABLE_WIDGETS) {
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700120 return;
121 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800122 mFlags &= ~FLAG_LISTENING;
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700123 super.stopListening();
124 }
125
Winson Chungef528762019-09-06 12:05:52 -0700126 public boolean isListening() {
127 return (mFlags & FLAG_LISTENING) != 0;
128 }
129
Sunny Goyal67419a12017-11-14 16:55:22 -0800130 /**
131 * Updates the resumed state of the host.
132 * When a host is not resumed, it defers calls to startListening until host is resumed again.
133 * But if the host was already listening, it will not call stopListening.
134 *
135 * @see #setListenIfResumed(boolean)
136 */
137 public void setResumed(boolean isResumed) {
138 if (isResumed == ((mFlags & FLAG_RESUMED) != 0)) {
139 return;
140 }
141 if (isResumed) {
142 mFlags |= FLAG_RESUMED;
143 // Start listening if we were supposed to start listening on resume
144 if ((mFlags & FLAG_LISTEN_IF_RESUMED) != 0 && (mFlags & FLAG_LISTENING) == 0) {
145 startListening();
146 }
147 } else {
148 mFlags &= ~FLAG_RESUMED;
149 }
150 }
151
152 /**
153 * Updates the listening state of the host. If the host is not resumed, startListening is
154 * deferred until next resume.
155 *
156 * @see #setResumed(boolean)
157 */
158 public void setListenIfResumed(boolean listenIfResumed) {
Sunny Goyal67419a12017-11-14 16:55:22 -0800159 if (listenIfResumed == ((mFlags & FLAG_LISTEN_IF_RESUMED) != 0)) {
160 return;
161 }
162 if (listenIfResumed) {
163 mFlags |= FLAG_LISTEN_IF_RESUMED;
164 if ((mFlags & FLAG_RESUMED) != 0) {
165 // If we are resumed, start listening immediately. Note we do not check for
166 // duplicate calls before calling startListening as startListening is safe to call
167 // multiple times.
168 startListening();
169 }
170 } else {
171 mFlags &= ~FLAG_LISTEN_IF_RESUMED;
172 stopListening();
173 }
174 }
175
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700176 @Override
177 public int allocateAppWidgetId() {
Sunny Goyal3e3d7592019-09-11 16:51:50 -0700178 if (WidgetsModel.GO_DISABLE_WIDGETS) {
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700179 return AppWidgetManager.INVALID_APPWIDGET_ID;
180 }
181
182 return super.allocateAppWidgetId();
183 }
184
185 public void addProviderChangeListener(ProviderChangedListener callback) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700186 mProviderChangeListeners.add(callback);
187 }
188
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700189 public void removeProviderChangeListener(ProviderChangedListener callback) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700190 mProviderChangeListeners.remove(callback);
191 }
192
Winson Chunga3f78e32012-06-14 11:59:51 -0700193 protected void onProvidersChanged() {
Sunny Goyal7e2a3602015-04-20 18:19:25 -0700194 if (!mProviderChangeListeners.isEmpty()) {
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700195 for (ProviderChangedListener callback : new ArrayList<>(mProviderChangeListeners)) {
196 callback.notifyWidgetProvidersChanged();
Sunny Goyal7e2a3602015-04-20 18:19:25 -0700197 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700198 }
Winson Chunga3f78e32012-06-14 11:59:51 -0700199 }
Adam Cohen59400422014-03-05 18:07:04 -0800200
Samuel Fufa9151c012020-02-24 17:06:28 -0800201 void addPendingView(int appWidgetId, PendingAppWidgetHostView view) {
202 mPendingViews.put(appWidgetId, view);
203 }
204
Adam Cohen59400422014-03-05 18:07:04 -0800205 public AppWidgetHostView createView(Context context, int appWidgetId,
206 LauncherAppWidgetProviderInfo appWidget) {
Sunny Goyal952e63d2017-08-16 04:59:08 -0700207 if (appWidget.isCustomWidget()) {
Adam Cohen59400422014-03-05 18:07:04 -0800208 LauncherAppWidgetHostView lahv = new LauncherAppWidgetHostView(context);
Adam Cohen59400422014-03-05 18:07:04 -0800209 lahv.setAppWidget(0, appWidget);
Pinyao Tingc7a6c292019-08-26 14:36:02 -0700210 CustomWidgetManager.INSTANCE.get(context).onViewCreated(lahv);
Adam Cohen59400422014-03-05 18:07:04 -0800211 return lahv;
Sunny Goyal29947f02017-12-18 13:49:44 -0800212 } else if ((mFlags & FLAG_LISTENING) == 0) {
213 DeferredAppWidgetHostView view = new DeferredAppWidgetHostView(context);
214 view.setAppWidget(appWidgetId, appWidget);
215 mViews.put(appWidgetId, view);
216 return view;
Adam Cohen59400422014-03-05 18:07:04 -0800217 } else {
Sunny Goyal712ee532016-11-04 10:19:58 -0700218 try {
219 return super.createView(context, appWidgetId, appWidget);
220 } catch (Exception e) {
221 if (!Utilities.isBinderSizeError(e)) {
222 throw new RuntimeException(e);
223 }
224
225 // If the exception was thrown while fetching the remote views, let the view stay.
226 // This will ensure that if the widget posts a valid update later, the view
227 // will update.
228 LauncherAppWidgetHostView view = mViews.get(appWidgetId);
229 if (view == null) {
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700230 view = onCreateView(mContext, appWidgetId, appWidget);
Sunny Goyal712ee532016-11-04 10:19:58 -0700231 }
232 view.setAppWidget(appWidgetId, appWidget);
233 view.switchToErrorView();
Samuel Fufa44043ec2019-08-26 15:00:30 -0700234 return view;
Sunny Goyal712ee532016-11-04 10:19:58 -0700235 }
Adam Cohen59400422014-03-05 18:07:04 -0800236 }
237 }
238
239 /**
240 * Called when the AppWidget provider for a AppWidget has been upgraded to a new apk.
241 */
242 @Override
243 protected void onProviderChanged(int appWidgetId, AppWidgetProviderInfo appWidget) {
244 LauncherAppWidgetProviderInfo info = LauncherAppWidgetProviderInfo.fromProviderInfo(
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700245 mContext, appWidget);
Adam Cohen59400422014-03-05 18:07:04 -0800246 super.onProviderChanged(appWidgetId, info);
Sunny Goyalec882042015-10-05 10:36:54 -0700247 // The super method updates the dimensions of the providerInfo. Update the
248 // launcher spans accordingly.
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700249 info.initSpans(mContext);
Adam Cohen59400422014-03-05 18:07:04 -0800250 }
Sunny Goyal712ee532016-11-04 10:19:58 -0700251
Samuel Fufa791ed472019-09-19 10:10:34 -0700252 /**
253 * Called on an appWidget is removed for a widgetId
Samuel Fufa9151c012020-02-24 17:06:28 -0800254 *
255 * @param appWidgetId TODO: make this override when SDK is updated
Samuel Fufa791ed472019-09-19 10:10:34 -0700256 */
257 public void onAppWidgetRemoved(int appWidgetId) {
Samuel Fufa44043ec2019-08-26 15:00:30 -0700258 if (mAppWidgetRemovedCallback == null) {
259 return;
260 }
261 mAppWidgetRemovedCallback.accept(appWidgetId);
262 }
263
Sunny Goyal712ee532016-11-04 10:19:58 -0700264 @Override
265 public void deleteAppWidgetId(int appWidgetId) {
266 super.deleteAppWidgetId(appWidgetId);
267 mViews.remove(appWidgetId);
268 }
269
270 @Override
Sunny Goyalc11fac32018-02-27 19:20:15 -0800271 public void clearViews() {
Sunny Goyal712ee532016-11-04 10:19:58 -0700272 super.clearViews();
273 mViews.clear();
274 }
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700275
276 public void startBindFlow(BaseActivity activity,
277 int appWidgetId, AppWidgetProviderInfo info, int requestCode) {
278
Sunny Goyal3e3d7592019-09-11 16:51:50 -0700279 if (WidgetsModel.GO_DISABLE_WIDGETS) {
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700280 sendActionCancelled(activity, requestCode);
281 return;
282 }
283
284 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND)
285 .putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
286 .putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.provider)
287 .putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE, info.getProfile());
288 // TODO: we need to make sure that this accounts for the options bundle.
289 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
290 activity.startActivityForResult(intent, requestCode);
291 }
292
293
294 public void startConfigActivity(BaseActivity activity, int widgetId, int requestCode) {
Sunny Goyal3e3d7592019-09-11 16:51:50 -0700295 if (WidgetsModel.GO_DISABLE_WIDGETS) {
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700296 sendActionCancelled(activity, requestCode);
297 return;
298 }
299
300 try {
301 startAppWidgetConfigureActivityForResult(activity, widgetId, 0, requestCode, null);
302 } catch (ActivityNotFoundException | SecurityException e) {
303 Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
304 sendActionCancelled(activity, requestCode);
305 }
306 }
307
308 private void sendActionCancelled(final BaseActivity activity, final int requestCode) {
Sunny Goyal67419a12017-11-14 16:55:22 -0800309 new Handler().post(() -> activity.onActivityResult(requestCode, RESULT_CANCELED, null));
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700310 }
311
312 /**
313 * Listener for getting notifications on provider changes.
314 */
315 public interface ProviderChangedListener {
316
317 void notifyWidgetProvidersChanged();
318 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700319}