blob: 56671a1582584d4486bf0529014a2b57f3a80616 [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 Goyal29947f02017-12-18 13:49:44 -080029import android.util.Log;
Sunny Goyal712ee532016-11-04 10:19:58 -070030import android.util.SparseArray;
Adam Cohen59400422014-03-05 18:07:04 -080031import android.view.LayoutInflater;
Sunny Goyal64a75aa2017-07-03 13:50:52 -070032import android.widget.Toast;
33
34import com.android.launcher3.config.FeatureFlags;
Sunny Goyal29947f02017-12-18 13:49:44 -080035import com.android.launcher3.widget.DeferredAppWidgetHostView;
36import com.android.launcher3.widget.LauncherAppWidgetHostView;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070037
Sunny Goyal0fc1be12014-08-11 17:05:23 -070038import java.util.ArrayList;
39
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
Sunny Goyal64a75aa2017-07-03 13:50:52 -070060 public LauncherAppWidgetHost(Context context) {
61 super(context, APPWIDGET_HOST_ID);
62 mContext = context;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070063 }
Adam Cohen9415d872010-09-13 14:49:43 -070064
The Android Open Source Project7376fae2009-03-11 12:11:58 -070065 @Override
Sunny Goyal712ee532016-11-04 10:19:58 -070066 protected LauncherAppWidgetHostView onCreateView(Context context, int appWidgetId,
The Android Open Source Project7376fae2009-03-11 12:11:58 -070067 AppWidgetProviderInfo appWidget) {
Sunny Goyal712ee532016-11-04 10:19:58 -070068 LauncherAppWidgetHostView view = new LauncherAppWidgetHostView(context);
69 mViews.put(appWidgetId, view);
70 return view;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070071 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -080072
73 @Override
Adam Cohen084c3182014-06-16 15:22:56 -070074 public void startListening() {
Sunny Goyal64a75aa2017-07-03 13:50:52 -070075 if (FeatureFlags.GO_DISABLE_WIDGETS) {
76 return;
77 }
Sunny Goyal67419a12017-11-14 16:55:22 -080078 mFlags |= FLAG_LISTENING;
Adam Cohen084c3182014-06-16 15:22:56 -070079 try {
80 super.startListening();
81 } catch (Exception e) {
Sunny Goyal712ee532016-11-04 10:19:58 -070082 if (!Utilities.isBinderSizeError(e)) {
Adam Cohen084c3182014-06-16 15:22:56 -070083 throw new RuntimeException(e);
84 }
Sunny Goyal712ee532016-11-04 10:19:58 -070085 // We're willing to let this slide. The exception is being caused by the list of
86 // RemoteViews which is being passed back. The startListening relationship will
87 // have been established by this point, and we will end up populating the
88 // widgets upon bind anyway. See issue 14255011 for more context.
Adam Cohen084c3182014-06-16 15:22:56 -070089 }
Sunny Goyal29947f02017-12-18 13:49:44 -080090
91 // We go in reverse order and inflate any deferred widget
92 for (int i = mViews.size() - 1; i >= 0; i--) {
93 LauncherAppWidgetHostView view = mViews.valueAt(i);
94 if (view instanceof DeferredAppWidgetHostView) {
95 view.reInflate();
96 }
97 }
Adam Cohen084c3182014-06-16 15:22:56 -070098 }
99
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700100 @Override
101 public void stopListening() {
102 if (FeatureFlags.GO_DISABLE_WIDGETS) {
103 return;
104 }
Sunny Goyal67419a12017-11-14 16:55:22 -0800105 mFlags &= ~FLAG_LISTENING;
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700106 super.stopListening();
107 }
108
Sunny Goyal67419a12017-11-14 16:55:22 -0800109 /**
110 * Updates the resumed state of the host.
111 * When a host is not resumed, it defers calls to startListening until host is resumed again.
112 * But if the host was already listening, it will not call stopListening.
113 *
114 * @see #setListenIfResumed(boolean)
115 */
116 public void setResumed(boolean isResumed) {
117 if (isResumed == ((mFlags & FLAG_RESUMED) != 0)) {
118 return;
119 }
120 if (isResumed) {
121 mFlags |= FLAG_RESUMED;
122 // Start listening if we were supposed to start listening on resume
123 if ((mFlags & FLAG_LISTEN_IF_RESUMED) != 0 && (mFlags & FLAG_LISTENING) == 0) {
124 startListening();
125 }
126 } else {
127 mFlags &= ~FLAG_RESUMED;
128 }
129 }
130
131 /**
132 * Updates the listening state of the host. If the host is not resumed, startListening is
133 * deferred until next resume.
134 *
135 * @see #setResumed(boolean)
136 */
137 public void setListenIfResumed(boolean listenIfResumed) {
138 if (!Utilities.ATLEAST_NOUGAT_MR1) {
139 return;
140 }
141 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() {
160 if (FeatureFlags.GO_DISABLE_WIDGETS) {
161 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);
187 LayoutInflater inflater = (LayoutInflater)
188 context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
189 inflater.inflate(appWidget.initialLayout, lahv);
190 lahv.setAppWidget(0, appWidget);
Adam Cohen59400422014-03-05 18:07:04 -0800191 return lahv;
Sunny Goyal29947f02017-12-18 13:49:44 -0800192 } else if ((mFlags & FLAG_LISTENING) == 0) {
193 DeferredAppWidgetHostView view = new DeferredAppWidgetHostView(context);
194 view.setAppWidget(appWidgetId, appWidget);
195 mViews.put(appWidgetId, view);
196 return view;
Adam Cohen59400422014-03-05 18:07:04 -0800197 } else {
Sunny Goyal712ee532016-11-04 10:19:58 -0700198 try {
199 return super.createView(context, appWidgetId, appWidget);
200 } catch (Exception e) {
201 if (!Utilities.isBinderSizeError(e)) {
202 throw new RuntimeException(e);
203 }
204
205 // If the exception was thrown while fetching the remote views, let the view stay.
206 // This will ensure that if the widget posts a valid update later, the view
207 // will update.
208 LauncherAppWidgetHostView view = mViews.get(appWidgetId);
209 if (view == null) {
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700210 view = onCreateView(mContext, appWidgetId, appWidget);
Sunny Goyal712ee532016-11-04 10:19:58 -0700211 }
212 view.setAppWidget(appWidgetId, appWidget);
213 view.switchToErrorView();
214 return view;
215 }
Adam Cohen59400422014-03-05 18:07:04 -0800216 }
217 }
218
219 /**
220 * Called when the AppWidget provider for a AppWidget has been upgraded to a new apk.
221 */
222 @Override
223 protected void onProviderChanged(int appWidgetId, AppWidgetProviderInfo appWidget) {
224 LauncherAppWidgetProviderInfo info = LauncherAppWidgetProviderInfo.fromProviderInfo(
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700225 mContext, appWidget);
Adam Cohen59400422014-03-05 18:07:04 -0800226 super.onProviderChanged(appWidgetId, info);
Sunny Goyalec882042015-10-05 10:36:54 -0700227 // The super method updates the dimensions of the providerInfo. Update the
228 // launcher spans accordingly.
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700229 info.initSpans(mContext);
Adam Cohen59400422014-03-05 18:07:04 -0800230 }
Sunny Goyal712ee532016-11-04 10:19:58 -0700231
232 @Override
233 public void deleteAppWidgetId(int appWidgetId) {
234 super.deleteAppWidgetId(appWidgetId);
235 mViews.remove(appWidgetId);
236 }
237
238 @Override
Sunny Goyalc11fac32018-02-27 19:20:15 -0800239 public void clearViews() {
Sunny Goyal712ee532016-11-04 10:19:58 -0700240 super.clearViews();
241 mViews.clear();
242 }
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700243
244 public void startBindFlow(BaseActivity activity,
245 int appWidgetId, AppWidgetProviderInfo info, int requestCode) {
246
247 if (FeatureFlags.GO_DISABLE_WIDGETS) {
248 sendActionCancelled(activity, requestCode);
249 return;
250 }
251
252 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND)
253 .putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
254 .putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.provider)
255 .putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE, info.getProfile());
256 // TODO: we need to make sure that this accounts for the options bundle.
257 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
258 activity.startActivityForResult(intent, requestCode);
259 }
260
261
262 public void startConfigActivity(BaseActivity activity, int widgetId, int requestCode) {
263 if (FeatureFlags.GO_DISABLE_WIDGETS) {
264 sendActionCancelled(activity, requestCode);
265 return;
266 }
267
268 try {
269 startAppWidgetConfigureActivityForResult(activity, widgetId, 0, requestCode, null);
270 } catch (ActivityNotFoundException | SecurityException e) {
271 Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
272 sendActionCancelled(activity, requestCode);
273 }
274 }
275
276 private void sendActionCancelled(final BaseActivity activity, final int requestCode) {
Sunny Goyal67419a12017-11-14 16:55:22 -0800277 new Handler().post(() -> activity.onActivityResult(requestCode, RESULT_CANCELED, null));
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700278 }
279
280 /**
281 * Listener for getting notifications on provider changes.
282 */
283 public interface ProviderChangedListener {
284
285 void notifyWidgetProvidersChanged();
286 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700287}