blob: d3e5350026a6d357250121c6e1811df203a943d5 [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 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
The Android Open Source Project7376fae2009-03-11 12:11:58 -070047 @Override
48 protected AppWidgetHostView onCreateView(Context context, int appWidgetId,
49 AppWidgetProviderInfo appWidget) {
Michael Jurka16fed412010-10-01 16:12:03 -070050 return new LauncherAppWidgetHostView(context);
The Android Open Source Project7376fae2009-03-11 12:11:58 -070051 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -080052
53 @Override
Adam Cohen084c3182014-06-16 15:22:56 -070054 public void startListening() {
55 try {
56 super.startListening();
57 } catch (Exception e) {
Sunny Goyal1b323792015-11-05 16:09:57 +053058 if (e.getCause() instanceof TransactionTooLargeException ||
59 e.getCause() instanceof DeadObjectException) {
Adam Cohen084c3182014-06-16 15:22:56 -070060 // We're willing to let this slide. The exception is being caused by the list of
61 // RemoteViews which is being passed back. The startListening relationship will
62 // have been established by this point, and we will end up populating the
63 // widgets upon bind anyway. See issue 14255011 for more context.
64 } else {
65 throw new RuntimeException(e);
66 }
67 }
68 }
69
Sunny Goyal0fc1be12014-08-11 17:05:23 -070070 public void addProviderChangeListener(Runnable callback) {
71 mProviderChangeListeners.add(callback);
72 }
73
74 public void removeProviderChangeListener(Runnable callback) {
75 mProviderChangeListeners.remove(callback);
76 }
77
Winson Chunga3f78e32012-06-14 11:59:51 -070078 protected void onProvidersChanged() {
Sunny Goyal7e2a3602015-04-20 18:19:25 -070079 if (!mProviderChangeListeners.isEmpty()) {
80 for (Runnable callback : new ArrayList<>(mProviderChangeListeners)) {
81 callback.run();
82 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -070083 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -080084
85 if (Utilities.ATLEAST_MARSHMALLOW) {
86 mLauncher.notifyWidgetProvidersChanged();
87 }
Winson Chunga3f78e32012-06-14 11:59:51 -070088 }
Adam Cohen59400422014-03-05 18:07:04 -080089
90 public AppWidgetHostView createView(Context context, int appWidgetId,
91 LauncherAppWidgetProviderInfo appWidget) {
92 if (appWidget.isCustomWidget) {
93 LauncherAppWidgetHostView lahv = new LauncherAppWidgetHostView(context);
94 LayoutInflater inflater = (LayoutInflater)
95 context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
96 inflater.inflate(appWidget.initialLayout, lahv);
97 lahv.setAppWidget(0, appWidget);
98 lahv.updateLastInflationOrientation();
99 return lahv;
100 } else {
101 return super.createView(context, appWidgetId, appWidget);
102 }
103 }
104
105 /**
106 * Called when the AppWidget provider for a AppWidget has been upgraded to a new apk.
107 */
108 @Override
109 protected void onProviderChanged(int appWidgetId, AppWidgetProviderInfo appWidget) {
110 LauncherAppWidgetProviderInfo info = LauncherAppWidgetProviderInfo.fromProviderInfo(
111 mLauncher, appWidget);
112 super.onProviderChanged(appWidgetId, info);
Sunny Goyalec882042015-10-05 10:36:54 -0700113 // The super method updates the dimensions of the providerInfo. Update the
114 // launcher spans accordingly.
115 info.initSpans();
Adam Cohen59400422014-03-05 18:07:04 -0800116 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700117}