1 /*
2  * Copyright (C) 2015 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.messaging.util;
18 
19 import android.content.Context;
20 import android.telephony.PhoneStateListener;
21 import android.telephony.ServiceState;
22 import android.telephony.TelephonyManager;
23 
24 import com.android.messaging.datamodel.data.ParticipantData;
25 
26 /**
27  * ConnectivityUtil listens to the network service state changes.
28  *
29  * On N and beyond, This class instance can be created via ConnectivityUtil(context, subId), use
30  * ConnectivityUtil(context) for others.
31  *
32  * Note that TelephonyManager has createForSubscriptionId() for a specific subId from N but listen()
33  * does not use the subId on the manager, and uses the default subId on PhoneStateListener. From O,
34  * the manager uses its' own subId in listen().
35  */
36 public class ConnectivityUtil {
37     // Assume not connected until informed differently
38     private volatile int mCurrentServiceState = ServiceState.STATE_POWER_OFF;
39 
40     private final TelephonyManager mTelephonyManager;
41 
42     private ConnectivityListener mListener;
43 
44     public interface ConnectivityListener {
onPhoneStateChanged(int serviceState)45         public void onPhoneStateChanged(int serviceState);
46     }
47 
ConnectivityUtil(final Context context)48     public ConnectivityUtil(final Context context) {
49         mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
50     }
51 
ConnectivityUtil(final Context context, final int subId)52     public ConnectivityUtil(final Context context, final int subId) {
53         Assert.isTrue(OsUtil.isAtLeastN());
54         mTelephonyManager =
55                 ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE))
56                         .createForSubscriptionId(subId);
57     }
58 
getCurrentServiceState()59     public int getCurrentServiceState() {
60         return mCurrentServiceState;
61     }
62 
63     private final PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
64         @Override
65         public void onServiceStateChanged(final ServiceState serviceState) {
66             if (mCurrentServiceState != serviceState.getState()) {
67                 mCurrentServiceState = serviceState.getState();
68                 onPhoneStateChanged(mCurrentServiceState);
69             }
70         }
71 
72         @Override
73         public void onDataConnectionStateChanged(final int state) {
74             mCurrentServiceState = (state == TelephonyManager.DATA_DISCONNECTED) ?
75                     ServiceState.STATE_OUT_OF_SERVICE : ServiceState.STATE_IN_SERVICE;
76         }
77     };
78 
onPhoneStateChanged(final int serviceState)79     private void onPhoneStateChanged(final int serviceState) {
80         final ConnectivityListener listener = mListener;
81         if (listener != null) {
82             listener.onPhoneStateChanged(serviceState);
83         }
84     }
85 
register(final ConnectivityListener listener)86     public void register(final ConnectivityListener listener) {
87         Assert.isTrue(mListener == null || mListener == listener);
88         if (mListener == null) {
89             if (mTelephonyManager != null) {
90                 mCurrentServiceState = (PhoneUtils.getDefault().isAirplaneModeOn() ?
91                         ServiceState.STATE_POWER_OFF : ServiceState.STATE_IN_SERVICE);
92                 mTelephonyManager.listen(mPhoneStateListener,
93                         PhoneStateListener.LISTEN_SERVICE_STATE);
94             }
95         }
96         mListener = listener;
97     }
98 
unregister()99     public void unregister() {
100         if (mListener != null) {
101             if (mTelephonyManager != null) {
102                 mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
103                 mCurrentServiceState = ServiceState.STATE_POWER_OFF;
104             }
105         }
106         mListener = null;
107     }
108 }
109