Files
packages_apps_Messaging/src/com/android/messaging/util/ConnectivityUtil.java
Taesu Lee dcfe928ff3 Fix creating ConnectivityUtil to work on any platform
After this CL, ConnectivityUtil for default subId is used again on pre
N because TelephonyManager has createForSubscriptionId() for a specific
subId from N.
Nevertheless, listen() uses the default subId on PhoneStateListener
instead of the subId on the manager still even N. On O and beyond,
ConnectivityUtil works correctly on multi-sim devices.

Revert "Fix missing computeIfAbsent() method in L_MR1 and M"

This reverts commit f0ccb76d8d.

Reason for revert: Manage ConnectivityUtil instances for subIds at least
                   on N instead.

Test: Manual

Signed-off-by: Taesu Lee <taesu82.lee@samsung.com>
Change-Id: I2f08928a6798a2ce275c5c75569ad379999d274c
2020-06-18 19:24:27 +09:00

109 lines
4.0 KiB
Java

/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.messaging.util;
import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
import android.telephony.TelephonyManager;
import com.android.messaging.datamodel.data.ParticipantData;
/**
* ConnectivityUtil listens to the network service state changes.
*
* On N and beyond, This class instance can be created via ConnectivityUtil(context, subId), use
* ConnectivityUtil(context) for others.
*
* Note that TelephonyManager has createForSubscriptionId() for a specific subId from N but listen()
* does not use the subId on the manager, and uses the default subId on PhoneStateListener. From O,
* the manager uses its' own subId in listen().
*/
public class ConnectivityUtil {
// Assume not connected until informed differently
private volatile int mCurrentServiceState = ServiceState.STATE_POWER_OFF;
private final TelephonyManager mTelephonyManager;
private ConnectivityListener mListener;
public interface ConnectivityListener {
public void onPhoneStateChanged(int serviceState);
}
public ConnectivityUtil(final Context context) {
mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
}
public ConnectivityUtil(final Context context, final int subId) {
Assert.isTrue(OsUtil.isAtLeastN());
mTelephonyManager =
((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE))
.createForSubscriptionId(subId);
}
public int getCurrentServiceState() {
return mCurrentServiceState;
}
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) {
final ConnectivityListener listener = mListener;
if (listener != null) {
listener.onPhoneStateChanged(serviceState);
}
}
public void register(final ConnectivityListener listener) {
Assert.isTrue(mListener == null || mListener == listener);
if (mListener == null) {
if (mTelephonyManager != null) {
mCurrentServiceState = (PhoneUtils.getDefault().isAirplaneModeOn() ?
ServiceState.STATE_POWER_OFF : ServiceState.STATE_IN_SERVICE);
mTelephonyManager.listen(mPhoneStateListener,
PhoneStateListener.LISTEN_SERVICE_STATE);
}
}
mListener = listener;
}
public void unregister() {
if (mListener != null) {
if (mTelephonyManager != null) {
mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
mCurrentServiceState = ServiceState.STATE_POWER_OFF;
}
}
mListener = null;
}
}