Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Sunny Goyal | 2e013ea | 2016-10-07 16:17:19 -0700 | [diff] [blame] | 17 | package com.android.launcher3.qsb; |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 18 | |
Sunny Goyal | 95cded5 | 2018-06-14 20:08:57 -0700 | [diff] [blame] | 19 | import static android.appwidget.AppWidgetManager.ACTION_APPWIDGET_BIND; |
| 20 | import static android.appwidget.AppWidgetManager.EXTRA_APPWIDGET_ID; |
| 21 | import static android.appwidget.AppWidgetManager.EXTRA_APPWIDGET_PROVIDER; |
| 22 | |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 23 | import android.app.Activity; |
| 24 | import android.app.Fragment; |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 25 | import android.app.SearchManager; |
Sunny Goyal | 2e013ea | 2016-10-07 16:17:19 -0700 | [diff] [blame] | 26 | import android.appwidget.AppWidgetHost; |
| 27 | import android.appwidget.AppWidgetHostView; |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 28 | import android.appwidget.AppWidgetManager; |
| 29 | import android.appwidget.AppWidgetProviderInfo; |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 30 | import android.content.ComponentName; |
| 31 | import android.content.Context; |
| 32 | import android.content.Intent; |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 33 | import android.graphics.Rect; |
| 34 | import android.os.Bundle; |
Samuel Fufa | 9ec8cfa | 2019-08-12 16:19:41 -0700 | [diff] [blame] | 35 | import android.provider.Settings; |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 36 | import android.util.AttributeSet; |
| 37 | import android.view.LayoutInflater; |
| 38 | import android.view.View; |
| 39 | import android.view.ViewGroup; |
| 40 | import android.widget.FrameLayout; |
| 41 | |
Samuel Fufa | ca37b8a | 2019-08-19 17:04:36 -0700 | [diff] [blame] | 42 | import androidx.annotation.NonNull; |
Samuel Fufa | 9ec8cfa | 2019-08-12 16:19:41 -0700 | [diff] [blame] | 43 | import androidx.annotation.Nullable; |
| 44 | |
Sunny Goyal | 2e013ea | 2016-10-07 16:17:19 -0700 | [diff] [blame] | 45 | import com.android.launcher3.AppWidgetResizeFrame; |
| 46 | import com.android.launcher3.InvariantDeviceProfile; |
| 47 | import com.android.launcher3.LauncherAppState; |
| 48 | import com.android.launcher3.R; |
| 49 | import com.android.launcher3.Utilities; |
Louis Begin | b07a355 | 2017-02-27 14:33:11 +0800 | [diff] [blame] | 50 | import com.android.launcher3.config.FeatureFlags; |
Sunny Goyal | ef92b82 | 2018-11-21 14:12:00 -0800 | [diff] [blame] | 51 | import com.android.launcher3.graphics.FragmentWithPreview; |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * A frame layout which contains a QSB. This internally uses fragment to bind the view, which |
| 55 | * allows it to contain the logic for {@link Fragment#startActivityForResult(Intent, int)}. |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 56 | * |
Sunny Goyal | 337c81f | 2019-12-10 12:19:13 -0800 | [diff] [blame] | 57 | * Note: WidgetManagerHelper can be disabled using FeatureFlags. In QSB, we should use |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 58 | * AppWidgetManager directly, so that it keeps working in that case. |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 59 | */ |
| 60 | public class QsbContainerView extends FrameLayout { |
| 61 | |
Samuel Fufa | 9ec8cfa | 2019-08-12 16:19:41 -0700 | [diff] [blame] | 62 | public static final String SEARCH_PROVIDER_SETTINGS_KEY = "SEARCH_PROVIDER_PACKAGE_NAME"; |
| 63 | |
Samuel Fufa | ca37b8a | 2019-08-19 17:04:36 -0700 | [diff] [blame] | 64 | /** |
| 65 | * Returns the package name for user configured search provider or from searchManager |
| 66 | * @param context |
| 67 | * @return String |
| 68 | */ |
| 69 | @Nullable |
| 70 | public static String getSearchWidgetPackageName(@NonNull Context context) { |
| 71 | String providerPkg = Settings.Global.getString(context.getContentResolver(), |
| 72 | SEARCH_PROVIDER_SETTINGS_KEY); |
| 73 | if (providerPkg == null) { |
| 74 | SearchManager searchManager = context.getSystemService(SearchManager.class); |
| 75 | ComponentName componentName = searchManager.getGlobalSearchActivity(); |
| 76 | if (componentName != null) { |
| 77 | providerPkg = searchManager.getGlobalSearchActivity().getPackageName(); |
| 78 | } |
| 79 | } |
| 80 | return providerPkg; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * returns it's AppWidgetProviderInfo using package name from getSearchWidgetPackageName |
| 85 | * @param context |
| 86 | * @return AppWidgetProviderInfo |
| 87 | */ |
| 88 | @Nullable |
| 89 | public static AppWidgetProviderInfo getSearchWidgetProviderInfo(@NonNull Context context) { |
| 90 | String providerPkg = getSearchWidgetPackageName(context); |
| 91 | if (providerPkg == null) { |
| 92 | return null; |
| 93 | } |
| 94 | |
| 95 | AppWidgetProviderInfo defaultWidgetForSearchPackage = null; |
| 96 | AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); |
| 97 | for (AppWidgetProviderInfo info : |
| 98 | appWidgetManager.getInstalledProvidersForPackage(providerPkg, null)) { |
| 99 | if (info.provider.getPackageName().equals(providerPkg) && info.configure == null) { |
| 100 | if ((info.widgetCategory |
| 101 | & AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX) != 0) { |
| 102 | return info; |
| 103 | } else if (defaultWidgetForSearchPackage == null) { |
| 104 | defaultWidgetForSearchPackage = info; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | return defaultWidgetForSearchPackage; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * returns componentName for searchWidget if package name is known. |
| 113 | */ |
| 114 | @Nullable |
| 115 | public static ComponentName getSearchComponentName(@NonNull Context context) { |
| 116 | AppWidgetProviderInfo providerInfo = |
| 117 | QsbContainerView.getSearchWidgetProviderInfo(context); |
| 118 | if (providerInfo != null) { |
| 119 | return providerInfo.provider; |
| 120 | } else { |
| 121 | String pkgName = QsbContainerView.getSearchWidgetPackageName(context); |
| 122 | if (pkgName != null) { |
| 123 | //we don't know the class name yet. we'll put the package name as placeholder |
| 124 | return new ComponentName(pkgName, pkgName); |
| 125 | } |
| 126 | return null; |
| 127 | } |
| 128 | } |
| 129 | |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 130 | public QsbContainerView(Context context) { |
| 131 | super(context); |
| 132 | } |
| 133 | |
| 134 | public QsbContainerView(Context context, AttributeSet attrs) { |
| 135 | super(context, attrs); |
| 136 | } |
| 137 | |
| 138 | public QsbContainerView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 139 | super(context, attrs, defStyleAttr); |
| 140 | } |
| 141 | |
| 142 | @Override |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 143 | public void setPadding(int left, int top, int right, int bottom) { |
| 144 | super.setPadding(0, 0, 0, 0); |
| 145 | } |
| 146 | |
Sunny Goyal | d0f43ce | 2018-05-30 17:35:24 -0700 | [diff] [blame] | 147 | protected void setPaddingUnchecked(int left, int top, int right, int bottom) { |
| 148 | super.setPadding(left, top, right, bottom); |
| 149 | } |
| 150 | |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 151 | /** |
| 152 | * A fragment to display the QSB. |
| 153 | */ |
Sunny Goyal | ef92b82 | 2018-11-21 14:12:00 -0800 | [diff] [blame] | 154 | public static class QsbFragment extends FragmentWithPreview { |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 155 | |
Sunny Goyal | 95cded5 | 2018-06-14 20:08:57 -0700 | [diff] [blame] | 156 | public static final int QSB_WIDGET_HOST_ID = 1026; |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 157 | private static final int REQUEST_BIND_QSB = 1; |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 158 | |
Sunny Goyal | 95cded5 | 2018-06-14 20:08:57 -0700 | [diff] [blame] | 159 | protected String mKeyWidgetId = "qsb_widget_id"; |
Sunny Goyal | 2e013ea | 2016-10-07 16:17:19 -0700 | [diff] [blame] | 160 | private QsbWidgetHost mQsbWidgetHost; |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 161 | private AppWidgetProviderInfo mWidgetInfo; |
Sunny Goyal | 2e013ea | 2016-10-07 16:17:19 -0700 | [diff] [blame] | 162 | private QsbWidgetHostView mQsb; |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 163 | |
Jon Miranda | 11ee2f6 | 2017-09-20 10:18:41 -0700 | [diff] [blame] | 164 | // We need to store the orientation here, due to a bug (b/64916689) that results in widgets |
| 165 | // being inflated in the wrong orientation. |
| 166 | private int mOrientation; |
| 167 | |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 168 | @Override |
Sunny Goyal | ef92b82 | 2018-11-21 14:12:00 -0800 | [diff] [blame] | 169 | public void onInit(Bundle savedInstanceState) { |
Sunny Goyal | 95cded5 | 2018-06-14 20:08:57 -0700 | [diff] [blame] | 170 | mQsbWidgetHost = createHost(); |
Jon Miranda | 11ee2f6 | 2017-09-20 10:18:41 -0700 | [diff] [blame] | 171 | mOrientation = getContext().getResources().getConfiguration().orientation; |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Sunny Goyal | 95cded5 | 2018-06-14 20:08:57 -0700 | [diff] [blame] | 174 | protected QsbWidgetHost createHost() { |
Sunny Goyal | ef92b82 | 2018-11-21 14:12:00 -0800 | [diff] [blame] | 175 | return new QsbWidgetHost(getContext(), QSB_WIDGET_HOST_ID, |
Samuel Fufa | 9ec8cfa | 2019-08-12 16:19:41 -0700 | [diff] [blame] | 176 | (c) -> new QsbWidgetHostView(c), this::rebindFragment); |
Sunny Goyal | 95cded5 | 2018-06-14 20:08:57 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Sunny Goyal | 02d3d43 | 2016-06-03 13:37:01 -0700 | [diff] [blame] | 179 | private FrameLayout mWrapper; |
| 180 | |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 181 | @Override |
| 182 | public View onCreateView( |
| 183 | LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 184 | |
Sunny Goyal | ef92b82 | 2018-11-21 14:12:00 -0800 | [diff] [blame] | 185 | mWrapper = new FrameLayout(getContext()); |
Louis Begin | b07a355 | 2017-02-27 14:33:11 +0800 | [diff] [blame] | 186 | // Only add the view when enabled |
Sunny Goyal | d0f43ce | 2018-05-30 17:35:24 -0700 | [diff] [blame] | 187 | if (isQsbEnabled()) { |
Samuel Fufa | ca37b8a | 2019-08-19 17:04:36 -0700 | [diff] [blame] | 188 | mQsbWidgetHost.startListening(); |
Louis Begin | b07a355 | 2017-02-27 14:33:11 +0800 | [diff] [blame] | 189 | mWrapper.addView(createQsb(mWrapper)); |
| 190 | } |
Sunny Goyal | 02d3d43 | 2016-06-03 13:37:01 -0700 | [diff] [blame] | 191 | return mWrapper; |
| 192 | } |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 193 | |
Sunny Goyal | 2e013ea | 2016-10-07 16:17:19 -0700 | [diff] [blame] | 194 | private View createQsb(ViewGroup container) { |
Sunny Goyal | 95cded5 | 2018-06-14 20:08:57 -0700 | [diff] [blame] | 195 | mWidgetInfo = getSearchWidgetProvider(); |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 196 | if (mWidgetInfo == null) { |
| 197 | // There is no search provider, just show the default widget. |
Sunny Goyal | 95cded5 | 2018-06-14 20:08:57 -0700 | [diff] [blame] | 198 | return getDefaultView(container, false /* show setup icon */); |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 199 | } |
Sunny Goyal | 95cded5 | 2018-06-14 20:08:57 -0700 | [diff] [blame] | 200 | Bundle opts = createBindOptions(); |
Sunny Goyal | ef92b82 | 2018-11-21 14:12:00 -0800 | [diff] [blame] | 201 | Context context = getContext(); |
| 202 | AppWidgetManager widgetManager = AppWidgetManager.getInstance(context); |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 203 | |
Sunny Goyal | ef92b82 | 2018-11-21 14:12:00 -0800 | [diff] [blame] | 204 | int widgetId = Utilities.getPrefs(context).getInt(mKeyWidgetId, -1); |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 205 | AppWidgetProviderInfo widgetInfo = widgetManager.getAppWidgetInfo(widgetId); |
| 206 | boolean isWidgetBound = (widgetInfo != null) && |
| 207 | widgetInfo.provider.equals(mWidgetInfo.provider); |
| 208 | |
Sunny Goyal | 2e013ea | 2016-10-07 16:17:19 -0700 | [diff] [blame] | 209 | int oldWidgetId = widgetId; |
Sunny Goyal | ef92b82 | 2018-11-21 14:12:00 -0800 | [diff] [blame] | 210 | if (!isWidgetBound && !isInPreviewMode()) { |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 211 | if (widgetId > -1) { |
Sunny Goyal | 2e013ea | 2016-10-07 16:17:19 -0700 | [diff] [blame] | 212 | // widgetId is already bound and its not the correct provider. reset host. |
| 213 | mQsbWidgetHost.deleteHost(); |
| 214 | } |
| 215 | |
| 216 | widgetId = mQsbWidgetHost.allocateAppWidgetId(); |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 217 | isWidgetBound = widgetManager.bindAppWidgetIdIfAllowed( |
| 218 | widgetId, mWidgetInfo.getProfile(), mWidgetInfo.provider, opts); |
Sunny Goyal | 2e013ea | 2016-10-07 16:17:19 -0700 | [diff] [blame] | 219 | if (!isWidgetBound) { |
| 220 | mQsbWidgetHost.deleteAppWidgetId(widgetId); |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 221 | widgetId = -1; |
| 222 | } |
| 223 | |
Sunny Goyal | 2e013ea | 2016-10-07 16:17:19 -0700 | [diff] [blame] | 224 | if (oldWidgetId != widgetId) { |
| 225 | saveWidgetId(widgetId); |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
| 229 | if (isWidgetBound) { |
Samuel Fufa | ca37b8a | 2019-08-19 17:04:36 -0700 | [diff] [blame] | 230 | mQsb = (QsbWidgetHostView) mQsbWidgetHost.createView(context, widgetId, |
| 231 | mWidgetInfo); |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 232 | mQsb.setId(R.id.qsb_widget); |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 233 | |
Sunny Goyal | ef92b82 | 2018-11-21 14:12:00 -0800 | [diff] [blame] | 234 | if (!isInPreviewMode()) { |
Sunny Goyal | aae6fbb | 2019-01-31 16:05:58 -0800 | [diff] [blame] | 235 | if (!containsAll(AppWidgetManager.getInstance(context) |
Sunny Goyal | ef92b82 | 2018-11-21 14:12:00 -0800 | [diff] [blame] | 236 | .getAppWidgetOptions(widgetId), opts)) { |
| 237 | mQsb.updateAppWidgetOptions(opts); |
| 238 | } |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 239 | } |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 240 | return mQsb; |
| 241 | } |
| 242 | |
| 243 | // Return a default widget with setup icon. |
Sunny Goyal | 95cded5 | 2018-06-14 20:08:57 -0700 | [diff] [blame] | 244 | return getDefaultView(container, true /* show setup icon */); |
Sunny Goyal | 2e013ea | 2016-10-07 16:17:19 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | private void saveWidgetId(int widgetId) { |
Sunny Goyal | ef92b82 | 2018-11-21 14:12:00 -0800 | [diff] [blame] | 248 | Utilities.getPrefs(getContext()).edit().putInt(mKeyWidgetId, widgetId).apply(); |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | @Override |
| 252 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 253 | if (requestCode == REQUEST_BIND_QSB) { |
| 254 | if (resultCode == Activity.RESULT_OK) { |
Sunny Goyal | 95cded5 | 2018-06-14 20:08:57 -0700 | [diff] [blame] | 255 | saveWidgetId(data.getIntExtra(EXTRA_APPWIDGET_ID, -1)); |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 256 | rebindFragment(); |
Sunny Goyal | 2e013ea | 2016-10-07 16:17:19 -0700 | [diff] [blame] | 257 | } else { |
| 258 | mQsbWidgetHost.deleteHost(); |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | @Override |
| 264 | public void onResume() { |
| 265 | super.onResume(); |
Jon Miranda | 11ee2f6 | 2017-09-20 10:18:41 -0700 | [diff] [blame] | 266 | if (mQsb != null && mQsb.isReinflateRequired(mOrientation)) { |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 267 | rebindFragment(); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | @Override |
| 272 | public void onDestroy() { |
Sunny Goyal | 2e013ea | 2016-10-07 16:17:19 -0700 | [diff] [blame] | 273 | mQsbWidgetHost.stopListening(); |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 274 | super.onDestroy(); |
| 275 | } |
| 276 | |
| 277 | private void rebindFragment() { |
Louis Begin | b07a355 | 2017-02-27 14:33:11 +0800 | [diff] [blame] | 278 | // Exit if the embedded qsb is disabled |
Sunny Goyal | d0f43ce | 2018-05-30 17:35:24 -0700 | [diff] [blame] | 279 | if (!isQsbEnabled()) { |
Louis Begin | b07a355 | 2017-02-27 14:33:11 +0800 | [diff] [blame] | 280 | return; |
| 281 | } |
| 282 | |
Sunny Goyal | ef92b82 | 2018-11-21 14:12:00 -0800 | [diff] [blame] | 283 | if (mWrapper != null && getContext() != null) { |
Sunny Goyal | 02d3d43 | 2016-06-03 13:37:01 -0700 | [diff] [blame] | 284 | mWrapper.removeAllViews(); |
Sunny Goyal | 2e013ea | 2016-10-07 16:17:19 -0700 | [diff] [blame] | 285 | mWrapper.addView(createQsb(mWrapper)); |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 286 | } |
| 287 | } |
Sunny Goyal | d0f43ce | 2018-05-30 17:35:24 -0700 | [diff] [blame] | 288 | |
| 289 | public boolean isQsbEnabled() { |
Jon Miranda | 7143ba6 | 2019-03-15 09:00:05 -0700 | [diff] [blame] | 290 | return FeatureFlags.QSB_ON_FIRST_SCREEN; |
Sunny Goyal | d0f43ce | 2018-05-30 17:35:24 -0700 | [diff] [blame] | 291 | } |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 292 | |
Sunny Goyal | 95cded5 | 2018-06-14 20:08:57 -0700 | [diff] [blame] | 293 | protected Bundle createBindOptions() { |
Sunny Goyal | ef92b82 | 2018-11-21 14:12:00 -0800 | [diff] [blame] | 294 | InvariantDeviceProfile idp = LauncherAppState.getIDP(getContext()); |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 295 | |
Sunny Goyal | 95cded5 | 2018-06-14 20:08:57 -0700 | [diff] [blame] | 296 | Bundle opts = new Bundle(); |
Sunny Goyal | ef92b82 | 2018-11-21 14:12:00 -0800 | [diff] [blame] | 297 | Rect size = AppWidgetResizeFrame.getWidgetSizeRanges(getContext(), |
Sunny Goyal | 95cded5 | 2018-06-14 20:08:57 -0700 | [diff] [blame] | 298 | idp.numColumns, 1, null); |
| 299 | opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, size.left); |
| 300 | opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, size.top); |
| 301 | opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, size.right); |
| 302 | opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, size.bottom); |
| 303 | return opts; |
| 304 | } |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 305 | |
Sunny Goyal | 95cded5 | 2018-06-14 20:08:57 -0700 | [diff] [blame] | 306 | protected View getDefaultView(ViewGroup container, boolean showSetupIcon) { |
| 307 | // Return a default widget with setup icon. |
| 308 | View v = QsbWidgetHostView.getDefaultView(container); |
| 309 | if (showSetupIcon) { |
| 310 | View setupButton = v.findViewById(R.id.btn_qsb_setup); |
| 311 | setupButton.setVisibility(View.VISIBLE); |
| 312 | setupButton.setOnClickListener((v2) -> startActivityForResult( |
| 313 | new Intent(ACTION_APPWIDGET_BIND) |
| 314 | .putExtra(EXTRA_APPWIDGET_ID, mQsbWidgetHost.allocateAppWidgetId()) |
| 315 | .putExtra(EXTRA_APPWIDGET_PROVIDER, mWidgetInfo.provider), |
| 316 | REQUEST_BIND_QSB)); |
| 317 | } |
| 318 | return v; |
| 319 | } |
| 320 | |
Samuel Fufa | 9ec8cfa | 2019-08-12 16:19:41 -0700 | [diff] [blame] | 321 | |
| 322 | /** |
Sunny Goyal | 95cded5 | 2018-06-14 20:08:57 -0700 | [diff] [blame] | 323 | * Returns a widget with category {@link AppWidgetProviderInfo#WIDGET_CATEGORY_SEARCHBOX} |
Samuel Fufa | 9ec8cfa | 2019-08-12 16:19:41 -0700 | [diff] [blame] | 324 | * provided by the package from getSearchProviderPackageName |
Sunny Goyal | 95cded5 | 2018-06-14 20:08:57 -0700 | [diff] [blame] | 325 | * If widgetCategory is not supported, or no such widget is found, returns the first widget |
| 326 | * provided by the package. |
| 327 | */ |
| 328 | protected AppWidgetProviderInfo getSearchWidgetProvider() { |
Samuel Fufa | ca37b8a | 2019-08-19 17:04:36 -0700 | [diff] [blame] | 329 | return getSearchWidgetProviderInfo(getContext()); |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 330 | } |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 331 | } |
Sunny Goyal | 2e013ea | 2016-10-07 16:17:19 -0700 | [diff] [blame] | 332 | |
Sunny Goyal | 95cded5 | 2018-06-14 20:08:57 -0700 | [diff] [blame] | 333 | public static class QsbWidgetHost extends AppWidgetHost { |
Sunny Goyal | 2e013ea | 2016-10-07 16:17:19 -0700 | [diff] [blame] | 334 | |
Sunny Goyal | 95cded5 | 2018-06-14 20:08:57 -0700 | [diff] [blame] | 335 | private final WidgetViewFactory mViewFactory; |
Samuel Fufa | 9ec8cfa | 2019-08-12 16:19:41 -0700 | [diff] [blame] | 336 | private final WidgetProvidersUpdateCallback mWidgetsUpdateCallback; |
Sunny Goyal | 2e013ea | 2016-10-07 16:17:19 -0700 | [diff] [blame] | 337 | |
Samuel Fufa | 9ec8cfa | 2019-08-12 16:19:41 -0700 | [diff] [blame] | 338 | public QsbWidgetHost(Context context, int hostId, WidgetViewFactory viewFactory, |
| 339 | WidgetProvidersUpdateCallback widgetProvidersUpdateCallback) { |
Sunny Goyal | 95cded5 | 2018-06-14 20:08:57 -0700 | [diff] [blame] | 340 | super(context, hostId); |
| 341 | mViewFactory = viewFactory; |
Samuel Fufa | 9ec8cfa | 2019-08-12 16:19:41 -0700 | [diff] [blame] | 342 | mWidgetsUpdateCallback = widgetProvidersUpdateCallback; |
| 343 | } |
| 344 | |
| 345 | public QsbWidgetHost(Context context, int hostId, WidgetViewFactory viewFactory) { |
| 346 | this(context, hostId, viewFactory, null); |
Sunny Goyal | 2e013ea | 2016-10-07 16:17:19 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | @Override |
| 350 | protected AppWidgetHostView onCreateView( |
| 351 | Context context, int appWidgetId, AppWidgetProviderInfo appWidget) { |
Sunny Goyal | 95cded5 | 2018-06-14 20:08:57 -0700 | [diff] [blame] | 352 | return mViewFactory.newView(context); |
Sunny Goyal | 2e013ea | 2016-10-07 16:17:19 -0700 | [diff] [blame] | 353 | } |
Samuel Fufa | 9ec8cfa | 2019-08-12 16:19:41 -0700 | [diff] [blame] | 354 | |
| 355 | @Override |
| 356 | protected void onProvidersChanged() { |
| 357 | super.onProvidersChanged(); |
| 358 | if (mWidgetsUpdateCallback != null) { |
| 359 | mWidgetsUpdateCallback.onProvidersUpdated(); |
| 360 | } |
| 361 | } |
Sunny Goyal | 2e013ea | 2016-10-07 16:17:19 -0700 | [diff] [blame] | 362 | } |
Sunny Goyal | 95cded5 | 2018-06-14 20:08:57 -0700 | [diff] [blame] | 363 | |
| 364 | public interface WidgetViewFactory { |
| 365 | |
| 366 | QsbWidgetHostView newView(Context context); |
| 367 | } |
Sunny Goyal | aae6fbb | 2019-01-31 16:05:58 -0800 | [diff] [blame] | 368 | |
| 369 | /** |
Samuel Fufa | 9ec8cfa | 2019-08-12 16:19:41 -0700 | [diff] [blame] | 370 | * Callback interface for packages list update. |
| 371 | */ |
| 372 | @FunctionalInterface |
| 373 | public interface WidgetProvidersUpdateCallback { |
| 374 | /** |
| 375 | * Gets called when widget providers list changes |
| 376 | */ |
| 377 | void onProvidersUpdated(); |
| 378 | } |
| 379 | |
| 380 | /** |
Sunny Goyal | aae6fbb | 2019-01-31 16:05:58 -0800 | [diff] [blame] | 381 | * Returns true if {@param original} contains all entries defined in {@param updates} and |
| 382 | * have the same value. |
| 383 | * The comparison uses {@link Object#equals(Object)} to compare the values. |
| 384 | */ |
| 385 | private static boolean containsAll(Bundle original, Bundle updates) { |
| 386 | for (String key : updates.keySet()) { |
| 387 | Object value1 = updates.get(key); |
| 388 | Object value2 = original.get(key); |
| 389 | if (value1 == null) { |
| 390 | if (value2 != null) { |
| 391 | return false; |
| 392 | } |
| 393 | } else if (!value1.equals(value2)) { |
| 394 | return false; |
| 395 | } |
| 396 | } |
| 397 | return true; |
| 398 | } |
Samuel Fufa | ca37b8a | 2019-08-19 17:04:36 -0700 | [diff] [blame] | 399 | |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 400 | } |