Call requestDismissKeyguard instead of using the
now deprecated FLAG_DISMISS_KEYGUARD.

Also, sleep 1 second after calling sleep.

These changes reduce the flakiness of this test.

bug: 127851803
bug: 129438382
Test: cts-tradefed run cts-dev -m CtsTrustedVoiceHostTestCases
On a prod PI device.

Change-Id: I43b104224a5d8b5962ec82119167958674b18e15
(cherry picked from commit e7ff263541ea59547effe246c07ab8f906f1c015)
diff --git a/hostsidetests/trustedvoice/app/AndroidManifest.xml b/hostsidetests/trustedvoice/app/AndroidManifest.xml
index 53b2981..f54af61 100755
--- a/hostsidetests/trustedvoice/app/AndroidManifest.xml
+++ b/hostsidetests/trustedvoice/app/AndroidManifest.xml
@@ -20,7 +20,8 @@
 
     <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
     <application>
-        <activity android:name=".TrustedVoiceActivity" >
+        <activity android:name=".TrustedVoiceActivity"
+                  android:turnScreenOn="true">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.LAUNCHER" />
diff --git a/hostsidetests/trustedvoice/app/src/android/trustedvoice/app/TrustedVoiceActivity.java b/hostsidetests/trustedvoice/app/src/android/trustedvoice/app/TrustedVoiceActivity.java
index 95c446a..32cc42c 100644
--- a/hostsidetests/trustedvoice/app/src/android/trustedvoice/app/TrustedVoiceActivity.java
+++ b/hostsidetests/trustedvoice/app/src/android/trustedvoice/app/TrustedVoiceActivity.java
@@ -17,12 +17,12 @@
 package android.trustedvoice.app;
 
 import android.app.Activity;
+import android.app.KeyguardManager;
+import android.app.KeyguardManager.KeyguardDismissCallback;
+import android.content.Context;
 import android.os.Bundle;
 import android.util.Log;
-import android.app.KeyguardManager;
-import android.content.Context;
 import android.view.WindowManager.LayoutParams;
-import java.lang.Override;
 
 /**
  * This activity when in foreground sets the FLAG_DISMISS_KEYGUARD.
@@ -37,14 +37,18 @@
    */
   private static final String TEST_STRING = "TrustedVoiceTestString";
 
+  private KeyguardManager mkeyguardManager;
+
   @Override
   public void onCreate(Bundle icicle) {
     super.onCreate(icicle);
+    mkeyguardManager =
+            (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
 
     // Unlock the keyguard.
-    getWindow().addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD
-            | LayoutParams.FLAG_TURN_SCREEN_ON
+    getWindow().addFlags(LayoutParams.FLAG_TURN_SCREEN_ON
             | LayoutParams.FLAG_KEEP_SCREEN_ON);
+    mkeyguardManager.requestDismissKeyguard(this, null);
   }
 
   @Override
@@ -52,8 +56,7 @@
     super.onWindowFocusChanged(hasFocus);
     if (hasFocus) {
       // Confirm that the keyguard was successfully unlocked.
-      KeyguardManager kM = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
-      if (!kM.isKeyguardLocked()) {
+      if (!mkeyguardManager.isKeyguardLocked()) {
         // Log the test string.
         Log.i(TAG, TEST_STRING);
       }
diff --git a/hostsidetests/trustedvoice/src/android/trustedvoice/cts/TrustedVoiceHostTest.java b/hostsidetests/trustedvoice/src/android/trustedvoice/cts/TrustedVoiceHostTest.java
index 7cea5e5..9420124 100644
--- a/hostsidetests/trustedvoice/src/android/trustedvoice/cts/TrustedVoiceHostTest.java
+++ b/hostsidetests/trustedvoice/src/android/trustedvoice/cts/TrustedVoiceHostTest.java
@@ -97,6 +97,8 @@
             getDevice().executeAdbCommand("logcat", "-c");
             // Lock the device
             getDevice().executeShellCommand(SLEEP_COMMAND);
+            // Add a delay to allow the device to go to sleep.
+            Thread.sleep(1000);
             // Start the APK and wait for it to complete.
             getDevice().executeShellCommand(START_COMMAND);
             // Adding delay for OEM specific features which could delay the time of printing the