summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSunny Goyal <sunnygoyal@google.com>2017-11-14 16:55:22 -0800
committerSunny Goyal <sunnygoyal@google.com>2017-11-14 18:49:43 -0800
commit67419a151226c38670cc40051bda9715a6ddcb37 (patch)
tree284542c0fde5ea6bbf516c2679457cec52f964e2
parent77f7b6682262675f25639c94c205e0ad37334c34 (diff)
downloadandroid_packages_apps_Trebuchet-67419a151226c38670cc40051bda9715a6ddcb37.tar.gz
android_packages_apps_Trebuchet-67419a151226c38670cc40051bda9715a6ddcb37.tar.bz2
android_packages_apps_Trebuchet-67419a151226c38670cc40051bda9715a6ddcb37.zip
Defering AppWidgetHost updates if Launcher resumes in an non-NORMAL state
Change-Id: Ib0ef587de7207a3bd1bb5051fe8599293dfb4d59
-rw-r--r--src/com/android/launcher3/Launcher.java10
-rw-r--r--src/com/android/launcher3/LauncherAppWidgetHost.java68
-rw-r--r--src/com/android/launcher3/LauncherStateManager.java1
3 files changed, 62 insertions, 17 deletions
diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java
index 2e930662d..553d8d0e5 100644
--- a/src/com/android/launcher3/Launcher.java
+++ b/src/com/android/launcher3/Launcher.java
@@ -767,10 +767,7 @@ public class Launcher extends BaseActivity
if (mLauncherCallbacks != null) {
mLauncherCallbacks.onStop();
}
-
- if (Utilities.ATLEAST_NOUGAT_MR1) {
- mAppWidgetHost.stopListening();
- }
+ mAppWidgetHost.setListenIfResumed(false);
if (!mAppLaunchSuccess) {
getUserEventDispatcher().logActionCommand(Action.Command.STOP,
@@ -787,10 +784,7 @@ public class Launcher extends BaseActivity
if (mLauncherCallbacks != null) {
mLauncherCallbacks.onStart();
}
-
- if (Utilities.ATLEAST_NOUGAT_MR1) {
- mAppWidgetHost.startListening();
- }
+ mAppWidgetHost.setListenIfResumed(true);
if (!isWorkspaceLoading()) {
NotificationListener.setNotificationsChangedListener(mPopupDataProvider);
diff --git a/src/com/android/launcher3/LauncherAppWidgetHost.java b/src/com/android/launcher3/LauncherAppWidgetHost.java
index 70440fa30..9aa74b38d 100644
--- a/src/com/android/launcher3/LauncherAppWidgetHost.java
+++ b/src/com/android/launcher3/LauncherAppWidgetHost.java
@@ -16,7 +16,8 @@
package com.android.launcher3;
-import android.app.Activity;
+import static android.app.Activity.RESULT_CANCELED;
+
import android.appwidget.AppWidgetHost;
import android.appwidget.AppWidgetHostView;
import android.appwidget.AppWidgetManager;
@@ -41,12 +42,17 @@ import java.util.ArrayList;
*/
public class LauncherAppWidgetHost extends AppWidgetHost {
+ 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;
+
public static final int APPWIDGET_HOST_ID = 1024;
private final ArrayList<ProviderChangedListener> mProviderChangeListeners = new ArrayList<>();
private final SparseArray<LauncherAppWidgetHostView> mViews = new SparseArray<>();
private final Context mContext;
+ private int mFlags = FLAG_RESUMED;
public LauncherAppWidgetHost(Context context) {
super(context, APPWIDGET_HOST_ID);
@@ -66,7 +72,7 @@ public class LauncherAppWidgetHost extends AppWidgetHost {
if (FeatureFlags.GO_DISABLE_WIDGETS) {
return;
}
-
+ mFlags |= FLAG_LISTENING;
try {
super.startListening();
} catch (Exception e) {
@@ -85,10 +91,59 @@ public class LauncherAppWidgetHost extends AppWidgetHost {
if (FeatureFlags.GO_DISABLE_WIDGETS) {
return;
}
-
+ mFlags &= ~FLAG_LISTENING;
super.stopListening();
}
+ /**
+ * 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 (!Utilities.ATLEAST_NOUGAT_MR1) {
+ return;
+ }
+ 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();
+ }
+ }
+
@Override
public int allocateAppWidgetId() {
if (FeatureFlags.GO_DISABLE_WIDGETS) {
@@ -203,12 +258,7 @@ public class LauncherAppWidgetHost extends AppWidgetHost {
}
private void sendActionCancelled(final BaseActivity activity, final int requestCode) {
- new Handler().post(new Runnable() {
- @Override
- public void run() {
- activity.onActivityResult(requestCode, Activity.RESULT_CANCELED, null);
- }
- });
+ new Handler().post(() -> activity.onActivityResult(requestCode, RESULT_CANCELED, null));
}
/**
diff --git a/src/com/android/launcher3/LauncherStateManager.java b/src/com/android/launcher3/LauncherStateManager.java
index 3b58ed8e0..1e6016b4d 100644
--- a/src/com/android/launcher3/LauncherStateManager.java
+++ b/src/com/android/launcher3/LauncherStateManager.java
@@ -249,6 +249,7 @@ public class LauncherStateManager {
mState.onStateDisabled(mLauncher);
mState = state;
mState.onStateEnabled(mLauncher);
+ mLauncher.getAppWidgetHost().setResumed(state == LauncherState.NORMAL);
}
/**