Play warning sound when launching emergency SOS
action.  Also add a temporary cancle button to
allow users to stop the countdown and close the
view before starting an emergency call.

Bug: 169946301
Test: Tested on device (pixel4)
Change-Id: I68031cc53d3f4108b60d27b9aa5b88f3007f0e51
diff --git a/res/drawable/bg_cancel_btn.xml b/res/drawable/bg_cancel_btn.xml
new file mode 100644
index 0000000..09d98be
--- /dev/null
+++ b/res/drawable/bg_cancel_btn.xml
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ 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.
+  -->
+
+<shape xmlns:android="http://schemas.android.com/apk/res/android"
+       android:shape="rectangle">
+    <stroke android:color="@color/emergency_action_accent" android:width="1dp" />
+    <corners android:radius="48dp" />
+</shape>
diff --git a/res/layout/emergency_action_fragment.xml b/res/layout/emergency_action_fragment.xml
index 8c1e0e0..c21f771 100644
--- a/res/layout/emergency_action_fragment.xml
+++ b/res/layout/emergency_action_fragment.xml
@@ -30,7 +30,7 @@
         android:fontFamily="sans-serif"
         android:textSize="24sp"
         android:lineHeight="32dp"
-        android:textColor="@color/emergency_action_title_color"
+        android:textColor="@color/emergency_action_accent"
         android:textAlignment="center"/>
 
     <TextView
@@ -58,4 +58,19 @@
             android:layout_gravity="center"/>
     </FrameLayout>
 
+    <Button
+        android:id="@+id/btn_cancel"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_gravity="center"
+        android:layout_marginHorizontal="48dp"
+        android:padding="32dp"
+        android:text="@string/cancel"
+        android:textAllCaps="false"
+        android:fontFamily="sans-serif"
+        android:textSize="20sp"
+        android:lineHeight="28dp"
+        android:textColor="@color/emergency_action_subtitle_color"
+        android:background="@drawable/bg_cancel_btn"/>
+
 </LinearLayout>
\ No newline at end of file
diff --git a/res/raw/alarm.ogg b/res/raw/alarm.ogg
new file mode 100644
index 0000000..f9b71ed
--- /dev/null
+++ b/res/raw/alarm.ogg
Binary files differ
diff --git a/res/values/colors.xml b/res/values/colors.xml
index db21037..4ade06b 100644
--- a/res/values/colors.xml
+++ b/res/values/colors.xml
@@ -28,7 +28,7 @@
     <color name="accent_color_light">@color/accent_material_light</color>
     <color name="accent_color_dark">@color/accent_material_dark</color>
 
-    <color name="emergency_action_title_color">#F28B82</color>
+    <color name="emergency_action_accent">#F28B82</color>
     <color name="emergency_action_subtitle_color">#FFFFFF</color>
 
 </resources>
diff --git a/src/com/android/emergency/action/EmergencyActionFragment.java b/src/com/android/emergency/action/EmergencyActionFragment.java
index 0968717..ae55ccd 100644
--- a/src/com/android/emergency/action/EmergencyActionFragment.java
+++ b/src/com/android/emergency/action/EmergencyActionFragment.java
@@ -20,11 +20,16 @@
 import static android.telecom.TelecomManager.EXTRA_CALL_SOURCE;
 import static android.telephony.emergency.EmergencyNumber.EMERGENCY_SERVICE_CATEGORY_POLICE;
 
+import android.app.KeyguardManager;
 import android.content.Context;
 import android.content.pm.PackageManager;
+import android.media.AudioAttributes;
+import android.media.MediaPlayer;
 import android.net.Uri;
 import android.os.Bundle;
 import android.os.CountDownTimer;
+import android.os.UserHandle;
+import android.provider.Settings;
 import android.support.v4.app.Fragment;
 import android.telecom.PhoneAccount;
 import android.telecom.TelecomManager;
@@ -35,6 +40,7 @@
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.Button;
 import android.widget.TextView;
 
 import androidx.annotation.NonNull;
@@ -52,9 +58,12 @@
     private static final String TAG = "EmergencyActionFrag";
     private static final String STATE_MILLIS_LEFT = "STATE_MILLIS_LEFT";
 
+    private MediaPlayer mMediaPlayer;
+
     private TelephonyManager mTelephonyManager;
     private TelecomManager mTelecomManager;
     private SubscriptionManager mSubscriptionManager;
+    private KeyguardManager mKeyguardManager;
     private CountDownTimer mCountDownTimer;
     private long mCountDownMillisLeft;
 
@@ -65,6 +74,7 @@
         mTelephonyManager = context.getSystemService(TelephonyManager.class);
         mSubscriptionManager = context.getSystemService(SubscriptionManager.class);
         mTelecomManager = context.getSystemService(TelecomManager.class);
+        mKeyguardManager = context.getSystemService(KeyguardManager.class);
     }
 
     @Override
@@ -75,6 +85,22 @@
         TextView subtitleView = view.findViewById(R.id.subtitle);
         subtitleView.setText(getString(R.string.emergency_action_subtitle, getEmergencyNumber()));
 
+        Button cancelButton = view.findViewById(R.id.btn_cancel);
+        cancelButton.setOnClickListener(
+                v -> {
+                    if (mKeyguardManager.isKeyguardLocked()) {
+                        mKeyguardManager.requestDismissKeyguard(
+                                getActivity(), new KeyguardManager.KeyguardDismissCallback() {
+                                    @Override
+                                    public void onDismissSucceeded() {
+                                        getActivity().finish();
+                                    }
+                                });
+                    } else {
+                        getActivity().finish();
+                    }
+                });
+
         if (savedInstanceState != null) {
             mCountDownMillisLeft = savedInstanceState.getLong(STATE_MILLIS_LEFT);
         } else {
@@ -89,6 +115,7 @@
     public void onStart() {
         super.onStart();
         startTimer();
+        playWarningSound();
     }
 
     @Override
@@ -107,6 +134,8 @@
             countDownAnimationView.stop();
             mCountDownTimer.cancel();
         }
+
+        stopWarningSound();
     }
 
     private String getEmergencyNumber() {
@@ -171,6 +200,46 @@
         countDownAnimationView.showCountDown();
     }
 
+    private boolean isPlayWarningSoundEnabled() {
+        return Settings.Secure.getIntForUser(getContext().getContentResolver(),
+                Settings.Secure.EMERGENCY_GESTURE_SOUND_ENABLED, 0, UserHandle.USER_CURRENT) != 0;
+    }
+
+    private void playWarningSound() {
+        if (!isPlayWarningSoundEnabled()) {
+            return;
+        }
+
+        if (mMediaPlayer == null) {
+            mMediaPlayer = MediaPlayer.create(
+                    getContext(),
+                    R.raw.alarm,
+                    new AudioAttributes.Builder()
+                            .setUsage(AudioAttributes.USAGE_ALARM)
+                            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
+                            .build(),
+                    /* audioSessionId= */ 0);
+        }
+
+        mMediaPlayer.setOnCompletionListener(mp -> mp.release());
+        mMediaPlayer.setOnErrorListener(
+                (MediaPlayer mp, int what, int extra) -> {
+                    Log.w(TAG, "MediaPlayer playback failed with error code: " + what
+                            + ", and extra code: " + extra);
+                    mp.release();
+                    return false;
+                });
+
+        mMediaPlayer.start();
+    }
+
+    private void stopWarningSound() {
+        if (mMediaPlayer.isPlaying()) {
+            mMediaPlayer.stop();
+            mMediaPlayer.release();
+        }
+    }
+
     private void startEmergencyCall() {
         Bundle extras = new Bundle();
         extras.putBoolean(TelecomManager.EXTRA_IS_USER_INTENT_EMERGENCY_CALL, true);