From fe544787612bb155c229acde9220a6336ad242cf Mon Sep 17 00:00:00 2001 From: Zhanglong Xia Date: Mon, 17 Mar 2025 17:10:39 +0800 Subject: [PATCH] [Thread] unregister all country code callbacks when CountryCode is not needed This commit unregisters all country code callbacks (WiFi, Telephony and Location) when the CountryCode module detects that the co-processor doesn't support setting the country code. Bug: b/402276117 Test: atest ThreadNetworkUnitTests:ThreadNetworkCountryCodeTest#setCountryCodeNotSupported_returnUnsupportedFeatureError_unregisterAllCallbacks Change-Id: I65d57e0e826202ad696335200645bf6d78c8b87c --- .../thread/ThreadNetworkCountryCode.java | 57 +++++++++++++++++-- .../thread/ThreadNetworkCountryCodeTest.java | 17 ++++++ 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/thread/service/java/com/android/server/thread/ThreadNetworkCountryCode.java b/thread/service/java/com/android/server/thread/ThreadNetworkCountryCode.java index 50e0b121e5..a96d06e15f 100644 --- a/thread/service/java/com/android/server/thread/ThreadNetworkCountryCode.java +++ b/thread/service/java/com/android/server/thread/ThreadNetworkCountryCode.java @@ -29,11 +29,13 @@ import android.content.IntentFilter; import android.location.Address; import android.location.Geocoder; import android.location.Location; +import android.location.LocationListener; import android.location.LocationManager; import android.net.thread.IOperationReceiver; import android.net.wifi.WifiManager; import android.net.wifi.WifiManager.ActiveCountryCodeChangedCallback; import android.os.Build; +import android.os.Bundle; import android.sysprop.ThreadNetworkProperties; import android.telephony.SubscriptionInfo; import android.telephony.SubscriptionManager; @@ -116,6 +118,10 @@ public class ThreadNetworkCountryCode { new ArrayMap(); private final ThreadPersistentSettings mPersistentSettings; + @Nullable private LocationListener mLocationListener; + @Nullable private WifiCountryCodeCallback mWifiCountryCodeCallback; + @Nullable private BroadcastReceiver mTelephonyBroadcastReceiver; + /** Indicates whether the Thread co-processor supports setting the country code. */ private boolean mIsCpSettingCountryCodeSupported = true; @@ -271,13 +277,40 @@ public class ThreadNetworkCountryCode { updateCountryCode(false /* forceUpdate */); } + private synchronized void unregisterAllCountryCodeCallbacks() { + unregisterGeocoderCountryCodeCallback(); + unregisterWifiCountryCodeCallback(); + unregisterTelephonyCountryCodeCallback(); + } + private synchronized void registerGeocoderCountryCodeCallback() { if ((mGeocoder != null) && isLocationUseForCountryCodeEnabled()) { + mLocationListener = + new LocationListener() { + public void onLocationChanged(Location location) { + setCountryCodeFromGeocodingLocation(location); + } + + // not used yet + public void onProviderDisabled(String provider) {} + + public void onProviderEnabled(String provider) {} + + public void onStatusChanged(String provider, int status, Bundle extras) {} + }; + mLocationManager.requestLocationUpdates( LocationManager.PASSIVE_PROVIDER, TIME_BETWEEN_LOCATION_UPDATES_MS, DISTANCE_BETWEEN_LOCALTION_UPDATES_METERS, - location -> setCountryCodeFromGeocodingLocation(location)); + mLocationListener); + } + } + + private synchronized void unregisterGeocoderCountryCodeCallback() { + if (mLocationListener != null) { + mLocationManager.removeUpdates(mLocationListener); + mLocationListener = null; } } @@ -317,8 +350,16 @@ public class ThreadNetworkCountryCode { private synchronized void registerWifiCountryCodeCallback() { if (mWifiManager != null) { + mWifiCountryCodeCallback = new WifiCountryCodeCallback(); mWifiManager.registerActiveCountryCodeChangedCallback( - r -> r.run(), new WifiCountryCodeCallback()); + r -> r.run(), mWifiCountryCodeCallback); + } + } + + private synchronized void unregisterWifiCountryCodeCallback() { + if ((mWifiManager != null) && (mWifiCountryCodeCallback != null)) { + mWifiManager.unregisterActiveCountryCodeChangedCallback(mWifiCountryCodeCallback); + mWifiCountryCodeCallback = null; } } @@ -357,7 +398,7 @@ public class ThreadNetworkCountryCode { return; } - BroadcastReceiver broadcastReceiver = + mTelephonyBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { @@ -381,11 +422,18 @@ public class ThreadNetworkCountryCode { }; mContext.registerReceiver( - broadcastReceiver, + mTelephonyBroadcastReceiver, new IntentFilter(TelephonyManager.ACTION_NETWORK_COUNTRY_CHANGED), Context.RECEIVER_EXPORTED); } + private synchronized void unregisterTelephonyCountryCodeCallback() { + if (mTelephonyBroadcastReceiver != null) { + mContext.unregisterReceiver(mTelephonyBroadcastReceiver); + mTelephonyBroadcastReceiver = null; + } + } + private synchronized void updateTelephonyCountryCodeFromSimCard() { List subscriptionInfoList = mSubscriptionManager.getActiveSubscriptionInfoList(); @@ -526,6 +574,7 @@ public class ThreadNetworkCountryCode { public void onError(int otError, String message) { if (otError == ERROR_UNSUPPORTED_FEATURE) { mIsCpSettingCountryCodeSupported = false; + unregisterAllCountryCodeCallbacks(); } LOG.e( diff --git a/thread/tests/unit/src/com/android/server/thread/ThreadNetworkCountryCodeTest.java b/thread/tests/unit/src/com/android/server/thread/ThreadNetworkCountryCodeTest.java index 2e58bc9e2c..6eb9b50959 100644 --- a/thread/tests/unit/src/com/android/server/thread/ThreadNetworkCountryCodeTest.java +++ b/thread/tests/unit/src/com/android/server/thread/ThreadNetworkCountryCodeTest.java @@ -474,6 +474,23 @@ public class ThreadNetworkCountryCodeTest { assertThat(mThreadNetworkCountryCode.getCountryCode()).isEqualTo(DEFAULT_COUNTRY_CODE); } + @Test + public void setCountryCodeNotSupported_returnUnsupportedFeatureError_unregisterAllCallbacks() { + mThreadNetworkCountryCode.initialize(); + assertThat(mThreadNetworkCountryCode.getCountryCode()).isEqualTo(DEFAULT_COUNTRY_CODE); + + mErrorUnsupportedFeatureSetCountryCode = true; + mThreadNetworkCountryCode.setOverrideCountryCode(TEST_COUNTRY_CODE_CN); + verify(mThreadNetworkControllerService) + .setCountryCode(eq(TEST_COUNTRY_CODE_CN), mOperationReceiverCaptor.capture()); + + verify(mLocationManager).removeUpdates(mLocationListenerCaptor.capture()); + verify(mWifiManager) + .unregisterActiveCountryCodeChangedCallback( + mWifiCountryCodeReceiverCaptor.capture()); + verify(mContext).unregisterReceiver(mTelephonyCountryCodeReceiverCaptor.capture()); + } + @Test public void settingsCountryCode_settingsCountryCodeIsActive_settingsCountryCodeIsUsed() { when(mPersistentSettings.get(KEY_COUNTRY_CODE)).thenReturn(TEST_COUNTRY_CODE_CN);