1 /*
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 
17 package com.android.launcher3;
18 
19 import static android.app.Activity.RESULT_CANCELED;
20 
21 import android.appwidget.AppWidgetHost;
22 import android.appwidget.AppWidgetHostView;
23 import android.appwidget.AppWidgetManager;
24 import android.appwidget.AppWidgetProviderInfo;
25 import android.content.ActivityNotFoundException;
26 import android.content.Context;
27 import android.content.Intent;
28 import android.os.Handler;
29 import android.util.SparseArray;
30 import android.widget.Toast;
31 
32 import com.android.launcher3.config.FeatureFlags;
33 import com.android.launcher3.widget.DeferredAppWidgetHostView;
34 import com.android.launcher3.widget.LauncherAppWidgetHostView;
35 import com.android.launcher3.widget.custom.CustomWidgetManager;
36 
37 import java.util.ArrayList;
38 import java.util.function.IntConsumer;
39 
40 
41 /**
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  */
46 public class LauncherAppWidgetHost extends AppWidgetHost {
47 
48     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 
52     public static final int APPWIDGET_HOST_ID = 1024;
53 
54     private final ArrayList<ProviderChangedListener> mProviderChangeListeners = new ArrayList<>();
55     private final SparseArray<LauncherAppWidgetHostView> mViews = new SparseArray<>();
56 
57     private final Context mContext;
58     private int mFlags = FLAG_RESUMED;
59 
60     private IntConsumer mAppWidgetRemovedCallback = null;
61 
LauncherAppWidgetHost(Context context)62     public LauncherAppWidgetHost(Context context) {
63         this(context, null);
64     }
65 
LauncherAppWidgetHost(Context context, IntConsumer appWidgetRemovedCallback)66     public LauncherAppWidgetHost(Context context,
67             IntConsumer appWidgetRemovedCallback) {
68         super(context, APPWIDGET_HOST_ID);
69         mContext = context;
70         mAppWidgetRemovedCallback = appWidgetRemovedCallback;
71     }
72 
73     @Override
onCreateView(Context context, int appWidgetId, AppWidgetProviderInfo appWidget)74     protected LauncherAppWidgetHostView onCreateView(Context context, int appWidgetId,
75             AppWidgetProviderInfo appWidget) {
76         LauncherAppWidgetHostView view = new LauncherAppWidgetHostView(context);
77         mViews.put(appWidgetId, view);
78         return view;
79     }
80 
81     @Override
startListening()82     public void startListening() {
83         if (FeatureFlags.GO_DISABLE_WIDGETS) {
84             return;
85         }
86         mFlags |= FLAG_LISTENING;
87         try {
88             super.startListening();
89         } catch (Exception e) {
90             if (!Utilities.isBinderSizeError(e)) {
91                 throw new RuntimeException(e);
92             }
93             // 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.
97         }
98 
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         }
106     }
107 
108     @Override
stopListening()109     public void stopListening() {
110         if (FeatureFlags.GO_DISABLE_WIDGETS) {
111             return;
112         }
113         mFlags &= ~FLAG_LISTENING;
114         super.stopListening();
115     }
116 
isListening()117     public boolean isListening() {
118         return (mFlags & FLAG_LISTENING) != 0;
119     }
120 
121     /**
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      */
setResumed(boolean isResumed)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      */
setListenIfResumed(boolean listenIfResumed)149     public void setListenIfResumed(boolean listenIfResumed) {
150         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 
167     @Override
allocateAppWidgetId()168     public int allocateAppWidgetId() {
169         if (FeatureFlags.GO_DISABLE_WIDGETS) {
170             return AppWidgetManager.INVALID_APPWIDGET_ID;
171         }
172 
173         return super.allocateAppWidgetId();
174     }
175 
addProviderChangeListener(ProviderChangedListener callback)176     public void addProviderChangeListener(ProviderChangedListener callback) {
177         mProviderChangeListeners.add(callback);
178     }
179 
removeProviderChangeListener(ProviderChangedListener callback)180     public void removeProviderChangeListener(ProviderChangedListener callback) {
181         mProviderChangeListeners.remove(callback);
182     }
183 
onProvidersChanged()184     protected void onProvidersChanged() {
185         if (!mProviderChangeListeners.isEmpty()) {
186             for (ProviderChangedListener callback : new ArrayList<>(mProviderChangeListeners)) {
187                 callback.notifyWidgetProvidersChanged();
188             }
189         }
190     }
191 
createView(Context context, int appWidgetId, LauncherAppWidgetProviderInfo appWidget)192     public AppWidgetHostView createView(Context context, int appWidgetId,
193             LauncherAppWidgetProviderInfo appWidget) {
194         if (appWidget.isCustomWidget()) {
195             LauncherAppWidgetHostView lahv = new LauncherAppWidgetHostView(context);
196             lahv.setAppWidget(0, appWidget);
197             CustomWidgetManager.INSTANCE.get(context).onViewCreated(lahv);
198             return lahv;
199         } else if ((mFlags & FLAG_LISTENING) == 0) {
200             DeferredAppWidgetHostView view = new DeferredAppWidgetHostView(context);
201             view.setAppWidget(appWidgetId, appWidget);
202             mViews.put(appWidgetId, view);
203             return view;
204         } else {
205             try {
206                 return super.createView(context, appWidgetId, appWidget);
207             } catch (Exception e) {
208                 if (!Utilities.isBinderSizeError(e)) {
209                     throw new RuntimeException(e);
210                 }
211 
212                 // If the exception was thrown while fetching the remote views, let the view stay.
213                 // This will ensure that if the widget posts a valid update later, the view
214                 // will update.
215                 LauncherAppWidgetHostView view = mViews.get(appWidgetId);
216                 if (view == null) {
217                     view = onCreateView(mContext, appWidgetId, appWidget);
218                 }
219                 view.setAppWidget(appWidgetId, appWidget);
220                 view.switchToErrorView();
221                 return view;
222             }
223         }
224     }
225 
226     /**
227      * Called when the AppWidget provider for a AppWidget has been upgraded to a new apk.
228      */
229     @Override
onProviderChanged(int appWidgetId, AppWidgetProviderInfo appWidget)230     protected void onProviderChanged(int appWidgetId, AppWidgetProviderInfo appWidget) {
231         LauncherAppWidgetProviderInfo info = LauncherAppWidgetProviderInfo.fromProviderInfo(
232                 mContext, appWidget);
233         super.onProviderChanged(appWidgetId, info);
234         // The super method updates the dimensions of the providerInfo. Update the
235         // launcher spans accordingly.
236         info.initSpans(mContext);
237     }
238 
239     /**
240      * Called on an appWidget is removed for a widgetId
241      * @param appWidgetId
242      * TODO: make this override when SDK is updated
243      */
onAppWidgetRemoved(int appWidgetId)244     public void onAppWidgetRemoved(int appWidgetId) {
245         if (mAppWidgetRemovedCallback == null) {
246             return;
247         }
248         mAppWidgetRemovedCallback.accept(appWidgetId);
249     }
250 
251     @Override
deleteAppWidgetId(int appWidgetId)252     public void deleteAppWidgetId(int appWidgetId) {
253         super.deleteAppWidgetId(appWidgetId);
254         mViews.remove(appWidgetId);
255     }
256 
257     @Override
clearViews()258     public void clearViews() {
259         super.clearViews();
260         mViews.clear();
261     }
262 
startBindFlow(BaseActivity activity, int appWidgetId, AppWidgetProviderInfo info, int requestCode)263     public void startBindFlow(BaseActivity activity,
264             int appWidgetId, AppWidgetProviderInfo info, int requestCode) {
265 
266         if (FeatureFlags.GO_DISABLE_WIDGETS) {
267             sendActionCancelled(activity, requestCode);
268             return;
269         }
270 
271         Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND)
272                 .putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
273                 .putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.provider)
274                 .putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE, info.getProfile());
275         // TODO: we need to make sure that this accounts for the options bundle.
276         // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
277         activity.startActivityForResult(intent, requestCode);
278     }
279 
280 
startConfigActivity(BaseActivity activity, int widgetId, int requestCode)281     public void startConfigActivity(BaseActivity activity, int widgetId, int requestCode) {
282         if (FeatureFlags.GO_DISABLE_WIDGETS) {
283             sendActionCancelled(activity, requestCode);
284             return;
285         }
286 
287         try {
288             startAppWidgetConfigureActivityForResult(activity, widgetId, 0, requestCode, null);
289         } catch (ActivityNotFoundException | SecurityException e) {
290             Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
291             sendActionCancelled(activity, requestCode);
292         }
293     }
294 
sendActionCancelled(final BaseActivity activity, final int requestCode)295     private void sendActionCancelled(final BaseActivity activity, final int requestCode) {
296         new Handler().post(() -> activity.onActivityResult(requestCode, RESULT_CANCELED, null));
297     }
298 
299     /**
300      * Listener for getting notifications on provider changes.
301      */
302     public interface ProviderChangedListener {
303 
notifyWidgetProvidersChanged()304         void notifyWidgetProvidersChanged();
305     }
306 }
307