From 55c3ebd0851aeea5495a714daa958bd044286735 Mon Sep 17 00:00:00 2001 From: Caren Date: Tue, 29 Nov 2022 12:52:34 -0800 Subject: [PATCH 1/4] Use Chrome Custom tab for opening articles Change-Id: I0024038f918a18d3c3e803587a7e1c159bfb9169 --- core/ui/build.gradle.kts | 1 + .../apps/nowinandroid/core/ui/NewsFeed.kt | 21 +++++++++++++++---- gradle/libs.versions.toml | 2 ++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/core/ui/build.gradle.kts b/core/ui/build.gradle.kts index 794b1573b..c76e4bb27 100644 --- a/core/ui/build.gradle.kts +++ b/core/ui/build.gradle.kts @@ -28,6 +28,7 @@ dependencies { implementation(project(":core:model")) implementation(project(":core:domain")) + implementation(libs.androidx.browser) implementation(libs.androidx.core.ktx) implementation(libs.coil.kt) implementation(libs.coil.kt.compose) diff --git a/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsFeed.kt b/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsFeed.kt index 32308bd63..e72b6286e 100644 --- a/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsFeed.kt +++ b/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsFeed.kt @@ -16,22 +16,25 @@ package com.google.samples.apps.nowinandroid.core.ui -import android.content.Intent +import android.content.Context import android.net.Uri +import androidx.browser.customtabs.CustomTabColorSchemeParams +import androidx.browser.customtabs.CustomTabsIntent import androidx.compose.foundation.lazy.LazyListScope import androidx.compose.foundation.lazy.grid.GridCells import androidx.compose.foundation.lazy.grid.LazyGridScope import androidx.compose.foundation.lazy.grid.LazyVerticalGrid import androidx.compose.foundation.lazy.grid.items +import androidx.compose.material3.MaterialTheme import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember +import androidx.compose.ui.graphics.toArgb import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.tooling.preview.Devices import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp -import androidx.core.content.ContextCompat import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme import com.google.samples.apps.nowinandroid.core.domain.model.SaveableNewsResource import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources @@ -51,13 +54,13 @@ fun LazyGridScope.newsFeed( val resourceUrl by remember { mutableStateOf(Uri.parse(saveableNewsResource.newsResource.url)) } - val launchResourceIntent = Intent(Intent.ACTION_VIEW, resourceUrl) val context = LocalContext.current + val backgroundColor = MaterialTheme.colorScheme.background.toArgb() NewsResourceCardExpanded( newsResource = saveableNewsResource.newsResource, isBookmarked = saveableNewsResource.isSaved, - onClick = { ContextCompat.startActivity(context, launchResourceIntent, null) }, + onClick = { launchCustomChromeTab(context, resourceUrl, backgroundColor) }, onToggleBookmark = { onNewsResourcesCheckedChanged( saveableNewsResource.newsResource.id, @@ -70,6 +73,16 @@ fun LazyGridScope.newsFeed( } } +private fun launchCustomChromeTab(context: Context, uri: Uri, toolbarColor: Int) { + val customTabBarColor = CustomTabColorSchemeParams.Builder() + .setToolbarColor(toolbarColor).build() + val customTabsIntent = CustomTabsIntent.Builder() + .setDefaultColorSchemeParams(customTabBarColor) + .build() + + customTabsIntent.launchUrl(context, uri) +} + /** * A sealed hierarchy describing the state of the feed of news resources. */ diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 5892e277d..6022856fe 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -4,6 +4,7 @@ androidDesugarJdkLibs = "1.2.0" androidGradlePlugin = "7.3.1" androidxActivity = "1.6.1" androidxAppCompat = "1.5.1" +androidxBrowser = "1.4.0" androidxComposeBom = "2022.11.00" androidxComposeCompiler = "1.3.2" androidxComposeRuntimeTracing = "1.0.0-alpha01" @@ -53,6 +54,7 @@ android-desugarJdkLibs = { group = "com.android.tools", name = "desugar_jdk_libs androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "androidxActivity" } androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "androidxAppCompat" } androidx-benchmark-macro = { group = "androidx.benchmark", name = "benchmark-macro-junit4", version.ref = "androidxMacroBenchmark" } +androidx-browser = { group = "androidx.browser", name = "browser", version.ref = "androidxBrowser" } androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "androidxComposeBom" } androidx-compose-foundation = { group = "androidx.compose.foundation", name = "foundation" } androidx-compose-foundation-layout = { group = "androidx.compose.foundation", name = "foundation-layout" } From 45f8e069c56c3e8c7aa89e8de70da4f7221c1a52 Mon Sep 17 00:00:00 2001 From: Caren Date: Fri, 2 Dec 2022 12:35:23 -0800 Subject: [PATCH 2/4] Implement custom chrome tabs in NewsResourceCardList as well Change-Id: Ie5851666117a7c756b3d9ad02d6e17be0d3ba904 --- .../google/samples/apps/nowinandroid/core/ui/NewsFeed.kt | 2 +- .../apps/nowinandroid/core/ui/NewsResourceCardList.kt | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsFeed.kt b/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsFeed.kt index e72b6286e..28c7217ff 100644 --- a/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsFeed.kt +++ b/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsFeed.kt @@ -73,7 +73,7 @@ fun LazyGridScope.newsFeed( } } -private fun launchCustomChromeTab(context: Context, uri: Uri, toolbarColor: Int) { +fun launchCustomChromeTab(context: Context, uri: Uri, toolbarColor: Int) { val customTabBarColor = CustomTabColorSchemeParams.Builder() .setToolbarColor(toolbarColor).build() val customTabsIntent = CustomTabsIntent.Builder() diff --git a/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardList.kt b/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardList.kt index 25a7bd2dc..6c5e7ffcb 100644 --- a/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardList.kt +++ b/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardList.kt @@ -20,7 +20,9 @@ import android.content.Intent import android.net.Uri import androidx.compose.foundation.lazy.LazyListScope import androidx.compose.foundation.lazy.items +import androidx.compose.material3.MaterialTheme import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.toArgb import androidx.compose.ui.platform.LocalContext import androidx.core.content.ContextCompat import com.google.samples.apps.nowinandroid.core.model.data.NewsResource @@ -47,8 +49,8 @@ fun LazyListScope.newsResourceCardItems( key = { newsResourceMapper(it).id }, itemContent = { item -> val newsResource = newsResourceMapper(item) - val launchResourceIntent = - Intent(Intent.ACTION_VIEW, Uri.parse(newsResource.url)) + val resourceUrl = Uri.parse(newsResource.url) + val backgroundColor = MaterialTheme.colorScheme.background.toArgb() val context = LocalContext.current NewsResourceCardExpanded( @@ -57,7 +59,7 @@ fun LazyListScope.newsResourceCardItems( onToggleBookmark = { onToggleBookmark(item) }, onClick = { when (onItemClick) { - null -> ContextCompat.startActivity(context, launchResourceIntent, null) + null -> launchCustomChromeTab(context, resourceUrl, backgroundColor) else -> onItemClick(item) } }, From 94e2bb0ec252bed96e3215a355a542e8ae2e5b5e Mon Sep 17 00:00:00 2001 From: Caren Date: Fri, 2 Dec 2022 12:37:57 -0800 Subject: [PATCH 3/4] Run spotless Change-Id: I1deee3c9b38d352afca0f646366598e32710ffca --- .../samples/apps/nowinandroid/core/ui/NewsResourceCardList.kt | 2 -- 1 file changed, 2 deletions(-) diff --git a/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardList.kt b/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardList.kt index 6c5e7ffcb..c76a8124d 100644 --- a/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardList.kt +++ b/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardList.kt @@ -16,7 +16,6 @@ package com.google.samples.apps.nowinandroid.core.ui -import android.content.Intent import android.net.Uri import androidx.compose.foundation.lazy.LazyListScope import androidx.compose.foundation.lazy.items @@ -24,7 +23,6 @@ import androidx.compose.material3.MaterialTheme import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.toArgb import androidx.compose.ui.platform.LocalContext -import androidx.core.content.ContextCompat import com.google.samples.apps.nowinandroid.core.model.data.NewsResource /** From 1280565466f35617f56bfa93fbaac34df6ab9e0e Mon Sep 17 00:00:00 2001 From: Caren Date: Mon, 5 Dec 2022 17:05:19 -0800 Subject: [PATCH 4/4] Add ColorInt annotation Change-Id: If6ea8e4c3a09c6de932797ddcb64bbfff1e52ede --- .../com/google/samples/apps/nowinandroid/core/ui/NewsFeed.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsFeed.kt b/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsFeed.kt index 28c7217ff..55d84af2f 100644 --- a/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsFeed.kt +++ b/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsFeed.kt @@ -18,6 +18,7 @@ package com.google.samples.apps.nowinandroid.core.ui import android.content.Context import android.net.Uri +import androidx.annotation.ColorInt import androidx.browser.customtabs.CustomTabColorSchemeParams import androidx.browser.customtabs.CustomTabsIntent import androidx.compose.foundation.lazy.LazyListScope @@ -73,7 +74,7 @@ fun LazyGridScope.newsFeed( } } -fun launchCustomChromeTab(context: Context, uri: Uri, toolbarColor: Int) { +fun launchCustomChromeTab(context: Context, uri: Uri, @ColorInt toolbarColor: Int) { val customTabBarColor = CustomTabColorSchemeParams.Builder() .setToolbarColor(toolbarColor).build() val customTabsIntent = CustomTabsIntent.Builder()