mike dooley | 21fb1af | 2016-12-05 14:25:53 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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.server.telecom; |
| 18 | |
| 19 | import android.Manifest; |
| 20 | import android.content.Context; |
| 21 | import android.content.pm.PackageManager; |
| 22 | import android.os.UserHandle; |
| 23 | import android.telecom.Log; |
| 24 | import com.android.internal.annotations.VisibleForTesting; |
| 25 | |
| 26 | /** |
mike dooley | 66f26d1 | 2016-12-19 13:25:47 -0800 | [diff] [blame] | 27 | * Helps with emergency calls by: |
| 28 | * 1. granting temporary location permission to the default dialer service during emergency calls |
| 29 | * 2. keeping track of the time of the last emergency call |
mike dooley | 21fb1af | 2016-12-05 14:25:53 -0800 | [diff] [blame] | 30 | */ |
| 31 | @VisibleForTesting |
mike dooley | 66f26d1 | 2016-12-19 13:25:47 -0800 | [diff] [blame] | 32 | public class EmergencyCallHelper { |
mike dooley | 21fb1af | 2016-12-05 14:25:53 -0800 | [diff] [blame] | 33 | private final Context mContext; |
| 34 | private final String mDefaultDialerPackage; |
| 35 | private final Timeouts.Adapter mTimeoutsAdapter; |
| 36 | private UserHandle mLocationPermissionGrantedToUser; |
mike dooley | 66f26d1 | 2016-12-19 13:25:47 -0800 | [diff] [blame] | 37 | private long mLastEmergencyCallTimestampMillis; |
mike dooley | 21fb1af | 2016-12-05 14:25:53 -0800 | [diff] [blame] | 38 | |
| 39 | @VisibleForTesting |
mike dooley | 66f26d1 | 2016-12-19 13:25:47 -0800 | [diff] [blame] | 40 | public EmergencyCallHelper( |
mike dooley | 21fb1af | 2016-12-05 14:25:53 -0800 | [diff] [blame] | 41 | Context context, |
| 42 | String defaultDialerPackage, |
| 43 | Timeouts.Adapter timeoutsAdapter) { |
| 44 | mContext = context; |
| 45 | mDefaultDialerPackage = defaultDialerPackage; |
| 46 | mTimeoutsAdapter = timeoutsAdapter; |
| 47 | } |
| 48 | |
| 49 | void maybeGrantTemporaryLocationPermission(Call call, UserHandle userHandle) { |
| 50 | if (shouldGrantTemporaryLocationPermission(call)) { |
| 51 | grantLocationPermission(userHandle, call); |
| 52 | } |
mike dooley | 66f26d1 | 2016-12-19 13:25:47 -0800 | [diff] [blame] | 53 | if (call != null && call.isEmergencyCall()) { |
| 54 | recordEmergencyCallTime(); |
| 55 | } |
mike dooley | 21fb1af | 2016-12-05 14:25:53 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void maybeRevokeTemporaryLocationPermission() { |
| 59 | if (wasGrantedTemporaryLocationPermission()) { |
| 60 | revokeLocationPermission(); |
| 61 | } |
| 62 | } |
| 63 | |
mike dooley | 66f26d1 | 2016-12-19 13:25:47 -0800 | [diff] [blame] | 64 | long getLastEmergencyCallTimeMillis() { |
| 65 | return mLastEmergencyCallTimestampMillis; |
| 66 | } |
| 67 | |
| 68 | private void recordEmergencyCallTime() { |
| 69 | mLastEmergencyCallTimestampMillis = System.currentTimeMillis(); |
| 70 | } |
| 71 | |
| 72 | private boolean isInEmergencyCallbackWindow() { |
| 73 | return System.currentTimeMillis() - getLastEmergencyCallTimeMillis() |
| 74 | < mTimeoutsAdapter.getEmergencyCallbackWindowMillis(mContext.getContentResolver()); |
| 75 | } |
| 76 | |
mike dooley | 21fb1af | 2016-12-05 14:25:53 -0800 | [diff] [blame] | 77 | private boolean shouldGrantTemporaryLocationPermission(Call call) { |
mike dooley | ce64190 | 2017-05-09 15:44:40 -0700 | [diff] [blame] | 78 | if (!mContext.getResources().getBoolean(R.bool.grant_location_permission_enabled)) { |
| 79 | Log.i(this, "ShouldGrantTemporaryLocationPermission, disabled by config"); |
| 80 | return false; |
| 81 | } |
mike dooley | 21fb1af | 2016-12-05 14:25:53 -0800 | [diff] [blame] | 82 | if (call == null) { |
| 83 | Log.i(this, "ShouldGrantTemporaryLocationPermission, no call"); |
| 84 | return false; |
| 85 | } |
| 86 | if (!call.isEmergencyCall() && !isInEmergencyCallbackWindow()) { |
| 87 | Log.i(this, "ShouldGrantTemporaryLocationPermission, not emergency"); |
| 88 | return false; |
| 89 | } |
| 90 | if (hasLocationPermission()) { |
| 91 | Log.i(this, "ShouldGrantTemporaryLocationPermission, already has location permission"); |
| 92 | return false; |
| 93 | } |
| 94 | Log.i(this, "ShouldGrantTemporaryLocationPermission, returning true"); |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | private void grantLocationPermission(UserHandle userHandle, Call call) { |
| 99 | Log.i(this, "Granting temporary location permission to " + mDefaultDialerPackage |
| 100 | + ", user: " + userHandle); |
| 101 | try { |
| 102 | mContext.getPackageManager().grantRuntimePermission(mDefaultDialerPackage, |
| 103 | Manifest.permission.ACCESS_FINE_LOCATION, userHandle); |
mike dooley | 66f26d1 | 2016-12-19 13:25:47 -0800 | [diff] [blame] | 104 | recordPermissionGrant(userHandle); |
mike dooley | 21fb1af | 2016-12-05 14:25:53 -0800 | [diff] [blame] | 105 | } catch (Exception e) { |
| 106 | Log.e(this, e, "Failed to grant location permission to " + mDefaultDialerPackage |
| 107 | + ", user: " + userHandle); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | private void revokeLocationPermission() { |
| 112 | Log.i(this, "Revoking temporary location permission from " + mDefaultDialerPackage |
| 113 | + ", user: " + mLocationPermissionGrantedToUser); |
| 114 | UserHandle userHandle = mLocationPermissionGrantedToUser; |
| 115 | clearPermissionGrant(); |
| 116 | try { |
| 117 | mContext.getPackageManager().revokeRuntimePermission(mDefaultDialerPackage, |
| 118 | Manifest.permission.ACCESS_FINE_LOCATION, userHandle); |
| 119 | } catch (Exception e) { |
| 120 | Log.e(this, e, "Failed to revoke location permission from " + mDefaultDialerPackage |
| 121 | + ", user: " + userHandle); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | private boolean hasLocationPermission() { |
| 126 | return mContext.getPackageManager().checkPermission( |
| 127 | Manifest.permission.ACCESS_FINE_LOCATION, mDefaultDialerPackage) |
| 128 | == PackageManager.PERMISSION_GRANTED; |
| 129 | } |
| 130 | |
mike dooley | 66f26d1 | 2016-12-19 13:25:47 -0800 | [diff] [blame] | 131 | private void recordPermissionGrant(UserHandle userHandle) { |
mike dooley | 21fb1af | 2016-12-05 14:25:53 -0800 | [diff] [blame] | 132 | mLocationPermissionGrantedToUser = userHandle; |
mike dooley | 21fb1af | 2016-12-05 14:25:53 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | private boolean wasGrantedTemporaryLocationPermission() { |
| 136 | return mLocationPermissionGrantedToUser != null; |
| 137 | } |
| 138 | |
| 139 | private void clearPermissionGrant() { |
| 140 | mLocationPermissionGrantedToUser = null; |
| 141 | } |
mike dooley | 21fb1af | 2016-12-05 14:25:53 -0800 | [diff] [blame] | 142 | } |