Merge "Accessibility of clear-all button" into ub-launcher3-edmonton
diff --git a/quickstep/res/layout/overview_clear_all_button.xml b/quickstep/res/layout/overview_clear_all_button.xml
index 1ada914..0dc5d7c 100644
--- a/quickstep/res/layout/overview_clear_all_button.xml
+++ b/quickstep/res/layout/overview_clear_all_button.xml
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 
-<TextView
+<com.android.quickstep.views.ClearAllButton
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/clear_all_button"
     style="@android:style/Widget.DeviceDefault.Button.Borderless"
@@ -10,5 +10,6 @@
     android:fontFamily="sans-serif-medium"
     android:text="@string/recents_clear_all"
     android:textColor="?attr/workspaceTextColor"
+    android:visibility="invisible"
     android:textSize="14sp"
 />
\ No newline at end of file
diff --git a/quickstep/src/com/android/quickstep/views/ClearAllButton.java b/quickstep/src/com/android/quickstep/views/ClearAllButton.java
new file mode 100644
index 0000000..14867ab
--- /dev/null
+++ b/quickstep/src/com/android/quickstep/views/ClearAllButton.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2018 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.quickstep.views;
+
+import static android.view.accessibility.AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS;
+
+import android.content.Context;
+import android.os.Bundle;
+import android.support.annotation.Nullable;
+import android.util.AttributeSet;
+import android.view.accessibility.AccessibilityNodeInfo;
+import android.widget.TextView;
+
+public class ClearAllButton extends TextView {
+    RecentsView mRecentsView;
+
+    public ClearAllButton(Context context, @Nullable AttributeSet attrs) {
+        super(context, attrs);
+    }
+
+    public void setRecentsView(RecentsView recentsView) {
+        mRecentsView = recentsView;
+    }
+
+    @Override
+    public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
+        super.onInitializeAccessibilityNodeInfo(info);
+        // Should be visible to accessibility even when completely covered by the task.
+        // Otherwise, we won't be able to scroll to it.
+        info.setVisibleToUser(true);
+    }
+
+    @Override
+    public boolean performAccessibilityAction(int action, Bundle arguments) {
+        final boolean res = super.performAccessibilityAction(action, arguments);
+        if (action == ACTION_ACCESSIBILITY_FOCUS) {
+            mRecentsView.revealClearAllButton();
+        }
+        return res;
+    }
+}
diff --git a/quickstep/src/com/android/quickstep/views/RecentsView.java b/quickstep/src/com/android/quickstep/views/RecentsView.java
index 8d6b890..b1f057b 100644
--- a/quickstep/src/com/android/quickstep/views/RecentsView.java
+++ b/quickstep/src/com/android/quickstep/views/RecentsView.java
@@ -272,6 +272,7 @@
             loader.unloadTaskData(task);
             loader.getHighResThumbnailLoader().onTaskInvisible(task);
         }
+        onChildViewsChanged();
     }
 
     public boolean isTaskViewVisible(TaskView tv) {
@@ -366,7 +367,6 @@
         if (mClearAllButton != null) {
             final float alpha = calculateClearAllButtonAlpha();
             mClearAllButton.setAlpha(alpha * mContentAlpha);
-            mClearAllButton.setVisibility(alpha == 0 ? INVISIBLE : VISIBLE);
         }
     }
 
@@ -379,7 +379,7 @@
     @Override
     public boolean onTouchEvent(MotionEvent ev) {
         if (ev.getAction() == MotionEvent.ACTION_DOWN && mTouchState == TOUCH_STATE_REST
-                && mScroller.isFinished() && mClearAllButton.getVisibility() == View.VISIBLE) {
+                && mScroller.isFinished() && mClearAllButton.getAlpha() > 0) {
             mClearAllButton.getHitRect(mTempRect);
             mTempRect.offset(-getLeft(), -getTop());
             if (mTempRect.contains((int) ev.getX(), (int) ev.getY())) {
@@ -1019,6 +1019,7 @@
         super.onViewAdded(child);
         child.setAlpha(mContentAlpha);
         setAdjacentScale(mAdjacentScale);
+        onChildViewsChanged();
     }
 
     @Override
@@ -1251,4 +1252,15 @@
         mClearAllButton = clearAllButton;
         updateClearAllButtonAlpha();
     }
+
+    private void onChildViewsChanged() {
+        final int childCount = getChildCount();
+        mClearAllButton.setAccessibilityTraversalAfter(
+                childCount == 0 ? NO_ID : getChildAt(childCount - 1).getId());
+        mClearAllButton.setVisibility(childCount == 0 ? INVISIBLE : VISIBLE);
+    }
+
+    public void revealClearAllButton() {
+        scrollTo(mIsRtl ? 0 : computeMaxScrollX(), 0);
+    }
 }
diff --git a/quickstep/src/com/android/quickstep/views/RecentsViewContainer.java b/quickstep/src/com/android/quickstep/views/RecentsViewContainer.java
index 15925b5..a951de9 100644
--- a/quickstep/src/com/android/quickstep/views/RecentsViewContainer.java
+++ b/quickstep/src/com/android/quickstep/views/RecentsViewContainer.java
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2018 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.quickstep.views;
 
 import static com.android.launcher3.userevent.nano.LauncherLogProto.Action.Touch.TAP;
@@ -9,7 +25,6 @@
 import android.util.FloatProperty;
 import android.view.Gravity;
 import android.view.MotionEvent;
-import android.view.View;
 
 import com.android.launcher3.InsettableFrameLayout;
 import com.android.launcher3.R;
@@ -31,7 +46,7 @@
     private final Rect mTempRect = new Rect();
 
     private RecentsView mRecentsView;
-    private View mClearAllButton;
+    private ClearAllButton mClearAllButton;
 
     public RecentsViewContainer(Context context, AttributeSet attrs) {
         super(context, attrs);
@@ -48,13 +63,15 @@
             mRecentsView.dismissAllTasks();
         });
 
-        mRecentsView = (RecentsView) findViewById(R.id.overview_panel);
+        mRecentsView = findViewById(R.id.overview_panel);
         final InsettableFrameLayout.LayoutParams params =
                 (InsettableFrameLayout.LayoutParams) mClearAllButton.getLayoutParams();
         params.gravity = Gravity.TOP | (RecentsView.FLIP_RECENTS ? Gravity.START : Gravity.END);
         mClearAllButton.setLayoutParams(params);
         mClearAllButton.forceHasOverlappingRendering(false);
+
         mRecentsView.setClearAllButton(mClearAllButton);
+        mClearAllButton.setRecentsView(mRecentsView);
     }
 
     @Override