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