1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
/*
* 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;
}
}
|