Merge "Don't crash when swiping up if there are no TaskViews" into ub-launcher3-qt-qpr1-dev
diff --git a/quickstep/recents_ui_overrides/src/com/android/quickstep/views/TaskMenuView.java b/quickstep/recents_ui_overrides/src/com/android/quickstep/views/TaskMenuView.java
index c1f6b82..742d6a2 100644
--- a/quickstep/recents_ui_overrides/src/com/android/quickstep/views/TaskMenuView.java
+++ b/quickstep/recents_ui_overrides/src/com/android/quickstep/views/TaskMenuView.java
@@ -26,6 +26,7 @@
 import android.graphics.Rect;
 import android.graphics.drawable.Drawable;
 import android.util.AttributeSet;
+import android.util.Log;
 import android.view.Gravity;
 import android.view.MotionEvent;
 import android.view.View;
@@ -40,6 +41,7 @@
 import com.android.launcher3.anim.AnimationSuccessListener;
 import com.android.launcher3.anim.Interpolators;
 import com.android.launcher3.anim.RoundedRectRevealOutlineProvider;
+import com.android.launcher3.testing.TestProtocol;
 import com.android.launcher3.util.Themes;
 import com.android.launcher3.views.BaseDragLayer;
 import com.android.quickstep.TaskOverlayFactory;
@@ -159,6 +161,9 @@
     }
 
     public static TaskMenuView showForTask(TaskView taskView) {
+        if (TestProtocol.sDebugTracing) {
+            Log.d(TestProtocol.WELLBEING_NO_TASK_MENU, "showForTask");
+        }
         BaseDraggingActivity activity = BaseDraggingActivity.fromContext(taskView.getContext());
         final TaskMenuView taskMenuView = (TaskMenuView) activity.getLayoutInflater().inflate(
                         R.layout.task_menu, activity.getDragLayer(), false);
@@ -166,9 +171,15 @@
     }
 
     private boolean populateAndShowForTask(TaskView taskView) {
+        if (TestProtocol.sDebugTracing) {
+            Log.d(TestProtocol.WELLBEING_NO_TASK_MENU, "populateAndShowForTask1");
+        }
         if (isAttachedToWindow()) {
             return false;
         }
+        if (TestProtocol.sDebugTracing) {
+            Log.d(TestProtocol.WELLBEING_NO_TASK_MENU, "populateAndShowForTask2");
+        }
         mActivity.getDragLayer().addView(this);
         mTaskView = taskView;
         addMenuOptions(mTaskView);
diff --git a/quickstep/recents_ui_overrides/src/com/android/quickstep/views/TaskView.java b/quickstep/recents_ui_overrides/src/com/android/quickstep/views/TaskView.java
index bfb9613..3eed281 100644
--- a/quickstep/recents_ui_overrides/src/com/android/quickstep/views/TaskView.java
+++ b/quickstep/recents_ui_overrides/src/com/android/quickstep/views/TaskView.java
@@ -53,6 +53,7 @@
 import com.android.launcher3.anim.AnimatorPlaybackController;
 import com.android.launcher3.anim.Interpolators;
 import com.android.launcher3.logging.UserEventDispatcher;
+import com.android.launcher3.testing.TestProtocol;
 import com.android.launcher3.userevent.nano.LauncherLogProto;
 import com.android.launcher3.userevent.nano.LauncherLogProto.Action.Direction;
 import com.android.launcher3.userevent.nano.LauncherLogProto.Action.Touch;
@@ -390,6 +391,9 @@
             mIconView.setDrawable(icon);
             mIconView.setOnClickListener(v -> showTaskMenu(Touch.TAP));
             mIconView.setOnLongClickListener(v -> {
+                if (TestProtocol.sDebugTracing) {
+                    Log.d(TestProtocol.WELLBEING_NO_TASK_MENU, "setOnLongClickListener");
+                }
                 requestDisallowInterceptTouchEvent(true);
                 return showTaskMenu(Touch.LONGPRESS);
             });
diff --git a/src/com/android/launcher3/PagedView.java b/src/com/android/launcher3/PagedView.java
index bbb3915..70b55a4 100644
--- a/src/com/android/launcher3/PagedView.java
+++ b/src/com/android/launcher3/PagedView.java
@@ -890,23 +890,7 @@
                 mTotalMotionX = 0;
                 mActivePointerId = ev.getPointerId(0);
 
-                /*
-                 * If being flinged and user touches the screen, initiate drag;
-                 * otherwise don't.  mScroller.isFinished should be false when
-                 * being flinged.
-                 */
-                final int xDist = Math.abs(mScroller.getFinalPos() - mScroller.getCurrPos());
-                final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
-
-                if (finishedScrolling) {
-                    mIsBeingDragged = false;
-                    if (!mScroller.isFinished() && !mFreeScroll) {
-                        setCurrentPage(getNextPage());
-                        pageEndTransition();
-                    }
-                } else {
-                    mIsBeingDragged = true;
-                }
+                updateIsBeingDraggedOnTouchDown();
 
                 break;
             }
@@ -929,6 +913,25 @@
         return mIsBeingDragged;
     }
 
+    /**
+     * If being flinged and user touches the screen, initiate drag; otherwise don't.
+     */
+    private void updateIsBeingDraggedOnTouchDown() {
+        // mScroller.isFinished should be false when being flinged.
+        final int xDist = Math.abs(mScroller.getFinalPos() - mScroller.getCurrPos());
+        final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
+
+        if (finishedScrolling) {
+            mIsBeingDragged = false;
+            if (!mScroller.isFinished() && !mFreeScroll) {
+                setCurrentPage(getNextPage());
+                pageEndTransition();
+            }
+        } else {
+            mIsBeingDragged = true;
+        }
+    }
+
     public boolean isHandlingTouch() {
         return mIsBeingDragged;
     }
@@ -1104,6 +1107,8 @@
 
         switch (action & MotionEvent.ACTION_MASK) {
         case MotionEvent.ACTION_DOWN:
+            updateIsBeingDraggedOnTouchDown();
+
             /*
              * If being flinged and user touches, stop the fling. isFinished
              * will be false if being flinged.
diff --git a/src/com/android/launcher3/WorkspaceItemInfo.java b/src/com/android/launcher3/WorkspaceItemInfo.java
index 050a8be..9451aae 100644
--- a/src/com/android/launcher3/WorkspaceItemInfo.java
+++ b/src/com/android/launcher3/WorkspaceItemInfo.java
@@ -212,7 +212,7 @@
     public ComponentName getTargetComponent() {
         ComponentName cn = super.getTargetComponent();
         if (cn == null && (itemType == Favorites.ITEM_TYPE_SHORTCUT
-                || hasStatusFlag(FLAG_SUPPORTS_WEB_UI | FLAG_AUTOINSTALL_ICON))) {
+                || hasStatusFlag(FLAG_SUPPORTS_WEB_UI|FLAG_AUTOINSTALL_ICON|FLAG_RESTORED_ICON))) {
             // Legacy shortcuts and promise icons with web UI may not have a componentName but just
             // a packageName. In that case create a dummy componentName instead of adding additional
             // check everywhere.
diff --git a/src/com/android/launcher3/model/PackageUpdatedTask.java b/src/com/android/launcher3/model/PackageUpdatedTask.java
index 4428c8e..2d1e94f 100644
--- a/src/com/android/launcher3/model/PackageUpdatedTask.java
+++ b/src/com/android/launcher3/model/PackageUpdatedTask.java
@@ -57,6 +57,7 @@
 import java.util.List;
 
 import static com.android.launcher3.WorkspaceItemInfo.FLAG_AUTOINSTALL_ICON;
+import static com.android.launcher3.WorkspaceItemInfo.FLAG_RESTORED_ICON;
 
 /**
  * Handles updates due to changes in package manager (app installed/updated/removed)
@@ -234,7 +235,7 @@
                                     isTargetValid = LauncherAppsCompat.getInstance(context)
                                             .isActivityEnabledForProfile(cn, mUser);
                                 }
-                                if (si.hasStatusFlag(FLAG_AUTOINSTALL_ICON)) {
+                                if (si.hasStatusFlag(FLAG_RESTORED_ICON | FLAG_AUTOINSTALL_ICON)) {
                                     if (updateWorkspaceItemIntent(context, si, packageName)) {
                                         infoUpdated = true;
                                     } else if (si.hasPromiseIconUi()) {
diff --git a/src/com/android/launcher3/testing/TestProtocol.java b/src/com/android/launcher3/testing/TestProtocol.java
index 232a764..19c557c 100644
--- a/src/com/android/launcher3/testing/TestProtocol.java
+++ b/src/com/android/launcher3/testing/TestProtocol.java
@@ -86,4 +86,5 @@
     public static final String APP_NOT_DISABLED = "b/139891609";
     public static final String ALL_APPS_UPON_RECENTS = "b/139941530";
     public static final String STABLE_STATE_MISMATCH = "b/140311911";
+    public static final String WELLBEING_NO_TASK_MENU = "b/141275518";
 }
diff --git a/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java b/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java
index dc890bb..840fdd2 100644
--- a/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java
+++ b/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java
@@ -60,6 +60,7 @@
 import com.android.launcher3.util.rule.FailureWatcher;
 import com.android.launcher3.util.rule.LauncherActivityRule;
 import com.android.launcher3.util.rule.ShellCommandRule;
+import com.android.launcher3.util.rule.TestStabilityRule;
 
 import org.junit.After;
 import org.junit.Before;
@@ -155,7 +156,8 @@
 
     @Rule
     public TestRule mOrderSensitiveRules = RuleChain.
-            outerRule(mActivityMonitor).
+            outerRule(new TestStabilityRule()).
+            around(mActivityMonitor).
             around(getRulesInsideActivityMonitor());
 
     public UiDevice getDevice() {
diff --git a/tests/src/com/android/launcher3/ui/TaplTestsLauncher3.java b/tests/src/com/android/launcher3/ui/TaplTestsLauncher3.java
index c2a3c1c..2cf6c2b 100644
--- a/tests/src/com/android/launcher3/ui/TaplTestsLauncher3.java
+++ b/tests/src/com/android/launcher3/ui/TaplTestsLauncher3.java
@@ -24,6 +24,8 @@
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
+import android.util.Log;
+
 import androidx.test.filters.LargeTest;
 import androidx.test.runner.AndroidJUnit4;
 
@@ -34,9 +36,9 @@
 import com.android.launcher3.tapl.AppIcon;
 import com.android.launcher3.tapl.AppIconMenu;
 import com.android.launcher3.tapl.AppIconMenuItem;
-import com.android.launcher3.tapl.TestHelpers;
 import com.android.launcher3.tapl.Widgets;
 import com.android.launcher3.tapl.Workspace;
+import com.android.launcher3.util.rule.TestStabilityRule.Stability;
 import com.android.launcher3.views.OptionsPopupView;
 import com.android.launcher3.widget.WidgetsFullSheet;
 import com.android.launcher3.widget.WidgetsRecyclerView;
@@ -343,4 +345,10 @@
     public static String getAppPackageName() {
         return getInstrumentation().getContext().getPackageName();
     }
+
+    @Test
+    @Stability
+    public void testTestStabilityAttribute() {
+        Log.d("TestStabilityRule", "Hello world!");
+    }
 }
diff --git a/tests/src/com/android/launcher3/util/rule/TestStabilityRule.java b/tests/src/com/android/launcher3/util/rule/TestStabilityRule.java
new file mode 100644
index 0000000..8391ae7
--- /dev/null
+++ b/tests/src/com/android/launcher3/util/rule/TestStabilityRule.java
@@ -0,0 +1,137 @@
+/*
+ * Copyright (C) 2019 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.
+ */
+package com.android.launcher3.util.rule;
+
+import static androidx.test.InstrumentationRegistry.getInstrumentation;
+
+import android.os.Build;
+import android.util.Log;
+
+import androidx.test.uiautomator.UiDevice;
+
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class TestStabilityRule implements TestRule {
+    private static final String TAG = "TestStabilityRule";
+    private static final Pattern LAUNCHER_BUILD =
+            Pattern.compile("^("
+                    + "(?<androidStudio>BuildFromAndroidStudio)|"
+                    + "(?<commandLine>[0-9]+-eng\\.[a-z]+\\.[0-9]+\\.[0-9]+)|"
+                    + "(?<presubmit>[0-9]+-P[0-9]+)|"
+                    + "(?<postsubmit>[0-9]+-[0-9]+|"
+                    + "(?<platform>[0-9]+))"
+                    + ")$");
+    private static final Pattern PLATFORM_BUILD =
+            Pattern.compile("^("
+                    + "(?<commandLine>eng\\.[a-z]+\\.[0-9]+\\.[0-9]+)|"
+                    + "(?<presubmit>P[0-9]+)|"
+                    + "(?<postsubmit>[0-9]+)"
+                    + ")$");
+
+    @Retention(RetentionPolicy.RUNTIME)
+    @Target(ElementType.METHOD)
+    public @interface Stability {
+    }
+
+    @Override
+    public Statement apply(Statement base, Description description) {
+        if (description.getAnnotation(Stability.class) != null) {
+            return new Statement() {
+                @Override
+                public void evaluate() throws Throwable {
+                    final String launcherVersion =
+                            getInstrumentation().
+                                    getContext().
+                                    getPackageManager().
+                                    getPackageInfo(
+                                            UiDevice.getInstance(getInstrumentation()).
+                                                    getLauncherPackageName(),
+                                            0).
+                                    versionName;
+
+                    final Matcher launcherBuildMatcher = LAUNCHER_BUILD.matcher(launcherVersion);
+
+                    boolean launcherLocalBuild = false;
+                    boolean launcherUnbundledPresubmit = false;
+                    boolean launcherUnbundledPostsubmit = false;
+                    boolean launcherPlatform = false;
+
+                    if (!launcherBuildMatcher.find()) {
+                        Log.e(TAG, "Match not found");
+                    } else if (launcherBuildMatcher.group("androidStudio") != null
+                            || launcherBuildMatcher.group("commandLine") != null) {
+                        launcherLocalBuild = true;
+                    } else if (launcherBuildMatcher.group("presubmit") != null) {
+                        launcherUnbundledPresubmit = true;
+                    } else if (launcherBuildMatcher.group("postsubmit") != null) {
+                        launcherUnbundledPostsubmit = true;
+                    } else if (launcherBuildMatcher.group("platform") != null) {
+                        launcherPlatform = true;
+                    } else {
+                        Log.e(TAG, "ERROR1");
+                    }
+
+                    boolean platformLocalBuild = false;
+                    boolean platformPresubmit = false;
+                    boolean platformPostsubmit = false;
+
+                    final String platformVersion = Build.VERSION.INCREMENTAL;
+                    final Matcher platformBuildMatcher = PLATFORM_BUILD.matcher(platformVersion);
+                    if (!platformBuildMatcher.find()) {
+                        Log.e(TAG, "Match not found");
+                    } else if (platformBuildMatcher.group("commandLine") != null) {
+                        platformLocalBuild = true;
+                    } else if (platformBuildMatcher.group("presubmit") != null) {
+                        platformPresubmit = true;
+                    } else if (platformBuildMatcher.group("postsubmit") != null) {
+                        platformPostsubmit = true;
+                    } else {
+                        Log.e(TAG, "ERROR2");
+                    }
+
+                    Log.d(TAG, "Launcher: " + launcherVersion + ", platform: " + platformVersion);
+
+                    if (launcherLocalBuild && (platformLocalBuild || platformPostsubmit)) {
+                        Log.d(TAG, "LOCAL RUN");
+                    } else if (launcherUnbundledPresubmit && platformPostsubmit) {
+                        Log.d(TAG, "UNBUNDLED PRESUBMIT");
+                    } else if (launcherUnbundledPostsubmit && platformPostsubmit) {
+                        Log.d(TAG, "UNBUNDLED POSTSUBMIT");
+                    } else if (launcherPlatform && platformPresubmit) {
+                        Log.d(TAG, "PLATFORM PRESUBMIT");
+                    } else if (launcherPlatform && platformPostsubmit) {
+                        Log.d(TAG, "PLATFORM POSTSUBMIT");
+                    } else {
+                        Log.e(TAG, "ERROR3");
+                    }
+
+                    base.evaluate();
+                }
+            };
+        } else {
+            return base;
+        }
+    }
+}