blob: 8c23ff30d7b825a452f48b548277399726bf8b7f [file] [log] [blame]
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001/*
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 Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070018
19import android.appwidget.AppWidgetHost;
20import android.appwidget.AppWidgetHostView;
21import android.appwidget.AppWidgetProviderInfo;
22import android.content.Context;
Sunny Goyal1b323792015-11-05 16:09:57 +053023import android.os.DeadObjectException;
Adam Cohen084c3182014-06-16 15:22:56 -070024import android.os.TransactionTooLargeException;
Adam Cohen59400422014-03-05 18:07:04 -080025import android.view.LayoutInflater;
Sunny Goyal8d595092015-07-06 12:22:14 -070026import android.view.View;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070027
Sunny Goyal0fc1be12014-08-11 17:05:23 -070028import java.util.ArrayList;
29
Adam Cohen59400422014-03-05 18:07:04 -080030
The Android Open Source Project7376fae2009-03-11 12:11:58 -070031/**
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 */
36public class LauncherAppWidgetHost extends AppWidgetHost {
Winson Chunga3f78e32012-06-14 11:59:51 -070037
Sunny Goyal0fc1be12014-08-11 17:05:23 -070038 private final ArrayList<Runnable> mProviderChangeListeners = new ArrayList<Runnable>();
39
Sunny Goyal8d595092015-07-06 12:22:14 -070040 private int mQsbWidgetId = -1;
41 private Launcher mLauncher;
Winson Chunga3f78e32012-06-14 11:59:51 -070042
43 public LauncherAppWidgetHost(Launcher launcher, int hostId) {
44 super(launcher, hostId);
45 mLauncher = launcher;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070046 }
Adam Cohen9415d872010-09-13 14:49:43 -070047
Sunny Goyal8d595092015-07-06 12:22:14 -070048 public void setQsbWidgetId(int widgetId) {
49 mQsbWidgetId = widgetId;
50 }
51
The Android Open Source Project7376fae2009-03-11 12:11:58 -070052 @Override
53 protected AppWidgetHostView onCreateView(Context context, int appWidgetId,
54 AppWidgetProviderInfo appWidget) {
Sunny Goyal8d595092015-07-06 12:22:14 -070055 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 Jurka16fed412010-10-01 16:12:03 -070065 return new LauncherAppWidgetHostView(context);
The Android Open Source Project7376fae2009-03-11 12:11:58 -070066 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -080067
68 @Override
Adam Cohen084c3182014-06-16 15:22:56 -070069 public void startListening() {
70 try {
71 super.startListening();
72 } catch (Exception e) {
Sunny Goyal1b323792015-11-05 16:09:57 +053073 if (e.getCause() instanceof TransactionTooLargeException ||
74 e.getCause() instanceof DeadObjectException) {
Adam Cohen084c3182014-06-16 15:22:56 -070075 // 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 Dubroy2313eff2011-01-11 20:01:31 -080086 public void stopListening() {
87 super.stopListening();
88 clearViews();
89 }
Winson Chunga3f78e32012-06-14 11:59:51 -070090
Sunny Goyal0fc1be12014-08-11 17:05:23 -070091 public void addProviderChangeListener(Runnable callback) {
92 mProviderChangeListeners.add(callback);
93 }
94
95 public void removeProviderChangeListener(Runnable callback) {
96 mProviderChangeListeners.remove(callback);
97 }
98
Winson Chunga3f78e32012-06-14 11:59:51 -070099 protected void onProvidersChanged() {
Sunny Goyal7e2a3602015-04-20 18:19:25 -0700100 if (!mProviderChangeListeners.isEmpty()) {
101 for (Runnable callback : new ArrayList<>(mProviderChangeListeners)) {
102 callback.run();
103 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700104 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800105
106 if (Utilities.ATLEAST_MARSHMALLOW) {
107 mLauncher.notifyWidgetProvidersChanged();
108 }
Winson Chunga3f78e32012-06-14 11:59:51 -0700109 }
Adam Cohen59400422014-03-05 18:07:04 -0800110
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 Goyalec882042015-10-05 10:36:54 -0700134 // The super method updates the dimensions of the providerInfo. Update the
135 // launcher spans accordingly.
136 info.initSpans();
Adam Cohen59400422014-03-05 18:07:04 -0800137 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700138}