Skip to content

Commit

Permalink
feat: Use new SplashScreen API on Android 12 and newer (API level 31 …
Browse files Browse the repository at this point in the history
…and up) (#4871)

This PR implements the new SplashScreen API on Android 12 and newer,
provides backwards compatibility using the Splash Screen compat library
(on API < 31, the old splash screen is shown). The implementation works
both with light and dark themes.

Tested on API 29 (backwards compatible) and API 32 (uses the new API).
  • Loading branch information
fogleins committed Nov 8, 2022
1 parent 871af2e commit 1be5da9
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ dependencies {
// ShowCaseView dependency
implementation(libs.showcaseview)

// SplashScreen on API 31 and higher
implementation("androidx.core:core-splashscreen:1.0.0")

// Unit Testing
testImplementation(libs.bundles.testing)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package openfoodfacts.github.scrachx.openfood.features.splash

import android.annotation.SuppressLint
import android.content.pm.ActivityInfo
import android.os.Build
import android.os.Bundle
import androidx.activity.viewModels
import androidx.core.splashscreen.SplashScreen
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import com.google.android.material.snackbar.BaseTransientBottomBar
Expand Down Expand Up @@ -32,7 +35,12 @@ class SplashActivity : BaseActivity() {
@SuppressLint("SourceLockedOrientationActivity")
@ExperimentalTime
public override fun onCreate(savedInstanceState: Bundle?) {
val splashScreen: SplashScreen? = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
installSplashScreen()
} else null
splashScreen?.setKeepOnScreenCondition { true }
super.onCreate(savedInstanceState)

binding = ActivitySplashBinding.inflate(layoutInflater)
setContentView(binding.root)

Expand Down
58 changes: 58 additions & 0 deletions app/src/main/res/values-night-v31/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">

<style name="OFFTheme" parent="Theme.MaterialComponents.DayNight.Bridge">
<!--<item name="colorPrimary">@color/material_blue_500</item>-->
<!--<item name="statusBarColor">@color/grey_300</item>-->
<!--<item name="colorBackground">@color/grey_50</item>-->
<item name="colorAccent">@color/brand_red</item>
<item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>
<item name="searchViewStyle">@style/customSearchView</item>

<item name="android:textColorLink">@color/brand_light_blue</item>

<!-- Material drawer -->
<item name="material_drawer_background">@color/material_drawer_dark_background</item>
<item name="material_drawer_primary_text">@color/material_drawer_dark_primary_text</item>
<item name="material_drawer_primary_icon">@color/material_drawer_dark_primary_icon</item>
<item name="material_drawer_secondary_text">@color/material_drawer_dark_secondary_text</item>
<item name="material_drawer_hint_text">@color/material_drawer_dark_hint_text</item>
<item name="material_drawer_divider">@color/material_drawer_dark_divider</item>
<item name="material_drawer_selected">@color/material_drawer_dark_selected</item> <!-- Material 2 defines 12% alpha, primary color -->
<item name="material_drawer_selected_legacy">@color/material_drawer_dark_selected
</item> <!-- Defines the color if legacy style (Material 1, is enabled) -->
<item name="material_drawer_selected_text">@color/material_drawer_dark_selected_text</item>
<item name="material_drawer_header_selection_text">@color/material_drawer_dark_header_selection_text</item>
<item name="material_drawer_header_selection_subtext">@color/material_drawer_header_selection_subtext</item>
<item name="material_drawer_legacy_style">false</item> <!-- Enables legacy Material 1 style -->
</style>

<style name="ButtonBorder" parent="ButtonRounded">
<item name="android:background">@drawable/rounded_button_shape</item>
<item name="android:textColor">@color/brand_light_blue</item>
<item name="android:textStyle">bold</item>
<item name="android:paddingLeft">@dimen/spacing_small</item>
<item name="android:paddingRight">@dimen/spacing_small</item>
<item name="android:paddingTop">@dimen/spacing_tiny</item>
<item name="android:paddingBottom">@dimen/spacing_tiny</item>
<item name="android:shadowColor">@android:color/transparent</item>
</style>

<style name="PreferenceThemeOverlay.v14" tools:override="true">
<item name="android:textColor">?android:textColorPrimary</item>
<item name="android:textColorSecondary">@color/grey_500</item>
</style>

<style name="BrandedFloatingActionButton" parent="Widget.Design.FloatingActionButton">
<item name="backgroundTint">@color/brand_light_blue</item>
<item name="android:tint">@color/grey_200</item>
</style>

<style name="SplashTheme" parent="Theme.SplashScreen.IconBackground">
<item name="windowSplashScreenBackground">@color/grey_800</item>
<item name="windowSplashScreenIconBackgroundColor">@color/grey_400</item>
<item name="postSplashScreenTheme">@style/Theme.AppCompat.DayNight</item>
<item name="windowSplashScreenAnimatedIcon">@mipmap/ic_launcher_round</item>
</style>

</resources>
8 changes: 8 additions & 0 deletions app/src/main/res/values-night/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,12 @@
<item name="backgroundTint">@color/brand_light_blue</item>
<item name="android:tint">@color/grey_200</item>
</style>

<style name="SplashTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>

</resources>

0 comments on commit 1be5da9

Please sign in to comment.