[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
This commit is contained in:
parent
f6187b99c7
commit
fe54478761
2 changed files with 70 additions and 4 deletions
|
|
@ -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<SubscriptionInfo> 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(
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Reference in a new issue