Merge "[Settings]Check the Satellite modem state instead of enabled state only." into main

This commit is contained in:
Tom Hsu
2024-08-01 05:27:06 +00:00
committed by Android (Google) Code Review
7 changed files with 87 additions and 34 deletions

View File

@@ -132,7 +132,7 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
new Thread(() -> { new Thread(() -> {
try { try {
mIsSatelliteOn.set(mSatelliteRepository.requestIsEnabled( mIsSatelliteOn.set(mSatelliteRepository.requestIsSessionStarted(
Executors.newSingleThreadExecutor()).get(3000, TimeUnit.MILLISECONDS)); Executors.newSingleThreadExecutor()).get(3000, TimeUnit.MILLISECONDS));
} catch (InterruptedException | ExecutionException | TimeoutException e) { } catch (InterruptedException | ExecutionException | TimeoutException e) {
Log.e(TAG, "Error to get satellite status : " + e); Log.e(TAG, "Error to get satellite status : " + e);

View File

@@ -79,7 +79,7 @@ public class BluetoothPairingDetail extends BluetoothDevicePairingDetailBase imp
boolean isSatelliteOn = true; boolean isSatelliteOn = true;
try { try {
isSatelliteOn = isSatelliteOn =
satelliteRepository.requestIsEnabled( satelliteRepository.requestIsSessionStarted(
Executors.newSingleThreadExecutor()).get(3000, TimeUnit.MILLISECONDS); Executors.newSingleThreadExecutor()).get(3000, TimeUnit.MILLISECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) { } catch (InterruptedException | ExecutionException | TimeoutException e) {
Log.e(TAG, "Error to get satellite status : " + e); Log.e(TAG, "Error to get satellite status : " + e);

View File

@@ -162,7 +162,8 @@ public class AirplaneModePreferenceController extends TogglePreferenceController
public void onResume() { public void onResume() {
try { try {
mIsSatelliteOn.set( mIsSatelliteOn.set(
mSatelliteRepository.requestIsEnabled(Executors.newSingleThreadExecutor()) mSatelliteRepository
.requestIsSessionStarted(Executors.newSingleThreadExecutor())
.get(2000, TimeUnit.MILLISECONDS)); .get(2000, TimeUnit.MILLISECONDS));
} catch (ExecutionException | TimeoutException | InterruptedException e) { } catch (ExecutionException | TimeoutException | InterruptedException e) {
Log.e(TAG, "Error to get satellite status : " + e); Log.e(TAG, "Error to get satellite status : " + e);

View File

@@ -25,6 +25,7 @@ import androidx.annotation.VisibleForTesting
import androidx.concurrent.futures.CallbackToFutureAdapter import androidx.concurrent.futures.CallbackToFutureAdapter
import com.google.common.util.concurrent.Futures.immediateFuture import com.google.common.util.concurrent.Futures.immediateFuture
import com.google.common.util.concurrent.ListenableFuture import com.google.common.util.concurrent.ListenableFuture
import java.util.concurrent.Executor
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.asExecutor import kotlinx.coroutines.asExecutor
@@ -32,7 +33,6 @@ import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.flowOf
import java.util.concurrent.Executor
import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.flowOn
/** /**
@@ -58,20 +58,26 @@ class SatelliteRepository(
} }
return CallbackToFutureAdapter.getFuture { completer -> return CallbackToFutureAdapter.getFuture { completer ->
satelliteManager.requestIsEnabled(executor, try {
object : OutcomeReceiver<Boolean, SatelliteManager.SatelliteException> { satelliteManager.requestIsEnabled(executor,
override fun onResult(result: Boolean) { object : OutcomeReceiver<Boolean, SatelliteManager.SatelliteException> {
Log.i(TAG, "Satellite modem enabled status: $result") override fun onResult(result: Boolean) {
completer.set(result) 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 { val callback = object : SatelliteModemStateCallback {
override fun onSatelliteModemStateChanged(state: Int) { override fun onSatelliteModemStateChanged(state: Int) {
val isSessionStarted = isSatelliteSessionStarted(state) val isSessionStarted = isSatelliteSessionStarted(state)
Log.i(TAG, "Satellite modem state changed: state=$state" Log.i(
+ ", isSessionStarted=$isSessionStarted") TAG, "Satellite modem state changed: state=$state"
+ ", isSessionStarted=$isSessionStarted"
)
completer.set(isSessionStarted) completer.set(isSessionStarted)
satelliteManager.unregisterForModemStateChanged(this) 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) { if (registerResult != SatelliteManager.SATELLITE_RESULT_SUCCESS) {
Log.w(TAG, "Failed to register for satellite modem state change: $registerResult") Log.w(TAG, "Failed to register for satellite modem state change: $registerResult")
completer.set(false) completer.set(false)
@@ -132,15 +145,21 @@ class SatelliteRepository(
return callbackFlow { return callbackFlow {
val callback = SatelliteModemStateCallback { state -> val callback = SatelliteModemStateCallback { state ->
val isSessionStarted = isSatelliteSessionStarted(state) val isSessionStarted = isSatelliteSessionStarted(state)
Log.i(TAG, "Satellite modem state changed: state=$state" Log.i(
+ ", isSessionStarted=$isSessionStarted") TAG, "Satellite modem state changed: state=$state"
+ ", isSessionStarted=$isSessionStarted"
)
trySend(isSessionStarted) trySend(isSessionStarted)
} }
var registerResult: Int = SatelliteManager.SATELLITE_RESULT_ERROR
val registerResult = satelliteManager.registerForModemStateChanged( try {
defaultDispatcher.asExecutor(), registerResult = satelliteManager.registerForModemStateChanged(
callback defaultDispatcher.asExecutor(),
) callback
)
} catch (e: IllegalStateException) {
Log.w(TAG, "IllegalStateException $e")
}
if (registerResult != SatelliteManager.SATELLITE_RESULT_SUCCESS) { if (registerResult != SatelliteManager.SATELLITE_RESULT_SUCCESS) {
// If the registration failed (e.g., device doesn't support satellite), // If the registration failed (e.g., device doesn't support satellite),
@@ -150,7 +169,13 @@ class SatelliteRepository(
trySend(false) trySend(false)
} }
awaitClose { satelliteManager.unregisterForModemStateChanged(callback) } awaitClose {
try {
satelliteManager.unregisterForModemStateChanged(callback)
} catch (e: IllegalStateException) {
Log.w(TAG, "IllegalStateException $e")
}
}
}.flowOn(Dispatchers.Default) }.flowOn(Dispatchers.Default)
} }

View File

@@ -139,7 +139,8 @@ public class WifiEnabler implements SwitchWidgetController.OnSwitchChangeListene
// Refresh satellite mode status. // Refresh satellite mode status.
try { try {
mIsSatelliteOn.set( mIsSatelliteOn.set(
mSatelliteRepository.requestIsEnabled(Executors.newSingleThreadExecutor()) mSatelliteRepository
.requestIsSessionStarted(Executors.newSingleThreadExecutor())
.get(2000, TimeUnit.MILLISECONDS)); .get(2000, TimeUnit.MILLISECONDS));
} catch (ExecutionException | TimeoutException | InterruptedException e) { } catch (ExecutionException | TimeoutException | InterruptedException e) {
Log.e(TAG, "Error to get satellite status : " + e); Log.e(TAG, "Error to get satellite status : " + e);

View File

@@ -431,7 +431,7 @@ public class WifiSlice implements CustomSliceable {
boolean isSatelliteOn = false; boolean isSatelliteOn = false;
try { try {
isSatelliteOn = isSatelliteOn =
satelliteRepository.requestIsEnabled(Executors.newSingleThreadExecutor()) satelliteRepository.requestIsSessionStarted(Executors.newSingleThreadExecutor())
.get(2000, TimeUnit.MILLISECONDS); .get(2000, TimeUnit.MILLISECONDS);
} catch (ExecutionException | TimeoutException | InterruptedException e) { } catch (ExecutionException | TimeoutException | InterruptedException e) {
Log.e(TAG, "Error to get satellite status : " + e); Log.e(TAG, "Error to get satellite status : " + e);

View File

@@ -91,7 +91,8 @@ class SatelliteRepositoryTest {
@Test @Test
fun requestIsSessionStarted_resultIsTrue() = runBlocking { fun requestIsSessionStarted_resultIsTrue() = runBlocking {
`when`(mockSatelliteManager.registerForModemStateChanged(any(), any()) `when`(
mockSatelliteManager.registerForModemStateChanged(any(), any())
).thenAnswer { invocation -> ).thenAnswer { invocation ->
val callback = invocation.getArgument<SatelliteModemStateCallback>(1) val callback = invocation.getArgument<SatelliteModemStateCallback>(1)
callback.onSatelliteModemStateChanged(SatelliteManager.SATELLITE_MODEM_STATE_CONNECTED) callback.onSatelliteModemStateChanged(SatelliteManager.SATELLITE_MODEM_STATE_CONNECTED)
@@ -105,7 +106,8 @@ class SatelliteRepositoryTest {
@Test @Test
fun requestIsSessionStarted_resultIsFalse() = runBlocking { fun requestIsSessionStarted_resultIsFalse() = runBlocking {
`when`(mockSatelliteManager.registerForModemStateChanged(any(), any()) `when`(
mockSatelliteManager.registerForModemStateChanged(any(), any())
).thenAnswer { invocation -> ).thenAnswer { invocation ->
val callback = invocation.getArgument<SatelliteModemStateCallback>(1) val callback = invocation.getArgument<SatelliteModemStateCallback>(1)
callback.onSatelliteModemStateChanged(SatelliteManager.SATELLITE_MODEM_STATE_OFF) callback.onSatelliteModemStateChanged(SatelliteManager.SATELLITE_MODEM_STATE_OFF)
@@ -119,7 +121,8 @@ class SatelliteRepositoryTest {
@Test @Test
fun requestIsSessionStarted_registerFailed() = runBlocking { fun requestIsSessionStarted_registerFailed() = runBlocking {
`when`(mockSatelliteManager.registerForModemStateChanged(any(), any()) `when`(
mockSatelliteManager.registerForModemStateChanged(any(), any())
).thenAnswer { ).thenAnswer {
SatelliteManager.SATELLITE_RESULT_ERROR SatelliteManager.SATELLITE_RESULT_ERROR
} }
@@ -129,6 +132,17 @@ class SatelliteRepositoryTest {
verify(mockSatelliteManager, never()).unregisterForModemStateChanged(any()) verify(mockSatelliteManager, never()).unregisterForModemStateChanged(any())
} }
@Test
fun requestIsSessionStarted_phoneCrash_registerFailed() = runBlocking {
`when`(
mockSatelliteManager.registerForModemStateChanged(any(), any())
).thenThrow(IllegalStateException("Telephony is null"))
val result: ListenableFuture<Boolean> = repository.requestIsSessionStarted(mockExecutor)
assertFalse(result.get())
verify(mockSatelliteManager, never()).unregisterForModemStateChanged(any())
}
@Test @Test
fun requestIsSessionStarted_nullSatelliteManager() = runBlocking { fun requestIsSessionStarted_nullSatelliteManager() = runBlocking {
`when`(spyContext.getSystemService(SatelliteManager::class.java)).thenReturn(null) `when`(spyContext.getSystemService(SatelliteManager::class.java)).thenReturn(null)
@@ -157,6 +171,17 @@ class SatelliteRepositoryTest {
assertFalse(result.get()) assertFalse(result.get())
} }
@Test
fun requestIsEnabled_phoneCrash_resultIsFalse() = runBlocking {
`when`(
mockSatelliteManager.requestIsEnabled(any(), any())
).thenThrow(IllegalStateException("Telephony is null"))
val result: ListenableFuture<Boolean> =
repository.requestIsEnabled(mockExecutor)
assertFalse(result.get())
}
@Test @Test
fun requestIsEnabled_exceptionFailure() = runBlocking { fun requestIsEnabled_exceptionFailure() = runBlocking {
@@ -232,7 +257,8 @@ class SatelliteRepositoryTest {
@Test @Test
fun getIsSessionStartedFlow_registerFailed() = runBlocking { fun getIsSessionStartedFlow_registerFailed() = runBlocking {
`when`(mockSatelliteManager.registerForModemStateChanged(any(), any()) `when`(
mockSatelliteManager.registerForModemStateChanged(any(), any())
).thenAnswer { ).thenAnswer {
SatelliteManager.SATELLITE_RESULT_ERROR SatelliteManager.SATELLITE_RESULT_ERROR
} }