blob: 70440fa309dcee05cb5c24259e30881db7af6836 [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
Sunny Goyal64a75aa2017-07-03 13:50:52 -070019import android.app.Activity;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070020import android.appwidget.AppWidgetHost;
21import android.appwidget.AppWidgetHostView;
Sunny Goyal64a75aa2017-07-03 13:50:52 -070022import android.appwidget.AppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070023import android.appwidget.AppWidgetProviderInfo;
Sunny Goyal64a75aa2017-07-03 13:50:52 -070024import android.content.ActivityNotFoundException;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070025import android.content.Context;
Sunny Goyal64a75aa2017-07-03 13:50:52 -070026import android.content.Intent;
27import android.os.Handler;
Sunny Goyal712ee532016-11-04 10:19:58 -070028import android.util.SparseArray;
Adam Cohen59400422014-03-05 18:07:04 -080029import android.view.LayoutInflater;
Sunny Goyal64a75aa2017-07-03 13:50:52 -070030import android.widget.Toast;
31
32import com.android.launcher3.config.FeatureFlags;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070033
Sunny Goyal0fc1be12014-08-11 17:05:23 -070034import java.util.ArrayList;
35
Adam Cohen59400422014-03-05 18:07:04 -080036
The Android Open Source Project7376fae2009-03-11 12:11:58 -070037/**
38 * Specific {@link AppWidgetHost} that creates our {@link LauncherAppWidgetHostView}
39 * which correctly captures all long-press events. This ensures that users can
40 * always pick up and move widgets.
41 */
42public class LauncherAppWidgetHost extends AppWidgetHost {
Winson Chunga3f78e32012-06-14 11:59:51 -070043
Sunny Goyal64a75aa2017-07-03 13:50:52 -070044 public static final int APPWIDGET_HOST_ID = 1024;
45
46 private final ArrayList<ProviderChangedListener> mProviderChangeListeners = new ArrayList<>();
Sunny Goyal712ee532016-11-04 10:19:58 -070047 private final SparseArray<LauncherAppWidgetHostView> mViews = new SparseArray<>();
Sunny Goyal0fc1be12014-08-11 17:05:23 -070048
Sunny Goyal64a75aa2017-07-03 13:50:52 -070049 private final Context mContext;
Winson Chunga3f78e32012-06-14 11:59:51 -070050
Sunny Goyal64a75aa2017-07-03 13:50:52 -070051 public LauncherAppWidgetHost(Context context) {
52 super(context, APPWIDGET_HOST_ID);
53 mContext = context;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070054 }
Adam Cohen9415d872010-09-13 14:49:43 -070055
The Android Open Source Project7376fae2009-03-11 12:11:58 -070056 @Override
Sunny Goyal712ee532016-11-04 10:19:58 -070057 protected LauncherAppWidgetHostView onCreateView(Context context, int appWidgetId,
The Android Open Source Project7376fae2009-03-11 12:11:58 -070058 AppWidgetProviderInfo appWidget) {
Sunny Goyal712ee532016-11-04 10:19:58 -070059 LauncherAppWidgetHostView view = new LauncherAppWidgetHostView(context);
60 mViews.put(appWidgetId, view);
61 return view;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070062 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -080063
64 @Override
Adam Cohen084c3182014-06-16 15:22:56 -070065 public void startListening() {
Sunny Goyal64a75aa2017-07-03 13:50:52 -070066 if (FeatureFlags.GO_DISABLE_WIDGETS) {
67 return;
68 }
69
Adam Cohen084c3182014-06-16 15:22:56 -070070 try {
71 super.startListening();
72 } catch (Exception e) {
Sunny Goyal712ee532016-11-04 10:19:58 -070073 if (!Utilities.isBinderSizeError(e)) {
Adam Cohen084c3182014-06-16 15:22:56 -070074 throw new RuntimeException(e);
75 }
Sunny Goyal712ee532016-11-04 10:19:58 -070076 // We're willing to let this slide. The exception is being caused by the list of
77 // RemoteViews which is being passed back. The startListening relationship will
78 // have been established by this point, and we will end up populating the
79 // widgets upon bind anyway. See issue 14255011 for more context.
Adam Cohen084c3182014-06-16 15:22:56 -070080 }
81 }
82
Sunny Goyal64a75aa2017-07-03 13:50:52 -070083 @Override
84 public void stopListening() {
85 if (FeatureFlags.GO_DISABLE_WIDGETS) {
86 return;
87 }
88
89 super.stopListening();
90 }
91
92 @Override
93 public int allocateAppWidgetId() {
94 if (FeatureFlags.GO_DISABLE_WIDGETS) {
95 return AppWidgetManager.INVALID_APPWIDGET_ID;
96 }
97
98 return super.allocateAppWidgetId();
99 }
100
101 public void addProviderChangeListener(ProviderChangedListener callback) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700102 mProviderChangeListeners.add(callback);
103 }
104
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700105 public void removeProviderChangeListener(ProviderChangedListener callback) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700106 mProviderChangeListeners.remove(callback);
107 }
108
Winson Chunga3f78e32012-06-14 11:59:51 -0700109 protected void onProvidersChanged() {
Sunny Goyal7e2a3602015-04-20 18:19:25 -0700110 if (!mProviderChangeListeners.isEmpty()) {
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700111 for (ProviderChangedListener callback : new ArrayList<>(mProviderChangeListeners)) {
112 callback.notifyWidgetProvidersChanged();
Sunny Goyal7e2a3602015-04-20 18:19:25 -0700113 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700114 }
Winson Chunga3f78e32012-06-14 11:59:51 -0700115 }
Adam Cohen59400422014-03-05 18:07:04 -0800116
117 public AppWidgetHostView createView(Context context, int appWidgetId,
118 LauncherAppWidgetProviderInfo appWidget) {
Sunny Goyal952e63d2017-08-16 04:59:08 -0700119 if (appWidget.isCustomWidget()) {
Adam Cohen59400422014-03-05 18:07:04 -0800120 LauncherAppWidgetHostView lahv = new LauncherAppWidgetHostView(context);
121 LayoutInflater inflater = (LayoutInflater)
122 context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
123 inflater.inflate(appWidget.initialLayout, lahv);
124 lahv.setAppWidget(0, appWidget);
Adam Cohen59400422014-03-05 18:07:04 -0800125 return lahv;
126 } else {
Sunny Goyal712ee532016-11-04 10:19:58 -0700127 try {
128 return super.createView(context, appWidgetId, appWidget);
129 } catch (Exception e) {
130 if (!Utilities.isBinderSizeError(e)) {
131 throw new RuntimeException(e);
132 }
133
134 // If the exception was thrown while fetching the remote views, let the view stay.
135 // This will ensure that if the widget posts a valid update later, the view
136 // will update.
137 LauncherAppWidgetHostView view = mViews.get(appWidgetId);
138 if (view == null) {
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700139 view = onCreateView(mContext, appWidgetId, appWidget);
Sunny Goyal712ee532016-11-04 10:19:58 -0700140 }
141 view.setAppWidget(appWidgetId, appWidget);
142 view.switchToErrorView();
143 return view;
144 }
Adam Cohen59400422014-03-05 18:07:04 -0800145 }
146 }
147
148 /**
149 * Called when the AppWidget provider for a AppWidget has been upgraded to a new apk.
150 */
151 @Override
152 protected void onProviderChanged(int appWidgetId, AppWidgetProviderInfo appWidget) {
153 LauncherAppWidgetProviderInfo info = LauncherAppWidgetProviderInfo.fromProviderInfo(
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700154 mContext, appWidget);
Adam Cohen59400422014-03-05 18:07:04 -0800155 super.onProviderChanged(appWidgetId, info);
Sunny Goyalec882042015-10-05 10:36:54 -0700156 // The super method updates the dimensions of the providerInfo. Update the
157 // launcher spans accordingly.
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700158 info.initSpans(mContext);
Adam Cohen59400422014-03-05 18:07:04 -0800159 }
Sunny Goyal712ee532016-11-04 10:19:58 -0700160
161 @Override
162 public void deleteAppWidgetId(int appWidgetId) {
163 super.deleteAppWidgetId(appWidgetId);
164 mViews.remove(appWidgetId);
165 }
166
167 @Override
168 protected void clearViews() {
169 super.clearViews();
170 mViews.clear();
171 }
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700172
173 public void startBindFlow(BaseActivity activity,
174 int appWidgetId, AppWidgetProviderInfo info, int requestCode) {
175
176 if (FeatureFlags.GO_DISABLE_WIDGETS) {
177 sendActionCancelled(activity, requestCode);
178 return;
179 }
180
181 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND)
182 .putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
183 .putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.provider)
184 .putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE, info.getProfile());
185 // TODO: we need to make sure that this accounts for the options bundle.
186 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
187 activity.startActivityForResult(intent, requestCode);
188 }
189
190
191 public void startConfigActivity(BaseActivity activity, int widgetId, int requestCode) {
192 if (FeatureFlags.GO_DISABLE_WIDGETS) {
193 sendActionCancelled(activity, requestCode);
194 return;
195 }
196
197 try {
198 startAppWidgetConfigureActivityForResult(activity, widgetId, 0, requestCode, null);
199 } catch (ActivityNotFoundException | SecurityException e) {
200 Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
201 sendActionCancelled(activity, requestCode);
202 }
203 }
204
205 private void sendActionCancelled(final BaseActivity activity, final int requestCode) {
206 new Handler().post(new Runnable() {
207 @Override
208 public void run() {
209 activity.onActivityResult(requestCode, Activity.RESULT_CANCELED, null);
210 }
211 });
212 }
213
214 /**
215 * Listener for getting notifications on provider changes.
216 */
217 public interface ProviderChangedListener {
218
219 void notifyWidgetProvidersChanged();
220 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700221}