Register/unregister all apps system action with accessibility

Bug: 136286274
Change-Id: Ia43d30b7cf078f56cc9887c8b3fb31924ad2b1b0
diff --git a/quickstep/src/com/android/launcher3/BaseQuickstepLauncher.java b/quickstep/src/com/android/launcher3/BaseQuickstepLauncher.java
index fa0e840..b3b0572 100644
--- a/quickstep/src/com/android/launcher3/BaseQuickstepLauncher.java
+++ b/quickstep/src/com/android/launcher3/BaseQuickstepLauncher.java
@@ -36,6 +36,7 @@
 
 import com.android.launcher3.LauncherState.ScaleAndTranslation;
 import com.android.launcher3.LauncherStateManager.StateHandler;
+import com.android.launcher3.accessibility.SystemActions;
 import com.android.launcher3.config.FeatureFlags;
 import com.android.launcher3.model.WellbeingModel;
 import com.android.launcher3.popup.SystemShortcut;
@@ -62,6 +63,8 @@
 public abstract class BaseQuickstepLauncher extends Launcher
         implements NavigationModeChangeListener {
 
+    protected SystemActions mSystemActions;
+
     /**
      * Reusable command for applying the back button alpha on the background thread.
      */
@@ -74,6 +77,7 @@
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
+        mSystemActions = new SystemActions(this);
 
         SysUINavigationMode.Mode mode = SysUINavigationMode.INSTANCE.get(this)
                 .addModeChangeListener(this);
@@ -132,6 +136,12 @@
     }
 
     @Override
+    public void onActivityResult(int requestCode, int resultCode, Intent data) {
+        super.onActivityResult(requestCode, resultCode, data);
+        mSystemActions.onActivityResult(requestCode);
+    }
+
+    @Override
     public void onEnterAnimationComplete() {
         super.onEnterAnimationComplete();
         // After the transition to home, enable the high-res thumbnail loader if it wasn't enabled
@@ -188,6 +198,15 @@
             // removes the task itself.
             startActivity(ProxyActivityStarter.getLaunchIntent(this, null));
         }
+
+        // Register all system actions once they are available
+        mSystemActions.register();
+    }
+
+    @Override
+    protected void onPause() {
+        super.onPause();
+        mSystemActions.unregister();
     }
 
     @Override
diff --git a/quickstep/src/com/android/launcher3/accessibility/SystemActions.java b/quickstep/src/com/android/launcher3/accessibility/SystemActions.java
new file mode 100644
index 0000000..669877f
--- /dev/null
+++ b/quickstep/src/com/android/launcher3/accessibility/SystemActions.java
@@ -0,0 +1,90 @@
+/*
+ * Copyright (C) 2020 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.accessibility;
+
+import static com.android.launcher3.LauncherState.ALL_APPS;
+import static com.android.launcher3.LauncherState.NORMAL;
+
+import android.app.PendingIntent;
+import android.app.RemoteAction;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.graphics.drawable.Icon;
+import android.view.accessibility.AccessibilityManager;
+import com.android.launcher3.Launcher;
+import com.android.launcher3.LauncherState;
+import com.android.launcher3.LauncherStateManager;
+import com.android.launcher3.R;
+
+/**
+ * Manages the launcher system actions presented to accessibility services.
+ */
+public class SystemActions {
+
+    /**
+     * System Action ID to show all apps.  This ID should follow the ones in
+     * com.android.systemui.accessibility.SystemActions.
+     */
+    private static final int SYSTEM_ACTION_ID_ALL_APPS = 100;
+
+    private Launcher mLauncher;
+    private AccessibilityManager mAccessibilityManager;
+    private RemoteAction mAllAppsAction;
+    private boolean mRegistered;
+
+    public SystemActions(Launcher launcher) {
+        mLauncher = launcher;
+        mAccessibilityManager = (AccessibilityManager) launcher.getSystemService(
+                Context.ACCESSIBILITY_SERVICE);
+        mAllAppsAction = new RemoteAction(
+                Icon.createWithResource(launcher, R.drawable.ic_apps),
+                launcher.getString(R.string.all_apps_label),
+                launcher.getString(R.string.all_apps_label),
+                launcher.createPendingResult(SYSTEM_ACTION_ID_ALL_APPS, new Intent(),
+                        0 /* flags */));
+    }
+
+    public void register() {
+        if (mRegistered) {
+            return;
+        }
+        mAccessibilityManager.registerSystemAction(mAllAppsAction, SYSTEM_ACTION_ID_ALL_APPS);
+        mRegistered = true;
+    }
+
+    public void unregister() {
+        if (!mRegistered) {
+            return;
+        }
+        mAccessibilityManager.unregisterSystemAction(SYSTEM_ACTION_ID_ALL_APPS);
+        mRegistered = false;
+    }
+
+    public void onActivityResult(int requestCode) {
+        if (requestCode == SYSTEM_ACTION_ID_ALL_APPS) {
+            showAllApps();
+        }
+    }
+
+    private void showAllApps() {
+        LauncherStateManager stateManager = mLauncher.getStateManager();
+        stateManager.goToState(NORMAL);
+        stateManager.goToState(ALL_APPS);
+    }
+}
diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java
index d6e8710..8e87d16 100644
--- a/src/com/android/launcher3/Launcher.java
+++ b/src/com/android/launcher3/Launcher.java
@@ -834,6 +834,7 @@
                         ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
             }
         }
+
         mDragLayer.clearAnimatedView();
     }