Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d26c804a authored by Tom Hsu's avatar Tom Hsu Committed by Android (Google) Code Review
Browse files

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

parents 12469371 90553d91
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -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);
+1 −1
Original line number Diff line number Diff line
@@ -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);
+2 −1
Original line number Diff line number Diff line
@@ -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);
+51 −26
Original line number Diff line number Diff line
@@ -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,6 +58,7 @@ class SatelliteRepository(
        }

        return CallbackToFutureAdapter.getFuture { completer ->
            try {
                satelliteManager.requestIsEnabled(executor,
                    object : OutcomeReceiver<Boolean, SatelliteManager.SatelliteException> {
                        override fun onResult(result: Boolean) {
@@ -72,6 +73,11 @@ class SatelliteRepository(
                        }
                    })
                "requestIsEnabled"
            } catch (e: IllegalStateException) {
                Log.w(TAG, "IllegalStateException $e")
                completer.set(false)
            }

        }
    }

@@ -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(
            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)
    }

+2 −1
Original line number Diff line number Diff line
@@ -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);
Loading