1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 *      http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15package com.android.internal.util;
16
17import android.content.Context;
18import android.content.Intent;
19import android.net.Uri;
20import android.os.Build;
21import android.os.UserHandle;
22import android.provider.Settings;
23
24/**
25 * A class that manages emergency affordances and enables immediate calling to emergency services
26 */
27public class EmergencyAffordanceManager {
28
29    public static final boolean ENABLED = true;
30
31    /**
32     * Global setting override with the number to call with the emergency affordance.
33     * @hide
34     */
35    private static final String EMERGENCY_CALL_NUMBER_SETTING = "emergency_affordance_number";
36
37    /**
38     * Global setting, whether the emergency affordance should be shown regardless of device state.
39     * The value is a boolean (1 or 0).
40     * @hide
41     */
42    private static final String FORCE_EMERGENCY_AFFORDANCE_SETTING = "force_emergency_affordance";
43
44    private final Context mContext;
45
46    public EmergencyAffordanceManager(Context context) {
47        mContext = context;
48    }
49
50    /**
51     * perform an emergency call.
52     */
53    public final void performEmergencyCall() {
54        performEmergencyCall(mContext);
55    }
56
57    private static Uri getPhoneUri(Context context) {
58        String number = context.getResources().getString(
59                com.android.internal.R.string.config_emergency_call_number);
60        if (Build.IS_DEBUGGABLE) {
61            String override = Settings.Global.getString(
62                    context.getContentResolver(), EMERGENCY_CALL_NUMBER_SETTING);
63            if (override != null) {
64                number = override;
65            }
66        }
67        return Uri.fromParts("tel", number, null);
68    }
69
70    private static void performEmergencyCall(Context context) {
71        Intent intent = new Intent(Intent.ACTION_CALL_EMERGENCY);
72        intent.setData(getPhoneUri(context));
73        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
74        context.startActivityAsUser(intent, UserHandle.CURRENT);
75    }
76
77    /**
78     * @return whether emergency affordance should be active.
79     */
80    public boolean needsEmergencyAffordance() {
81        if (!ENABLED) {
82            return false;
83        }
84        if (forceShowing()) {
85            return true;
86        }
87        return isEmergencyAffordanceNeeded();
88    }
89
90    private boolean isEmergencyAffordanceNeeded() {
91        return Settings.Global.getInt(mContext.getContentResolver(),
92                Settings.Global.EMERGENCY_AFFORDANCE_NEEDED, 0) != 0;
93    }
94
95
96    private boolean forceShowing() {
97        return Settings.Global.getInt(mContext.getContentResolver(),
98                FORCE_EMERGENCY_AFFORDANCE_SETTING, 0) != 0;
99    }
100}
101