blob: de7c6100086f06054a7b75330e3aae2216e2919a [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;
Adam Cohen084c3182014-06-16 15:22:56 -070023import android.os.TransactionTooLargeException;
Adam Cohen59400422014-03-05 18:07:04 -080024import android.view.LayoutInflater;
Sunny Goyal8d595092015-07-06 12:22:14 -070025import android.view.View;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070026
Sunny Goyal0fc1be12014-08-11 17:05:23 -070027import java.util.ArrayList;
28
Adam Cohen59400422014-03-05 18:07:04 -080029
The Android Open Source Project7376fae2009-03-11 12:11:58 -070030/**
31 * Specific {@link AppWidgetHost} that creates our {@link LauncherAppWidgetHostView}
32 * which correctly captures all long-press events. This ensures that users can
33 * always pick up and move widgets.
34 */
35public class LauncherAppWidgetHost extends AppWidgetHost {
Winson Chunga3f78e32012-06-14 11:59:51 -070036
Sunny Goyal0fc1be12014-08-11 17:05:23 -070037 private final ArrayList<Runnable> mProviderChangeListeners = new ArrayList<Runnable>();
38
Sunny Goyal8d595092015-07-06 12:22:14 -070039 private int mQsbWidgetId = -1;
40 private Launcher mLauncher;
Winson Chunga3f78e32012-06-14 11:59:51 -070041
42 public LauncherAppWidgetHost(Launcher launcher, int hostId) {
43 super(launcher, hostId);
44 mLauncher = launcher;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070045 }
Adam Cohen9415d872010-09-13 14:49:43 -070046
Sunny Goyal8d595092015-07-06 12:22:14 -070047 public void setQsbWidgetId(int widgetId) {
48 mQsbWidgetId = widgetId;
49 }
50
The Android Open Source Project7376fae2009-03-11 12:11:58 -070051 @Override
52 protected AppWidgetHostView onCreateView(Context context, int appWidgetId,
53 AppWidgetProviderInfo appWidget) {
Sunny Goyal8d595092015-07-06 12:22:14 -070054 if (appWidgetId == mQsbWidgetId) {
55 return new LauncherAppWidgetHostView(context) {
56
57 @Override
58 protected View getErrorView() {
59 // For the QSB, show an empty view instead of an error view.
60 return new View(getContext());
61 }
62 };
63 }
Michael Jurka16fed412010-10-01 16:12:03 -070064 return new LauncherAppWidgetHostView(context);
The Android Open Source Project7376fae2009-03-11 12:11:58 -070065 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -080066
67 @Override
Adam Cohen084c3182014-06-16 15:22:56 -070068 public void startListening() {
69 try {
70 super.startListening();
71 } catch (Exception e) {
72 if (e.getCause() instanceof TransactionTooLargeException) {
73 // We're willing to let this slide. The exception is being caused by the list of
74 // RemoteViews which is being passed back. The startListening relationship will
75 // have been established by this point, and we will end up populating the
76 // widgets upon bind anyway. See issue 14255011 for more context.
77 } else {
78 throw new RuntimeException(e);
79 }
80 }
81 }
82
83 @Override
Patrick Dubroy2313eff2011-01-11 20:01:31 -080084 public void stopListening() {
85 super.stopListening();
86 clearViews();
87 }
Winson Chunga3f78e32012-06-14 11:59:51 -070088
Sunny Goyal0fc1be12014-08-11 17:05:23 -070089 public void addProviderChangeListener(Runnable callback) {
90 mProviderChangeListeners.add(callback);
91 }
92
93 public void removeProviderChangeListener(Runnable callback) {
94 mProviderChangeListeners.remove(callback);
95 }
96
Winson Chunga3f78e32012-06-14 11:59:51 -070097 protected void onProvidersChanged() {
Hyunyoung Song227239e2015-05-04 18:17:35 -070098 mLauncher.getModel().loadAndBindWidgetsAndShortcuts(mLauncher, mLauncher,
99 true /* refresh */);
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 }
Winson Chunga3f78e32012-06-14 11:59:51 -0700105 }
Adam Cohen59400422014-03-05 18:07:04 -0800106
107 public AppWidgetHostView createView(Context context, int appWidgetId,
108 LauncherAppWidgetProviderInfo appWidget) {
109 if (appWidget.isCustomWidget) {
110 LauncherAppWidgetHostView lahv = new LauncherAppWidgetHostView(context);
111 LayoutInflater inflater = (LayoutInflater)
112 context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
113 inflater.inflate(appWidget.initialLayout, lahv);
114 lahv.setAppWidget(0, appWidget);
115 lahv.updateLastInflationOrientation();
116 return lahv;
117 } else {
118 return super.createView(context, appWidgetId, appWidget);
119 }
120 }
121
122 /**
123 * Called when the AppWidget provider for a AppWidget has been upgraded to a new apk.
124 */
125 @Override
126 protected void onProviderChanged(int appWidgetId, AppWidgetProviderInfo appWidget) {
127 LauncherAppWidgetProviderInfo info = LauncherAppWidgetProviderInfo.fromProviderInfo(
128 mLauncher, appWidget);
129 super.onProviderChanged(appWidgetId, info);
130 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700131}