Converting message.obj to AsyncResult to deliver context

Bug: 327757676
Test: atest com.android.phone.satellite.accesscontrol.SatelliteAccessControllerTest
Test: Manual test, verified no issue when the config data is downloaded from the server

Change-Id: I29598ecdc7ff0504349d58e54d9194de6ace730b
diff --git a/src/com/android/phone/satellite/accesscontrol/SatelliteAccessController.java b/src/com/android/phone/satellite/accesscontrol/SatelliteAccessController.java
index 36280e9..45cd611 100644
--- a/src/com/android/phone/satellite/accesscontrol/SatelliteAccessController.java
+++ b/src/com/android/phone/satellite/accesscontrol/SatelliteAccessController.java
@@ -28,6 +28,7 @@
 import android.location.Location;
 import android.location.LocationManager;
 import android.location.LocationRequest;
+import android.os.AsyncResult;
 import android.os.Build;
 import android.os.Bundle;
 import android.os.CancellationSignal;
@@ -220,7 +221,8 @@
                 cleanupOnDeviceAccessControllerResources();
                 break;
             case EVENT_CONFIG_DATA_UPDATED:
-                updateSatelliteConfigData((Context) msg.obj);
+                AsyncResult ar = (AsyncResult) msg.obj;
+                updateSatelliteConfigData((Context) ar.userObj);
                 break;
             default:
                 logw("SatelliteAccessControllerHandler: unexpected message code: " + msg.what);
@@ -349,8 +351,7 @@
      * Update country codes, S2CellFile and satellite region allowed by ConfigUpdater
      * or CarrierConfig
      */
-    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE)
-    public void updateSatelliteConfigData(Context context) {
+    private void updateSatelliteConfigData(Context context) {
         logd("updateSatelliteConfigData");
 
         SatelliteConfig satelliteConfig = mSatelliteController.getSatelliteConfig();
diff --git a/tests/src/com/android/phone/satellite/accesscontrol/SatelliteAccessControllerTest.java b/tests/src/com/android/phone/satellite/accesscontrol/SatelliteAccessControllerTest.java
index e6f70aa..045cc1d 100644
--- a/tests/src/com/android/phone/satellite/accesscontrol/SatelliteAccessControllerTest.java
+++ b/tests/src/com/android/phone/satellite/accesscontrol/SatelliteAccessControllerTest.java
@@ -21,9 +21,12 @@
 import static android.telephony.satellite.SatelliteManager.SATELLITE_RESULT_REQUEST_NOT_SUPPORTED;
 import static android.telephony.satellite.SatelliteManager.SATELLITE_RESULT_SUCCESS;
 
+import static com.android.phone.satellite.accesscontrol.SatelliteAccessController.EVENT_CONFIG_DATA_UPDATED;
+
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.mockito.ArgumentMatchers.any;
@@ -46,10 +49,13 @@
 import android.location.Location;
 import android.location.LocationManager;
 import android.location.LocationRequest;
+import android.os.AsyncResult;
 import android.os.Bundle;
 import android.os.CancellationSignal;
+import android.os.Handler;
 import android.os.HandlerThread;
 import android.os.Looper;
+import android.os.Message;
 import android.os.ResultReceiver;
 import android.telecom.TelecomManager;
 import android.testing.TestableLooper;
@@ -128,12 +134,20 @@
     private TestableLooper mTestableLooper;
     private Phone[] mPhones;
     private TestSatelliteAccessController mSatelliteAccessControllerUT;
+
     @Captor
     private ArgumentCaptor<CancellationSignal> mLocationRequestCancellationSignalCaptor;
     @Captor
     private ArgumentCaptor<Consumer<Location>> mLocationRequestConsumerCaptor;
     @Captor
     private ArgumentCaptor<ResultReceiver> mResultReceiverFromSatelliteControllerCaptor;
+    @Captor
+    private ArgumentCaptor<Handler> mConfigUpdateHandlerCaptor;
+    @Captor
+    private ArgumentCaptor<Integer> mConfigUpdateIntCaptor;
+    @Captor
+    private ArgumentCaptor<Object> mConfigUpdateObjectCaptor;
+
     private boolean mQueriedSatelliteAllowed = false;
     private int mQueriedSatelliteAllowedResultCode = SATELLITE_RESULT_SUCCESS;
     private Semaphore mSatelliteAllowedSemaphore = new Semaphore(0);
@@ -542,11 +556,20 @@
 
     @Test
     public void testUpdateSatelliteConfigData() {
+        verify(mMockSatelliteController).registerForConfigUpdateChanged(
+                mConfigUpdateHandlerCaptor.capture(), mConfigUpdateIntCaptor.capture(),
+                mConfigUpdateObjectCaptor.capture());
+
+        assertSame(mConfigUpdateHandlerCaptor.getValue(), mSatelliteAccessControllerUT);
+        assertSame(mConfigUpdateIntCaptor.getValue(), EVENT_CONFIG_DATA_UPDATED);
+        assertSame(mConfigUpdateObjectCaptor.getValue(), mMockContext);
+
         // Verify the case when the configParser is not exist.
         SatelliteConfigParser spyConfigParserNull =
                 spy(new SatelliteConfigParser((byte[]) null));
         doReturn(spyConfigParserNull).when(mMockSatelliteController).getSatelliteConfigParser();
-        mSatelliteAccessControllerUT.updateSatelliteConfigData(mMockContext);
+
+        sendConfigUpdateChangedEvent(mMockContext);
 
         assertNull(spyConfigParserNull.getConfig());
 
@@ -554,7 +577,8 @@
         SatelliteConfigParser spyConfigParserEmpty =
                 spy(new SatelliteConfigParser("test".getBytes()));
         doReturn(spyConfigParserEmpty).when(mMockSatelliteController).getSatelliteConfigParser();
-        mSatelliteAccessControllerUT.updateSatelliteConfigData(mMockContext);
+
+        sendConfigUpdateChangedEvent(mMockContext);
 
         assertNull(spyConfigParserEmpty.getConfig());
 
@@ -570,12 +594,20 @@
         doReturn(targetSatS2FilePath).when(mockSatelliteConfig).getSatelliteS2CellFile(any());
         doReturn(mockSatelliteConfig).when(mMockSatelliteController).getSatelliteConfig();
 
-        mSatelliteAccessControllerUT.updateSatelliteConfigData(mMockContext);
+        sendConfigUpdateChangedEvent(mMockContext);
+
         verify(mockSatelliteConfig, times(0)).getDeviceSatelliteCountryCodes();
         verify(mockSatelliteConfig, times(0)).isSatelliteDataForAllowedRegion();
         verify(mockSatelliteConfig, times(2)).getSatelliteS2CellFile(any());
     }
 
+    private void sendConfigUpdateChangedEvent(Context context) {
+        Message msg = mSatelliteAccessControllerUT.obtainMessage(EVENT_CONFIG_DATA_UPDATED);
+        msg.obj = new AsyncResult(context, SATELLITE_RESULT_SUCCESS, null);
+        msg.sendToTarget();
+        mTestableLooper.processAllMessages();
+    }
+
     private void clearAllInvocations() {
         clearInvocations(mMockSatelliteController);
         clearInvocations(mMockSatelliteOnDeviceAccessController);