Messaging: Get rid of deprecated PhoneStateListener
TelephonyCallback is the new thing to use Change-Id: I3c44210bfc02814af5d4110f507b94d0f0e8a4a0
This commit is contained in:
@@ -18,10 +18,14 @@
|
|||||||
package com.android.messaging.util;
|
package com.android.messaging.util;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.telephony.PhoneStateListener;
|
import android.os.Handler;
|
||||||
|
import android.os.Looper;
|
||||||
import android.telephony.ServiceState;
|
import android.telephony.ServiceState;
|
||||||
|
import android.telephony.TelephonyCallback;
|
||||||
import android.telephony.TelephonyManager;
|
import android.telephony.TelephonyManager;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ConnectivityUtil listens to the network service state changes.
|
* ConnectivityUtil listens to the network service state changes.
|
||||||
*/
|
*/
|
||||||
@@ -30,6 +34,7 @@ public class ConnectivityUtil {
|
|||||||
private volatile int mCurrentServiceState = ServiceState.STATE_POWER_OFF;
|
private volatile int mCurrentServiceState = ServiceState.STATE_POWER_OFF;
|
||||||
|
|
||||||
private final TelephonyManager mTelephonyManager;
|
private final TelephonyManager mTelephonyManager;
|
||||||
|
private final Handler mHandler;
|
||||||
|
|
||||||
private ConnectivityListener mListener;
|
private ConnectivityListener mListener;
|
||||||
|
|
||||||
@@ -41,23 +46,10 @@ public class ConnectivityUtil {
|
|||||||
mTelephonyManager =
|
mTelephonyManager =
|
||||||
((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE))
|
((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE))
|
||||||
.createForSubscriptionId(subId);
|
.createForSubscriptionId(subId);
|
||||||
|
mHandler = new Handler(Looper.getMainLooper());
|
||||||
}
|
}
|
||||||
|
|
||||||
private final PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
|
private final PhoneStateListener mPhoneStateListener = new PhoneStateListener();
|
||||||
@Override
|
|
||||||
public void onServiceStateChanged(final ServiceState serviceState) {
|
|
||||||
if (mCurrentServiceState != serviceState.getState()) {
|
|
||||||
mCurrentServiceState = serviceState.getState();
|
|
||||||
onPhoneStateChanged(mCurrentServiceState);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDataConnectionStateChanged(final int state) {
|
|
||||||
mCurrentServiceState = (state == TelephonyManager.DATA_DISCONNECTED) ?
|
|
||||||
ServiceState.STATE_OUT_OF_SERVICE : ServiceState.STATE_IN_SERVICE;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
private void onPhoneStateChanged(final int serviceState) {
|
private void onPhoneStateChanged(final int serviceState) {
|
||||||
final ConnectivityListener listener = mListener;
|
final ConnectivityListener listener = mListener;
|
||||||
@@ -72,8 +64,8 @@ public class ConnectivityUtil {
|
|||||||
if (mTelephonyManager != null) {
|
if (mTelephonyManager != null) {
|
||||||
mCurrentServiceState = (PhoneUtils.getDefault().isAirplaneModeOn() ?
|
mCurrentServiceState = (PhoneUtils.getDefault().isAirplaneModeOn() ?
|
||||||
ServiceState.STATE_POWER_OFF : ServiceState.STATE_IN_SERVICE);
|
ServiceState.STATE_POWER_OFF : ServiceState.STATE_IN_SERVICE);
|
||||||
mTelephonyManager.listen(mPhoneStateListener,
|
mTelephonyManager.registerTelephonyCallback(mHandler::post,
|
||||||
PhoneStateListener.LISTEN_SERVICE_STATE);
|
mPhoneStateListener);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mListener = listener;
|
mListener = listener;
|
||||||
@@ -82,10 +74,28 @@ public class ConnectivityUtil {
|
|||||||
public void unregister() {
|
public void unregister() {
|
||||||
if (mListener != null) {
|
if (mListener != null) {
|
||||||
if (mTelephonyManager != null) {
|
if (mTelephonyManager != null) {
|
||||||
mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
|
mTelephonyManager.unregisterTelephonyCallback(mPhoneStateListener);
|
||||||
mCurrentServiceState = ServiceState.STATE_POWER_OFF;
|
mCurrentServiceState = ServiceState.STATE_POWER_OFF;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mListener = null;
|
mListener = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class PhoneStateListener extends TelephonyCallback implements
|
||||||
|
TelephonyCallback.DataConnectionStateListener, TelephonyCallback.ServiceStateListener {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDataConnectionStateChanged(int state, int networkType) {
|
||||||
|
mCurrentServiceState = (state == TelephonyManager.DATA_DISCONNECTED) ?
|
||||||
|
ServiceState.STATE_OUT_OF_SERVICE : ServiceState.STATE_IN_SERVICE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onServiceStateChanged(@NonNull ServiceState serviceState) {
|
||||||
|
if (mCurrentServiceState != serviceState.getState()) {
|
||||||
|
mCurrentServiceState = serviceState.getState();
|
||||||
|
onPhoneStateChanged(mCurrentServiceState);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user