1 /*
2  * Copyright (C) 2021 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.settings.emergency;
18 
19 import static android.telecom.TelecomManager.EXTRA_CALL_SOURCE;
20 
21 import android.content.ContentProvider;
22 import android.content.ContentValues;
23 import android.content.Context;
24 import android.content.pm.PackageManager;
25 import android.database.Cursor;
26 import android.net.Uri;
27 import android.os.Binder;
28 import android.os.Bundle;
29 import android.telecom.PhoneAccount;
30 import android.telecom.TelecomManager;
31 import android.text.TextUtils;
32 import android.util.Log;
33 
34 import com.android.settings.R;
35 import com.android.settingslib.emergencynumber.EmergencyNumberUtils;
36 
37 /**
38  * ContentProvider to delegate emergency action work
39  */
40 public class EmergencyActionContentProvider extends ContentProvider {
41     private static final String TAG = "EmergencyActionContentP";
42 
43     private static final String ACTION_START_EMERGENCY_CALL =
44             "com.android.settings.emergency.MAKE_EMERGENCY_CALL";
45 
46     @Override
call(String authority, String method, String arg, Bundle extras)47     public Bundle call(String authority, String method, String arg, Bundle extras) {
48         int uid = Binder.getCallingUid();
49         Log.d(TAG, "calling pid/uid" + Binder.getCallingPid() + "/" + uid);
50         if (!isEmergencyInfo(getContext())) {
51             throw new SecurityException("Uid is not allowed: " + uid);
52         }
53         if (!TextUtils.equals(method, ACTION_START_EMERGENCY_CALL)) {
54             throw new IllegalArgumentException("Unsupported operation");
55         }
56         placeEmergencyCall(getContext());
57         return new Bundle();
58     }
59 
60     @Override
onCreate()61     public boolean onCreate() {
62         return true;
63     }
64 
65     @Override
query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)66     public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
67             String sortOrder) {
68         return null;
69     }
70 
71     @Override
getType(Uri uri)72     public String getType(Uri uri) {
73         return null;
74     }
75 
76     @Override
insert(Uri uri, ContentValues values)77     public Uri insert(Uri uri, ContentValues values) {
78         return null;
79     }
80 
81     @Override
delete(Uri uri, String selection, String[] selectionArgs)82     public int delete(Uri uri, String selection, String[] selectionArgs) {
83         return 0;
84     }
85 
86     @Override
update(Uri uri, ContentValues values, String selection, String[] selectionArgs)87     public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
88         return 0;
89     }
90 
isEmergencyInfo(Context context)91     private static boolean isEmergencyInfo(Context context) {
92         final int callingUid = Binder.getCallingUid();
93         final String callingPackage = context.getPackageManager().getPackagesForUid(callingUid)[0];
94         return TextUtils.equals(callingPackage,
95                 context.getString(R.string.config_aosp_emergency_package_name));
96     }
97 
placeEmergencyCall(Context context)98     private static void placeEmergencyCall(Context context) {
99         if (!context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
100             Log.i(TAG, "Telephony is not supported, skipping.");
101             return;
102         }
103         Bundle extras = new Bundle();
104         extras.putBoolean(TelecomManager.EXTRA_IS_USER_INTENT_EMERGENCY_CALL, true);
105         extras.putInt(EXTRA_CALL_SOURCE, TelecomManager.CALL_SOURCE_EMERGENCY_SHORTCUT);
106         TelecomManager telecomManager = context.getSystemService(TelecomManager.class);
107         EmergencyNumberUtils emergencyNumberUtils = new EmergencyNumberUtils(context);
108         telecomManager.placeCall(
109                 Uri.fromParts(PhoneAccount.SCHEME_TEL, emergencyNumberUtils.getPoliceNumber(),
110                         /* fragment= */ null), extras);
111     }
112 }
113