blob: 7f5ac5276072a3c3e28fe2d14d4198ca4ba1b3dd [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;
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 Goyal64a75aa2017-07-03 13:50:52 -070074 if (FeatureFlags.GO_DISABLE_WIDGETS) {
75 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() {
101 if (FeatureFlags.GO_DISABLE_WIDGETS) {
102 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
Sunny Goyal67419a12017-11-14 16:55:22 -0800108 /**
109 * Updates the resumed state of the host.
110 * When a host is not resumed, it defers calls to startListening until host is resumed again.
111 * But if the host was already listening, it will not call stopListening.
112 *
113 * @see #setListenIfResumed(boolean)
114 */
115 public void setResumed(boolean isResumed) {
116 if (isResumed == ((mFlags & FLAG_RESUMED) != 0)) {
117 return;
118 }
119 if (isResumed) {
120 mFlags |= FLAG_RESUMED;
121 // Start listening if we were supposed to start listening on resume
122 if ((mFlags & FLAG_LISTEN_IF_RESUMED) != 0 && (mFlags & FLAG_LISTENING) == 0) {
123 startListening();
124 }
125 } else {
126 mFlags &= ~FLAG_RESUMED;
127 }
128 }
129
130 /**
131 * Updates the listening state of the host. If the host is not resumed, startListening is
132 * deferred until next resume.
133 *
134 * @see #setResumed(boolean)
135 */
136 public void setListenIfResumed(boolean listenIfResumed) {
Sunny Goyal67419a12017-11-14 16:55:22 -0800137 if (listenIfResumed == ((mFlags & FLAG_LISTEN_IF_RESUMED) != 0)) {
138 return;
139 }
140 if (listenIfResumed) {
141 mFlags |= FLAG_LISTEN_IF_RESUMED;
142 if ((mFlags & FLAG_RESUMED) != 0) {
143 // If we are resumed, start listening immediately. Note we do not check for
144 // duplicate calls before calling startListening as startListening is safe to call
145 // multiple times.
146 startListening();
147 }
148 } else {
149 mFlags &= ~FLAG_LISTEN_IF_RESUMED;
150 stopListening();
151 }
152 }
153
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700154 @Override
155 public int allocateAppWidgetId() {
156 if (FeatureFlags.GO_DISABLE_WIDGETS) {
157 return AppWidgetManager.INVALID_APPWIDGET_ID;
158 }
159
160 return super.allocateAppWidgetId();
161 }
162
163 public void addProviderChangeListener(ProviderChangedListener callback) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700164 mProviderChangeListeners.add(callback);
165 }
166
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700167 public void removeProviderChangeListener(ProviderChangedListener callback) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700168 mProviderChangeListeners.remove(callback);
169 }
170
Winson Chunga3f78e32012-06-14 11:59:51 -0700171 protected void onProvidersChanged() {
Sunny Goyal7e2a3602015-04-20 18:19:25 -0700172 if (!mProviderChangeListeners.isEmpty()) {
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700173 for (ProviderChangedListener callback : new ArrayList<>(mProviderChangeListeners)) {
174 callback.notifyWidgetProvidersChanged();
Sunny Goyal7e2a3602015-04-20 18:19:25 -0700175 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700176 }
Winson Chunga3f78e32012-06-14 11:59:51 -0700177 }
Adam Cohen59400422014-03-05 18:07:04 -0800178
179 public AppWidgetHostView createView(Context context, int appWidgetId,
180 LauncherAppWidgetProviderInfo appWidget) {
Sunny Goyal952e63d2017-08-16 04:59:08 -0700181 if (appWidget.isCustomWidget()) {
Adam Cohen59400422014-03-05 18:07:04 -0800182 LauncherAppWidgetHostView lahv = new LauncherAppWidgetHostView(context);
183 LayoutInflater inflater = (LayoutInflater)
184 context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
185 inflater.inflate(appWidget.initialLayout, lahv);
186 lahv.setAppWidget(0, appWidget);
Adam Cohen59400422014-03-05 18:07:04 -0800187 return lahv;
Sunny Goyal29947f02017-12-18 13:49:44 -0800188 } else if ((mFlags & FLAG_LISTENING) == 0) {
189 DeferredAppWidgetHostView view = new DeferredAppWidgetHostView(context);
190 view.setAppWidget(appWidgetId, appWidget);
191 mViews.put(appWidgetId, view);
192 return view;
Adam Cohen59400422014-03-05 18:07:04 -0800193 } else {
Sunny Goyal712ee532016-11-04 10:19:58 -0700194 try {
195 return super.createView(context, appWidgetId, appWidget);
196 } catch (Exception e) {
197 if (!Utilities.isBinderSizeError(e)) {
198 throw new RuntimeException(e);
199 }
200
201 // If the exception was thrown while fetching the remote views, let the view stay.
202 // This will ensure that if the widget posts a valid update later, the view
203 // will update.
204 LauncherAppWidgetHostView view = mViews.get(appWidgetId);
205 if (view == null) {
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700206 view = onCreateView(mContext, appWidgetId, appWidget);
Sunny Goyal712ee532016-11-04 10:19:58 -0700207 }
208 view.setAppWidget(appWidgetId, appWidget);
209 view.switchToErrorView();
210 return view;
211 }
Adam Cohen59400422014-03-05 18:07:04 -0800212 }
213 }
214
215 /**
216 * Called when the AppWidget provider for a AppWidget has been upgraded to a new apk.
217 */
218 @Override
219 protected void onProviderChanged(int appWidgetId, AppWidgetProviderInfo appWidget) {
220 LauncherAppWidgetProviderInfo info = LauncherAppWidgetProviderInfo.fromProviderInfo(
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700221 mContext, appWidget);
Adam Cohen59400422014-03-05 18:07:04 -0800222 super.onProviderChanged(appWidgetId, info);
Sunny Goyalec882042015-10-05 10:36:54 -0700223 // The super method updates the dimensions of the providerInfo. Update the
224 // launcher spans accordingly.
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700225 info.initSpans(mContext);
Adam Cohen59400422014-03-05 18:07:04 -0800226 }
Sunny Goyal712ee532016-11-04 10:19:58 -0700227
228 @Override
229 public void deleteAppWidgetId(int appWidgetId) {
230 super.deleteAppWidgetId(appWidgetId);
231 mViews.remove(appWidgetId);
232 }
233
234 @Override
Sunny Goyalc11fac32018-02-27 19:20:15 -0800235 public void clearViews() {
Sunny Goyal712ee532016-11-04 10:19:58 -0700236 super.clearViews();
237 mViews.clear();
238 }
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700239
240 public void startBindFlow(BaseActivity activity,
241 int appWidgetId, AppWidgetProviderInfo info, int requestCode) {
242
243 if (FeatureFlags.GO_DISABLE_WIDGETS) {
244 sendActionCancelled(activity, requestCode);
245 return;
246 }
247
248 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND)
249 .putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
250 .putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.provider)
251 .putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE, info.getProfile());
252 // TODO: we need to make sure that this accounts for the options bundle.
253 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
254 activity.startActivityForResult(intent, requestCode);
255 }
256
257
258 public void startConfigActivity(BaseActivity activity, int widgetId, int requestCode) {
259 if (FeatureFlags.GO_DISABLE_WIDGETS) {
260 sendActionCancelled(activity, requestCode);
261 return;
262 }
263
264 try {
265 startAppWidgetConfigureActivityForResult(activity, widgetId, 0, requestCode, null);
266 } catch (ActivityNotFoundException | SecurityException e) {
267 Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
268 sendActionCancelled(activity, requestCode);
269 }
270 }
271
272 private void sendActionCancelled(final BaseActivity activity, final int requestCode) {
Sunny Goyal67419a12017-11-14 16:55:22 -0800273 new Handler().post(() -> activity.onActivityResult(requestCode, RESULT_CANCELED, null));
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700274 }
275
276 /**
277 * Listener for getting notifications on provider changes.
278 */
279 public interface ProviderChangedListener {
280
281 void notifyWidgetProvidersChanged();
282 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700283}