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; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 30 | import android.view.LayoutInflater; |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 31 | import android.widget.Toast; |
| 32 | |
| 33 | import com.android.launcher3.config.FeatureFlags; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 34 | |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 35 | import java.util.ArrayList; |
| 36 | |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 37 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 38 | /** |
| 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 | */ |
| 43 | public class LauncherAppWidgetHost extends AppWidgetHost { |
Winson Chung | a3f78e3 | 2012-06-14 11:59:51 -0700 | [diff] [blame] | 44 | |
Sunny Goyal | 67419a1 | 2017-11-14 16:55:22 -0800 | [diff] [blame^] | 45 | 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 Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 49 | public static final int APPWIDGET_HOST_ID = 1024; |
| 50 | |
| 51 | private final ArrayList<ProviderChangedListener> mProviderChangeListeners = new ArrayList<>(); |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 52 | private final SparseArray<LauncherAppWidgetHostView> mViews = new SparseArray<>(); |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 53 | |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 54 | private final Context mContext; |
Sunny Goyal | 67419a1 | 2017-11-14 16:55:22 -0800 | [diff] [blame^] | 55 | private int mFlags = FLAG_RESUMED; |
Winson Chung | a3f78e3 | 2012-06-14 11:59:51 -0700 | [diff] [blame] | 56 | |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 57 | public LauncherAppWidgetHost(Context context) { |
| 58 | super(context, APPWIDGET_HOST_ID); |
| 59 | mContext = context; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 60 | } |
Adam Cohen | 9415d87 | 2010-09-13 14:49:43 -0700 | [diff] [blame] | 61 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 62 | @Override |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 63 | protected LauncherAppWidgetHostView onCreateView(Context context, int appWidgetId, |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 64 | AppWidgetProviderInfo appWidget) { |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 65 | LauncherAppWidgetHostView view = new LauncherAppWidgetHostView(context); |
| 66 | mViews.put(appWidgetId, view); |
| 67 | return view; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 68 | } |
Patrick Dubroy | 2313eff | 2011-01-11 20:01:31 -0800 | [diff] [blame] | 69 | |
| 70 | @Override |
Adam Cohen | 084c318 | 2014-06-16 15:22:56 -0700 | [diff] [blame] | 71 | public void startListening() { |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 72 | if (FeatureFlags.GO_DISABLE_WIDGETS) { |
| 73 | return; |
| 74 | } |
Sunny Goyal | 67419a1 | 2017-11-14 16:55:22 -0800 | [diff] [blame^] | 75 | mFlags |= FLAG_LISTENING; |
Adam Cohen | 084c318 | 2014-06-16 15:22:56 -0700 | [diff] [blame] | 76 | try { |
| 77 | super.startListening(); |
| 78 | } catch (Exception e) { |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 79 | if (!Utilities.isBinderSizeError(e)) { |
Adam Cohen | 084c318 | 2014-06-16 15:22:56 -0700 | [diff] [blame] | 80 | throw new RuntimeException(e); |
| 81 | } |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 82 | // 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 Cohen | 084c318 | 2014-06-16 15:22:56 -0700 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 89 | @Override |
| 90 | public void stopListening() { |
| 91 | if (FeatureFlags.GO_DISABLE_WIDGETS) { |
| 92 | return; |
| 93 | } |
Sunny Goyal | 67419a1 | 2017-11-14 16:55:22 -0800 | [diff] [blame^] | 94 | mFlags &= ~FLAG_LISTENING; |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 95 | super.stopListening(); |
| 96 | } |
| 97 | |
Sunny Goyal | 67419a1 | 2017-11-14 16:55:22 -0800 | [diff] [blame^] | 98 | /** |
| 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 Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 147 | @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 Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 157 | mProviderChangeListeners.add(callback); |
| 158 | } |
| 159 | |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 160 | public void removeProviderChangeListener(ProviderChangedListener callback) { |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 161 | mProviderChangeListeners.remove(callback); |
| 162 | } |
| 163 | |
Winson Chung | a3f78e3 | 2012-06-14 11:59:51 -0700 | [diff] [blame] | 164 | protected void onProvidersChanged() { |
Sunny Goyal | 7e2a360 | 2015-04-20 18:19:25 -0700 | [diff] [blame] | 165 | if (!mProviderChangeListeners.isEmpty()) { |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 166 | for (ProviderChangedListener callback : new ArrayList<>(mProviderChangeListeners)) { |
| 167 | callback.notifyWidgetProvidersChanged(); |
Sunny Goyal | 7e2a360 | 2015-04-20 18:19:25 -0700 | [diff] [blame] | 168 | } |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 169 | } |
Winson Chung | a3f78e3 | 2012-06-14 11:59:51 -0700 | [diff] [blame] | 170 | } |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 171 | |
| 172 | public AppWidgetHostView createView(Context context, int appWidgetId, |
| 173 | LauncherAppWidgetProviderInfo appWidget) { |
Sunny Goyal | 952e63d | 2017-08-16 04:59:08 -0700 | [diff] [blame] | 174 | if (appWidget.isCustomWidget()) { |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 175 | 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 Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 180 | return lahv; |
| 181 | } else { |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 182 | 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 Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 194 | view = onCreateView(mContext, appWidgetId, appWidget); |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 195 | } |
| 196 | view.setAppWidget(appWidgetId, appWidget); |
| 197 | view.switchToErrorView(); |
| 198 | return view; |
| 199 | } |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 200 | } |
| 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 Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 209 | mContext, appWidget); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 210 | super.onProviderChanged(appWidgetId, info); |
Sunny Goyal | ec88204 | 2015-10-05 10:36:54 -0700 | [diff] [blame] | 211 | // The super method updates the dimensions of the providerInfo. Update the |
| 212 | // launcher spans accordingly. |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 213 | info.initSpans(mContext); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 214 | } |
Sunny Goyal | 712ee53 | 2016-11-04 10:19:58 -0700 | [diff] [blame] | 215 | |
| 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 Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 227 | |
| 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 Goyal | 67419a1 | 2017-11-14 16:55:22 -0800 | [diff] [blame^] | 261 | new Handler().post(() -> activity.onActivityResult(requestCode, RESULT_CANCELED, null)); |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Listener for getting notifications on provider changes. |
| 266 | */ |
| 267 | public interface ProviderChangedListener { |
| 268 | |
| 269 | void notifyWidgetProvidersChanged(); |
| 270 | } |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 271 | } |