summaryrefslogtreecommitdiff
path: root/EmergencyGestureAction/src/com/android/emergency/action/sensoryfeedback/EmergencyActionAlarmHelper.java
blob: b2d6ff61ea4ab5b65dbe0c02b66b0126a098ad10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * 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.emergency.action.sensoryfeedback;

import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.provider.Settings;
import android.util.Log;

import com.android.emergency.R;


public class EmergencyActionAlarmHelper {
    private static final String TAG = "AlarmSoundManager";
    private static final int USER_SET_ALARM_VOLUME_UNKNOWN = -1;
    private final Context mContext;

    private MediaPlayer mMediaPlayer;
    private AudioManager mAudioManager;
    private int mUserSetAlarmVolume;
    private boolean mResetAlarmVolumeNeeded;

    public EmergencyActionAlarmHelper(Context context) {
        this(context, USER_SET_ALARM_VOLUME_UNKNOWN);
    }

    public int getUserSetAlarmVolume() {
        return mUserSetAlarmVolume;
    }

    public EmergencyActionAlarmHelper(Context context, int userSetAlarmVolume) {
        mContext = context;
        mAudioManager = context.getSystemService(AudioManager.class);
        mUserSetAlarmVolume = userSetAlarmVolume;
    }

    public void playWarningSound() {
        if (!isPlayWarningSoundEnabled()) {
            return;
        }

        if (mMediaPlayer == null) {
            mMediaPlayer = MediaPlayer.create(
                    mContext,
                    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;
                });

        setAlarmVolumeToFull();
        mMediaPlayer.start();
    }

    public void stopWarningSound() {
        if (mMediaPlayer != null) {
            try {
                mMediaPlayer.stop();
                mMediaPlayer.release();
            } catch (IllegalStateException e) {
                Log.w(TAG, "Exception when trying to stop media player");
            }
            mMediaPlayer = null;
        }

        resetAlarmVolume();
    }

    private void setAlarmVolumeToFull() {
        int streamType = AudioManager.STREAM_ALARM;
        if (mUserSetAlarmVolume == USER_SET_ALARM_VOLUME_UNKNOWN) {
            mUserSetAlarmVolume = mAudioManager.getStreamVolume(streamType);
        }
        mResetAlarmVolumeNeeded = true;

        Log.d(TAG, "Setting alarm volume from " + mUserSetAlarmVolume + "to full");
        mAudioManager.setStreamVolume(streamType,
                mAudioManager.getStreamMaxVolume(streamType), 0);
    }

    private void resetAlarmVolume() {
        if (mResetAlarmVolumeNeeded) {
            Log.d(TAG, "Resetting alarm volume to back to " + mUserSetAlarmVolume);
            mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM, mUserSetAlarmVolume, 0);
            mResetAlarmVolumeNeeded = false;
        }
    }

    private boolean isPlayWarningSoundEnabled() {
        return Settings.Secure.getInt(mContext.getContentResolver(),
                Settings.Secure.EMERGENCY_GESTURE_SOUND_ENABLED, 0) != 0;
    }
}