Merge changes I65d57e0e,I8ca1f7ae into main
* changes:
[Thread] unregister all country code callbacks when CountryCode is not needed
[Thread] do not set the country code again if the co-processor doesn't support it
diff --git a/thread/service/java/com/android/server/thread/ThreadNetworkCountryCode.java b/thread/service/java/com/android/server/thread/ThreadNetworkCountryCode.java
index ff0e2c1..a96d06e 100644
--- a/thread/service/java/com/android/server/thread/ThreadNetworkCountryCode.java
+++ b/thread/service/java/com/android/server/thread/ThreadNetworkCountryCode.java
@@ -16,6 +16,7 @@
package com.android.server.thread;
+import static android.net.thread.ThreadNetworkException.ERROR_UNSUPPORTED_FEATURE;
import static com.android.server.thread.ThreadPersistentSettings.KEY_COUNTRY_CODE;
import android.annotation.Nullable;
@@ -28,11 +29,13 @@
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;
@@ -115,6 +118,13 @@
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;
+
@Nullable private CountryCodeInfo mCurrentCountryCodeInfo;
@Nullable private CountryCodeInfo mLocationCountryCodeInfo;
@Nullable private CountryCodeInfo mOverrideCountryCodeInfo;
@@ -267,13 +277,40 @@
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;
}
}
@@ -313,8 +350,16 @@
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;
}
}
@@ -353,7 +398,7 @@
return;
}
- BroadcastReceiver broadcastReceiver =
+ mTelephonyBroadcastReceiver =
new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
@@ -377,11 +422,18 @@
};
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<SubscriptionInfo> subscriptionInfoList =
mSubscriptionManager.getActiveSubscriptionInfoList();
@@ -520,6 +572,11 @@
@Override
public void onError(int otError, String message) {
+ if (otError == ERROR_UNSUPPORTED_FEATURE) {
+ mIsCpSettingCountryCodeSupported = false;
+ unregisterAllCountryCodeCallbacks();
+ }
+
LOG.e(
"Error "
+ otError
@@ -546,6 +603,11 @@
return;
}
+ if (!mIsCpSettingCountryCodeSupported) {
+ LOG.i("Thread co-processor doesn't support setting the country code");
+ return;
+ }
+
LOG.i("Set country code: " + countryCodeInfo);
mThreadNetworkControllerService.setCountryCode(
countryCodeInfo.getCountryCode().toUpperCase(Locale.ROOT),
@@ -592,6 +654,7 @@
/** Dumps the current state of this ThreadNetworkCountryCode object. */
public synchronized void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
pw.println("---- Dump of ThreadNetworkCountryCode begin ----");
+ pw.println("mIsCpSettingCountryCodeSupported: " + mIsCpSettingCountryCodeSupported);
pw.println("mOverrideCountryCodeInfo : " + mOverrideCountryCodeInfo);
pw.println("mTelephonyCountryCodeSlotInfoMap: " + mTelephonyCountryCodeSlotInfoMap);
pw.println("mTelephonyCountryCodeInfo : " + mTelephonyCountryCodeInfo);
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 139f4c8..6eb9b50 100644
--- a/thread/tests/unit/src/com/android/server/thread/ThreadNetworkCountryCodeTest.java
+++ b/thread/tests/unit/src/com/android/server/thread/ThreadNetworkCountryCodeTest.java
@@ -17,6 +17,7 @@
package com.android.server.thread;
import static android.net.thread.ThreadNetworkException.ERROR_INTERNAL_ERROR;
+import static android.net.thread.ThreadNetworkException.ERROR_UNSUPPORTED_FEATURE;
import static com.android.server.thread.ThreadNetworkCountryCode.DEFAULT_COUNTRY_CODE;
import static com.android.server.thread.ThreadPersistentSettings.KEY_COUNTRY_CODE;
@@ -109,6 +110,7 @@
private ThreadNetworkCountryCode mThreadNetworkCountryCode;
private boolean mErrorSetCountryCode;
+ private boolean mErrorUnsupportedFeatureSetCountryCode;
@Captor private ArgumentCaptor<LocationListener> mLocationListenerCaptor;
@Captor private ArgumentCaptor<Geocoder.GeocodeListener> mGeocodeListenerCaptor;
@@ -143,6 +145,10 @@
if (mErrorSetCountryCode) {
cb.onError(ERROR_INTERNAL_ERROR, new String("Invalid country code"));
+ } else if (mErrorUnsupportedFeatureSetCountryCode) {
+ cb.onError(
+ ERROR_UNSUPPORTED_FEATURE,
+ new String("Setting country code is not supported"));
} else {
cb.onSuccess();
}
@@ -453,6 +459,39 @@
}
@Test
+ public void setCountryCodeNotSupported_returnUnsupportedFeatureError_countryCodeNotSetAgain() {
+ 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());
+
+ mThreadNetworkCountryCode.setOverrideCountryCode(TEST_COUNTRY_CODE_US);
+ verifyNoMoreInteractions(mThreadNetworkControllerService);
+
+ 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);
mThreadNetworkCountryCode.initialize();
@@ -468,6 +507,7 @@
mThreadNetworkCountryCode.dump(new FileDescriptor(), printWriter, null);
String outputString = stringWriter.toString();
+ assertThat(outputString).contains("mIsCpSettingCountryCodeSupported");
assertThat(outputString).contains("mOverrideCountryCodeInfo");
assertThat(outputString).contains("mTelephonyCountryCodeSlotInfoMap");
assertThat(outputString).contains("mTelephonyCountryCodeInfo");