• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.view.LayoutInflater;
31 import android.widget.Toast;
32 
33 import com.android.launcher3.config.FeatureFlags;
34 import com.android.launcher3.widget.DeferredAppWidgetHostView;
35 import com.android.launcher3.widget.LauncherAppWidgetHostView;
36 
37 import java.util.ArrayList;
38 
39 
40 /**
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  */
45 public class LauncherAppWidgetHost extends AppWidgetHost {
46 
47     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 
51     public static final int APPWIDGET_HOST_ID = 1024;
52 
53     private final ArrayList<ProviderChangedListener> mProviderChangeListeners = new ArrayList<>();
54     private final SparseArray<LauncherAppWidgetHostView> mViews = new SparseArray<>();
55 
56     private final Context mContext;
57     private int mFlags = FLAG_RESUMED;
58 
LauncherAppWidgetHost(Context context)59     public LauncherAppWidgetHost(Context context) {
60         super(context, APPWIDGET_HOST_ID);
61         mContext = context;
62     }
63 
64     @Override
onCreateView(Context context, int appWidgetId, AppWidgetProviderInfo appWidget)65     protected LauncherAppWidgetHostView onCreateView(Context context, int appWidgetId,
66             AppWidgetProviderInfo appWidget) {
67         LauncherAppWidgetHostView view = new LauncherAppWidgetHostView(context);
68         mViews.put(appWidgetId, view);
69         return view;
70     }
71 
72     @Override
startListening()73     public void startListening() {
74         if (FeatureFlags.GO_DISABLE_WIDGETS) {
75             return;
76         }
77         mFlags |= FLAG_LISTENING;
78         try {
79             super.startListening();
80         } catch (Exception e) {
81             if (!Utilities.isBinderSizeError(e)) {
82                 throw new RuntimeException(e);
83             }
84             // 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.
88         }
89 
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         }
97     }
98 
99     @Override
stopListening()100     public void stopListening() {
101         if (FeatureFlags.GO_DISABLE_WIDGETS) {
102             return;
103         }
104         mFlags &= ~FLAG_LISTENING;
105         super.stopListening();
106     }
107 
108     /**
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      */
setResumed(boolean isResumed)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      */
setListenIfResumed(boolean listenIfResumed)136     public void setListenIfResumed(boolean listenIfResumed) {
137         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 
154     @Override
allocateAppWidgetId()155     public int allocateAppWidgetId() {
156         if (FeatureFlags.GO_DISABLE_WIDGETS) {
157             return AppWidgetManager.INVALID_APPWIDGET_ID;
158         }
159 
160         return super.allocateAppWidgetId();
161     }
162 
addProviderChangeListener(ProviderChangedListener callback)163     public void addProviderChangeListener(ProviderChangedListener callback) {
164         mProviderChangeListeners.add(callback);
165     }
166 
removeProviderChangeListener(ProviderChangedListener callback)167     public void removeProviderChangeListener(ProviderChangedListener callback) {
168         mProviderChangeListeners.remove(callback);
169     }
170 
onProvidersChanged()171     protected void onProvidersChanged() {
172         if (!mProviderChangeListeners.isEmpty()) {
173             for (ProviderChangedListener callback : new ArrayList<>(mProviderChangeListeners)) {
174                 callback.notifyWidgetProvidersChanged();
175             }
176         }
177     }
178 
createView(Context context, int appWidgetId, LauncherAppWidgetProviderInfo appWidget)179     public AppWidgetHostView createView(Context context, int appWidgetId,
180             LauncherAppWidgetProviderInfo appWidget) {
181         if (appWidget.isCustomWidget()) {
182             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);
187             return lahv;
188         } 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;
193         } else {
194             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) {
206                     view = onCreateView(mContext, appWidgetId, appWidget);
207                 }
208                 view.setAppWidget(appWidgetId, appWidget);
209                 view.switchToErrorView();
210                 return  view;
211             }
212         }
213     }
214 
215     /**
216      * Called when the AppWidget provider for a AppWidget has been upgraded to a new apk.
217      */
218     @Override
onProviderChanged(int appWidgetId, AppWidgetProviderInfo appWidget)219     protected void onProviderChanged(int appWidgetId, AppWidgetProviderInfo appWidget) {
220         LauncherAppWidgetProviderInfo info = LauncherAppWidgetProviderInfo.fromProviderInfo(
221                 mContext, appWidget);
222         super.onProviderChanged(appWidgetId, info);
223         // The super method updates the dimensions of the providerInfo. Update the
224         // launcher spans accordingly.
225         info.initSpans(mContext);
226     }
227 
228     @Override
deleteAppWidgetId(int appWidgetId)229     public void deleteAppWidgetId(int appWidgetId) {
230         super.deleteAppWidgetId(appWidgetId);
231         mViews.remove(appWidgetId);
232     }
233 
234     @Override
clearViews()235     public void clearViews() {
236         super.clearViews();
237         mViews.clear();
238     }
239 
startBindFlow(BaseActivity activity, int appWidgetId, AppWidgetProviderInfo info, int requestCode)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 
startConfigActivity(BaseActivity activity, int widgetId, int requestCode)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 
sendActionCancelled(final BaseActivity activity, final int requestCode)272     private void sendActionCancelled(final BaseActivity activity, final int requestCode) {
273         new Handler().post(() -> activity.onActivityResult(requestCode, RESULT_CANCELED, null));
274     }
275 
276     /**
277      * Listener for getting notifications on provider changes.
278      */
279     public interface ProviderChangedListener {
280 
notifyWidgetProvidersChanged()281         void notifyWidgetProvidersChanged();
282     }
283 }
284