Merge "NIU Actions: Add NIU Actions tooltip" into sc-dev
diff --git a/go/quickstep/res/drawable/arrow_toast_rounded_background.xml b/go/quickstep/res/drawable/arrow_toast_rounded_background.xml
new file mode 100644
index 0000000..9c815fd
--- /dev/null
+++ b/go/quickstep/res/drawable/arrow_toast_rounded_background.xml
@@ -0,0 +1,19 @@
+<!--
+    Copyright (C) 2021 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.
+-->
+<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
+    <solid android:color="@color/arrow_tip_view_bg" />
+    <corners android:radius="@dimen/tooltip_corner_radius" />
+</shape>
diff --git a/go/quickstep/res/values/colors.xml b/go/quickstep/res/values/colors.xml
index 8034be2..4ce7669 100644
--- a/go/quickstep/res/values/colors.xml
+++ b/go/quickstep/res/values/colors.xml
@@ -23,4 +23,8 @@
     <!-- Modal Dialogs -->
     <color name="go_modal_dialog_background">#FFFFFF</color>
     <color name="go_modal_dialog_background_dark">#424242</color>
+
+    <!-- Tooltip Color -->
+    <color name="arrow_tip_view_bg">#1A73E8</color>
+    <color name="arrow_tip_view_content">#FFFFFF</color>
 </resources>
diff --git a/go/quickstep/res/values/dimens.xml b/go/quickstep/res/values/dimens.xml
index 0a7ac45..c14df50 100644
--- a/go/quickstep/res/values/dimens.xml
+++ b/go/quickstep/res/values/dimens.xml
@@ -36,4 +36,8 @@
     <dimen name="modal_dialog_vertical_spacer">12dp</dimen>
     <dimen name="modal_dialog_corner_radius">8dp</dimen>
     <dimen name="confirmation_dialog_text_height">216dp</dimen>
+
+    <!-- Tooltip -->
+    <dimen name="tooltip_corner_radius">8dp</dimen>
+    <dimen name="tooltip_top_margin">3dp</dimen>
 </resources>
diff --git a/go/quickstep/src/com/android/quickstep/TaskOverlayFactoryGo.java b/go/quickstep/src/com/android/quickstep/TaskOverlayFactoryGo.java
index 37f5352..f3c7a02 100644
--- a/go/quickstep/src/com/android/quickstep/TaskOverlayFactoryGo.java
+++ b/go/quickstep/src/com/android/quickstep/TaskOverlayFactoryGo.java
@@ -16,6 +16,8 @@
 
 package com.android.quickstep;
 
+import static android.view.Surface.ROTATION_0;
+
 import static com.android.quickstep.views.OverviewActionsView.DISABLED_NO_THUMBNAIL;
 import static com.android.quickstep.views.OverviewActionsView.DISABLED_ROTATED;
 
@@ -30,6 +32,7 @@
 import android.graphics.Color;
 import android.graphics.Matrix;
 import android.graphics.drawable.ColorDrawable;
+import android.os.Handler;
 import android.os.SystemClock;
 import android.os.UserManager;
 import android.provider.Settings;
@@ -46,7 +49,8 @@
 import com.android.launcher3.R;
 import com.android.launcher3.Utilities;
 import com.android.quickstep.util.AssistContentRequester;
-import com.android.quickstep.views.OverviewActionsView;
+import com.android.quickstep.util.RecentsOrientedState;
+import com.android.quickstep.views.GoOverviewActionsView;
 import com.android.quickstep.views.TaskThumbnailView;
 import com.android.systemui.shared.recents.model.Task;
 import com.android.systemui.shared.recents.model.ThumbnailData;
@@ -67,6 +71,9 @@
     private static final String NIU_ACTIONS_CONFIRMED = "launcher_go.niu_actions_confirmed";
     private static final String TAG = "TaskOverlayFactoryGo";
 
+    public static final String LISTEN_TOOL_TIP_SEEN = "launcher.go_listen_tip_seen";
+    public static final String TRANSLATE_TOOL_TIP_SEEN = "launcher.go_translate_tip_seen";
+
     private AssistContentRequester mContentRequester;
 
     public TaskOverlayFactoryGo(Context context) {
@@ -84,7 +91,7 @@
      * Overlay on each task handling Overview Action Buttons.
      * @param <T> The type of View in which the overlay will be placed
      */
-    public static final class TaskOverlayGo<T extends OverviewActionsView> extends TaskOverlay {
+    public static final class TaskOverlayGo<T extends GoOverviewActionsView> extends TaskOverlay {
         private String mNIUPackageName;
         private String mTaskPackageName;
         private String mWebUrl;
@@ -99,6 +106,7 @@
                 AssistContentRequester assistContentRequester) {
             super(taskThumbnailView);
             mFactoryContentRequester = assistContentRequester;
+            mSharedPreferences = Utilities.getPrefs(mApplicationContext);
         }
 
         /**
@@ -134,6 +142,18 @@
 
             int taskId = task.key.id;
             mFactoryContentRequester.requestAssistContent(taskId, this::onAssistContentReceived);
+
+            RecentsOrientedState orientedState =
+                    mThumbnailView.getTaskView().getRecentsView().getPagedViewOrientedState();
+            boolean isInLandscape = orientedState.getDisplayRotation() != ROTATION_0;
+
+            // show tooltips in portrait mode only
+            // TODO: remove If check once b/183714277 is fixed
+            if (!isInLandscape) {
+                new Handler().post(() -> {
+                    showTooltipsIfUnseen();
+                });
+            }
         }
 
         /** Provide Assist Content to the overlay. */
@@ -149,6 +169,12 @@
             mWebUrl = null;
         }
 
+        @Override
+        public void updateOrientationState(RecentsOrientedState state) {
+            super.updateOrientationState(state);
+            ((GoOverviewActionsView) getActionsView()).updateOrientationState(state);
+        }
+
         /**
          * Creates and sends an Intent corresponding to the button that was clicked
          */
@@ -275,6 +301,20 @@
         private void onNiuActionsConfirmationReject(View v) {
             mConfirmationDialog.cancel();
         }
+
+        /**
+         * Checks and Shows the tooltip if they are not seen by user
+         * Order of tooltips are translate and then listen
+         */
+        private void showTooltipsIfUnseen() {
+            if (!mSharedPreferences.getBoolean(TRANSLATE_TOOL_TIP_SEEN, false)) {
+                ((GoOverviewActionsView) getActionsView()).showTranslateToolTip();
+                mSharedPreferences.edit().putBoolean(TRANSLATE_TOOL_TIP_SEEN, true).apply();
+            } else if (!mSharedPreferences.getBoolean(LISTEN_TOOL_TIP_SEEN, false)) {
+                ((GoOverviewActionsView) getActionsView()).showListenToolTip();
+                mSharedPreferences.edit().putBoolean(LISTEN_TOOL_TIP_SEEN, true).apply();
+            }
+        }
     }
 
     /**
diff --git a/go/quickstep/src/com/android/quickstep/views/GoOverviewActionsView.java b/go/quickstep/src/com/android/quickstep/views/GoOverviewActionsView.java
index 9997d16..5b535a2 100644
--- a/go/quickstep/src/com/android/quickstep/views/GoOverviewActionsView.java
+++ b/go/quickstep/src/com/android/quickstep/views/GoOverviewActionsView.java
@@ -21,14 +21,20 @@
 import android.view.View;
 
 import androidx.annotation.Nullable;
+import androidx.annotation.Px;
 
 import com.android.launcher3.R;
+import com.android.launcher3.views.ArrowTipView;
 import com.android.quickstep.TaskOverlayFactoryGo.OverlayUICallbacksGo;
+import com.android.quickstep.util.RecentsOrientedState;
 
 /**
  * View for showing Go-specific action buttons in Overview
  */
-public final class GoOverviewActionsView extends OverviewActionsView<OverlayUICallbacksGo> {
+public class GoOverviewActionsView extends OverviewActionsView<OverlayUICallbacksGo> {
+
+    private ArrowTipView mArrowTipView;
+
     public GoOverviewActionsView(Context context) {
         this(context, null);
     }
@@ -72,4 +78,46 @@
             mCallbacks.onSearch();
         }
     }
+
+    /**
+     * Shows Tooltip for action icons
+     */
+    private void showToolTip(int viewId, int textResourceId) {
+        int[] location = new int[2];
+        @Px int topMargin = getResources().getDimensionPixelSize(R.dimen.tooltip_top_margin);
+        findViewById(viewId).getLocationOnScreen(location);
+        mArrowTipView = new ArrowTipView(getContext(),  /* isPointingUp= */ false)
+            .showAtLocation(getResources().getString(textResourceId),
+                /* arrowXCoord= */ location[0] + findViewById(viewId).getWidth() / 2,
+                /* yCoord= */ location[1] - topMargin);
+
+        mArrowTipView.bringToFront();
+    }
+
+    /**
+     * Shows Tooltip for listen action icon
+     */
+    public void showListenToolTip() {
+        showToolTip(/* viewId= */ R.id.action_listen,
+                /* textResourceId= */ R.string.tooltip_listen);
+    }
+
+    /**
+     * Shows Tooltip for translate action icon
+     */
+    public void showTranslateToolTip() {
+        showToolTip(/* viewId= */ R.id.action_translate,
+                /* textResourceId= */ R.string.tooltip_translate);
+    }
+
+    /**
+     * Called when device orientation is changed
+     */
+    public void updateOrientationState(RecentsOrientedState orientedState) {
+        // dismiss tooltip
+        boolean canLauncherRotate = orientedState.canRecentsActivityRotate();
+        if (mArrowTipView != null && !canLauncherRotate) {
+            mArrowTipView.close(/* animate= */ false);
+        }
+    }
 }
diff --git a/quickstep/src/com/android/quickstep/TaskOverlayFactory.java b/quickstep/src/com/android/quickstep/TaskOverlayFactory.java
index 06a3c70..e75d751 100644
--- a/quickstep/src/com/android/quickstep/TaskOverlayFactory.java
+++ b/quickstep/src/com/android/quickstep/TaskOverlayFactory.java
@@ -267,6 +267,12 @@
             return mThumbnailView.getScaledInsets();
         }
 
+        /**
+         * Called when the device rotated.
+         */
+        public void updateOrientationState(RecentsOrientedState state) {
+        }
+
         protected void showBlockedByPolicyMessage() {
             Toast.makeText(
                     mThumbnailView.getContext(),
diff --git a/quickstep/src/com/android/quickstep/util/RecentsOrientedState.java b/quickstep/src/com/android/quickstep/util/RecentsOrientedState.java
index bfd9008..c515bdf 100644
--- a/quickstep/src/com/android/quickstep/util/RecentsOrientedState.java
+++ b/quickstep/src/com/android/quickstep/util/RecentsOrientedState.java
@@ -65,7 +65,7 @@
  * This class has initial default state assuming the device and foreground app have
  * no ({@link Surface#ROTATION_0} rotation.
  */
-public final class RecentsOrientedState implements
+public class RecentsOrientedState implements
         SharedPreferences.OnSharedPreferenceChangeListener {
 
     private static final String TAG = "RecentsOrientedState";
diff --git a/quickstep/src/com/android/quickstep/views/TaskView.java b/quickstep/src/com/android/quickstep/views/TaskView.java
index 7690102..2d322e9 100644
--- a/quickstep/src/com/android/quickstep/views/TaskView.java
+++ b/quickstep/src/com/android/quickstep/views/TaskView.java
@@ -800,6 +800,7 @@
         mIconView.setRotation(orientationHandler.getDegreesRotated());
         snapshotParams.topMargin = deviceProfile.overviewTaskThumbnailTopMarginPx;
         mSnapshotView.setLayoutParams(snapshotParams);
+        getThumbnail().getTaskOverlay().updateOrientationState(orientationState);
     }
 
     private void setIconAndDimTransitionProgress(float progress, boolean invert) {