1 /* 2 * Copyright (C) 2016 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.internal.policy; 18 19 import android.content.Context; 20 import android.content.Intent; 21 import android.net.Uri; 22 import android.os.Build; 23 import android.os.UserHandle; 24 import android.provider.Settings; 25 26 /** 27 * A class that manages emergency affordances and enables immediate calling to emergency services 28 */ 29 public class EmergencyAffordanceManager { 30 31 public static final boolean ENABLED = true; 32 33 /** 34 * Global setting override with the number to call with the emergency affordance. 35 * @hide 36 */ 37 private static final String EMERGENCY_CALL_NUMBER_SETTING = "emergency_affordance_number"; 38 39 /** 40 * Global setting, whether the emergency affordance should be shown regardless of device state. 41 * The value is a boolean (1 or 0). 42 * @hide 43 */ 44 private static final String FORCE_EMERGENCY_AFFORDANCE_SETTING = "force_emergency_affordance"; 45 46 private final Context mContext; 47 EmergencyAffordanceManager(Context context)48 public EmergencyAffordanceManager(Context context) { 49 mContext = context; 50 } 51 52 /** 53 * perform an emergency call. 54 */ performEmergencyCall()55 public final void performEmergencyCall() { 56 performEmergencyCall(mContext); 57 } 58 getPhoneUri(Context context)59 private static Uri getPhoneUri(Context context) { 60 String number = context.getResources().getString( 61 com.android.internal.R.string.config_emergency_call_number); 62 if (Build.IS_DEBUGGABLE) { 63 String override = Settings.Global.getString( 64 context.getContentResolver(), EMERGENCY_CALL_NUMBER_SETTING); 65 if (override != null) { 66 number = override; 67 } 68 } 69 return Uri.fromParts("tel", number, null); 70 } 71 performEmergencyCall(Context context)72 private static void performEmergencyCall(Context context) { 73 Intent intent = new Intent(Intent.ACTION_CALL_EMERGENCY); 74 intent.setData(getPhoneUri(context)); 75 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 76 context.startActivityAsUser(intent, UserHandle.CURRENT); 77 } 78 79 /** 80 * @return whether emergency affordance should be active. 81 */ needsEmergencyAffordance()82 public boolean needsEmergencyAffordance() { 83 if (!ENABLED) { 84 return false; 85 } 86 if (forceShowing()) { 87 return true; 88 } 89 return isEmergencyAffordanceNeeded(); 90 } 91 isEmergencyAffordanceNeeded()92 private boolean isEmergencyAffordanceNeeded() { 93 return Settings.Global.getInt(mContext.getContentResolver(), 94 Settings.Global.EMERGENCY_AFFORDANCE_NEEDED, 0) != 0; 95 } 96 97 forceShowing()98 private boolean forceShowing() { 99 return Settings.Global.getInt(mContext.getContentResolver(), 100 FORCE_EMERGENCY_AFFORDANCE_SETTING, 0) != 0; 101 } 102 } 103