blob: cfa4d733fdca5dea83a65b09b18100b7ce18989d [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;
Pinyao Tingc7a6c292019-08-26 14:36:02 -070035import com.android.launcher3.widget.custom.CustomWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070036
Sunny Goyal0fc1be12014-08-11 17:05:23 -070037import java.util.ArrayList;
38
Adam Cohen59400422014-03-05 18:07:04 -080039
The Android Open Source Project7376fae2009-03-11 12:11:58 -070040/**
41 * Specific {@link AppWidgetHost} that creates our {@link LauncherAppWidgetHostView}
42 * which correctly captures all long-press events. This ensures that users can
43 * always pick up and move widgets.
44 */
45public class LauncherAppWidgetHost extends AppWidgetHost {
Winson Chunga3f78e32012-06-14 11:59:51 -070046
Sunny Goyal67419a12017-11-14 16:55:22 -080047 private static final int FLAG_LISTENING = 1;
48 private static final int FLAG_RESUMED = 1 << 1;
49 private static final int FLAG_LISTEN_IF_RESUMED = 1 << 2;
50
Sunny Goyal64a75aa2017-07-03 13:50:52 -070051 public static final int APPWIDGET_HOST_ID = 1024;
52
53 private final ArrayList<ProviderChangedListener> mProviderChangeListeners = new ArrayList<>();
Sunny Goyal712ee532016-11-04 10:19:58 -070054 private final SparseArray<LauncherAppWidgetHostView> mViews = new SparseArray<>();
Sunny Goyal0fc1be12014-08-11 17:05:23 -070055
Sunny Goyal64a75aa2017-07-03 13:50:52 -070056 private final Context mContext;
Sunny Goyal67419a12017-11-14 16:55:22 -080057 private int mFlags = FLAG_RESUMED;
Winson Chunga3f78e32012-06-14 11:59:51 -070058
Sunny Goyal64a75aa2017-07-03 13:50:52 -070059 public LauncherAppWidgetHost(Context context) {
60 super(context, APPWIDGET_HOST_ID);
61 mContext = context;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070062 }
Adam Cohen9415d872010-09-13 14:49:43 -070063
The Android Open Source Project7376fae2009-03-11 12:11:58 -070064 @Override
Sunny Goyal712ee532016-11-04 10:19:58 -070065 protected LauncherAppWidgetHostView onCreateView(Context context, int appWidgetId,
The Android Open Source Project7376fae2009-03-11 12:11:58 -070066 AppWidgetProviderInfo appWidget) {
Sunny Goyal712ee532016-11-04 10:19:58 -070067 LauncherAppWidgetHostView view = new LauncherAppWidgetHostView(context);
68 mViews.put(appWidgetId, view);
69 return view;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070070 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -080071
72 @Override
Adam Cohen084c3182014-06-16 15:22:56 -070073 public void startListening() {
Sunny Goyal3e3d7592019-09-11 16:51:50 -070074 if (WidgetsModel.GO_DISABLE_WIDGETS) {
Sunny Goyal64a75aa2017-07-03 13:50:52 -070075 return;
76 }
Sunny Goyal67419a12017-11-14 16:55:22 -080077 mFlags |= FLAG_LISTENING;
Adam Cohen084c3182014-06-16 15:22:56 -070078 try {
79 super.startListening();
80 } catch (Exception e) {
Sunny Goyal712ee532016-11-04 10:19:58 -070081 if (!Utilities.isBinderSizeError(e)) {
Adam Cohen084c3182014-06-16 15:22:56 -070082 throw new RuntimeException(e);
83 }
Sunny Goyal712ee532016-11-04 10:19:58 -070084 // We're willing to let this slide. The exception is being caused by the list of
85 // RemoteViews which is being passed back. The startListening relationship will
86 // have been established by this point, and we will end up populating the
87 // widgets upon bind anyway. See issue 14255011 for more context.
Adam Cohen084c3182014-06-16 15:22:56 -070088 }
Sunny Goyal29947f02017-12-18 13:49:44 -080089
90 // We go in reverse order and inflate any deferred widget
91 for (int i = mViews.size() - 1; i >= 0; i--) {
92 LauncherAppWidgetHostView view = mViews.valueAt(i);
93 if (view instanceof DeferredAppWidgetHostView) {
94 view.reInflate();
95 }
96 }
Adam Cohen084c3182014-06-16 15:22:56 -070097 }
98
Sunny Goyal64a75aa2017-07-03 13:50:52 -070099 @Override
100 public void stopListening() {
Sunny Goyal3e3d7592019-09-11 16:51:50 -0700101 if (WidgetsModel.GO_DISABLE_WIDGETS) {
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700102 return;
103 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800104 mFlags &= ~FLAG_LISTENING;
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700105 super.stopListening();
106 }
107
Winson Chungef528762019-09-06 12:05:52 -0700108 public boolean isListening() {
109 return (mFlags & FLAG_LISTENING) != 0;
110 }
111
Sunny Goyal67419a12017-11-14 16:55:22 -0800112 /**
113 * Updates the resumed state of the host.
114 * When a host is not resumed, it defers calls to startListening until host is resumed again.
115 * But if the host was already listening, it will not call stopListening.
116 *
117 * @see #setListenIfResumed(boolean)
118 */
119 public void setResumed(boolean isResumed) {
120 if (isResumed == ((mFlags & FLAG_RESUMED) != 0)) {
121 return;
122 }
123 if (isResumed) {
124 mFlags |= FLAG_RESUMED;
125 // Start listening if we were supposed to start listening on resume
126 if ((mFlags & FLAG_LISTEN_IF_RESUMED) != 0 && (mFlags & FLAG_LISTENING) == 0) {
127 startListening();
128 }
129 } else {
130 mFlags &= ~FLAG_RESUMED;
131 }
132 }
133
134 /**
135 * Updates the listening state of the host. If the host is not resumed, startListening is
136 * deferred until next resume.
137 *
138 * @see #setResumed(boolean)
139 */
140 public void setListenIfResumed(boolean listenIfResumed) {
Sunny Goyal67419a12017-11-14 16:55:22 -0800141 if (listenIfResumed == ((mFlags & FLAG_LISTEN_IF_RESUMED) != 0)) {
142 return;
143 }
144 if (listenIfResumed) {
145 mFlags |= FLAG_LISTEN_IF_RESUMED;
146 if ((mFlags & FLAG_RESUMED) != 0) {
147 // If we are resumed, start listening immediately. Note we do not check for
148 // duplicate calls before calling startListening as startListening is safe to call
149 // multiple times.
150 startListening();
151 }
152 } else {
153 mFlags &= ~FLAG_LISTEN_IF_RESUMED;
154 stopListening();
155 }
156 }
157
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700158 @Override
159 public int allocateAppWidgetId() {
Sunny Goyal3e3d7592019-09-11 16:51:50 -0700160 if (WidgetsModel.GO_DISABLE_WIDGETS) {
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700161 return AppWidgetManager.INVALID_APPWIDGET_ID;
162 }
163
164 return super.allocateAppWidgetId();
165 }
166
167 public void addProviderChangeListener(ProviderChangedListener callback) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700168 mProviderChangeListeners.add(callback);
169 }
170
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700171 public void removeProviderChangeListener(ProviderChangedListener callback) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700172 mProviderChangeListeners.remove(callback);
173 }
174
Winson Chunga3f78e32012-06-14 11:59:51 -0700175 protected void onProvidersChanged() {
Sunny Goyal7e2a3602015-04-20 18:19:25 -0700176 if (!mProviderChangeListeners.isEmpty()) {
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700177 for (ProviderChangedListener callback : new ArrayList<>(mProviderChangeListeners)) {
178 callback.notifyWidgetProvidersChanged();
Sunny Goyal7e2a3602015-04-20 18:19:25 -0700179 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700180 }
Winson Chunga3f78e32012-06-14 11:59:51 -0700181 }
Adam Cohen59400422014-03-05 18:07:04 -0800182
183 public AppWidgetHostView createView(Context context, int appWidgetId,
184 LauncherAppWidgetProviderInfo appWidget) {
Sunny Goyal952e63d2017-08-16 04:59:08 -0700185 if (appWidget.isCustomWidget()) {
Adam Cohen59400422014-03-05 18:07:04 -0800186 LauncherAppWidgetHostView lahv = new LauncherAppWidgetHostView(context);
Adam Cohen59400422014-03-05 18:07:04 -0800187 lahv.setAppWidget(0, appWidget);
Pinyao Tingc7a6c292019-08-26 14:36:02 -0700188 CustomWidgetManager.INSTANCE.get(context).onViewCreated(lahv);
Adam Cohen59400422014-03-05 18:07:04 -0800189 return lahv;
Sunny Goyal29947f02017-12-18 13:49:44 -0800190 } else if ((mFlags & FLAG_LISTENING) == 0) {
191 DeferredAppWidgetHostView view = new DeferredAppWidgetHostView(context);
192 view.setAppWidget(appWidgetId, appWidget);
193 mViews.put(appWidgetId, view);
194 return view;
Adam Cohen59400422014-03-05 18:07:04 -0800195 } else {
Sunny Goyal712ee532016-11-04 10:19:58 -0700196 try {
197 return super.createView(context, appWidgetId, appWidget);
198 } catch (Exception e) {
199 if (!Utilities.isBinderSizeError(e)) {
200 throw new RuntimeException(e);
201 }
202
203 // If the exception was thrown while fetching the remote views, let the view stay.
204 // This will ensure that if the widget posts a valid update later, the view
205 // will update.
206 LauncherAppWidgetHostView view = mViews.get(appWidgetId);
207 if (view == null) {
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700208 view = onCreateView(mContext, appWidgetId, appWidget);
Sunny Goyal712ee532016-11-04 10:19:58 -0700209 }
210 view.setAppWidget(appWidgetId, appWidget);
211 view.switchToErrorView();
212 return view;
213 }
Adam Cohen59400422014-03-05 18:07:04 -0800214 }
215 }
216
217 /**
218 * Called when the AppWidget provider for a AppWidget has been upgraded to a new apk.
219 */
220 @Override
221 protected void onProviderChanged(int appWidgetId, AppWidgetProviderInfo appWidget) {
222 LauncherAppWidgetProviderInfo info = LauncherAppWidgetProviderInfo.fromProviderInfo(
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700223 mContext, appWidget);
Adam Cohen59400422014-03-05 18:07:04 -0800224 super.onProviderChanged(appWidgetId, info);
Sunny Goyalec882042015-10-05 10:36:54 -0700225 // The super method updates the dimensions of the providerInfo. Update the
226 // launcher spans accordingly.
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700227 info.initSpans(mContext);
Adam Cohen59400422014-03-05 18:07:04 -0800228 }
Sunny Goyal712ee532016-11-04 10:19:58 -0700229
230 @Override
231 public void deleteAppWidgetId(int appWidgetId) {
232 super.deleteAppWidgetId(appWidgetId);
233 mViews.remove(appWidgetId);
234 }
235
236 @Override
Sunny Goyalc11fac32018-02-27 19:20:15 -0800237 public void clearViews() {
Sunny Goyal712ee532016-11-04 10:19:58 -0700238 super.clearViews();
239 mViews.clear();
240 }
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700241
242 public void startBindFlow(BaseActivity activity,
243 int appWidgetId, AppWidgetProviderInfo info, int requestCode) {
244
Sunny Goyal3e3d7592019-09-11 16:51:50 -0700245 if (WidgetsModel.GO_DISABLE_WIDGETS) {
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700246 sendActionCancelled(activity, requestCode);
247 return;
248 }
249
250 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND)
251 .putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
252 .putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.provider)
253 .putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE, info.getProfile());
254 // TODO: we need to make sure that this accounts for the options bundle.
255 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
256 activity.startActivityForResult(intent, requestCode);
257 }
258
259
260 public void startConfigActivity(BaseActivity activity, int widgetId, int requestCode) {
Sunny Goyal3e3d7592019-09-11 16:51:50 -0700261 if (WidgetsModel.GO_DISABLE_WIDGETS) {
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700262 sendActionCancelled(activity, requestCode);
263 return;
264 }
265
266 try {
267 startAppWidgetConfigureActivityForResult(activity, widgetId, 0, requestCode, null);
268 } catch (ActivityNotFoundException | SecurityException e) {
269 Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
270 sendActionCancelled(activity, requestCode);
271 }
272 }
273
274 private void sendActionCancelled(final BaseActivity activity, final int requestCode) {
Sunny Goyal67419a12017-11-14 16:55:22 -0800275 new Handler().post(() -> activity.onActivityResult(requestCode, RESULT_CANCELED, null));
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700276 }
277
278 /**
279 * Listener for getting notifications on provider changes.
280 */
281 public interface ProviderChangedListener {
282
283 void notifyWidgetProvidersChanged();
284 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700285}