diff --git a/src/com/android/settings/bluetooth/BluetoothEnabler.java b/src/com/android/settings/bluetooth/BluetoothEnabler.java index df5cc72fae1..a5d0bc67769 100644 --- a/src/com/android/settings/bluetooth/BluetoothEnabler.java +++ b/src/com/android/settings/bluetooth/BluetoothEnabler.java @@ -132,7 +132,7 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh new Thread(() -> { try { - mIsSatelliteOn.set(mSatelliteRepository.requestIsEnabled( + mIsSatelliteOn.set(mSatelliteRepository.requestIsSessionStarted( Executors.newSingleThreadExecutor()).get(3000, TimeUnit.MILLISECONDS)); } catch (InterruptedException | ExecutionException | TimeoutException e) { Log.e(TAG, "Error to get satellite status : " + e); diff --git a/src/com/android/settings/bluetooth/BluetoothPairingDetail.java b/src/com/android/settings/bluetooth/BluetoothPairingDetail.java index ca538540807..ebb07bdca6e 100644 --- a/src/com/android/settings/bluetooth/BluetoothPairingDetail.java +++ b/src/com/android/settings/bluetooth/BluetoothPairingDetail.java @@ -79,7 +79,7 @@ public class BluetoothPairingDetail extends BluetoothDevicePairingDetailBase imp boolean isSatelliteOn = true; try { isSatelliteOn = - satelliteRepository.requestIsEnabled( + satelliteRepository.requestIsSessionStarted( Executors.newSingleThreadExecutor()).get(3000, TimeUnit.MILLISECONDS); } catch (InterruptedException | ExecutionException | TimeoutException e) { Log.e(TAG, "Error to get satellite status : " + e); diff --git a/src/com/android/settings/network/AirplaneModePreferenceController.java b/src/com/android/settings/network/AirplaneModePreferenceController.java index b1f6e5052ff..d4bd4a369c0 100644 --- a/src/com/android/settings/network/AirplaneModePreferenceController.java +++ b/src/com/android/settings/network/AirplaneModePreferenceController.java @@ -162,7 +162,8 @@ public class AirplaneModePreferenceController extends TogglePreferenceController public void onResume() { try { mIsSatelliteOn.set( - mSatelliteRepository.requestIsEnabled(Executors.newSingleThreadExecutor()) + mSatelliteRepository + .requestIsSessionStarted(Executors.newSingleThreadExecutor()) .get(2000, TimeUnit.MILLISECONDS)); } catch (ExecutionException | TimeoutException | InterruptedException e) { Log.e(TAG, "Error to get satellite status : " + e); diff --git a/src/com/android/settings/network/SatelliteRepository.kt b/src/com/android/settings/network/SatelliteRepository.kt index 565fbf349d4..b7c25f4f658 100644 --- a/src/com/android/settings/network/SatelliteRepository.kt +++ b/src/com/android/settings/network/SatelliteRepository.kt @@ -25,6 +25,7 @@ import androidx.annotation.VisibleForTesting import androidx.concurrent.futures.CallbackToFutureAdapter import com.google.common.util.concurrent.Futures.immediateFuture import com.google.common.util.concurrent.ListenableFuture +import java.util.concurrent.Executor import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.asExecutor @@ -32,7 +33,6 @@ import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.callbackFlow import kotlinx.coroutines.flow.flowOf -import java.util.concurrent.Executor import kotlinx.coroutines.flow.flowOn /** @@ -58,20 +58,26 @@ class SatelliteRepository( } return CallbackToFutureAdapter.getFuture { completer -> - satelliteManager.requestIsEnabled(executor, - object : OutcomeReceiver { - override fun onResult(result: Boolean) { - Log.i(TAG, "Satellite modem enabled status: $result") - completer.set(result) - } + try { + satelliteManager.requestIsEnabled(executor, + object : OutcomeReceiver { + override fun onResult(result: Boolean) { + Log.i(TAG, "Satellite modem enabled status: $result") + completer.set(result) + } + + override fun onError(error: SatelliteManager.SatelliteException) { + super.onError(error) + Log.w(TAG, "Can't get satellite modem enabled status", error) + completer.set(false) + } + }) + "requestIsEnabled" + } catch (e: IllegalStateException) { + Log.w(TAG, "IllegalStateException $e") + completer.set(false) + } - override fun onError(error: SatelliteManager.SatelliteException) { - super.onError(error) - Log.w(TAG, "Can't get satellite modem enabled status", error) - completer.set(false) - } - }) - "requestIsEnabled" } } @@ -96,14 +102,21 @@ class SatelliteRepository( val callback = object : SatelliteModemStateCallback { override fun onSatelliteModemStateChanged(state: Int) { val isSessionStarted = isSatelliteSessionStarted(state) - Log.i(TAG, "Satellite modem state changed: state=$state" - + ", isSessionStarted=$isSessionStarted") + Log.i( + TAG, "Satellite modem state changed: state=$state" + + ", isSessionStarted=$isSessionStarted" + ) completer.set(isSessionStarted) satelliteManager.unregisterForModemStateChanged(this) } } - val registerResult = satelliteManager.registerForModemStateChanged(executor, callback) + var registerResult = SatelliteManager.SATELLITE_MODEM_STATE_UNKNOWN + try { + registerResult = satelliteManager.registerForModemStateChanged(executor, callback) + } catch (e: IllegalStateException) { + Log.w(TAG, "IllegalStateException $e") + } if (registerResult != SatelliteManager.SATELLITE_RESULT_SUCCESS) { Log.w(TAG, "Failed to register for satellite modem state change: $registerResult") completer.set(false) @@ -132,15 +145,21 @@ class SatelliteRepository( return callbackFlow { val callback = SatelliteModemStateCallback { state -> val isSessionStarted = isSatelliteSessionStarted(state) - Log.i(TAG, "Satellite modem state changed: state=$state" - + ", isSessionStarted=$isSessionStarted") + Log.i( + TAG, "Satellite modem state changed: state=$state" + + ", isSessionStarted=$isSessionStarted" + ) trySend(isSessionStarted) } - - val registerResult = satelliteManager.registerForModemStateChanged( - defaultDispatcher.asExecutor(), - callback - ) + var registerResult: Int = SatelliteManager.SATELLITE_RESULT_ERROR + try { + registerResult = satelliteManager.registerForModemStateChanged( + defaultDispatcher.asExecutor(), + callback + ) + } catch (e: IllegalStateException) { + Log.w(TAG, "IllegalStateException $e") + } if (registerResult != SatelliteManager.SATELLITE_RESULT_SUCCESS) { // If the registration failed (e.g., device doesn't support satellite), @@ -150,7 +169,13 @@ class SatelliteRepository( trySend(false) } - awaitClose { satelliteManager.unregisterForModemStateChanged(callback) } + awaitClose { + try { + satelliteManager.unregisterForModemStateChanged(callback) + } catch (e: IllegalStateException) { + Log.w(TAG, "IllegalStateException $e") + } + } }.flowOn(Dispatchers.Default) } diff --git a/src/com/android/settings/wifi/WifiEnabler.java b/src/com/android/settings/wifi/WifiEnabler.java index d1cf7d679cd..bbb014bf34d 100644 --- a/src/com/android/settings/wifi/WifiEnabler.java +++ b/src/com/android/settings/wifi/WifiEnabler.java @@ -139,7 +139,8 @@ public class WifiEnabler implements SwitchWidgetController.OnSwitchChangeListene // Refresh satellite mode status. try { mIsSatelliteOn.set( - mSatelliteRepository.requestIsEnabled(Executors.newSingleThreadExecutor()) + mSatelliteRepository + .requestIsSessionStarted(Executors.newSingleThreadExecutor()) .get(2000, TimeUnit.MILLISECONDS)); } catch (ExecutionException | TimeoutException | InterruptedException e) { Log.e(TAG, "Error to get satellite status : " + e); diff --git a/src/com/android/settings/wifi/slice/WifiSlice.java b/src/com/android/settings/wifi/slice/WifiSlice.java index ff448a86692..3bb50d35817 100644 --- a/src/com/android/settings/wifi/slice/WifiSlice.java +++ b/src/com/android/settings/wifi/slice/WifiSlice.java @@ -431,7 +431,7 @@ public class WifiSlice implements CustomSliceable { boolean isSatelliteOn = false; try { isSatelliteOn = - satelliteRepository.requestIsEnabled(Executors.newSingleThreadExecutor()) + satelliteRepository.requestIsSessionStarted(Executors.newSingleThreadExecutor()) .get(2000, TimeUnit.MILLISECONDS); } catch (ExecutionException | TimeoutException | InterruptedException e) { Log.e(TAG, "Error to get satellite status : " + e); diff --git a/tests/robotests/src/com/android/settings/network/SatelliteRepositoryTest.kt b/tests/robotests/src/com/android/settings/network/SatelliteRepositoryTest.kt index 62fd10a951c..619d290f0c4 100644 --- a/tests/robotests/src/com/android/settings/network/SatelliteRepositoryTest.kt +++ b/tests/robotests/src/com/android/settings/network/SatelliteRepositoryTest.kt @@ -91,7 +91,8 @@ class SatelliteRepositoryTest { @Test fun requestIsSessionStarted_resultIsTrue() = runBlocking { - `when`(mockSatelliteManager.registerForModemStateChanged(any(), any()) + `when`( + mockSatelliteManager.registerForModemStateChanged(any(), any()) ).thenAnswer { invocation -> val callback = invocation.getArgument(1) callback.onSatelliteModemStateChanged(SatelliteManager.SATELLITE_MODEM_STATE_CONNECTED) @@ -105,7 +106,8 @@ class SatelliteRepositoryTest { @Test fun requestIsSessionStarted_resultIsFalse() = runBlocking { - `when`(mockSatelliteManager.registerForModemStateChanged(any(), any()) + `when`( + mockSatelliteManager.registerForModemStateChanged(any(), any()) ).thenAnswer { invocation -> val callback = invocation.getArgument(1) callback.onSatelliteModemStateChanged(SatelliteManager.SATELLITE_MODEM_STATE_OFF) @@ -119,7 +121,8 @@ class SatelliteRepositoryTest { @Test fun requestIsSessionStarted_registerFailed() = runBlocking { - `when`(mockSatelliteManager.registerForModemStateChanged(any(), any()) + `when`( + mockSatelliteManager.registerForModemStateChanged(any(), any()) ).thenAnswer { SatelliteManager.SATELLITE_RESULT_ERROR } @@ -129,6 +132,17 @@ class SatelliteRepositoryTest { verify(mockSatelliteManager, never()).unregisterForModemStateChanged(any()) } + @Test + fun requestIsSessionStarted_phoneCrash_registerFailed() = runBlocking { + `when`( + mockSatelliteManager.registerForModemStateChanged(any(), any()) + ).thenThrow(IllegalStateException("Telephony is null")) + + val result: ListenableFuture = repository.requestIsSessionStarted(mockExecutor) + assertFalse(result.get()) + verify(mockSatelliteManager, never()).unregisterForModemStateChanged(any()) + } + @Test fun requestIsSessionStarted_nullSatelliteManager() = runBlocking { `when`(spyContext.getSystemService(SatelliteManager::class.java)).thenReturn(null) @@ -157,6 +171,17 @@ class SatelliteRepositoryTest { assertFalse(result.get()) } + @Test + fun requestIsEnabled_phoneCrash_resultIsFalse() = runBlocking { + `when`( + mockSatelliteManager.requestIsEnabled(any(), any()) + ).thenThrow(IllegalStateException("Telephony is null")) + + val result: ListenableFuture = + repository.requestIsEnabled(mockExecutor) + assertFalse(result.get()) + } + @Test fun requestIsEnabled_exceptionFailure() = runBlocking { @@ -232,7 +257,8 @@ class SatelliteRepositoryTest { @Test fun getIsSessionStartedFlow_registerFailed() = runBlocking { - `when`(mockSatelliteManager.registerForModemStateChanged(any(), any()) + `when`( + mockSatelliteManager.registerForModemStateChanged(any(), any()) ).thenAnswer { SatelliteManager.SATELLITE_RESULT_ERROR }