1 /*
2  * Copyright 2020 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.google.android.iwlan;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentFilter;
23 import android.net.wifi.WifiManager;
24 import android.telephony.CarrierConfigManager;
25 import android.telephony.SubscriptionManager;
26 import android.telephony.TelephonyManager;
27 import android.telephony.data.ApnSetting;
28 import android.util.Log;
29 
30 import com.google.android.iwlan.epdg.EpdgSelector;
31 
32 public class IwlanBroadcastReceiver extends BroadcastReceiver {
33     private static final String TAG = "IwlanBroadcastReceiver";
34 
35     private static boolean mIsReceiverRegistered = false;
36     private static IwlanBroadcastReceiver mInstance;
37 
startListening(Context context)38     public static void startListening(Context context) {
39         if (mIsReceiverRegistered) {
40             Log.d(TAG, "startListening: Receiver already registered");
41             return;
42         }
43         IntentFilter intentFilter = new IntentFilter();
44         intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
45         intentFilter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
46         context.registerReceiver(getInstance(), intentFilter);
47         mIsReceiverRegistered = true;
48     }
49 
stopListening(Context context)50     public static void stopListening(Context context) {
51         if (!mIsReceiverRegistered) {
52             Log.d(TAG, "stopListening: Receiver not registered!");
53             return;
54         }
55         context.unregisterReceiver(getInstance());
56         mIsReceiverRegistered = false;
57     }
58 
getInstance()59     private static IwlanBroadcastReceiver getInstance() {
60         if (mInstance == null) {
61             mInstance = new IwlanBroadcastReceiver();
62         }
63         return mInstance;
64     }
65 
66     @Override
onReceive(Context context, Intent intent)67     public void onReceive(Context context, Intent intent) {
68         String action = intent.getAction();
69         Log.d(TAG, "onReceive: " + action);
70         switch (action) {
71             case CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED:
72             case Intent.ACTION_AIRPLANE_MODE_CHANGED:
73             case WifiManager.WIFI_STATE_CHANGED_ACTION:
74                 IwlanEventListener.onBroadcastReceived(intent);
75                 break;
76             case TelephonyManager.ACTION_CARRIER_SIGNAL_PCO_VALUE:
77                 processCarrierSignalPcoValue(intent);
78                 break;
79         }
80     }
81 
processCarrierSignalPcoValue(Intent intent)82     private void processCarrierSignalPcoValue(Intent intent) {
83         Log.d(TAG, "on CARRIER_SIGNAL_PCO_VALUE intent");
84         int intentSubId =
85                 intent.getIntExtra(
86                         SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX,
87                         SubscriptionManager.INVALID_SUBSCRIPTION_ID);
88         int intentSlotIndex = SubscriptionManager.getSlotIndex(intentSubId);
89         Log.d(TAG, "intentSubId:" + intentSubId + " intentSlotIndex:" + intentSlotIndex);
90 
91         if (intentSlotIndex != SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
92 
93             int apnBitMask = intent.getIntExtra(TelephonyManager.EXTRA_APN_TYPE, 0);
94 
95             if ((apnBitMask & ApnSetting.TYPE_IMS) != 0) {
96                 int pcoId = intent.getIntExtra(TelephonyManager.EXTRA_PCO_ID, 0);
97                 byte[] pcoData = intent.getByteArrayExtra(TelephonyManager.EXTRA_PCO_VALUE);
98 
99                 Log.d(TAG, "PcoID:" + String.format("0x%04x", pcoId) + " PcoData:" + pcoData);
100 
101                 Context mContext = IwlanDataService.getContext();
102 
103                 if (mContext != null) {
104                     int PCO_ID_IPv6 =
105                             IwlanHelper.getConfig(
106                                     CarrierConfigManager.Iwlan.KEY_EPDG_PCO_ID_IPV6_INT,
107                                     mContext,
108                                     intentSlotIndex);
109                     int PCO_ID_IPv4 =
110                             IwlanHelper.getConfig(
111                                     CarrierConfigManager.Iwlan.KEY_EPDG_PCO_ID_IPV4_INT,
112                                     mContext,
113                                     intentSlotIndex);
114 
115                     Log.d(
116                             TAG,
117                             "PCO_ID_IPv6:"
118                                     + String.format("0x%04x", PCO_ID_IPv6)
119                                     + " PCO_ID_IPv4:"
120                                     + String.format("0x%04x", PCO_ID_IPv4));
121 
122                     if (pcoId == PCO_ID_IPv6 || pcoId == PCO_ID_IPv4) {
123                         Log.d(TAG, "SetPcoData to EpdgSelector");
124                         EpdgSelector selector =
125                                 EpdgSelector.getSelectorInstance(mContext, intentSlotIndex);
126                         boolean ret = selector.setPcoData(pcoId, pcoData);
127                     } else {
128                         Log.d(TAG, "Unwanted PcoID " + pcoId);
129                     }
130                 } else {
131                     Log.e(TAG, "Null context");
132                 }
133             } else {
134                 Log.d(TAG, "Unwanted Apntype " + apnBitMask);
135             }
136         } else {
137             Log.e(TAG, "Invalid slot index");
138         }
139     }
140 }
141