The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 18 | |
Sunny Goyal | 67419a1 | 2017-11-14 16:55:22 -0800 | [diff] [blame] | 19 | import static android.app.Activity.RESULT_CANCELED; |
| 20 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 21 | import android.appwidget.AppWidgetHost; |
| 22 | import android.appwidget.AppWidgetHostView; |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 23 | import android.appwidget.AppWidgetManager; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 24 | import android.appwidget.AppWidgetProviderInfo; |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 25 | import android.content.ActivityNotFoundException; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 26 | import android.content.Context; |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 27 | import android.content.Intent; |
| 28 | import android.os.Handler; |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 29 | import android.util.SparseArray; |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 30 | import android.widget.Toast; |
| 31 | |
| 32 | import com.android.launcher3.config.FeatureFlags; |
Sunny Goyal | 29947f0 | 2017-12-18 13:49:44 -0800 | [diff] [blame] | 33 | import com.android.launcher3.widget.DeferredAppWidgetHostView; |
| 34 | import com.android.launcher3.widget.LauncherAppWidgetHostView; |
Pinyao Ting | c7a6c29 | 2019-08-26 14:36:02 -0700 | [diff] [blame^] | 35 | import com.android.launcher3.widget.custom.CustomWidgetManager; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 36 | |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 37 | import java.util.ArrayList; |
| 38 | |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 39 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 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 { |
Winson Chung | a3f78e3 | 2012-06-14 11:59:51 -0700 | [diff] [blame] | 46 | |
Sunny Goyal | 67419a1 | 2017-11-14 16:55:22 -0800 | [diff] [blame] | 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 | |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 51 | public static final int APPWIDGET_HOST_ID = 1024; |
| 52 | |
| 53 | private final ArrayList<ProviderChangedListener> mProviderChangeListeners = new ArrayList<>(); |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 54 | private final SparseArray<LauncherAppWidgetHostView> mViews = new SparseArray<>(); |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 55 | |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 56 | private final Context mContext; |
Sunny Goyal | 67419a1 | 2017-11-14 16:55:22 -0800 | [diff] [blame] | 57 | private int mFlags = FLAG_RESUMED; |
Winson Chung | a3f78e3 | 2012-06-14 11:59:51 -0700 | [diff] [blame] | 58 | |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 59 | public LauncherAppWidgetHost(Context context) { |
| 60 | super(context, APPWIDGET_HOST_ID); |
| 61 | mContext = context; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 62 | } |
Adam Cohen | 9415d87 | 2010-09-13 14:49:43 -0700 | [diff] [blame] | 63 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 64 | @Override |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 65 | protected LauncherAppWidgetHostView onCreateView(Context context, int appWidgetId, |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 66 | AppWidgetProviderInfo appWidget) { |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 67 | LauncherAppWidgetHostView view = new LauncherAppWidgetHostView(context); |
| 68 | mViews.put(appWidgetId, view); |
| 69 | return view; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 70 | } |
Patrick Dubroy | 2313eff | 2011-01-11 20:01:31 -0800 | [diff] [blame] | 71 | |
| 72 | @Override |
Adam Cohen | 084c318 | 2014-06-16 15:22:56 -0700 | [diff] [blame] | 73 | public void startListening() { |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 74 | if (FeatureFlags.GO_DISABLE_WIDGETS) { |
| 75 | return; |
| 76 | } |
Sunny Goyal | 67419a1 | 2017-11-14 16:55:22 -0800 | [diff] [blame] | 77 | mFlags |= FLAG_LISTENING; |
Adam Cohen | 084c318 | 2014-06-16 15:22:56 -0700 | [diff] [blame] | 78 | try { |
| 79 | super.startListening(); |
| 80 | } catch (Exception e) { |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 81 | if (!Utilities.isBinderSizeError(e)) { |
Adam Cohen | 084c318 | 2014-06-16 15:22:56 -0700 | [diff] [blame] | 82 | throw new RuntimeException(e); |
| 83 | } |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 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. |
Adam Cohen | 084c318 | 2014-06-16 15:22:56 -0700 | [diff] [blame] | 88 | } |
Sunny Goyal | 29947f0 | 2017-12-18 13:49:44 -0800 | [diff] [blame] | 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 | } |
Adam Cohen | 084c318 | 2014-06-16 15:22:56 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 99 | @Override |
| 100 | public void stopListening() { |
| 101 | if (FeatureFlags.GO_DISABLE_WIDGETS) { |
| 102 | return; |
| 103 | } |
Sunny Goyal | 67419a1 | 2017-11-14 16:55:22 -0800 | [diff] [blame] | 104 | mFlags &= ~FLAG_LISTENING; |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 105 | super.stopListening(); |
| 106 | } |
| 107 | |
Sunny Goyal | 67419a1 | 2017-11-14 16:55:22 -0800 | [diff] [blame] | 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 | */ |
| 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 Goyal | 67419a1 | 2017-11-14 16:55:22 -0800 | [diff] [blame] | 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 | |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 154 | @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 Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 164 | mProviderChangeListeners.add(callback); |
| 165 | } |
| 166 | |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 167 | public void removeProviderChangeListener(ProviderChangedListener callback) { |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 168 | mProviderChangeListeners.remove(callback); |
| 169 | } |
| 170 | |
Winson Chung | a3f78e3 | 2012-06-14 11:59:51 -0700 | [diff] [blame] | 171 | protected void onProvidersChanged() { |
Sunny Goyal | 7e2a360 | 2015-04-20 18:19:25 -0700 | [diff] [blame] | 172 | if (!mProviderChangeListeners.isEmpty()) { |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 173 | for (ProviderChangedListener callback : new ArrayList<>(mProviderChangeListeners)) { |
| 174 | callback.notifyWidgetProvidersChanged(); |
Sunny Goyal | 7e2a360 | 2015-04-20 18:19:25 -0700 | [diff] [blame] | 175 | } |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 176 | } |
Winson Chung | a3f78e3 | 2012-06-14 11:59:51 -0700 | [diff] [blame] | 177 | } |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 178 | |
| 179 | public AppWidgetHostView createView(Context context, int appWidgetId, |
| 180 | LauncherAppWidgetProviderInfo appWidget) { |
Sunny Goyal | 952e63d | 2017-08-16 04:59:08 -0700 | [diff] [blame] | 181 | if (appWidget.isCustomWidget()) { |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 182 | LauncherAppWidgetHostView lahv = new LauncherAppWidgetHostView(context); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 183 | lahv.setAppWidget(0, appWidget); |
Pinyao Ting | c7a6c29 | 2019-08-26 14:36:02 -0700 | [diff] [blame^] | 184 | CustomWidgetManager.INSTANCE.get(context).onViewCreated(lahv); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 185 | return lahv; |
Sunny Goyal | 29947f0 | 2017-12-18 13:49:44 -0800 | [diff] [blame] | 186 | } else if ((mFlags & FLAG_LISTENING) == 0) { |
| 187 | DeferredAppWidgetHostView view = new DeferredAppWidgetHostView(context); |
| 188 | view.setAppWidget(appWidgetId, appWidget); |
| 189 | mViews.put(appWidgetId, view); |
| 190 | return view; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 191 | } else { |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 192 | try { |
| 193 | return super.createView(context, appWidgetId, appWidget); |
| 194 | } catch (Exception e) { |
| 195 | if (!Utilities.isBinderSizeError(e)) { |
| 196 | throw new RuntimeException(e); |
| 197 | } |
| 198 | |
| 199 | // If the exception was thrown while fetching the remote views, let the view stay. |
| 200 | // This will ensure that if the widget posts a valid update later, the view |
| 201 | // will update. |
| 202 | LauncherAppWidgetHostView view = mViews.get(appWidgetId); |
| 203 | if (view == null) { |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 204 | view = onCreateView(mContext, appWidgetId, appWidget); |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 205 | } |
| 206 | view.setAppWidget(appWidgetId, appWidget); |
| 207 | view.switchToErrorView(); |
| 208 | return view; |
| 209 | } |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Called when the AppWidget provider for a AppWidget has been upgraded to a new apk. |
| 215 | */ |
| 216 | @Override |
| 217 | protected void onProviderChanged(int appWidgetId, AppWidgetProviderInfo appWidget) { |
| 218 | LauncherAppWidgetProviderInfo info = LauncherAppWidgetProviderInfo.fromProviderInfo( |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 219 | mContext, appWidget); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 220 | super.onProviderChanged(appWidgetId, info); |
Sunny Goyal | ec88204 | 2015-10-05 10:36:54 -0700 | [diff] [blame] | 221 | // The super method updates the dimensions of the providerInfo. Update the |
| 222 | // launcher spans accordingly. |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 223 | info.initSpans(mContext); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 224 | } |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 225 | |
| 226 | @Override |
| 227 | public void deleteAppWidgetId(int appWidgetId) { |
| 228 | super.deleteAppWidgetId(appWidgetId); |
| 229 | mViews.remove(appWidgetId); |
| 230 | } |
| 231 | |
| 232 | @Override |
Sunny Goyal | c11fac3 | 2018-02-27 19:20:15 -0800 | [diff] [blame] | 233 | public void clearViews() { |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 234 | super.clearViews(); |
| 235 | mViews.clear(); |
| 236 | } |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 237 | |
| 238 | public void startBindFlow(BaseActivity activity, |
| 239 | int appWidgetId, AppWidgetProviderInfo info, int requestCode) { |
| 240 | |
| 241 | if (FeatureFlags.GO_DISABLE_WIDGETS) { |
| 242 | sendActionCancelled(activity, requestCode); |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND) |
| 247 | .putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId) |
| 248 | .putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.provider) |
| 249 | .putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE, info.getProfile()); |
| 250 | // TODO: we need to make sure that this accounts for the options bundle. |
| 251 | // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options); |
| 252 | activity.startActivityForResult(intent, requestCode); |
| 253 | } |
| 254 | |
| 255 | |
| 256 | public void startConfigActivity(BaseActivity activity, int widgetId, int requestCode) { |
| 257 | if (FeatureFlags.GO_DISABLE_WIDGETS) { |
| 258 | sendActionCancelled(activity, requestCode); |
| 259 | return; |
| 260 | } |
| 261 | |
| 262 | try { |
| 263 | startAppWidgetConfigureActivityForResult(activity, widgetId, 0, requestCode, null); |
| 264 | } catch (ActivityNotFoundException | SecurityException e) { |
| 265 | Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); |
| 266 | sendActionCancelled(activity, requestCode); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | private void sendActionCancelled(final BaseActivity activity, final int requestCode) { |
Sunny Goyal | 67419a1 | 2017-11-14 16:55:22 -0800 | [diff] [blame] | 271 | new Handler().post(() -> activity.onActivityResult(requestCode, RESULT_CANCELED, null)); |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Listener for getting notifications on provider changes. |
| 276 | */ |
| 277 | public interface ProviderChangedListener { |
| 278 | |
| 279 | void notifyWidgetProvidersChanged(); |
| 280 | } |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 281 | } |