Return proper error code for requestIsSatelliteCommunicationAllowedForCurrentLocation

Flag: EXEMPT bugfix
Bug: 346842487
Test: SatelliteAccessControllerTest, SatelliteManagerTestOnMockService
System tests with Skylo

Change-Id: Ibc10d7bba2cf622f7633f95cd03ae59b854fe9ce
Merged-In: Ibc10d7bba2cf622f7633f95cd03ae59b854fe9ce
diff --git a/src/com/android/phone/satellite/accesscontrol/SatelliteAccessController.java b/src/com/android/phone/satellite/accesscontrol/SatelliteAccessController.java
index ef919ef..293061d 100644
--- a/src/com/android/phone/satellite/accesscontrol/SatelliteAccessController.java
+++ b/src/com/android/phone/satellite/accesscontrol/SatelliteAccessController.java
@@ -19,6 +19,7 @@
 import static android.telephony.satellite.SatelliteManager.KEY_SATELLITE_COMMUNICATION_ALLOWED;
 import static android.telephony.satellite.SatelliteManager.KEY_SATELLITE_PROVISIONED;
 import static android.telephony.satellite.SatelliteManager.KEY_SATELLITE_SUPPORTED;
+import static android.telephony.satellite.SatelliteManager.SATELLITE_RESULT_LOCATION_NOT_AVAILABLE;
 import static android.telephony.satellite.SatelliteManager.SATELLITE_RESULT_REQUEST_NOT_SUPPORTED;
 import static android.telephony.satellite.SatelliteManager.SATELLITE_RESULT_SUCCESS;
 
@@ -205,6 +206,12 @@
             "config_updater_satellite_country_codes";
     private static final String CONFIG_UPDATER_SATELLITE_IS_ALLOW_ACCESS_CONTROL_KEY =
             "config_updater_satellite_is_allow_access_control";
+
+    private static final String LATEST_SATELLITE_COMMUNICATION_ALLOWED_SET_TIME_KEY =
+            "latest_satellite_communication_allowed_set_time";
+    private static final String LATEST_SATELLITE_COMMUNICATION_ALLOWED_KEY =
+            "latest_satellite_communication_allowed";
+
     private SharedPreferences mSharedPreferences;
     private final ConfigUpdaterMetricsStats mConfigUpdaterMetricsStats;
     @Nullable
@@ -257,6 +264,7 @@
         // loadConfigUpdaterConfigs has to be called after loadOverlayConfigs
         // since config updater config has higher priority and thus can override overlay config
         loadConfigUpdaterConfigs();
+        loadCachedLatestSatelliteCommunicationAllowedState();
         mSatelliteController.registerForConfigUpdateChanged(this, EVENT_CONFIG_DATA_UPDATED,
                 context);
         if (s2CellFile != null) {
@@ -606,6 +614,22 @@
         }
     }
 
+    private void persistLatestSatelliteCommunicationAllowedState() {
+        if (mSharedPreferences == null) {
+            ploge("persistLatestSatelliteCommunicationAllowedState: mSharedPreferences is null");
+            return;
+        }
+
+        try {
+            mSharedPreferences.edit().putLong(LATEST_SATELLITE_COMMUNICATION_ALLOWED_SET_TIME_KEY,
+                    mLatestSatelliteCommunicationAllowedSetTime).apply();
+            mSharedPreferences.edit().putBoolean(LATEST_SATELLITE_COMMUNICATION_ALLOWED_KEY,
+                    mLatestSatelliteCommunicationAllowed).apply();
+        } catch (Exception ex) {
+            ploge("persistLatestSatelliteCommunicationAllowedState error : " + ex);
+        }
+    }
+
     /**
      * Update country codes and S2CellFile with the new data from ConfigUpdater
      */
@@ -739,6 +763,27 @@
         mIsSatelliteAllowAccessControl = isSatelliteAllowAccessControl;
     }
 
+    private void loadCachedLatestSatelliteCommunicationAllowedState() {
+        if (mSharedPreferences == null) {
+            ploge("loadCachedLatestSatelliteCommunicationAllowedState: mSharedPreferences is null");
+            return;
+        }
+
+        try {
+            mLatestSatelliteCommunicationAllowedSetTime =
+                    mSharedPreferences.getLong(LATEST_SATELLITE_COMMUNICATION_ALLOWED_SET_TIME_KEY,
+                            0);
+            mLatestSatelliteCommunicationAllowed =
+                    mSharedPreferences.getBoolean(LATEST_SATELLITE_COMMUNICATION_ALLOWED_KEY,
+                            false);
+        } catch (Exception ex) {
+            ploge("loadCachedLatestSatelliteCommunicationAllowedState: ex=" + ex);
+        }
+        plogd("mLatestSatelliteCommunicationAllowedSetTime="
+                + mLatestSatelliteCommunicationAllowedSetTime
+                + ", mLatestSatelliteCommunicationAllowed=" + mLatestSatelliteCommunicationAllowed);
+    }
+
     private long getLocationFreshDurationNanos() {
         synchronized (mLock) {
             if (mIsOverlayConfigOverridden) {
@@ -860,7 +905,9 @@
 
     private void sendSatelliteAllowResultToReceivers(int resultCode, Bundle resultData,
             boolean allowed) {
-        updateCurrentSatelliteAllowedState(allowed);
+        if (resultCode == SATELLITE_RESULT_SUCCESS) {
+            updateCurrentSatelliteAllowedState(allowed);
+        }
         synchronized (mLock) {
             for (ResultReceiver resultReceiver : mSatelliteAllowResultReceivers) {
                 resultReceiver.send(resultCode, resultData);
@@ -924,7 +971,7 @@
         }
     }
 
-    /* returns true,if the latest query was executed in 24Hr, or returns false. */
+    /* returns true if the latest query was executed in 12Hr, or returns false. */
     private boolean isCommunicationAllowedCacheValid() {
         if (mLatestSatelliteCommunicationAllowedSetTime > 0) {
             long currentTime = SystemClock.elapsedRealtimeNanos();
@@ -1033,14 +1080,15 @@
             } else {
                 plogd("current location is not available");
                 if (isCommunicationAllowedCacheValid()) {
-                    plogd("onCurrentLocationAvailable: 24Hr cache is still valid, using it");
+                    plogd("onCurrentLocationAvailable: 12Hr cache is still valid, using it");
                     bundle.putBoolean(KEY_SATELLITE_COMMUNICATION_ALLOWED,
                             mLatestSatelliteCommunicationAllowed);
                     sendSatelliteAllowResultToReceivers(SATELLITE_RESULT_SUCCESS, bundle,
                             mLatestSatelliteCommunicationAllowed);
                 } else {
                     bundle.putBoolean(KEY_SATELLITE_COMMUNICATION_ALLOWED, false);
-                    sendSatelliteAllowResultToReceivers(SATELLITE_RESULT_SUCCESS, bundle, false);
+                    sendSatelliteAllowResultToReceivers(
+                            SATELLITE_RESULT_LOCATION_NOT_AVAILABLE, bundle, false);
                 }
             }
         }
@@ -1075,6 +1123,7 @@
                         satelliteAllowed);
                 mLatestSatelliteCommunicationAllowed = satelliteAllowed;
                 mLatestSatelliteCommunicationAllowedSetTime = SystemClock.elapsedRealtimeNanos();
+                persistLatestSatelliteCommunicationAllowedState();
             } catch (Exception ex) {
                 ploge("checkSatelliteAccessRestrictionForLocation: ex=" + ex);
                 reportAnomaly(UUID_ON_DEVICE_LOOKUP_EXCEPTION,
@@ -1510,9 +1559,11 @@
                 mLatestSatelliteCommunicationAllowedSetTime = 0;
                 mLatestSatelliteCommunicationAllowed = false;
                 mCurrentSatelliteAllowedState = false;
+                persistLatestSatelliteCommunicationAllowedState();
             } else if ("clear_cache_only".equalsIgnoreCase(state)) {
                 mLatestSatelliteCommunicationAllowedSetTime = 0;
                 mLatestSatelliteCommunicationAllowed = false;
+                persistLatestSatelliteCommunicationAllowedState();
             } else {
                 loge("setIsSatelliteCommunicationAllowedForCurrentLocationCache: invalid state="
                         + state);
diff --git a/tests/src/com/android/phone/satellite/accesscontrol/SatelliteAccessControllerTest.java b/tests/src/com/android/phone/satellite/accesscontrol/SatelliteAccessControllerTest.java
index f34b4a7..784b3e5 100644
--- a/tests/src/com/android/phone/satellite/accesscontrol/SatelliteAccessControllerTest.java
+++ b/tests/src/com/android/phone/satellite/accesscontrol/SatelliteAccessControllerTest.java
@@ -17,6 +17,7 @@
 package com.android.phone.satellite.accesscontrol;
 
 import static android.telephony.satellite.SatelliteManager.KEY_SATELLITE_COMMUNICATION_ALLOWED;
+import static android.telephony.satellite.SatelliteManager.SATELLITE_RESULT_LOCATION_NOT_AVAILABLE;
 import static android.telephony.satellite.SatelliteManager.SATELLITE_RESULT_MODEM_ERROR;
 import static android.telephony.satellite.SatelliteManager.SATELLITE_RESULT_REQUEST_NOT_SUPPORTED;
 import static android.telephony.satellite.SatelliteManager.SATELLITE_RESULT_SUCCESS;
@@ -397,8 +398,10 @@
         assertEquals(SATELLITE_RESULT_SUCCESS, mQueriedSatelliteAllowedResultCode);
         assertTrue(mQueriedSatelliteAllowed);
 
-        // Timed out to wait for current location. No cached country codes.
+        // Timed out to wait for current location. No cached allowed state.
         clearAllInvocations();
+        mSatelliteAccessControllerUT.setIsSatelliteCommunicationAllowedForCurrentLocationCache(
+                "cache_clear_and_not_allowed");
         when(mMockCountryDetector.getCurrentNetworkCountryIso()).thenReturn(EMPTY_STRING_LIST);
         when(mMockTelecomManager.isInEmergencyCall()).thenReturn(false);
         when(mMockPhone.isInEcm()).thenReturn(true);
@@ -424,9 +427,7 @@
                 any(SatelliteOnDeviceAccessController.LocationToken.class));
         assertTrue(waitForRequestIsSatelliteAllowedForCurrentLocationResult(
                 mSatelliteAllowedSemaphore, 1));
-        assertEquals(SATELLITE_RESULT_SUCCESS,
-                mQueriedSatelliteAllowedResultCode);
-        assertTrue(mQueriedSatelliteAllowed);
+        assertEquals(SATELLITE_RESULT_LOCATION_NOT_AVAILABLE, mQueriedSatelliteAllowedResultCode);
 
         // Network country codes are not available. TelecomManager.isInEmergencyCall() returns
         // false. No phone is in ECM. Last known location is not fresh. Cached country codes should
@@ -453,7 +454,6 @@
                 mSatelliteAllowedSemaphore, 1));
         assertEquals(SATELLITE_RESULT_SUCCESS, mQueriedSatelliteAllowedResultCode);
         assertFalse(mQueriedSatelliteAllowed);
-
     }
 
     @Test
@@ -471,7 +471,7 @@
 
         // These APIs are executed during loadRemoteConfigs
         verify(mMockSharedPreferences, times(1)).getStringSet(anyString(), any());
-        verify(mMockSharedPreferences, times(1)).getBoolean(anyString(), anyBoolean());
+        verify(mMockSharedPreferences, times(2)).getBoolean(anyString(), anyBoolean());
 
         // satelliteConfig is null
         SatelliteConfigParser spyConfigParser =