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