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

Commit 664e4158 authored by Chaohui Wang's avatar Chaohui Wang
Browse files

Introduce configurable dispatcher in SpaEnvironment

SPA components now use a default dispatcher provided by
`SpaEnvironment.defaultDispatcher`. This allows for easier
customization and testing. `RestrictedBaseSwitchPreference`,
`BroadcastReceiverFlow`, and `RestrictedSwitchPreferencePresenter`
have been updated to use this new dispatcher via `SpaDispatchers`.

Other changes:
- Make SpaIntent.KEY_DESTINATION public by removing @VisibleForTesting
- Update test target SDK to 36.
- Explicit ignore `trySend` return value in `BroadcastReceiverFlow`
  by assigning return value to unused.

Bug: 422439682
Flag: EXEMPT refactor
Test: unit tests still pase
Change-Id: Ifc97830b0d863454645707fe61e2e0720bbfb503
parent 3f3b78ce
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -31,6 +31,10 @@ android {
        minSdk = 23
    }

    testOptions {
        targetSdk = 36
    }

    sourceSets.getByName("main") {
        kotlin.setSrcDirs(listOf("src"))
        res.setSrcDirs(listOf("res"))
+26 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.settingslib.spa.coroutines

import com.android.settingslib.spa.framework.common.SpaEnvironmentFactory
import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.Dispatchers

object SpaDispatchers {
    val Default: CoroutineContext
        get() = SpaEnvironmentFactory.optionalInstance?.defaultDispatcher ?: Dispatchers.Default
}
+3 −3
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@ import android.content.IntentFilter
import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi
import kotlinx.coroutines.Dispatchers
import com.android.settingslib.spa.coroutines.SpaDispatchers
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
@@ -50,7 +50,7 @@ fun Context.broadcastReceiverFlow(intentFilter: IntentFilter, flags: Int): Flow<
                object : BroadcastReceiver() {
                    override fun onReceive(context: Context, intent: Intent) {
                        Log.d(TAG, "onReceive: $intent")
                        trySend(intent)
                        val unused = trySend(intent)
                    }
                }
            registerReceiver(broadcastReceiver, intentFilter, flags)
@@ -59,4 +59,4 @@ fun Context.broadcastReceiverFlow(intentFilter: IntentFilter, flags: Int): Flow<
        }
        .catch { e -> Log.e(TAG, "Error while broadcastReceiverFlow", e) }
        .conflate()
        .flowOn(Dispatchers.Default)
        .flowOn(SpaDispatchers.Default)
+5 −0
Original line number Diff line number Diff line
@@ -24,6 +24,8 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import com.android.settingslib.spa.framework.util.SystemProperties
import com.android.settingslib.spa.restricted.RestrictedRepository
import kotlinx.coroutines.Dispatchers
import kotlin.coroutines.CoroutineContext

private const val TAG = "SpaEnvironment"

@@ -108,6 +110,9 @@ abstract class SpaEnvironment(context: Context) {
    // Specify provider authorities for debugging purpose.
    open val searchProviderAuthorities: String? = null

    /** Specify default dispatcher. */
    open val defaultDispatcher: CoroutineContext = Dispatchers.Default

    /** Specify whether expressive design is enabled. */
    open val isSpaExpressiveEnabled by lazy {
        SystemProperties.getBoolean("is_expressive_design_enabled", false)
+1 −1
Original line number Diff line number Diff line
@@ -28,7 +28,7 @@ const val SESSION_BROWSE = "browse"
const val SESSION_SEARCH = "search"
const val SESSION_EXTERNAL = "external"

@VisibleForTesting const val KEY_DESTINATION = "spaActivityDestination"
const val KEY_DESTINATION = "spaActivityDestination"
@VisibleForTesting const val KEY_HIGHLIGHT_ITEM_KEY = "highlightKey"
internal const val KEY_SESSION_SOURCE_NAME = "sessionSource"

Loading