2009-03-03 19:32:27 -08:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2009 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2013-06-05 22:57:57 -04:00
|
|
|
package com.android.launcher3;
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2017-11-14 16:55:22 -08:00
|
|
|
import static android.app.Activity.RESULT_CANCELED;
|
|
|
|
|
|
2009-03-11 12:11:58 -07:00
|
|
|
import android.appwidget.AppWidgetHost;
|
|
|
|
|
import android.appwidget.AppWidgetHostView;
|
2017-07-03 13:50:52 -07:00
|
|
|
import android.appwidget.AppWidgetManager;
|
2009-03-11 12:11:58 -07:00
|
|
|
import android.appwidget.AppWidgetProviderInfo;
|
2017-07-03 13:50:52 -07:00
|
|
|
import android.content.ActivityNotFoundException;
|
2009-03-03 19:32:27 -08:00
|
|
|
import android.content.Context;
|
2017-07-03 13:50:52 -07:00
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.os.Handler;
|
2016-11-04 10:19:58 -07:00
|
|
|
import android.util.SparseArray;
|
2017-07-03 13:50:52 -07:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
2019-09-11 16:51:50 -07:00
|
|
|
import com.android.launcher3.model.WidgetsModel;
|
2020-04-15 12:43:01 -07:00
|
|
|
import com.android.launcher3.testing.TestLogging;
|
|
|
|
|
import com.android.launcher3.testing.TestProtocol;
|
2017-12-18 13:49:44 -08:00
|
|
|
import com.android.launcher3.widget.DeferredAppWidgetHostView;
|
|
|
|
|
import com.android.launcher3.widget.LauncherAppWidgetHostView;
|
2020-02-24 17:06:28 -08:00
|
|
|
import com.android.launcher3.widget.PendingAppWidgetHostView;
|
2019-08-26 14:36:02 -07:00
|
|
|
import com.android.launcher3.widget.custom.CustomWidgetManager;
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2014-08-11 17:05:23 -07:00
|
|
|
import java.util.ArrayList;
|
2019-08-26 15:00:30 -07:00
|
|
|
import java.util.function.IntConsumer;
|
2014-08-11 17:05:23 -07:00
|
|
|
|
2014-03-05 18:07:04 -08:00
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
/**
|
2009-03-11 12:11:58 -07:00
|
|
|
* Specific {@link AppWidgetHost} that creates our {@link LauncherAppWidgetHostView}
|
2009-03-03 19:32:27 -08:00
|
|
|
* which correctly captures all long-press events. This ensures that users can
|
2009-03-11 12:11:58 -07:00
|
|
|
* always pick up and move widgets.
|
2009-03-03 19:32:27 -08:00
|
|
|
*/
|
2009-03-11 12:11:58 -07:00
|
|
|
public class LauncherAppWidgetHost extends AppWidgetHost {
|
2012-06-14 11:59:51 -07:00
|
|
|
|
2017-11-14 16:55:22 -08:00
|
|
|
private static final int FLAG_LISTENING = 1;
|
|
|
|
|
private static final int FLAG_RESUMED = 1 << 1;
|
|
|
|
|
private static final int FLAG_LISTEN_IF_RESUMED = 1 << 2;
|
|
|
|
|
|
2017-07-03 13:50:52 -07:00
|
|
|
public static final int APPWIDGET_HOST_ID = 1024;
|
|
|
|
|
|
|
|
|
|
private final ArrayList<ProviderChangedListener> mProviderChangeListeners = new ArrayList<>();
|
2016-11-04 10:19:58 -07:00
|
|
|
private final SparseArray<LauncherAppWidgetHostView> mViews = new SparseArray<>();
|
2020-02-24 17:06:28 -08:00
|
|
|
private final SparseArray<PendingAppWidgetHostView> mPendingViews = new SparseArray<>();
|
2014-08-11 17:05:23 -07:00
|
|
|
|
2017-07-03 13:50:52 -07:00
|
|
|
private final Context mContext;
|
2017-11-14 16:55:22 -08:00
|
|
|
private int mFlags = FLAG_RESUMED;
|
2012-06-14 11:59:51 -07:00
|
|
|
|
2019-08-26 15:00:30 -07:00
|
|
|
private IntConsumer mAppWidgetRemovedCallback = null;
|
|
|
|
|
|
2020-02-24 17:06:28 -08:00
|
|
|
|
2017-07-03 13:50:52 -07:00
|
|
|
public LauncherAppWidgetHost(Context context) {
|
2019-08-26 15:00:30 -07:00
|
|
|
this(context, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public LauncherAppWidgetHost(Context context,
|
|
|
|
|
IntConsumer appWidgetRemovedCallback) {
|
2017-07-03 13:50:52 -07:00
|
|
|
super(context, APPWIDGET_HOST_ID);
|
|
|
|
|
mContext = context;
|
2019-08-26 15:00:30 -07:00
|
|
|
mAppWidgetRemovedCallback = appWidgetRemovedCallback;
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
2010-09-13 14:49:43 -07:00
|
|
|
|
2009-03-11 12:11:58 -07:00
|
|
|
@Override
|
2016-11-04 10:19:58 -07:00
|
|
|
protected LauncherAppWidgetHostView onCreateView(Context context, int appWidgetId,
|
2009-03-11 12:11:58 -07:00
|
|
|
AppWidgetProviderInfo appWidget) {
|
2020-02-24 17:06:28 -08:00
|
|
|
final LauncherAppWidgetHostView view;
|
|
|
|
|
if (mPendingViews.get(appWidgetId) != null) {
|
|
|
|
|
view = mPendingViews.get(appWidgetId);
|
|
|
|
|
mPendingViews.remove(appWidgetId);
|
|
|
|
|
} else {
|
|
|
|
|
view = new LauncherAppWidgetHostView(context);
|
|
|
|
|
}
|
2016-11-04 10:19:58 -07:00
|
|
|
mViews.put(appWidgetId, view);
|
|
|
|
|
return view;
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
2011-01-11 20:01:31 -08:00
|
|
|
|
2014-06-16 15:22:56 -07:00
|
|
|
@Override
|
|
|
|
|
public void startListening() {
|
2019-09-11 16:51:50 -07:00
|
|
|
if (WidgetsModel.GO_DISABLE_WIDGETS) {
|
2017-07-03 13:50:52 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2017-11-14 16:55:22 -08:00
|
|
|
mFlags |= FLAG_LISTENING;
|
2014-06-16 15:22:56 -07:00
|
|
|
try {
|
|
|
|
|
super.startListening();
|
|
|
|
|
} catch (Exception e) {
|
2016-11-04 10:19:58 -07:00
|
|
|
if (!Utilities.isBinderSizeError(e)) {
|
2014-06-16 15:22:56 -07:00
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
2016-11-04 10:19:58 -07:00
|
|
|
// We're willing to let this slide. The exception is being caused by the list of
|
|
|
|
|
// RemoteViews which is being passed back. The startListening relationship will
|
|
|
|
|
// have been established by this point, and we will end up populating the
|
|
|
|
|
// widgets upon bind anyway. See issue 14255011 for more context.
|
2014-06-16 15:22:56 -07:00
|
|
|
}
|
2017-12-18 13:49:44 -08:00
|
|
|
|
|
|
|
|
// We go in reverse order and inflate any deferred widget
|
|
|
|
|
for (int i = mViews.size() - 1; i >= 0; i--) {
|
|
|
|
|
LauncherAppWidgetHostView view = mViews.valueAt(i);
|
|
|
|
|
if (view instanceof DeferredAppWidgetHostView) {
|
|
|
|
|
view.reInflate();
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-06-16 15:22:56 -07:00
|
|
|
}
|
|
|
|
|
|
2017-07-03 13:50:52 -07:00
|
|
|
@Override
|
|
|
|
|
public void stopListening() {
|
2019-09-11 16:51:50 -07:00
|
|
|
if (WidgetsModel.GO_DISABLE_WIDGETS) {
|
2017-07-03 13:50:52 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2017-11-14 16:55:22 -08:00
|
|
|
mFlags &= ~FLAG_LISTENING;
|
2017-07-03 13:50:52 -07:00
|
|
|
super.stopListening();
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-06 12:05:52 -07:00
|
|
|
public boolean isListening() {
|
|
|
|
|
return (mFlags & FLAG_LISTENING) != 0;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-14 16:55:22 -08:00
|
|
|
/**
|
|
|
|
|
* Updates the resumed state of the host.
|
|
|
|
|
* When a host is not resumed, it defers calls to startListening until host is resumed again.
|
|
|
|
|
* But if the host was already listening, it will not call stopListening.
|
|
|
|
|
*
|
|
|
|
|
* @see #setListenIfResumed(boolean)
|
|
|
|
|
*/
|
|
|
|
|
public void setResumed(boolean isResumed) {
|
|
|
|
|
if (isResumed == ((mFlags & FLAG_RESUMED) != 0)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (isResumed) {
|
|
|
|
|
mFlags |= FLAG_RESUMED;
|
|
|
|
|
// Start listening if we were supposed to start listening on resume
|
|
|
|
|
if ((mFlags & FLAG_LISTEN_IF_RESUMED) != 0 && (mFlags & FLAG_LISTENING) == 0) {
|
|
|
|
|
startListening();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
mFlags &= ~FLAG_RESUMED;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Updates the listening state of the host. If the host is not resumed, startListening is
|
|
|
|
|
* deferred until next resume.
|
|
|
|
|
*
|
|
|
|
|
* @see #setResumed(boolean)
|
|
|
|
|
*/
|
|
|
|
|
public void setListenIfResumed(boolean listenIfResumed) {
|
|
|
|
|
if (listenIfResumed == ((mFlags & FLAG_LISTEN_IF_RESUMED) != 0)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (listenIfResumed) {
|
|
|
|
|
mFlags |= FLAG_LISTEN_IF_RESUMED;
|
|
|
|
|
if ((mFlags & FLAG_RESUMED) != 0) {
|
|
|
|
|
// If we are resumed, start listening immediately. Note we do not check for
|
|
|
|
|
// duplicate calls before calling startListening as startListening is safe to call
|
|
|
|
|
// multiple times.
|
|
|
|
|
startListening();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
mFlags &= ~FLAG_LISTEN_IF_RESUMED;
|
|
|
|
|
stopListening();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-03 13:50:52 -07:00
|
|
|
@Override
|
|
|
|
|
public int allocateAppWidgetId() {
|
2019-09-11 16:51:50 -07:00
|
|
|
if (WidgetsModel.GO_DISABLE_WIDGETS) {
|
2017-07-03 13:50:52 -07:00
|
|
|
return AppWidgetManager.INVALID_APPWIDGET_ID;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return super.allocateAppWidgetId();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void addProviderChangeListener(ProviderChangedListener callback) {
|
2014-08-11 17:05:23 -07:00
|
|
|
mProviderChangeListeners.add(callback);
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-03 13:50:52 -07:00
|
|
|
public void removeProviderChangeListener(ProviderChangedListener callback) {
|
2014-08-11 17:05:23 -07:00
|
|
|
mProviderChangeListeners.remove(callback);
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-14 11:59:51 -07:00
|
|
|
protected void onProvidersChanged() {
|
2015-04-20 18:19:25 -07:00
|
|
|
if (!mProviderChangeListeners.isEmpty()) {
|
2017-07-03 13:50:52 -07:00
|
|
|
for (ProviderChangedListener callback : new ArrayList<>(mProviderChangeListeners)) {
|
|
|
|
|
callback.notifyWidgetProvidersChanged();
|
2015-04-20 18:19:25 -07:00
|
|
|
}
|
2014-08-11 17:05:23 -07:00
|
|
|
}
|
2012-06-14 11:59:51 -07:00
|
|
|
}
|
2014-03-05 18:07:04 -08:00
|
|
|
|
2020-02-24 17:06:28 -08:00
|
|
|
void addPendingView(int appWidgetId, PendingAppWidgetHostView view) {
|
|
|
|
|
mPendingViews.put(appWidgetId, view);
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-05 18:07:04 -08:00
|
|
|
public AppWidgetHostView createView(Context context, int appWidgetId,
|
|
|
|
|
LauncherAppWidgetProviderInfo appWidget) {
|
2017-08-16 04:59:08 -07:00
|
|
|
if (appWidget.isCustomWidget()) {
|
2014-03-05 18:07:04 -08:00
|
|
|
LauncherAppWidgetHostView lahv = new LauncherAppWidgetHostView(context);
|
|
|
|
|
lahv.setAppWidget(0, appWidget);
|
2019-08-26 14:36:02 -07:00
|
|
|
CustomWidgetManager.INSTANCE.get(context).onViewCreated(lahv);
|
2014-03-05 18:07:04 -08:00
|
|
|
return lahv;
|
2017-12-18 13:49:44 -08:00
|
|
|
} else if ((mFlags & FLAG_LISTENING) == 0) {
|
|
|
|
|
DeferredAppWidgetHostView view = new DeferredAppWidgetHostView(context);
|
|
|
|
|
view.setAppWidget(appWidgetId, appWidget);
|
|
|
|
|
mViews.put(appWidgetId, view);
|
|
|
|
|
return view;
|
2014-03-05 18:07:04 -08:00
|
|
|
} else {
|
2016-11-04 10:19:58 -07:00
|
|
|
try {
|
|
|
|
|
return super.createView(context, appWidgetId, appWidget);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
if (!Utilities.isBinderSizeError(e)) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the exception was thrown while fetching the remote views, let the view stay.
|
|
|
|
|
// This will ensure that if the widget posts a valid update later, the view
|
|
|
|
|
// will update.
|
|
|
|
|
LauncherAppWidgetHostView view = mViews.get(appWidgetId);
|
|
|
|
|
if (view == null) {
|
2017-07-03 13:50:52 -07:00
|
|
|
view = onCreateView(mContext, appWidgetId, appWidget);
|
2016-11-04 10:19:58 -07:00
|
|
|
}
|
|
|
|
|
view.setAppWidget(appWidgetId, appWidget);
|
|
|
|
|
view.switchToErrorView();
|
2019-08-26 15:00:30 -07:00
|
|
|
return view;
|
2016-11-04 10:19:58 -07:00
|
|
|
}
|
2014-03-05 18:07:04 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Called when the AppWidget provider for a AppWidget has been upgraded to a new apk.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
protected void onProviderChanged(int appWidgetId, AppWidgetProviderInfo appWidget) {
|
|
|
|
|
LauncherAppWidgetProviderInfo info = LauncherAppWidgetProviderInfo.fromProviderInfo(
|
2017-07-03 13:50:52 -07:00
|
|
|
mContext, appWidget);
|
2014-03-05 18:07:04 -08:00
|
|
|
super.onProviderChanged(appWidgetId, info);
|
2015-10-05 10:36:54 -07:00
|
|
|
// The super method updates the dimensions of the providerInfo. Update the
|
|
|
|
|
// launcher spans accordingly.
|
2017-07-03 13:50:52 -07:00
|
|
|
info.initSpans(mContext);
|
2014-03-05 18:07:04 -08:00
|
|
|
}
|
2016-11-04 10:19:58 -07:00
|
|
|
|
2019-09-19 10:10:34 -07:00
|
|
|
/**
|
|
|
|
|
* Called on an appWidget is removed for a widgetId
|
2020-02-24 17:06:28 -08:00
|
|
|
*
|
|
|
|
|
* @param appWidgetId TODO: make this override when SDK is updated
|
2019-09-19 10:10:34 -07:00
|
|
|
*/
|
|
|
|
|
public void onAppWidgetRemoved(int appWidgetId) {
|
2019-08-26 15:00:30 -07:00
|
|
|
if (mAppWidgetRemovedCallback == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
mAppWidgetRemovedCallback.accept(appWidgetId);
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-04 10:19:58 -07:00
|
|
|
@Override
|
|
|
|
|
public void deleteAppWidgetId(int appWidgetId) {
|
|
|
|
|
super.deleteAppWidgetId(appWidgetId);
|
|
|
|
|
mViews.remove(appWidgetId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2018-02-27 19:20:15 -08:00
|
|
|
public void clearViews() {
|
2016-11-04 10:19:58 -07:00
|
|
|
super.clearViews();
|
|
|
|
|
mViews.clear();
|
|
|
|
|
}
|
2017-07-03 13:50:52 -07:00
|
|
|
|
|
|
|
|
public void startBindFlow(BaseActivity activity,
|
|
|
|
|
int appWidgetId, AppWidgetProviderInfo info, int requestCode) {
|
|
|
|
|
|
2019-09-11 16:51:50 -07:00
|
|
|
if (WidgetsModel.GO_DISABLE_WIDGETS) {
|
2017-07-03 13:50:52 -07:00
|
|
|
sendActionCancelled(activity, requestCode);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND)
|
|
|
|
|
.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
|
|
|
|
|
.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.provider)
|
|
|
|
|
.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE, info.getProfile());
|
|
|
|
|
// TODO: we need to make sure that this accounts for the options bundle.
|
|
|
|
|
// intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
|
|
|
|
|
activity.startActivityForResult(intent, requestCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void startConfigActivity(BaseActivity activity, int widgetId, int requestCode) {
|
2019-09-11 16:51:50 -07:00
|
|
|
if (WidgetsModel.GO_DISABLE_WIDGETS) {
|
2017-07-03 13:50:52 -07:00
|
|
|
sendActionCancelled(activity, requestCode);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2020-04-15 12:43:01 -07:00
|
|
|
TestLogging.recordEvent(TestProtocol.SEQUENCE_MAIN, "start: startConfigActivity");
|
2017-07-03 13:50:52 -07:00
|
|
|
startAppWidgetConfigureActivityForResult(activity, widgetId, 0, requestCode, null);
|
|
|
|
|
} catch (ActivityNotFoundException | SecurityException e) {
|
|
|
|
|
Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
|
|
|
|
|
sendActionCancelled(activity, requestCode);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void sendActionCancelled(final BaseActivity activity, final int requestCode) {
|
2017-11-14 16:55:22 -08:00
|
|
|
new Handler().post(() -> activity.onActivityResult(requestCode, RESULT_CANCELED, null));
|
2017-07-03 13:50:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Listener for getting notifications on provider changes.
|
|
|
|
|
*/
|
|
|
|
|
public interface ProviderChangedListener {
|
|
|
|
|
|
|
|
|
|
void notifyWidgetProvidersChanged();
|
|
|
|
|
}
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|