blob: 9aa74b38d5589e686794bc5634b0e53512019599 [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;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070034
Sunny Goyal0fc1be12014-08-11 17:05:23 -070035import java.util.ArrayList;
36
Adam Cohen59400422014-03-05 18:07:04 -080037
The Android Open Source Project7376fae2009-03-11 12:11:58 -070038/**
39 * Specific {@link AppWidgetHost} that creates our {@link LauncherAppWidgetHostView}
40 * which correctly captures all long-press events. This ensures that users can
41 * always pick up and move widgets.
42 */
43public class LauncherAppWidgetHost extends AppWidgetHost {
Winson Chunga3f78e32012-06-14 11:59:51 -070044
Sunny Goyal67419a12017-11-14 16:55:22 -080045 private static final int FLAG_LISTENING = 1;
46 private static final int FLAG_RESUMED = 1 << 1;
47 private static final int FLAG_LISTEN_IF_RESUMED = 1 << 2;
48
Sunny Goyal64a75aa2017-07-03 13:50:52 -070049 public static final int APPWIDGET_HOST_ID = 1024;
50
51 private final ArrayList<ProviderChangedListener> mProviderChangeListeners = new ArrayList<>();
Sunny Goyal712ee532016-11-04 10:19:58 -070052 private final SparseArray<LauncherAppWidgetHostView> mViews = new SparseArray<>();
Sunny Goyal0fc1be12014-08-11 17:05:23 -070053
Sunny Goyal64a75aa2017-07-03 13:50:52 -070054 private final Context mContext;
Sunny Goyal67419a12017-11-14 16:55:22 -080055 private int mFlags = FLAG_RESUMED;
Winson Chunga3f78e32012-06-14 11:59:51 -070056
Sunny Goyal64a75aa2017-07-03 13:50:52 -070057 public LauncherAppWidgetHost(Context context) {
58 super(context, APPWIDGET_HOST_ID);
59 mContext = context;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070060 }
Adam Cohen9415d872010-09-13 14:49:43 -070061
The Android Open Source Project7376fae2009-03-11 12:11:58 -070062 @Override
Sunny Goyal712ee532016-11-04 10:19:58 -070063 protected LauncherAppWidgetHostView onCreateView(Context context, int appWidgetId,
The Android Open Source Project7376fae2009-03-11 12:11:58 -070064 AppWidgetProviderInfo appWidget) {
Sunny Goyal712ee532016-11-04 10:19:58 -070065 LauncherAppWidgetHostView view = new LauncherAppWidgetHostView(context);
66 mViews.put(appWidgetId, view);
67 return view;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070068 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -080069
70 @Override
Adam Cohen084c3182014-06-16 15:22:56 -070071 public void startListening() {
Sunny Goyal64a75aa2017-07-03 13:50:52 -070072 if (FeatureFlags.GO_DISABLE_WIDGETS) {
73 return;
74 }
Sunny Goyal67419a12017-11-14 16:55:22 -080075 mFlags |= FLAG_LISTENING;
Adam Cohen084c3182014-06-16 15:22:56 -070076 try {
77 super.startListening();
78 } catch (Exception e) {
Sunny Goyal712ee532016-11-04 10:19:58 -070079 if (!Utilities.isBinderSizeError(e)) {
Adam Cohen084c3182014-06-16 15:22:56 -070080 throw new RuntimeException(e);
81 }
Sunny Goyal712ee532016-11-04 10:19:58 -070082 // We're willing to let this slide. The exception is being caused by the list of
83 // RemoteViews which is being passed back. The startListening relationship will
84 // have been established by this point, and we will end up populating the
85 // widgets upon bind anyway. See issue 14255011 for more context.
Adam Cohen084c3182014-06-16 15:22:56 -070086 }
87 }
88
Sunny Goyal64a75aa2017-07-03 13:50:52 -070089 @Override
90 public void stopListening() {
91 if (FeatureFlags.GO_DISABLE_WIDGETS) {
92 return;
93 }
Sunny Goyal67419a12017-11-14 16:55:22 -080094 mFlags &= ~FLAG_LISTENING;
Sunny Goyal64a75aa2017-07-03 13:50:52 -070095 super.stopListening();
96 }
97
Sunny Goyal67419a12017-11-14 16:55:22 -080098 /**
99 * Updates the resumed state of the host.
100 * When a host is not resumed, it defers calls to startListening until host is resumed again.
101 * But if the host was already listening, it will not call stopListening.
102 *
103 * @see #setListenIfResumed(boolean)
104 */
105 public void setResumed(boolean isResumed) {
106 if (isResumed == ((mFlags & FLAG_RESUMED) != 0)) {
107 return;
108 }
109 if (isResumed) {
110 mFlags |= FLAG_RESUMED;
111 // Start listening if we were supposed to start listening on resume
112 if ((mFlags & FLAG_LISTEN_IF_RESUMED) != 0 && (mFlags & FLAG_LISTENING) == 0) {
113 startListening();
114 }
115 } else {
116 mFlags &= ~FLAG_RESUMED;
117 }
118 }
119
120 /**
121 * Updates the listening state of the host. If the host is not resumed, startListening is
122 * deferred until next resume.
123 *
124 * @see #setResumed(boolean)
125 */
126 public void setListenIfResumed(boolean listenIfResumed) {
127 if (!Utilities.ATLEAST_NOUGAT_MR1) {
128 return;
129 }
130 if (listenIfResumed == ((mFlags & FLAG_LISTEN_IF_RESUMED) != 0)) {
131 return;
132 }
133 if (listenIfResumed) {
134 mFlags |= FLAG_LISTEN_IF_RESUMED;
135 if ((mFlags & FLAG_RESUMED) != 0) {
136 // If we are resumed, start listening immediately. Note we do not check for
137 // duplicate calls before calling startListening as startListening is safe to call
138 // multiple times.
139 startListening();
140 }
141 } else {
142 mFlags &= ~FLAG_LISTEN_IF_RESUMED;
143 stopListening();
144 }
145 }
146
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700147 @Override
148 public int allocateAppWidgetId() {
149 if (FeatureFlags.GO_DISABLE_WIDGETS) {
150 return AppWidgetManager.INVALID_APPWIDGET_ID;
151 }
152
153 return super.allocateAppWidgetId();
154 }
155
156 public void addProviderChangeListener(ProviderChangedListener callback) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700157 mProviderChangeListeners.add(callback);
158 }
159
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700160 public void removeProviderChangeListener(ProviderChangedListener callback) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700161 mProviderChangeListeners.remove(callback);
162 }
163
Winson Chunga3f78e32012-06-14 11:59:51 -0700164 protected void onProvidersChanged() {
Sunny Goyal7e2a3602015-04-20 18:19:25 -0700165 if (!mProviderChangeListeners.isEmpty()) {
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700166 for (ProviderChangedListener callback : new ArrayList<>(mProviderChangeListeners)) {
167 callback.notifyWidgetProvidersChanged();
Sunny Goyal7e2a3602015-04-20 18:19:25 -0700168 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700169 }
Winson Chunga3f78e32012-06-14 11:59:51 -0700170 }
Adam Cohen59400422014-03-05 18:07:04 -0800171
172 public AppWidgetHostView createView(Context context, int appWidgetId,
173 LauncherAppWidgetProviderInfo appWidget) {
Sunny Goyal952e63d2017-08-16 04:59:08 -0700174 if (appWidget.isCustomWidget()) {
Adam Cohen59400422014-03-05 18:07:04 -0800175 LauncherAppWidgetHostView lahv = new LauncherAppWidgetHostView(context);
176 LayoutInflater inflater = (LayoutInflater)
177 context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
178 inflater.inflate(appWidget.initialLayout, lahv);
179 lahv.setAppWidget(0, appWidget);
Adam Cohen59400422014-03-05 18:07:04 -0800180 return lahv;
181 } else {
Sunny Goyal712ee532016-11-04 10:19:58 -0700182 try {
183 return super.createView(context, appWidgetId, appWidget);
184 } catch (Exception e) {
185 if (!Utilities.isBinderSizeError(e)) {
186 throw new RuntimeException(e);
187 }
188
189 // If the exception was thrown while fetching the remote views, let the view stay.
190 // This will ensure that if the widget posts a valid update later, the view
191 // will update.
192 LauncherAppWidgetHostView view = mViews.get(appWidgetId);
193 if (view == null) {
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700194 view = onCreateView(mContext, appWidgetId, appWidget);
Sunny Goyal712ee532016-11-04 10:19:58 -0700195 }
196 view.setAppWidget(appWidgetId, appWidget);
197 view.switchToErrorView();
198 return view;
199 }
Adam Cohen59400422014-03-05 18:07:04 -0800200 }
201 }
202
203 /**
204 * Called when the AppWidget provider for a AppWidget has been upgraded to a new apk.
205 */
206 @Override
207 protected void onProviderChanged(int appWidgetId, AppWidgetProviderInfo appWidget) {
208 LauncherAppWidgetProviderInfo info = LauncherAppWidgetProviderInfo.fromProviderInfo(
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700209 mContext, appWidget);
Adam Cohen59400422014-03-05 18:07:04 -0800210 super.onProviderChanged(appWidgetId, info);
Sunny Goyalec882042015-10-05 10:36:54 -0700211 // The super method updates the dimensions of the providerInfo. Update the
212 // launcher spans accordingly.
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700213 info.initSpans(mContext);
Adam Cohen59400422014-03-05 18:07:04 -0800214 }
Sunny Goyal712ee532016-11-04 10:19:58 -0700215
216 @Override
217 public void deleteAppWidgetId(int appWidgetId) {
218 super.deleteAppWidgetId(appWidgetId);
219 mViews.remove(appWidgetId);
220 }
221
222 @Override
223 protected void clearViews() {
224 super.clearViews();
225 mViews.clear();
226 }
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700227
228 public void startBindFlow(BaseActivity activity,
229 int appWidgetId, AppWidgetProviderInfo info, int requestCode) {
230
231 if (FeatureFlags.GO_DISABLE_WIDGETS) {
232 sendActionCancelled(activity, requestCode);
233 return;
234 }
235
236 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND)
237 .putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
238 .putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.provider)
239 .putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE, info.getProfile());
240 // TODO: we need to make sure that this accounts for the options bundle.
241 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
242 activity.startActivityForResult(intent, requestCode);
243 }
244
245
246 public void startConfigActivity(BaseActivity activity, int widgetId, int requestCode) {
247 if (FeatureFlags.GO_DISABLE_WIDGETS) {
248 sendActionCancelled(activity, requestCode);
249 return;
250 }
251
252 try {
253 startAppWidgetConfigureActivityForResult(activity, widgetId, 0, requestCode, null);
254 } catch (ActivityNotFoundException | SecurityException e) {
255 Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
256 sendActionCancelled(activity, requestCode);
257 }
258 }
259
260 private void sendActionCancelled(final BaseActivity activity, final int requestCode) {
Sunny Goyal67419a12017-11-14 16:55:22 -0800261 new Handler().post(() -> activity.onActivityResult(requestCode, RESULT_CANCELED, null));
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700262 }
263
264 /**
265 * Listener for getting notifications on provider changes.
266 */
267 public interface ProviderChangedListener {
268
269 void notifyWidgetProvidersChanged();
270 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700271}