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