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

Commit 28b85b58 authored by Chaohui Wang's avatar Chaohui Wang
Browse files

Refresh "Choose network" summary when service state changes

This summary display service connection state, which should be refreshed
when service state changes.

Fix: 313026209
Test: manual - on Mobile Settings
Test: unit test
Change-Id: I6ca2f89e05f21460a7db055f037919b6ebd19182
parent 72d638e6
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -27,7 +27,11 @@ import java.util.concurrent.Executor;

/**
 * {@link TelephonyCallback} to listen to Allowed Network Types changed
 *
 * @deprecated Please use {@link com.android.settings.network.telephony.AllowedNetworkTypesFlowKt}
 * instead.
 */
@Deprecated
public class AllowedNetworkTypesListener extends TelephonyCallback implements
        TelephonyCallback.AllowedNetworkTypesListener {
    private static final String LOG_TAG = "NetworkModeListener";
+3 −2
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ package com.android.settings.network.helper;
import android.telephony.ServiceState;
import android.telephony.TelephonyCallback;
import android.telephony.TelephonyManager;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
@@ -34,8 +33,10 @@ import java.util.function.Consumer;
 * Only got update when Lifecycle.State is considered as STARTED or RESUMED.
 *
 * {@code null} when status unknown. Other values are {@link ServiceState}.
 *
 * @deprecated Please us {@link com.android.settings.network.telephony.ServiceStateFlowKt} instead.
 */
@VisibleForTesting
@Deprecated
public class ServiceStateStatus extends LiveData<ServiceState> {
    private static final String TAG = "ServiceStateStatus";

+39 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.network.telephony

import android.content.Context
import android.telephony.TelephonyCallback
import android.telephony.TelephonyManager
import android.util.Log
import kotlinx.coroutines.flow.Flow

private const val TAG = "AllowedNetworkTypesFlow"

/** Creates an instance of a cold Flow for Allowed Network Types of given [subId]. */
fun Context.allowedNetworkTypesFlow(subId: Int): Flow<Long> = telephonyCallbackFlow(subId) {
    object : TelephonyCallback(), TelephonyCallback.AllowedNetworkTypesListener {
        override fun onAllowedNetworkTypesChanged(reason: Int, allowedNetworkType: Long) {
            if (reason == TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER ||
                reason == TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_CARRIER
            ) {
                trySend(allowedNetworkType)
                Log.d(TAG, "[$subId] reason: $reason, allowedNetworkType: $allowedNetworkType")
            }
        }
    }
}
+3 −16
Original line number Diff line number Diff line
@@ -18,28 +18,15 @@ package com.android.settings.network.telephony

import android.content.Context
import android.telephony.TelephonyCallback
import android.telephony.TelephonyManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.asExecutor
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.flowOn

/**
 * Flow for call state.
 */
fun Context.callStateFlow(subId: Int): Flow<Int> = callbackFlow {
    val telephonyManager = getSystemService(TelephonyManager::class.java)!!
        .createForSubscriptionId(subId)

    val callback = object : TelephonyCallback(), TelephonyCallback.CallStateListener {
fun Context.callStateFlow(subId: Int): Flow<Int> = telephonyCallbackFlow(subId) {
    object : TelephonyCallback(), TelephonyCallback.CallStateListener {
        override fun onCallStateChanged(state: Int) {
            trySend(state)
        }
    }
    telephonyManager.registerTelephonyCallback(Dispatchers.Default.asExecutor(), callback)

    awaitClose { telephonyManager.unregisterTelephonyCallback(callback) }
}.conflate().flowOn(Dispatchers.Default)
}
+35 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.network.telephony

import android.content.Context
import android.telephony.ServiceState
import android.telephony.TelephonyCallback
import android.util.Log
import kotlinx.coroutines.flow.Flow

private const val TAG = "ServiceStateFlow"

/** Creates an instance of a cold Flow for [ServiceState] of given [subId]. */
fun Context.serviceStateFlow(subId: Int): Flow<ServiceState> = telephonyCallbackFlow(subId) {
    object : TelephonyCallback(), TelephonyCallback.ServiceStateListener {
        override fun onServiceStateChanged(serviceState: ServiceState) {
            trySend(serviceState)
            Log.d(TAG, "[$subId] serviceState: $serviceState")
        }
    }
}
Loading