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.model.WidgetsModel;
33 import com.android.launcher3.testing.TestLogging;
34 import com.android.launcher3.testing.TestProtocol;
35 import com.android.launcher3.widget.DeferredAppWidgetHostView;
36 import com.android.launcher3.widget.LauncherAppWidgetHostView;
37 import com.android.launcher3.widget.PendingAppWidgetHostView;
38 import com.android.launcher3.widget.custom.CustomWidgetManager;
39 
40 import java.util.ArrayList;
41 import java.util.function.IntConsumer;
42 
43 
44 /**
45  * Specific {@link AppWidgetHost} that creates our {@link LauncherAppWidgetHostView}
46  * which correctly captures all long-press events. This ensures that users can
47  * always pick up and move widgets.
48  */
49 public class LauncherAppWidgetHost extends AppWidgetHost {
50 
51     private static final int FLAG_LISTENING = 1;
52     private static final int FLAG_RESUMED = 1 << 1;
53     private static final int FLAG_LISTEN_IF_RESUMED = 1 << 2;
54 
55     public static final int APPWIDGET_HOST_ID = 1024;
56 
57     private final ArrayList<ProviderChangedListener> mProviderChangeListeners = new ArrayList<>();
58     private final SparseArray<LauncherAppWidgetHostView> mViews = new SparseArray<>();
59     private final SparseArray<PendingAppWidgetHostView> mPendingViews = new SparseArray<>();
60 
61     private final Context mContext;
62     private int mFlags = FLAG_RESUMED;
63 
64     private IntConsumer mAppWidgetRemovedCallback = null;
65 
66 
LauncherAppWidgetHost(Context context)67     public LauncherAppWidgetHost(Context context) {
68         this(context, null);
69     }
70 
LauncherAppWidgetHost(Context context, IntConsumer appWidgetRemovedCallback)71     public LauncherAppWidgetHost(Context context,
72             IntConsumer appWidgetRemovedCallback) {
73         super(context, APPWIDGET_HOST_ID);
74         mContext = context;
75         mAppWidgetRemovedCallback = appWidgetRemovedCallback;
76     }
77 
78     @Override
onCreateView(Context context, int appWidgetId, AppWidgetProviderInfo appWidget)79     protected LauncherAppWidgetHostView onCreateView(Context context, int appWidgetId,
80             AppWidgetProviderInfo appWidget) {
81         final LauncherAppWidgetHostView view;
82         if (mPendingViews.get(appWidgetId) != null) {
83             view = mPendingViews.get(appWidgetId);
84             mPendingViews.remove(appWidgetId);
85         } else {
86             view = new LauncherAppWidgetHostView(context);
87         }
88         mViews.put(appWidgetId, view);
89         return view;
90     }
91 
92     @Override
startListening()93     public void startListening() {
94         if (WidgetsModel.GO_DISABLE_WIDGETS) {
95             return;
96         }
97         mFlags |= FLAG_LISTENING;
98         try {
99             super.startListening();
100         } catch (Exception e) {
101             if (!Utilities.isBinderSizeError(e)) {
102                 throw new RuntimeException(e);
103             }
104             // We're willing to let this slide. The exception is being caused by the list of
105             // RemoteViews which is being passed back. The startListening relationship will
106             // have been established by this point, and we will end up populating the
107             // widgets upon bind anyway. See issue 14255011 for more context.
108         }
109 
110         // We go in reverse order and inflate any deferred widget
111         for (int i = mViews.size() - 1; i >= 0; i--) {
112             LauncherAppWidgetHostView view = mViews.valueAt(i);
113             if (view instanceof DeferredAppWidgetHostView) {
114                 view.reInflate();
115             }
116         }
117     }
118 
119     @Override
stopListening()120     public void stopListening() {
121         if (WidgetsModel.GO_DISABLE_WIDGETS) {
122             return;
123         }
124         mFlags &= ~FLAG_LISTENING;
125         super.stopListening();
126     }
127 
isListening()128     public boolean isListening() {
129         return (mFlags & FLAG_LISTENING) != 0;
130     }
131 
132     /**
133      * Updates the resumed state of the host.
134      * When a host is not resumed, it defers calls to startListening until host is resumed again.
135      * But if the host was already listening, it will not call stopListening.
136      *
137      * @see #setListenIfResumed(boolean)
138      */
setResumed(boolean isResumed)139     public void setResumed(boolean isResumed) {
140         if (isResumed == ((mFlags & FLAG_RESUMED) != 0)) {
141             return;
142         }
143         if (isResumed) {
144             mFlags |= FLAG_RESUMED;
145             // Start listening if we were supposed to start listening on resume
146             if ((mFlags & FLAG_LISTEN_IF_RESUMED) != 0 && (mFlags & FLAG_LISTENING) == 0) {
147                 startListening();
148             }
149         } else {
150             mFlags &= ~FLAG_RESUMED;
151         }
152     }
153 
154     /**
155      * Updates the listening state of the host. If the host is not resumed, startListening is
156      * deferred until next resume.
157      *
158      * @see #setResumed(boolean)
159      */
setListenIfResumed(boolean listenIfResumed)160     public void setListenIfResumed(boolean listenIfResumed) {
161         if (listenIfResumed == ((mFlags & FLAG_LISTEN_IF_RESUMED) != 0)) {
162             return;
163         }
164         if (listenIfResumed) {
165             mFlags |= FLAG_LISTEN_IF_RESUMED;
166             if ((mFlags & FLAG_RESUMED) != 0) {
167                 // If we are resumed, start listening immediately. Note we do not check for
168                 // duplicate calls before calling startListening as startListening is safe to call
169                 // multiple times.
170                 startListening();
171             }
172         } else {
173             mFlags &= ~FLAG_LISTEN_IF_RESUMED;
174             stopListening();
175         }
176     }
177 
178     @Override
allocateAppWidgetId()179     public int allocateAppWidgetId() {
180         if (WidgetsModel.GO_DISABLE_WIDGETS) {
181             return AppWidgetManager.INVALID_APPWIDGET_ID;
182         }
183 
184         return super.allocateAppWidgetId();
185     }
186 
addProviderChangeListener(ProviderChangedListener callback)187     public void addProviderChangeListener(ProviderChangedListener callback) {
188         mProviderChangeListeners.add(callback);
189     }
190 
removeProviderChangeListener(ProviderChangedListener callback)191     public void removeProviderChangeListener(ProviderChangedListener callback) {
192         mProviderChangeListeners.remove(callback);
193     }
194 
onProvidersChanged()195     protected void onProvidersChanged() {
196         if (!mProviderChangeListeners.isEmpty()) {
197             for (ProviderChangedListener callback : new ArrayList<>(mProviderChangeListeners)) {
198                 callback.notifyWidgetProvidersChanged();
199             }
200         }
201     }
202 
addPendingView(int appWidgetId, PendingAppWidgetHostView view)203     void addPendingView(int appWidgetId, PendingAppWidgetHostView view) {
204         mPendingViews.put(appWidgetId, view);
205     }
206 
createView(Context context, int appWidgetId, LauncherAppWidgetProviderInfo appWidget)207     public AppWidgetHostView createView(Context context, int appWidgetId,
208             LauncherAppWidgetProviderInfo appWidget) {
209         if (appWidget.isCustomWidget()) {
210             LauncherAppWidgetHostView lahv = new LauncherAppWidgetHostView(context);
211             lahv.setAppWidget(0, appWidget);
212             CustomWidgetManager.INSTANCE.get(context).onViewCreated(lahv);
213             return lahv;
214         } else if ((mFlags & FLAG_LISTENING) == 0) {
215             DeferredAppWidgetHostView view = new DeferredAppWidgetHostView(context);
216             view.setAppWidget(appWidgetId, appWidget);
217             mViews.put(appWidgetId, view);
218             return view;
219         } else {
220             try {
221                 return super.createView(context, appWidgetId, appWidget);
222             } catch (Exception e) {
223                 if (!Utilities.isBinderSizeError(e)) {
224                     throw new RuntimeException(e);
225                 }
226 
227                 // If the exception was thrown while fetching the remote views, let the view stay.
228                 // This will ensure that if the widget posts a valid update later, the view
229                 // will update.
230                 LauncherAppWidgetHostView view = mViews.get(appWidgetId);
231                 if (view == null) {
232                     view = onCreateView(mContext, appWidgetId, appWidget);
233                 }
234                 view.setAppWidget(appWidgetId, appWidget);
235                 view.switchToErrorView();
236                 return view;
237             }
238         }
239     }
240 
241     /**
242      * Called when the AppWidget provider for a AppWidget has been upgraded to a new apk.
243      */
244     @Override
onProviderChanged(int appWidgetId, AppWidgetProviderInfo appWidget)245     protected void onProviderChanged(int appWidgetId, AppWidgetProviderInfo appWidget) {
246         LauncherAppWidgetProviderInfo info = LauncherAppWidgetProviderInfo.fromProviderInfo(
247                 mContext, appWidget);
248         super.onProviderChanged(appWidgetId, info);
249         // The super method updates the dimensions of the providerInfo. Update the
250         // launcher spans accordingly.
251         info.initSpans(mContext);
252     }
253 
254     /**
255      * Called on an appWidget is removed for a widgetId
256      *
257      * @param appWidgetId TODO: make this override when SDK is updated
258      */
onAppWidgetRemoved(int appWidgetId)259     public void onAppWidgetRemoved(int appWidgetId) {
260         if (mAppWidgetRemovedCallback == null) {
261             return;
262         }
263         mAppWidgetRemovedCallback.accept(appWidgetId);
264     }
265 
266     @Override
deleteAppWidgetId(int appWidgetId)267     public void deleteAppWidgetId(int appWidgetId) {
268         super.deleteAppWidgetId(appWidgetId);
269         mViews.remove(appWidgetId);
270     }
271 
272     @Override
clearViews()273     public void clearViews() {
274         super.clearViews();
275         mViews.clear();
276     }
277 
startBindFlow(BaseActivity activity, int appWidgetId, AppWidgetProviderInfo info, int requestCode)278     public void startBindFlow(BaseActivity activity,
279             int appWidgetId, AppWidgetProviderInfo info, int requestCode) {
280 
281         if (WidgetsModel.GO_DISABLE_WIDGETS) {
282             sendActionCancelled(activity, requestCode);
283             return;
284         }
285 
286         Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND)
287                 .putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
288                 .putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.provider)
289                 .putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE, info.getProfile());
290         // TODO: we need to make sure that this accounts for the options bundle.
291         // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
292         activity.startActivityForResult(intent, requestCode);
293     }
294 
295 
startConfigActivity(BaseActivity activity, int widgetId, int requestCode)296     public void startConfigActivity(BaseActivity activity, int widgetId, int requestCode) {
297         if (WidgetsModel.GO_DISABLE_WIDGETS) {
298             sendActionCancelled(activity, requestCode);
299             return;
300         }
301 
302         try {
303             TestLogging.recordEvent(TestProtocol.SEQUENCE_MAIN, "start: startConfigActivity");
304             startAppWidgetConfigureActivityForResult(activity, widgetId, 0, requestCode, null);
305         } catch (ActivityNotFoundException | SecurityException e) {
306             Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
307             sendActionCancelled(activity, requestCode);
308         }
309     }
310 
sendActionCancelled(final BaseActivity activity, final int requestCode)311     private void sendActionCancelled(final BaseActivity activity, final int requestCode) {
312         new Handler().post(() -> activity.onActivityResult(requestCode, RESULT_CANCELED, null));
313     }
314 
315     /**
316      * Listener for getting notifications on provider changes.
317      */
318     public interface ProviderChangedListener {
319 
notifyWidgetProvidersChanged()320         void notifyWidgetProvidersChanged();
321     }
322 }
323