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.emergency;
18 
19 
20 import static com.android.settingslib.emergencynumber.EmergencyNumberUtils.EMERGENCY_GESTURE_CALL_NUMBER;
21 import static com.android.settingslib.emergencynumber.EmergencyNumberUtils.EMERGENCY_GESTURE_UI_SHOWING_VALUE;
22 import static com.android.settingslib.emergencynumber.EmergencyNumberUtils.EMERGENCY_NUMBER_OVERRIDE_AUTHORITY;
23 import static com.android.settingslib.emergencynumber.EmergencyNumberUtils.EMERGENCY_SETTING_OFF;
24 import static com.android.settingslib.emergencynumber.EmergencyNumberUtils.EMERGENCY_SETTING_ON;
25 import static com.android.settingslib.emergencynumber.EmergencyNumberUtils.EMERGENCY_SETTING_VALUE;
26 import static com.android.settingslib.emergencynumber.EmergencyNumberUtils.METHOD_NAME_GET_EMERGENCY_GESTURE_ENABLED;
27 import static com.android.settingslib.emergencynumber.EmergencyNumberUtils.METHOD_NAME_GET_EMERGENCY_GESTURE_SOUND_ENABLED;
28 import static com.android.settingslib.emergencynumber.EmergencyNumberUtils.METHOD_NAME_GET_EMERGENCY_NUMBER_OVERRIDE;
29 import static com.android.settingslib.emergencynumber.EmergencyNumberUtils.METHOD_NAME_SET_EMERGENCY_GESTURE;
30 import static com.android.settingslib.emergencynumber.EmergencyNumberUtils.METHOD_NAME_SET_EMERGENCY_GESTURE_UI_SHOWING;
31 import static com.android.settingslib.emergencynumber.EmergencyNumberUtils.METHOD_NAME_SET_EMERGENCY_NUMBER_OVERRIDE;
32 import static com.android.settingslib.emergencynumber.EmergencyNumberUtils.METHOD_NAME_SET_EMERGENCY_SOUND;
33 
34 import android.content.ContentProvider;
35 import android.content.ContentValues;
36 import android.content.Context;
37 import android.content.SharedPreferences;
38 import android.database.Cursor;
39 import android.net.Uri;
40 import android.os.Binder;
41 import android.os.Bundle;
42 import android.os.SystemClock;
43 import android.provider.Settings;
44 import android.util.Log;
45 
46 /**
47  * Content provider that gets/sets emergency number override for emergency gesture.
48  */
49 public class EmergencyGestureContentProvider extends ContentProvider {
50     private static final String TAG = "EmergencyNumberContentP";
51     private static final boolean DEBUG = true;
52     private static final String SHARED_PREFERENCES_NAME =
53             "local_emergency_number_override_shared_pref";
54 
55     @Override
call(String authority, String method, String arg, Bundle extras)56     public Bundle call(String authority, String method, String arg, Bundle extras) {
57         Log.d(TAG, "calling pid/uid" + Binder.getCallingPid() + "/" + Binder.getCallingUid());
58         final Bundle bundle = new Bundle();
59         final SharedPreferences preferences = getContext().getSharedPreferences(
60                 SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
61         switch (method) {
62             case METHOD_NAME_GET_EMERGENCY_NUMBER_OVERRIDE:
63                 if (DEBUG) {
64                     Log.d(TAG, METHOD_NAME_GET_EMERGENCY_NUMBER_OVERRIDE);
65                 }
66                 bundle.putString(EMERGENCY_GESTURE_CALL_NUMBER,
67                         preferences.getString(EMERGENCY_GESTURE_CALL_NUMBER, null));
68                 break;
69             case METHOD_NAME_SET_EMERGENCY_NUMBER_OVERRIDE:
70                 if (DEBUG) {
71                     Log.d(TAG, METHOD_NAME_SET_EMERGENCY_NUMBER_OVERRIDE);
72                 }
73                 final String inputNumber = extras.getString(EMERGENCY_GESTURE_CALL_NUMBER);
74                 preferences.edit().putString(EMERGENCY_GESTURE_CALL_NUMBER, inputNumber).apply();
75                 getContext().getContentResolver().notifyChange(EMERGENCY_NUMBER_OVERRIDE_AUTHORITY,
76                         null);
77                 break;
78             case METHOD_NAME_SET_EMERGENCY_GESTURE:
79                 if (DEBUG) {
80                     Log.d(TAG, METHOD_NAME_SET_EMERGENCY_GESTURE);
81                 }
82                 final int gestureSettingValue = extras.getInt(EMERGENCY_SETTING_VALUE);
83                 Settings.Secure.putInt(getContext().getContentResolver(),
84                         Settings.Secure.EMERGENCY_GESTURE_ENABLED, gestureSettingValue);
85                 break;
86             case METHOD_NAME_SET_EMERGENCY_GESTURE_UI_SHOWING:
87                 if (DEBUG) {
88                     Log.d(TAG, METHOD_NAME_SET_EMERGENCY_GESTURE_UI_SHOWING);
89                 }
90                 long now = SystemClock.elapsedRealtime();
91                 final int gestureUiShowingValue = extras.getInt(
92                         EMERGENCY_GESTURE_UI_SHOWING_VALUE);
93                 Settings.Secure.putInt(getContext().getContentResolver(),
94                         Settings.Secure.EMERGENCY_GESTURE_UI_SHOWING, gestureUiShowingValue);
95                 if (gestureUiShowingValue != 0) {
96                     Settings.Secure.putLong(getContext().getContentResolver(),
97                             Settings.Secure.EMERGENCY_GESTURE_UI_LAST_STARTED_MILLIS, now);
98                 }
99                 break;
100             case METHOD_NAME_SET_EMERGENCY_SOUND:
101                 if (DEBUG) {
102                     Log.d(TAG, METHOD_NAME_SET_EMERGENCY_SOUND);
103                 }
104                 final int soundSettingValue = extras.getInt(EMERGENCY_SETTING_VALUE);
105                 Settings.Secure.putInt(getContext().getContentResolver(),
106                         Settings.Secure.EMERGENCY_GESTURE_SOUND_ENABLED, soundSettingValue);
107                 break;
108             case METHOD_NAME_GET_EMERGENCY_GESTURE_ENABLED:
109                 if (DEBUG) {
110                     Log.d(TAG, METHOD_NAME_GET_EMERGENCY_GESTURE_ENABLED);
111                 }
112                 bundle.putInt(EMERGENCY_SETTING_VALUE,
113                         Settings.Secure.getInt(getContext().getContentResolver(),
114                                 Settings.Secure.EMERGENCY_GESTURE_ENABLED,
115                                 isDefaultEmergencyGestureEnabled(getContext()) ?
116                                         EMERGENCY_SETTING_ON : EMERGENCY_SETTING_OFF));
117                 break;
118             case METHOD_NAME_GET_EMERGENCY_GESTURE_SOUND_ENABLED:
119                 if (DEBUG) {
120                     Log.d(TAG, METHOD_NAME_GET_EMERGENCY_GESTURE_SOUND_ENABLED);
121                 }
122                 bundle.putInt(EMERGENCY_SETTING_VALUE,
123                         Settings.Secure.getInt(getContext().getContentResolver(),
124                                 Settings.Secure.EMERGENCY_GESTURE_SOUND_ENABLED,
125                                 isDefaultEmergencyGestureSoundEnabled(getContext()) ?
126                                         EMERGENCY_SETTING_ON : EMERGENCY_SETTING_OFF));
127                 break;
128         }
129         return bundle;
130     }
131 
132     @Override
onCreate()133     public boolean onCreate() {
134         return true;
135     }
136 
137     @Override
query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)138     public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
139             String sortOrder) {
140         throw new UnsupportedOperationException();
141     }
142 
143     @Override
getType(Uri uri)144     public String getType(Uri uri) {
145         throw new UnsupportedOperationException();
146     }
147 
148     @Override
insert(Uri uri, ContentValues values)149     public Uri insert(Uri uri, ContentValues values) {
150         throw new UnsupportedOperationException();
151     }
152 
153     @Override
delete(Uri uri, String selection, String[] selectionArgs)154     public int delete(Uri uri, String selection, String[] selectionArgs) {
155         throw new UnsupportedOperationException();
156     }
157 
158     @Override
update(Uri uri, ContentValues values, String selection, String[] selectionArgs)159     public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
160         throw new UnsupportedOperationException();
161     }
162 
isDefaultEmergencyGestureEnabled(Context context)163     private boolean isDefaultEmergencyGestureEnabled(Context context) {
164         return context.getResources().getBoolean(
165                 com.android.internal.R.bool.config_defaultEmergencyGestureEnabled);
166     }
167 
isDefaultEmergencyGestureSoundEnabled(Context context)168     private boolean isDefaultEmergencyGestureSoundEnabled(Context context) {
169         return context.getResources().getBoolean(
170                 com.android.internal.R.bool.config_defaultEmergencyGestureSoundEnabled);
171     }
172 }
173