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 CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED: 74 case Intent.ACTION_AIRPLANE_MODE_CHANGED: 75 case WifiManager.WIFI_STATE_CHANGED_ACTION: 76 IwlanEventListener.onBroadcastReceived(intent); 77 break; 78 case TelephonyManager.ACTION_CARRIER_SIGNAL_PCO_VALUE: 79 processCarrierSignalPcoValue(intent); 80 break; 81 } 82 } 83 processCarrierSignalPcoValue(Intent intent)84 private void processCarrierSignalPcoValue(Intent intent) { 85 Log.d(TAG, "on CARRIER_SIGNAL_PCO_VALUE intent"); 86 int intentSubId = 87 intent.getIntExtra( 88 SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, 89 SubscriptionManager.INVALID_SUBSCRIPTION_ID); 90 int intentSlotIndex = SubscriptionManager.getSlotIndex(intentSubId); 91 Log.d(TAG, "intentSubId:" + intentSubId + " intentSlotIndex:" + intentSlotIndex); 92 93 if (intentSlotIndex != SubscriptionManager.INVALID_SIM_SLOT_INDEX) { 94 95 int apnBitMask = intent.getIntExtra(TelephonyManager.EXTRA_APN_TYPE, 0); 96 97 if ((apnBitMask & ApnSetting.TYPE_IMS) != 0) { 98 int pcoId = intent.getIntExtra(TelephonyManager.EXTRA_PCO_ID, 0); 99 byte[] pcoData = intent.getByteArrayExtra(TelephonyManager.EXTRA_PCO_VALUE); 100 101 Log.d( 102 TAG, 103 "PcoID:" 104 + String.format("0x%04x", pcoId) 105 + " PcoData:" 106 + Arrays.toString(pcoData)); 107 108 Context mContext = IwlanDataService.getContext(); 109 110 if (mContext != null) { 111 int PCO_ID_IPv6 = 112 IwlanHelper.getConfig( 113 CarrierConfigManager.Iwlan.KEY_EPDG_PCO_ID_IPV6_INT, 114 mContext, 115 intentSlotIndex); 116 int PCO_ID_IPv4 = 117 IwlanHelper.getConfig( 118 CarrierConfigManager.Iwlan.KEY_EPDG_PCO_ID_IPV4_INT, 119 mContext, 120 intentSlotIndex); 121 122 Log.d( 123 TAG, 124 "PCO_ID_IPv6:" 125 + String.format("0x%04x", PCO_ID_IPv6) 126 + " PCO_ID_IPv4:" 127 + String.format("0x%04x", PCO_ID_IPv4)); 128 129 if (pcoId == PCO_ID_IPv6 || pcoId == PCO_ID_IPv4) { 130 Log.d(TAG, "SetPcoData to EpdgSelector"); 131 EpdgSelector selector = 132 EpdgSelector.getSelectorInstance(mContext, intentSlotIndex); 133 boolean ret = selector.setPcoData(pcoId, pcoData); 134 } else { 135 Log.d(TAG, "Unwanted PcoID " + pcoId); 136 } 137 } else { 138 Log.e(TAG, "Null context"); 139 } 140 } else { 141 Log.d(TAG, "Unwanted Apntype " + apnBitMask); 142 } 143 } else { 144 Log.e(TAG, "Invalid slot index"); 145 } 146 } 147 } 148