Prototype launching FED after detecting 5 taps power button

This launches FED, not the panic UI. And it's not launching through
sysui as designed.

It also does not deal with keyguard so screen will flash when tapping 5
times.

Left TODOs in code to fix later.

Test: manual
Bug: 161394591
Change-Id: I9791279a2bc13355a8b79af665ef449cd6040c7b
diff --git a/services/core/java/com/android/server/GestureLauncherService.java b/services/core/java/com/android/server/GestureLauncherService.java
index c87dcd7..b3d4085 100644
--- a/services/core/java/com/android/server/GestureLauncherService.java
+++ b/services/core/java/com/android/server/GestureLauncherService.java
@@ -38,6 +38,7 @@
 import android.os.Trace;
 import android.os.UserHandle;
 import android.provider.Settings;
+import android.telecom.TelecomManager;
 import android.util.MutableBoolean;
 import android.util.Slog;
 import android.view.KeyEvent;
@@ -457,6 +458,8 @@
             }
         } else if (launchPanic) {
             Slog.i(TAG, "Panic gesture detected, launching panic.");
+            launchPanic = handlePanicButtonGesture();
+            // TODO(b/160006048): Add logging
         }
         mMetricsLogger.histogram("power_consecutive_short_tap_count",
                 mPowerButtonSlowConsecutiveTaps);
@@ -501,6 +504,46 @@
         }
     }
 
+    /**
+     * @return true if panic ui was launched, false otherwise.
+     */
+    @VisibleForTesting
+    boolean handlePanicButtonGesture() {
+        // TODO(b/160006048): This is the wrong way to launch panic ui. Rewrite this to go
+        //  through SysUI
+        Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
+                "GestureLauncher:handlePanicButtonGesture");
+        try {
+            boolean userSetupComplete = Settings.Secure.getIntForUser(mContext.getContentResolver(),
+                    Settings.Secure.USER_SETUP_COMPLETE, 0, UserHandle.USER_CURRENT) != 0;
+            if (!userSetupComplete) {
+                if (DBG) {
+                    Slog.d(TAG, String.format(
+                            "userSetupComplete = %s, ignoring panic gesture.",
+                            userSetupComplete));
+                }
+                return false;
+            }
+            if (DBG) {
+                Slog.d(TAG, String.format(
+                        "userSetupComplete = %s, performing panic gesture.",
+                        userSetupComplete));
+            }
+            // TODO(b/160006048): Not all devices have telephony. Check system feature first.
+            TelecomManager telecomManager = (TelecomManager) mContext.getSystemService(
+                    Context.TELECOM_SERVICE);
+            mContext.startActivity(telecomManager.createLaunchEmergencyDialerIntent(null).addFlags(
+                    Intent.FLAG_ACTIVITY_NEW_TASK
+                            | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
+                            | Intent.FLAG_ACTIVITY_SINGLE_TOP).putExtra(
+                    "com.android.phone.EmergencyDialer.extra.ENTRY_TYPE",
+                    2)); // 2 maps to power button, forcing into fast emergency dialer experience.
+            return true;
+        } finally {
+            Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
+        }
+    }
+
     private final BroadcastReceiver mUserReceiver = new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {
diff --git a/services/tests/servicestests/src/com/android/server/GestureLauncherServiceTest.java b/services/tests/servicestests/src/com/android/server/GestureLauncherServiceTest.java
index 6f37ff5..c91bb93 100644
--- a/services/tests/servicestests/src/com/android/server/GestureLauncherServiceTest.java
+++ b/services/tests/servicestests/src/com/android/server/GestureLauncherServiceTest.java
@@ -28,11 +28,13 @@
 
 import android.app.StatusBarManager;
 import android.content.Context;
+import android.content.Intent;
 import android.content.res.Resources;
 import android.os.Looper;
 import android.os.UserHandle;
 import android.platform.test.annotations.Presubmit;
 import android.provider.Settings;
+import android.telecom.TelecomManager;
 import android.test.mock.MockContentResolver;
 import android.util.MutableBoolean;
 import android.view.KeyEvent;
@@ -80,6 +82,7 @@
     private @Mock Context mContext;
     private @Mock Resources mResources;
     private @Mock StatusBarManagerInternal mStatusBarManagerInternal;
+    private @Mock TelecomManager mTelecomManager;
     private @Mock MetricsLogger mMetricsLogger;
     private MockContentResolver mContentResolver;
     private GestureLauncherService mGestureLauncherService;
@@ -104,6 +107,8 @@
         mContentResolver = new MockContentResolver(mContext);
         mContentResolver.addProvider(Settings.AUTHORITY, new FakeSettingsProvider());
         when(mContext.getContentResolver()).thenReturn(mContentResolver);
+        when(mContext.getSystemService(Context.TELECOM_SERVICE)).thenReturn(mTelecomManager);
+        when(mTelecomManager.createLaunchEmergencyDialerIntent(null)).thenReturn(new Intent());
 
         mGestureLauncherService = new GestureLauncherService(mContext, mMetricsLogger);
     }
@@ -176,6 +181,13 @@
     }
 
     @Test
+    public void testHandlePanicGesture_userSetupComplete() {
+        withUserSetupCompleteValue(true);
+
+        assertTrue(mGestureLauncherService.handlePanicButtonGesture());
+    }
+
+    @Test
     public void testHandleCameraLaunchGesture_userSetupNotComplete() {
         withUserSetupCompleteValue(false);
 
@@ -184,6 +196,13 @@
     }
 
     @Test
+    public void testHandlePanicGesture_userSetupNotComplete() {
+        withUserSetupCompleteValue(false);
+
+        assertFalse(mGestureLauncherService.handlePanicButtonGesture());
+    }
+
+    @Test
     public void testInterceptPowerKeyDown_firstPowerDownCameraPowerGestureOnInteractive() {
         withCameraDoubleTapPowerEnableConfigValue(true);
         withCameraDoubleTapPowerDisableSettingValue(0);