blob: 5573c5c151af579801f7d2a178aacd114ca60af9 [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) {
119 if (appWidget.isCustomWidget) {
120 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);
125 lahv.updateLastInflationOrientation();
126 return lahv;
127 } else {
Sunny Goyal712ee532016-11-04 10:19:58 -0700128 try {
129 return super.createView(context, appWidgetId, appWidget);
130 } catch (Exception e) {
131 if (!Utilities.isBinderSizeError(e)) {
132 throw new RuntimeException(e);
133 }
134
135 // If the exception was thrown while fetching the remote views, let the view stay.
136 // This will ensure that if the widget posts a valid update later, the view
137 // will update.
138 LauncherAppWidgetHostView view = mViews.get(appWidgetId);
139 if (view == null) {
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700140 view = onCreateView(mContext, appWidgetId, appWidget);
Sunny Goyal712ee532016-11-04 10:19:58 -0700141 }
142 view.setAppWidget(appWidgetId, appWidget);
143 view.switchToErrorView();
144 return view;
145 }
Adam Cohen59400422014-03-05 18:07:04 -0800146 }
147 }
148
149 /**
150 * Called when the AppWidget provider for a AppWidget has been upgraded to a new apk.
151 */
152 @Override
153 protected void onProviderChanged(int appWidgetId, AppWidgetProviderInfo appWidget) {
154 LauncherAppWidgetProviderInfo info = LauncherAppWidgetProviderInfo.fromProviderInfo(
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700155 mContext, appWidget);
Adam Cohen59400422014-03-05 18:07:04 -0800156 super.onProviderChanged(appWidgetId, info);
Sunny Goyalec882042015-10-05 10:36:54 -0700157 // The super method updates the dimensions of the providerInfo. Update the
158 // launcher spans accordingly.
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700159 info.initSpans(mContext);
Adam Cohen59400422014-03-05 18:07:04 -0800160 }
Sunny Goyal712ee532016-11-04 10:19:58 -0700161
162 @Override
163 public void deleteAppWidgetId(int appWidgetId) {
164 super.deleteAppWidgetId(appWidgetId);
165 mViews.remove(appWidgetId);
166 }
167
168 @Override
169 protected void clearViews() {
170 super.clearViews();
171 mViews.clear();
172 }
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700173
174 public void startBindFlow(BaseActivity activity,
175 int appWidgetId, AppWidgetProviderInfo info, int requestCode) {
176
177 if (FeatureFlags.GO_DISABLE_WIDGETS) {
178 sendActionCancelled(activity, requestCode);
179 return;
180 }
181
182 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND)
183 .putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
184 .putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.provider)
185 .putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE, info.getProfile());
186 // TODO: we need to make sure that this accounts for the options bundle.
187 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
188 activity.startActivityForResult(intent, requestCode);
189 }
190
191
192 public void startConfigActivity(BaseActivity activity, int widgetId, int requestCode) {
193 if (FeatureFlags.GO_DISABLE_WIDGETS) {
194 sendActionCancelled(activity, requestCode);
195 return;
196 }
197
198 try {
199 startAppWidgetConfigureActivityForResult(activity, widgetId, 0, requestCode, null);
200 } catch (ActivityNotFoundException | SecurityException e) {
201 Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
202 sendActionCancelled(activity, requestCode);
203 }
204 }
205
206 private void sendActionCancelled(final BaseActivity activity, final int requestCode) {
207 new Handler().post(new Runnable() {
208 @Override
209 public void run() {
210 activity.onActivityResult(requestCode, Activity.RESULT_CANCELED, null);
211 }
212 });
213 }
214
215 /**
216 * Listener for getting notifications on provider changes.
217 */
218 public interface ProviderChangedListener {
219
220 void notifyWidgetProvidersChanged();
221 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700222}