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

Commit 86f0bde3 authored by Yuchen Sun's avatar Yuchen Sun Committed by Android (Google) Code Review
Browse files

Merge "Remove Slice from SettingsEntry." into main

parents 721c657e 509ea425
Loading
Loading
Loading
Loading
+0 −58
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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.framework.common

import androidx.lifecycle.LiveData
import androidx.slice.Slice
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch

open class EntrySliceData : LiveData<Slice?>() {
    private val asyncRunnerScope = CoroutineScope(Dispatchers.IO)
    private var asyncRunnerJob: Job? = null
    private var asyncActionJob: Job? = null
    private var isActive = false

    open suspend fun asyncRunner() {}

    open suspend fun asyncAction() {}

    override fun onActive() {
        asyncRunnerJob?.cancel()
        asyncRunnerJob = asyncRunnerScope.launch { asyncRunner() }
        isActive = true
    }

    override fun onInactive() {
        asyncRunnerJob?.cancel()
        asyncRunnerJob = null
        asyncActionJob?.cancel()
        asyncActionJob = null
        isActive = false
    }

    fun isActive(): Boolean {
        return isActive
    }

    fun doAction() {
        asyncActionJob?.cancel()
        asyncActionJob = asyncRunnerScope.launch { asyncAction() }
    }
}
+0 −15
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@

package com.android.settingslib.spa.framework.common

import android.net.Uri
import android.os.Bundle
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
@@ -42,7 +41,6 @@ val LocalEntryDataProvider =
typealias UiLayerRenderer = @Composable (arguments: Bundle?) -> Unit
typealias StatusDataGetter = (arguments: Bundle?) -> EntryStatusData?
typealias SearchDataGetter = (arguments: Bundle?) -> EntrySearchData?
typealias SliceDataGetter = (sliceUri: Uri, arguments: Bundle?) -> EntrySliceData?

/**
 * Defines data of a Settings entry.
@@ -80,9 +78,6 @@ data class SettingsEntry(
    // If so, for instance, we'll reindex its status for search.
    val hasMutableStatus: Boolean = false,

    // Indicate whether the entry has SliceProvider support.
    val hasSliceSupport: Boolean = false,

    /**
     * ========================================
     * Defines entry APIs to get data here.
@@ -101,12 +96,6 @@ data class SettingsEntry(
     */
    private val searchDataImpl: SearchDataGetter = { null },

    /**
     * API to get Slice data of this entry. The Slice data is implemented as a LiveData,
     * and is associated with the Slice's lifecycle (pin / unpin) by the framework.
     */
    private val sliceDataImpl: SliceDataGetter = { _: Uri, _: Bundle? -> null },

    /**
     * API to Render UI of this entry directly. For now, we use it in the internal injection, to
     * support the case that the injection page owner wants to maintain both data and UI of the
@@ -137,10 +126,6 @@ data class SettingsEntry(
        return searchDataImpl(fullArgument(runtimeArguments))
    }

    fun getSliceData(sliceUri: Uri, runtimeArguments: Bundle? = null): EntrySliceData? {
        return sliceDataImpl(sliceUri, fullArgument(runtimeArguments))
    }

    @Composable
    fun UiLayout(runtimeArguments: Bundle? = null) {
        val arguments = remember { fullArgument(runtimeArguments) }
+0 −11
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@

package com.android.settingslib.spa.framework.common

import android.net.Uri
import android.os.Bundle
import androidx.compose.runtime.remember
import com.android.settingslib.spa.framework.util.genEntryId
@@ -36,13 +35,11 @@ class SettingsEntryBuilder(private val name: String, private val owner: Settings
    private var isAllowSearch: Boolean = false
    private var isSearchDataDynamic: Boolean = false
    private var hasMutableStatus: Boolean = false
    private var hasSliceSupport: Boolean = false

    // Functions
    private var uiLayoutFn: UiLayerRenderer = { }
    private var statusDataFn: StatusDataGetter = { null }
    private var searchDataFn: SearchDataGetter = { null }
    private var sliceDataFn: SliceDataGetter = { _: Uri, _: Bundle? -> null }

    fun build(): SettingsEntry {
        val page = fromPage ?: owner
@@ -62,12 +59,10 @@ class SettingsEntryBuilder(private val name: String, private val owner: Settings
            isAllowSearch = isEnabled && isAllowSearch,
            isSearchDataDynamic = isSearchDataDynamic,
            hasMutableStatus = hasMutableStatus,
            hasSliceSupport = isEnabled && hasSliceSupport,

            // functions
            statusDataImpl = statusDataFn,
            searchDataImpl = searchDataFn,
            sliceDataImpl = sliceDataFn,
            uiLayoutImpl = uiLayoutFn,
        )
    }
@@ -123,12 +118,6 @@ class SettingsEntryBuilder(private val name: String, private val owner: Settings
        return this
    }

    fun setSliceDataFn(fn: SliceDataGetter): SettingsEntryBuilder {
        this.sliceDataFn = fn
        this.hasSliceSupport = true
        return this
    }

    fun setUiLayoutFn(fn: UiLayerRenderer): SettingsEntryBuilder {
        this.uiLayoutFn = fn
        return this
+1 −1
Original line number Diff line number Diff line
@@ -74,7 +74,7 @@ abstract class SpaEnvironment(context: Context) {
    // Set your SpaLogger implementation, for any SPA events logging.
    open val logger: SpaLogger = object : SpaLogger {}

    // Specify class name of browse activity and slice broadcast receiver, which is used to
    // Specify class name of browse activity, which is used to
    // generate the necessary intents.
    open val browseActivityClass: Class<out Activity>? = null

+0 −49
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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.slice

import android.net.Uri
import android.os.Bundle
import com.android.settingslib.spa.framework.util.KEY_DESTINATION
import com.android.settingslib.spa.framework.util.KEY_HIGHLIGHT_ENTRY

// Defines SliceUri, which contains special query parameters:
//  -- KEY_DESTINATION: The route that this slice is navigated to.
//  -- KEY_HIGHLIGHT_ENTRY: The entry id of this slice
//  Other parameters can considered as runtime parameters.
// Use {entryId, runtimeParams} as the unique Id of this Slice.
typealias SliceUri = Uri

fun SliceUri.getEntryId(): String? {
    return getQueryParameter(KEY_HIGHLIGHT_ENTRY)
}

fun Uri.Builder.appendSpaParams(
    destination: String? = null,
    entryId: String? = null,
    runtimeArguments: Bundle? = null
): Uri.Builder {
    if (destination != null) appendQueryParameter(KEY_DESTINATION, destination)
    if (entryId != null) appendQueryParameter(KEY_HIGHLIGHT_ENTRY, entryId)
    if (runtimeArguments != null) {
        for (key in runtimeArguments.keySet()) {
            appendQueryParameter(key, runtimeArguments.getString(key, ""))
        }
    }
    return this
}
Loading