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 | |
| 19 | import android.appwidget.AppWidgetHost; |
| 20 | import android.appwidget.AppWidgetHostView; |
| 21 | import android.appwidget.AppWidgetProviderInfo; |
| 22 | import android.content.Context; |
Sunny Goyal | 1b32379 | 2015-11-05 16:09:57 +0530 | [diff] [blame] | 23 | import android.os.DeadObjectException; |
Adam Cohen | 084c318 | 2014-06-16 15:22:56 -0700 | [diff] [blame] | 24 | import android.os.TransactionTooLargeException; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 25 | import android.view.LayoutInflater; |
Sunny Goyal | 8d59509 | 2015-07-06 12:22:14 -0700 | [diff] [blame] | 26 | import android.view.View; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 27 | |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 28 | import java.util.ArrayList; |
| 29 | |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 30 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 31 | /** |
| 32 | * Specific {@link AppWidgetHost} that creates our {@link LauncherAppWidgetHostView} |
| 33 | * which correctly captures all long-press events. This ensures that users can |
| 34 | * always pick up and move widgets. |
| 35 | */ |
| 36 | public class LauncherAppWidgetHost extends AppWidgetHost { |
Winson Chung | a3f78e3 | 2012-06-14 11:59:51 -0700 | [diff] [blame] | 37 | |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 38 | private final ArrayList<Runnable> mProviderChangeListeners = new ArrayList<Runnable>(); |
| 39 | |
Sunny Goyal | 8d59509 | 2015-07-06 12:22:14 -0700 | [diff] [blame] | 40 | private int mQsbWidgetId = -1; |
| 41 | private Launcher mLauncher; |
Winson Chung | a3f78e3 | 2012-06-14 11:59:51 -0700 | [diff] [blame] | 42 | |
| 43 | public LauncherAppWidgetHost(Launcher launcher, int hostId) { |
| 44 | super(launcher, hostId); |
| 45 | mLauncher = launcher; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 46 | } |
Adam Cohen | 9415d87 | 2010-09-13 14:49:43 -0700 | [diff] [blame] | 47 | |
Sunny Goyal | 8d59509 | 2015-07-06 12:22:14 -0700 | [diff] [blame] | 48 | public void setQsbWidgetId(int widgetId) { |
| 49 | mQsbWidgetId = widgetId; |
| 50 | } |
| 51 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 52 | @Override |
| 53 | protected AppWidgetHostView onCreateView(Context context, int appWidgetId, |
| 54 | AppWidgetProviderInfo appWidget) { |
Sunny Goyal | 8d59509 | 2015-07-06 12:22:14 -0700 | [diff] [blame] | 55 | if (appWidgetId == mQsbWidgetId) { |
| 56 | return new LauncherAppWidgetHostView(context) { |
| 57 | |
| 58 | @Override |
| 59 | protected View getErrorView() { |
| 60 | // For the QSB, show an empty view instead of an error view. |
| 61 | return new View(getContext()); |
| 62 | } |
| 63 | }; |
| 64 | } |
Michael Jurka | 16fed41 | 2010-10-01 16:12:03 -0700 | [diff] [blame] | 65 | return new LauncherAppWidgetHostView(context); |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 66 | } |
Patrick Dubroy | 2313eff | 2011-01-11 20:01:31 -0800 | [diff] [blame] | 67 | |
| 68 | @Override |
Adam Cohen | 084c318 | 2014-06-16 15:22:56 -0700 | [diff] [blame] | 69 | public void startListening() { |
| 70 | try { |
| 71 | super.startListening(); |
| 72 | } catch (Exception e) { |
Sunny Goyal | 1b32379 | 2015-11-05 16:09:57 +0530 | [diff] [blame] | 73 | if (e.getCause() instanceof TransactionTooLargeException || |
| 74 | e.getCause() instanceof DeadObjectException) { |
Adam Cohen | 084c318 | 2014-06-16 15:22:56 -0700 | [diff] [blame] | 75 | // We're willing to let this slide. The exception is being caused by the list of |
| 76 | // RemoteViews which is being passed back. The startListening relationship will |
| 77 | // have been established by this point, and we will end up populating the |
| 78 | // widgets upon bind anyway. See issue 14255011 for more context. |
| 79 | } else { |
| 80 | throw new RuntimeException(e); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | @Override |
Patrick Dubroy | 2313eff | 2011-01-11 20:01:31 -0800 | [diff] [blame] | 86 | public void stopListening() { |
| 87 | super.stopListening(); |
| 88 | clearViews(); |
| 89 | } |
Winson Chung | a3f78e3 | 2012-06-14 11:59:51 -0700 | [diff] [blame] | 90 | |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 91 | public void addProviderChangeListener(Runnable callback) { |
| 92 | mProviderChangeListeners.add(callback); |
| 93 | } |
| 94 | |
| 95 | public void removeProviderChangeListener(Runnable callback) { |
| 96 | mProviderChangeListeners.remove(callback); |
| 97 | } |
| 98 | |
Winson Chung | a3f78e3 | 2012-06-14 11:59:51 -0700 | [diff] [blame] | 99 | protected void onProvidersChanged() { |
Sunny Goyal | 7e2a360 | 2015-04-20 18:19:25 -0700 | [diff] [blame] | 100 | if (!mProviderChangeListeners.isEmpty()) { |
| 101 | for (Runnable callback : new ArrayList<>(mProviderChangeListeners)) { |
| 102 | callback.run(); |
| 103 | } |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 104 | } |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame^] | 105 | |
| 106 | if (Utilities.ATLEAST_MARSHMALLOW) { |
| 107 | mLauncher.notifyWidgetProvidersChanged(); |
| 108 | } |
Winson Chung | a3f78e3 | 2012-06-14 11:59:51 -0700 | [diff] [blame] | 109 | } |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 110 | |
| 111 | public AppWidgetHostView createView(Context context, int appWidgetId, |
| 112 | LauncherAppWidgetProviderInfo appWidget) { |
| 113 | if (appWidget.isCustomWidget) { |
| 114 | LauncherAppWidgetHostView lahv = new LauncherAppWidgetHostView(context); |
| 115 | LayoutInflater inflater = (LayoutInflater) |
| 116 | context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 117 | inflater.inflate(appWidget.initialLayout, lahv); |
| 118 | lahv.setAppWidget(0, appWidget); |
| 119 | lahv.updateLastInflationOrientation(); |
| 120 | return lahv; |
| 121 | } else { |
| 122 | return super.createView(context, appWidgetId, appWidget); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Called when the AppWidget provider for a AppWidget has been upgraded to a new apk. |
| 128 | */ |
| 129 | @Override |
| 130 | protected void onProviderChanged(int appWidgetId, AppWidgetProviderInfo appWidget) { |
| 131 | LauncherAppWidgetProviderInfo info = LauncherAppWidgetProviderInfo.fromProviderInfo( |
| 132 | mLauncher, appWidget); |
| 133 | super.onProviderChanged(appWidgetId, info); |
Sunny Goyal | ec88204 | 2015-10-05 10:36:54 -0700 | [diff] [blame] | 134 | // The super method updates the dimensions of the providerInfo. Update the |
| 135 | // launcher spans accordingly. |
| 136 | info.initSpans(); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 137 | } |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 138 | } |