blob: 64ec730079c119e9e171aeab2a9c2c25e37210d5 [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;
Adam Cohen59400422014-03-05 18:07:04 -080030import android.view.LayoutInflater;
Sunny Goyal64a75aa2017-07-03 13:50:52 -070031import android.widget.Toast;
32
33import com.android.launcher3.config.FeatureFlags;
Sunny Goyal29947f02017-12-18 13:49:44 -080034import com.android.launcher3.widget.DeferredAppWidgetHostView;
35import com.android.launcher3.widget.LauncherAppWidgetHostView;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070036
Sunny Goyal0fc1be12014-08-11 17:05:23 -070037import java.util.ArrayList;
Samuel Fufa44043ec2019-08-26 15:00:30 -070038import java.util.function.IntConsumer;
Sunny Goyal0fc1be12014-08-11 17:05:23 -070039
Adam Cohen59400422014-03-05 18:07:04 -080040
The Android Open Source Project7376fae2009-03-11 12:11:58 -070041/**
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 */
46public class LauncherAppWidgetHost extends AppWidgetHost {
Winson Chunga3f78e32012-06-14 11:59:51 -070047
Sunny Goyal67419a12017-11-14 16:55:22 -080048 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 Goyal64a75aa2017-07-03 13:50:52 -070052 public static final int APPWIDGET_HOST_ID = 1024;
53
54 private final ArrayList<ProviderChangedListener> mProviderChangeListeners = new ArrayList<>();
Sunny Goyal712ee532016-11-04 10:19:58 -070055 private final SparseArray<LauncherAppWidgetHostView> mViews = new SparseArray<>();
Sunny Goyal0fc1be12014-08-11 17:05:23 -070056
Sunny Goyal64a75aa2017-07-03 13:50:52 -070057 private final Context mContext;
Sunny Goyal67419a12017-11-14 16:55:22 -080058 private int mFlags = FLAG_RESUMED;
Winson Chunga3f78e32012-06-14 11:59:51 -070059
Samuel Fufa44043ec2019-08-26 15:00:30 -070060 private IntConsumer mAppWidgetRemovedCallback = null;
61
Sunny Goyal64a75aa2017-07-03 13:50:52 -070062 public LauncherAppWidgetHost(Context context) {
Samuel Fufa44043ec2019-08-26 15:00:30 -070063 this(context, null);
64 }
65
66 public LauncherAppWidgetHost(Context context,
67 IntConsumer appWidgetRemovedCallback) {
Sunny Goyal64a75aa2017-07-03 13:50:52 -070068 super(context, APPWIDGET_HOST_ID);
69 mContext = context;
Samuel Fufa44043ec2019-08-26 15:00:30 -070070 mAppWidgetRemovedCallback = appWidgetRemovedCallback;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070071 }
Adam Cohen9415d872010-09-13 14:49:43 -070072
The Android Open Source Project7376fae2009-03-11 12:11:58 -070073 @Override
Sunny Goyal712ee532016-11-04 10:19:58 -070074 protected LauncherAppWidgetHostView onCreateView(Context context, int appWidgetId,
The Android Open Source Project7376fae2009-03-11 12:11:58 -070075 AppWidgetProviderInfo appWidget) {
Sunny Goyal712ee532016-11-04 10:19:58 -070076 LauncherAppWidgetHostView view = new LauncherAppWidgetHostView(context);
77 mViews.put(appWidgetId, view);
78 return view;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070079 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -080080
81 @Override
Adam Cohen084c3182014-06-16 15:22:56 -070082 public void startListening() {
Sunny Goyal64a75aa2017-07-03 13:50:52 -070083 if (FeatureFlags.GO_DISABLE_WIDGETS) {
84 return;
85 }
Sunny Goyal67419a12017-11-14 16:55:22 -080086 mFlags |= FLAG_LISTENING;
Adam Cohen084c3182014-06-16 15:22:56 -070087 try {
88 super.startListening();
89 } catch (Exception e) {
Sunny Goyal712ee532016-11-04 10:19:58 -070090 if (!Utilities.isBinderSizeError(e)) {
Adam Cohen084c3182014-06-16 15:22:56 -070091 throw new RuntimeException(e);
92 }
Sunny Goyal712ee532016-11-04 10:19:58 -070093 // 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 Cohen084c3182014-06-16 15:22:56 -070097 }
Sunny Goyal29947f02017-12-18 13:49:44 -080098
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 Cohen084c3182014-06-16 15:22:56 -0700106 }
107
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700108 @Override
109 public void stopListening() {
110 if (FeatureFlags.GO_DISABLE_WIDGETS) {
111 return;
112 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800113 mFlags &= ~FLAG_LISTENING;
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700114 super.stopListening();
115 }
116
Winson Chungef528762019-09-06 12:05:52 -0700117 public boolean isListening() {
118 return (mFlags & FLAG_LISTENING) != 0;
119 }
120
Sunny Goyal67419a12017-11-14 16:55:22 -0800121 /**
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 Goyal67419a12017-11-14 16:55:22 -0800150 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 Goyal64a75aa2017-07-03 13:50:52 -0700167 @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 Goyal0fc1be12014-08-11 17:05:23 -0700177 mProviderChangeListeners.add(callback);
178 }
179
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700180 public void removeProviderChangeListener(ProviderChangedListener callback) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700181 mProviderChangeListeners.remove(callback);
182 }
183
Winson Chunga3f78e32012-06-14 11:59:51 -0700184 protected void onProvidersChanged() {
Sunny Goyal7e2a3602015-04-20 18:19:25 -0700185 if (!mProviderChangeListeners.isEmpty()) {
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700186 for (ProviderChangedListener callback : new ArrayList<>(mProviderChangeListeners)) {
187 callback.notifyWidgetProvidersChanged();
Sunny Goyal7e2a3602015-04-20 18:19:25 -0700188 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700189 }
Winson Chunga3f78e32012-06-14 11:59:51 -0700190 }
Adam Cohen59400422014-03-05 18:07:04 -0800191
192 public AppWidgetHostView createView(Context context, int appWidgetId,
193 LauncherAppWidgetProviderInfo appWidget) {
Sunny Goyal952e63d2017-08-16 04:59:08 -0700194 if (appWidget.isCustomWidget()) {
Adam Cohen59400422014-03-05 18:07:04 -0800195 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 Cohen59400422014-03-05 18:07:04 -0800200 return lahv;
Sunny Goyal29947f02017-12-18 13:49:44 -0800201 } 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 Cohen59400422014-03-05 18:07:04 -0800206 } else {
Sunny Goyal712ee532016-11-04 10:19:58 -0700207 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 Goyal64a75aa2017-07-03 13:50:52 -0700219 view = onCreateView(mContext, appWidgetId, appWidget);
Sunny Goyal712ee532016-11-04 10:19:58 -0700220 }
221 view.setAppWidget(appWidgetId, appWidget);
222 view.switchToErrorView();
Samuel Fufa44043ec2019-08-26 15:00:30 -0700223 return view;
Sunny Goyal712ee532016-11-04 10:19:58 -0700224 }
Adam Cohen59400422014-03-05 18:07:04 -0800225 }
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 Goyal64a75aa2017-07-03 13:50:52 -0700234 mContext, appWidget);
Adam Cohen59400422014-03-05 18:07:04 -0800235 super.onProviderChanged(appWidgetId, info);
Sunny Goyalec882042015-10-05 10:36:54 -0700236 // The super method updates the dimensions of the providerInfo. Update the
237 // launcher spans accordingly.
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700238 info.initSpans(mContext);
Adam Cohen59400422014-03-05 18:07:04 -0800239 }
Sunny Goyal712ee532016-11-04 10:19:58 -0700240
Samuel Fufa791ed472019-09-19 10:10:34 -0700241 /**
242 * Called on an appWidget is removed for a widgetId
243 * @param appWidgetId
244 * TODO: make this override when SDK is updated
245 */
246 public void onAppWidgetRemoved(int appWidgetId) {
Samuel Fufa44043ec2019-08-26 15:00:30 -0700247 if (mAppWidgetRemovedCallback == null) {
248 return;
249 }
250 mAppWidgetRemovedCallback.accept(appWidgetId);
251 }
252
Sunny Goyal712ee532016-11-04 10:19:58 -0700253 @Override
254 public void deleteAppWidgetId(int appWidgetId) {
255 super.deleteAppWidgetId(appWidgetId);
256 mViews.remove(appWidgetId);
257 }
258
259 @Override
Sunny Goyalc11fac32018-02-27 19:20:15 -0800260 public void clearViews() {
Sunny Goyal712ee532016-11-04 10:19:58 -0700261 super.clearViews();
262 mViews.clear();
263 }
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700264
265 public void startBindFlow(BaseActivity activity,
266 int appWidgetId, AppWidgetProviderInfo info, int requestCode) {
267
268 if (FeatureFlags.GO_DISABLE_WIDGETS) {
269 sendActionCancelled(activity, requestCode);
270 return;
271 }
272
273 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND)
274 .putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
275 .putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.provider)
276 .putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE, info.getProfile());
277 // TODO: we need to make sure that this accounts for the options bundle.
278 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
279 activity.startActivityForResult(intent, requestCode);
280 }
281
282
283 public void startConfigActivity(BaseActivity activity, int widgetId, int requestCode) {
284 if (FeatureFlags.GO_DISABLE_WIDGETS) {
285 sendActionCancelled(activity, requestCode);
286 return;
287 }
288
289 try {
290 startAppWidgetConfigureActivityForResult(activity, widgetId, 0, requestCode, null);
291 } catch (ActivityNotFoundException | SecurityException e) {
292 Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
293 sendActionCancelled(activity, requestCode);
294 }
295 }
296
297 private void sendActionCancelled(final BaseActivity activity, final int requestCode) {
Sunny Goyal67419a12017-11-14 16:55:22 -0800298 new Handler().post(() -> activity.onActivityResult(requestCode, RESULT_CANCELED, null));
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700299 }
300
301 /**
302 * Listener for getting notifications on provider changes.
303 */
304 public interface ProviderChangedListener {
305
306 void notifyWidgetProvidersChanged();
307 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700308}