• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.emergency.action.sensoryfeedback;
18 
19 import android.content.Context;
20 import android.media.AudioAttributes;
21 import android.media.AudioManager;
22 import android.media.MediaPlayer;
23 import android.provider.Settings;
24 import android.util.Log;
25 
26 import com.android.emergency.R;
27 
28 
29 public class EmergencyActionAlarmHelper {
30     private static final String TAG = "AlarmSoundManager";
31     private static final int USER_SET_ALARM_VOLUME_UNKNOWN = -1;
32     private final Context mContext;
33 
34     private MediaPlayer mMediaPlayer;
35     private AudioManager mAudioManager;
36     private int mUserSetAlarmVolume;
37     private boolean mResetAlarmVolumeNeeded;
38 
EmergencyActionAlarmHelper(Context context)39     public EmergencyActionAlarmHelper(Context context) {
40         this(context, USER_SET_ALARM_VOLUME_UNKNOWN);
41     }
42 
getUserSetAlarmVolume()43     public int getUserSetAlarmVolume() {
44         return mUserSetAlarmVolume;
45     }
46 
EmergencyActionAlarmHelper(Context context, int userSetAlarmVolume)47     public EmergencyActionAlarmHelper(Context context, int userSetAlarmVolume) {
48         mContext = context;
49         mAudioManager = context.getSystemService(AudioManager.class);
50         mUserSetAlarmVolume = userSetAlarmVolume;
51     }
52 
playWarningSound()53     public void playWarningSound() {
54         if (!isPlayWarningSoundEnabled()) {
55             return;
56         }
57 
58         if (mMediaPlayer == null) {
59             mMediaPlayer = MediaPlayer.create(
60                     mContext,
61                     R.raw.alarm,
62                     new AudioAttributes.Builder()
63                             .setUsage(AudioAttributes.USAGE_ALARM)
64                             .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
65                             .build(),
66                     /* audioSessionId= */ 0);
67         }
68 
69         mMediaPlayer.setOnCompletionListener(mp -> mp.release());
70         mMediaPlayer.setOnErrorListener(
71                 (MediaPlayer mp, int what, int extra) -> {
72                     Log.w(TAG, "MediaPlayer playback failed with error code: " + what
73                             + ", and extra code: " + extra);
74                     mp.release();
75                     return false;
76                 });
77 
78         setAlarmVolumeToFull();
79         mMediaPlayer.start();
80     }
81 
stopWarningSound()82     public void stopWarningSound() {
83         if (mMediaPlayer != null) {
84             try {
85                 mMediaPlayer.stop();
86                 mMediaPlayer.release();
87             } catch (IllegalStateException e) {
88                 Log.w(TAG, "Exception when trying to stop media player");
89             }
90             mMediaPlayer = null;
91         }
92 
93         resetAlarmVolume();
94     }
95 
setAlarmVolumeToFull()96     private void setAlarmVolumeToFull() {
97         int streamType = AudioManager.STREAM_ALARM;
98         if (mUserSetAlarmVolume == USER_SET_ALARM_VOLUME_UNKNOWN) {
99             mUserSetAlarmVolume = mAudioManager.getStreamVolume(streamType);
100         }
101         mResetAlarmVolumeNeeded = true;
102 
103         Log.d(TAG, "Setting alarm volume from " + mUserSetAlarmVolume + "to full");
104         mAudioManager.setStreamVolume(streamType,
105                 mAudioManager.getStreamMaxVolume(streamType), 0);
106     }
107 
resetAlarmVolume()108     private void resetAlarmVolume() {
109         if (mResetAlarmVolumeNeeded) {
110             Log.d(TAG, "Resetting alarm volume to back to " + mUserSetAlarmVolume);
111             mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM, mUserSetAlarmVolume, 0);
112             mResetAlarmVolumeNeeded = false;
113         }
114     }
115 
isPlayWarningSoundEnabled()116     private boolean isPlayWarningSoundEnabled() {
117         return Settings.Secure.getInt(mContext.getContentResolver(),
118                 Settings.Secure.EMERGENCY_GESTURE_SOUND_ENABLED, 0) != 0;
119     }
120 }
121