Merge "Create SpaEnvironment"

This commit is contained in:
Chaohui Wang
2022-09-28 12:14:48 +00:00
committed by Android (Google) Code Review
9 changed files with 58 additions and 41 deletions

View File

@@ -22,7 +22,7 @@ buildscript {
}
}
plugins {
id 'com.android.application' version '7.3.0-rc01' apply false
id 'com.android.library' version '7.3.0-rc01' apply false
id 'com.android.application' version '7.3.0' apply false
id 'com.android.library' version '7.3.0' apply false
id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
}

View File

@@ -18,8 +18,4 @@ package com.android.settingslib.spa.gallery
import com.android.settingslib.spa.framework.DebugActivity
class GalleryDebugActivity : DebugActivity(
SpaEnvironment.EntryRepository,
browseActivityClass = MainActivity::class.java,
entryProviderAuthorities = "com.android.spa.gallery.provider",
)
class GalleryDebugActivity : DebugActivity(GallerySpaEnvironment)

View File

@@ -18,7 +18,4 @@ package com.android.settingslib.spa.gallery
import com.android.settingslib.spa.framework.EntryProvider
class GalleryEntryProvider : EntryProvider(
SpaEnvironment.EntryRepository,
browseActivityClass = MainActivity::class.java,
)
class GalleryEntryProvider : EntryProvider(GallerySpaEnvironment)

View File

@@ -18,9 +18,9 @@ package com.android.settingslib.spa.gallery
import android.os.Bundle
import androidx.navigation.NamedNavArgument
import com.android.settingslib.spa.framework.common.SettingsEntryRepository
import com.android.settingslib.spa.framework.common.SettingsPage
import com.android.settingslib.spa.framework.common.SettingsPageProviderRepository
import com.android.settingslib.spa.framework.common.SpaEnvironment
import com.android.settingslib.spa.gallery.button.ActionButtonPageProvider
import com.android.settingslib.spa.gallery.home.HomePageProvider
import com.android.settingslib.spa.gallery.page.ArgumentPageProvider
@@ -62,9 +62,8 @@ fun createSettingsPage(
)
}
object SpaEnvironment {
val PageProviderRepository: SettingsPageProviderRepository by
lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
object GallerySpaEnvironment : SpaEnvironment() {
override val pageProviderRepository = lazy {
SettingsPageProviderRepository(
allPageProviders = listOf(
HomePageProvider,
@@ -88,9 +87,7 @@ object SpaEnvironment {
)
}
val EntryRepository: SettingsEntryRepository by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
SettingsEntryRepository(PageProviderRepository)
}
override val browseActivityClass = MainActivity::class.java
// TODO: add other environment setup here.
override val entryProviderAuthorities = "com.android.spa.gallery.provider"
}

View File

@@ -18,4 +18,4 @@ package com.android.settingslib.spa.gallery
import com.android.settingslib.spa.framework.BrowseActivity
class MainActivity : BrowseActivity(SpaEnvironment.PageProviderRepository)
class MainActivity : BrowseActivity(GallerySpaEnvironment)

View File

@@ -31,7 +31,7 @@ import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import com.android.settingslib.spa.R
import com.android.settingslib.spa.framework.common.SettingsPageProviderRepository
import com.android.settingslib.spa.framework.common.SpaEnvironment
import com.android.settingslib.spa.framework.compose.localNavController
import com.android.settingslib.spa.framework.theme.SettingsTheme
import com.android.settingslib.spa.framework.util.navRoute
@@ -48,9 +48,9 @@ const val NULL_PAGE_NAME = "NULL"
* $ adb shell am start -n <BrowseActivityComponent> -e spa:SpaActivity:destination HOME
* $ adb shell am start -n <BrowseActivityComponent> -e spa:SpaActivity:destination ARGUMENT/bar/5
*/
open class BrowseActivity(
private val sppRepository: SettingsPageProviderRepository,
) : ComponentActivity() {
open class BrowseActivity(spaEnvironment: SpaEnvironment) : ComponentActivity() {
private val sppRepository by spaEnvironment.pageProviderRepository
override fun onCreate(savedInstanceState: Bundle?) {
setTheme(R.style.Theme_SpaLib_DayNight)
super.onCreate(savedInstanceState)

View File

@@ -35,8 +35,8 @@ import androidx.navigation.navArgument
import com.android.settingslib.spa.R
import com.android.settingslib.spa.framework.BrowseActivity.Companion.KEY_DESTINATION
import com.android.settingslib.spa.framework.common.SettingsEntry
import com.android.settingslib.spa.framework.common.SettingsEntryRepository
import com.android.settingslib.spa.framework.common.SettingsPage
import com.android.settingslib.spa.framework.common.SpaEnvironment
import com.android.settingslib.spa.framework.compose.localNavController
import com.android.settingslib.spa.framework.compose.navigator
import com.android.settingslib.spa.framework.compose.toState
@@ -61,11 +61,9 @@ private const val PARAM_NAME_ENTRY_ID = "eid"
* For gallery, Activity = com.android.settingslib.spa.gallery/.GalleryDebugActivity
* For SettingsGoogle, Activity = com.android.settings/.spa.SpaDebugActivity
*/
open class DebugActivity(
private val entryRepository: SettingsEntryRepository,
private val browseActivityClass: Class<*>,
private val entryProviderAuthorities: String? = null,
) : ComponentActivity() {
open class DebugActivity(private val spaEnvironment: SpaEnvironment) : ComponentActivity() {
private val entryRepository by spaEnvironment.entryRepository
override fun onCreate(savedInstanceState: Bundle?) {
setTheme(R.style.Theme_SpaLib_DayNight)
super.onCreate(savedInstanceState)
@@ -79,7 +77,7 @@ open class DebugActivity(
}
private fun displayDebugMessage() {
if (entryProviderAuthorities == null) return
val entryProviderAuthorities = spaEnvironment.entryProviderAuthorities ?: return
try {
val query = EntryProvider.QueryEnum.PAGE_INFO_QUERY
@@ -216,7 +214,7 @@ open class DebugActivity(
if (page.hasRuntimeParam()) return null
val context = LocalContext.current
val route = page.buildRoute()
val intent = Intent(context, browseActivityClass).apply {
val intent = Intent(context, spaEnvironment.browseActivityClass).apply {
putExtra(KEY_DESTINATION, route)
}
return {
@@ -230,7 +228,7 @@ open class DebugActivity(
if (entry.hasRuntimeParam()) return null
val context = LocalContext.current
val route = entry.buildRoute()
val intent = Intent(context, browseActivityClass).apply {
val intent = Intent(context, spaEnvironment.browseActivityClass).apply {
putExtra(KEY_DESTINATION, route)
}
return {

View File

@@ -28,8 +28,8 @@ import android.database.Cursor
import android.database.MatrixCursor
import android.net.Uri
import android.util.Log
import com.android.settingslib.spa.framework.common.SettingsEntryRepository
import com.android.settingslib.spa.framework.common.SettingsPage
import com.android.settingslib.spa.framework.common.SpaEnvironment
/**
* The content provider to return entry related data, which can be used for search and hierarchy.
@@ -42,10 +42,9 @@ import com.android.settingslib.spa.framework.common.SettingsPage
* $ adb shell content query --uri content://<AuthorityPath>/page_info
* $ adb shell content query --uri content://<AuthorityPath>/entry_info
*/
open class EntryProvider(
private val entryRepository: SettingsEntryRepository,
private val browseActivityClass: Class<*>? = null,
) : ContentProvider() {
open class EntryProvider(spaEnvironment: SpaEnvironment) : ContentProvider() {
private val entryRepository by spaEnvironment.entryRepository
private val browseActivityClass = spaEnvironment.browseActivityClass
/**
* Enum to define all column names in provider.
@@ -220,7 +219,7 @@ open class EntryProvider(
}
private fun createBrowsePageIntent(page: SettingsPage): Intent {
if (context == null || browseActivityClass == null || page.hasRuntimeParam())
if (context == null || page.hasRuntimeParam())
return Intent()
return Intent().setComponent(ComponentName(context!!, browseActivityClass)).apply {
@@ -231,8 +230,7 @@ open class EntryProvider(
private fun createBrowsePageAdbCommand(page: SettingsPage): String? {
if (context == null || page.hasRuntimeParam()) return null
val packageName = context!!.packageName
val activityName =
browseActivityClass?.name?.replace(packageName, "") ?: "<browse-activity-class>"
val activityName = browseActivityClass.name.replace(packageName, "")
return "adb shell am start -n $packageName/$activityName" +
" -e ${BrowseActivity.KEY_DESTINATION} ${page.buildRoute()}"
}

View File

@@ -0,0 +1,31 @@
/*
* 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 android.app.Activity
abstract class SpaEnvironment {
abstract val pageProviderRepository: Lazy<SettingsPageProviderRepository>
val entryRepository = lazy { SettingsEntryRepository(pageProviderRepository.value) }
abstract val browseActivityClass: Class<out Activity>
open val entryProviderAuthorities: String? = null
// TODO: add other environment setup here.
}