summaryrefslogtreecommitdiff
path: root/src/com/android/emergency/action/EmergencyActionFragment.java
blob: e7b47584fe9bef8298889e3afcca9e8904eec0f5 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 * 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;

import static android.telecom.TelecomManager.EXTRA_CALL_SOURCE;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.media.AudioAttributes;
import android.media.AudioManager;
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;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.android.emergency.R;
import com.android.emergency.widgets.countdown.CountDownAnimationView;
import com.android.emergency.widgets.slider.OnSlideCompleteListener;
import com.android.emergency.widgets.slider.SliderView;
import com.android.settingslib.emergencynumber.EmergencyNumberUtils;

import java.time.Duration;

public class EmergencyActionFragment extends Fragment implements OnSlideCompleteListener {

    private static final String TAG = "EmergencyActionFrag";
    private static final String STATE_MILLIS_LEFT = "STATE_MILLIS_LEFT";

    private MediaPlayer mMediaPlayer;
    private AudioManager mAudioManager;
    private TelecomManager mTelecomManager;
    private CountDownTimer mCountDownTimer;
    private EmergencyNumberUtils mEmergencyNumberUtils;
    private long mCountDownMillisLeft;
    private int mUserSetAlarmVolume;
    private boolean mResetAlarmVolumeNeeded;
    private boolean mCountdownCancelled;
    private boolean mCountdownFinished;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mAudioManager = context.getSystemService(AudioManager.class);
        mEmergencyNumberUtils = new EmergencyNumberUtils(context);
        mTelecomManager = context.getSystemService(TelecomManager.class);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        // Ignore the larger font scale if users set it in general system settings since we already
        // have relatively large font size on this page, and we need to display all content on one
        // page without scrolling.
        Configuration configuration = getResources().getConfiguration();
        if (configuration.fontScale > 1) {
            configuration.fontScale = (float) 1;

            DisplayMetrics metrics = new DisplayMetrics();
            metrics.scaledDensity = configuration.fontScale * metrics.density;
            configuration.densityDpi = (int) getResources().getDisplayMetrics().xdpi;
        }

        View view = inflater.cloneInContext(getContext().createConfigurationContext(configuration))
                .inflate(R.layout.emergency_action_fragment, container, false);

        TextView subtitleView = view.findViewById(R.id.subtitle);
        subtitleView.setText(getString(R.string.emergency_action_subtitle,
                mEmergencyNumberUtils.getPoliceNumber()));

        SliderView cancelButton = view.findViewById(R.id.btn_cancel);
        cancelButton.setSlideCompleteListener(this);

        if (savedInstanceState != null) {
            mCountDownMillisLeft = savedInstanceState.getLong(STATE_MILLIS_LEFT);
        } else {
            Activity activity = getActivity();
            Intent intent = null;
            if (activity != null) {
                intent = activity.getIntent();
            }
            if (intent != null) {
                mCountDownMillisLeft = intent.getLongExtra(STATE_MILLIS_LEFT,
                        getResources().getInteger(R.integer.emergency_action_count_down_millis));
            } else {
                mCountDownMillisLeft =
                        getResources().getInteger(R.integer.emergency_action_count_down_millis);
            }
        }

        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        startTimer();
        playWarningSound();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putLong(STATE_MILLIS_LEFT, mCountDownMillisLeft);
    }

    @Override
    public void onStop() {
        super.onStop();

        if (mCountDownTimer != null) {
            CountDownAnimationView countDownAnimationView =
                    getView().findViewById(R.id.count_down_view);
            countDownAnimationView.stop();
            mCountDownTimer.cancel();
        }

        stopWarningSound();
        if (!mCountdownCancelled && !mCountdownFinished) {
            Log.d(TAG,
                    "Emergency countdown UI dismissed without being cancelled/finished, "
                            + "continuing countdown in background");
            // TODO(b/172075832): Continue countdown in a foreground service.
        }
    }

    @Override
    public void onSlideComplete() {
        mCountdownCancelled = true;
        getActivity().finish();
    }

    private void startTimer() {
        CountDownAnimationView countDownAnimationView =
                getView().findViewById(R.id.count_down_view);

        if (mCountDownTimer != null) {
            countDownAnimationView.stop();
            mCountDownTimer.cancel();
        }

        mCountDownTimer =
                new CountDownTimer(
                        mCountDownMillisLeft,
                        getResources().getInteger(R.integer.emergency_action_count_down_interval)) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        CountDownAnimationView countDownAnimationView =
                                getView().findViewById(R.id.count_down_view);
                        if (countDownAnimationView != null) {
                            countDownAnimationView.setCountDownLeft(
                                    Duration.ofMillis(millisUntilFinished));
                        }

                        mCountDownMillisLeft = millisUntilFinished;
                    }

                    @Override
                    public void onFinish() {
                        mCountdownFinished = true;
                        startEmergencyCall();
                        getActivity().finish();
                    }
                };

        mCountDownTimer.start();

        countDownAnimationView.start(Duration.ofMillis(mCountDownMillisLeft));
        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;
                });

        setAlarmVolumeToFull();
        mMediaPlayer.start();
    }

    private 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;
        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 void startEmergencyCall() {
        Bundle extras = new Bundle();
        extras.putBoolean(TelecomManager.EXTRA_IS_USER_INTENT_EMERGENCY_CALL, true);
        extras.putInt(EXTRA_CALL_SOURCE, TelecomManager.CALL_SOURCE_EMERGENCY_SHORTCUT);

        mTelecomManager.placeCall(
                Uri.fromParts(PhoneAccount.SCHEME_TEL, mEmergencyNumberUtils.getPoliceNumber(),
                        null), extras);
    }
}