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