summaryrefslogtreecommitdiff
path: root/location/java/com
diff options
context:
space:
mode:
author Yu-Han Yang <yuhany@google.com> 2021-09-22 23:03:20 -0700
committer Yu-Han Yang <yuhany@google.com> 2021-11-03 13:40:09 -0700
commit1096bce6b82fc26903f8a6771cc15b164558ae1f (patch)
treebeff74067c985cd4c0cb46ec3c255684689a461d /location/java/com
parentc5dcc019ad063335230a536ebafbb103fc3180fa (diff)
Use TelephonyCallback to replace the deprecated broadcast
Bug: 196115028 Test: manual Change-Id: I53e8053d7f03e3263986d4e8ce03ebfbcfd79316
Diffstat (limited to 'location/java/com')
-rw-r--r--location/java/com/android/internal/location/GpsNetInitiatedHandler.java86
1 files changed, 44 insertions, 42 deletions
diff --git a/location/java/com/android/internal/location/GpsNetInitiatedHandler.java b/location/java/com/android/internal/location/GpsNetInitiatedHandler.java
index 4ce9a320a159..190e1cc083bd 100644
--- a/location/java/com/android/internal/location/GpsNetInitiatedHandler.java
+++ b/location/java/com/android/internal/location/GpsNetInitiatedHandler.java
@@ -16,6 +16,8 @@
package com.android.internal.location;
+import android.Manifest;
+import android.annotation.RequiresPermission;
import android.app.Notification;
import android.app.NotificationManager;
import android.compat.annotation.UnsupportedAppUsage;
@@ -28,8 +30,9 @@ import android.location.LocationManager;
import android.os.RemoteException;
import android.os.SystemClock;
import android.os.UserHandle;
-import android.telephony.PhoneStateListener;
+import android.telephony.TelephonyCallback;
import android.telephony.TelephonyManager;
+import android.telephony.emergency.EmergencyNumber;
import android.util.Log;
import com.android.internal.R;
@@ -90,7 +93,6 @@ public class GpsNetInitiatedHandler {
private final Context mContext;
private final TelephonyManager mTelephonyManager;
- private final PhoneStateListener mPhoneStateListener;
// parent gps location provider
private final LocationManager mLocationManager;
@@ -138,33 +140,37 @@ public class GpsNetInitiatedHandler {
public int requestorIdEncoding;
@UnsupportedAppUsage
public int textEncoding;
- };
-
- public static class GpsNiResponse {
- /* User response, one of the values in GpsUserResponseType */
- int userResponse;
- };
-
- private final BroadcastReceiver mBroadcastReciever = new BroadcastReceiver() {
-
- @Override public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- if (action.equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
- String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
- /*
- Tracks the emergency call:
- mIsInEmergencyCall records if the phone is in emergency call or not. It will
- be set to true when the phone is having emergency call, and then will
- be set to false by mPhoneStateListener when the emergency call ends.
- */
- mIsInEmergencyCall = mTelephonyManager.isEmergencyNumber(phoneNumber);
- if (DEBUG) Log.v(TAG, "ACTION_NEW_OUTGOING_CALL - " + getInEmergency());
- } else if (action.equals(LocationManager.MODE_CHANGED_ACTION)) {
- updateLocationMode();
- if (DEBUG) Log.d(TAG, "location enabled :" + getLocationEnabled());
+ }
+
+ private class EmergencyCallListener extends TelephonyCallback implements
+ TelephonyCallback.OutgoingEmergencyCallListener,
+ TelephonyCallback.CallStateListener {
+
+ @Override
+ @RequiresPermission(Manifest.permission.READ_ACTIVE_EMERGENCY_SESSION)
+ public void onOutgoingEmergencyCall(EmergencyNumber placedEmergencyNumber,
+ int subscriptionId) {
+ mIsInEmergencyCall = true;
+ if (DEBUG) Log.d(TAG, "onOutgoingEmergencyCall(): inEmergency = " + getInEmergency());
+ }
+
+ @Override
+ @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
+ public void onCallStateChanged(int state) {
+ if (DEBUG) Log.d(TAG, "onCallStateChanged(): state is " + state);
+ // listening for emergency call ends
+ if (state == TelephonyManager.CALL_STATE_IDLE) {
+ if (mIsInEmergencyCall) {
+ mCallEndElapsedRealtimeMillis = SystemClock.elapsedRealtime();
+ mIsInEmergencyCall = false;
+ }
}
}
- };
+ }
+
+ // The internal implementation of TelephonyManager uses WeakReference so we have to keep a
+ // reference here.
+ private final EmergencyCallListener mEmergencyCallListener = new EmergencyCallListener();
/**
* The notification that is shown when a network-initiated notification
@@ -190,26 +196,22 @@ public class GpsNetInitiatedHandler {
updateLocationMode();
mTelephonyManager =
(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
+ mTelephonyManager.registerTelephonyCallback(mContext.getMainExecutor(),
+ mEmergencyCallListener);
+
+ BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
- mPhoneStateListener = new PhoneStateListener() {
@Override
- public void onCallStateChanged(int state, String incomingNumber) {
- if (DEBUG) Log.d(TAG, "onCallStateChanged(): state is "+ state);
- // listening for emergency call ends
- if (state == TelephonyManager.CALL_STATE_IDLE) {
- if (mIsInEmergencyCall) {
- mCallEndElapsedRealtimeMillis = SystemClock.elapsedRealtime();
- mIsInEmergencyCall = false;
- }
+ public void onReceive(Context context, Intent intent) {
+ String action = intent.getAction();
+ if (action.equals(LocationManager.MODE_CHANGED_ACTION)) {
+ updateLocationMode();
+ if (DEBUG) Log.d(TAG, "location enabled :" + getLocationEnabled());
}
}
};
- mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
-
- IntentFilter intentFilter = new IntentFilter();
- intentFilter.addAction(Intent.ACTION_NEW_OUTGOING_CALL);
- intentFilter.addAction(LocationManager.MODE_CHANGED_ACTION);
- mContext.registerReceiver(mBroadcastReciever, intentFilter);
+ mContext.registerReceiver(broadcastReceiver,
+ new IntentFilter(LocationManager.MODE_CHANGED_ACTION));
}
public void setSuplEsEnabled(boolean isEnabled) {