Skip to content

Commit

Permalink
Re-implement splash theming
Browse files Browse the repository at this point in the history
- Remove SplashActivity as we can dynamically change the theme in HomeActivity
  This should result in a nice improvement in app startup time.
- Add support fot platform splash themes on API 31+
- Renamed themes to have App namespace
  • Loading branch information
gujjwal00 committed Dec 24, 2023
1 parent 1d02f9b commit 12f235f
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 43 deletions.
12 changes: 5 additions & 7 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:theme="@style/App.Theme">

<activity-alias
android:name=".StartupActivity"
android:exported="true"
android:targetActivity=".ui.home.SplashActivity">
android:targetActivity=".ui.home.HomeActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Expand All @@ -47,10 +47,8 @@
</activity-alias>

<activity
android:name=".ui.home.SplashActivity"
android:theme="@style/SplashTheme" />

<activity android:name=".ui.home.HomeActivity" />
android:name=".ui.home.HomeActivity"
android:theme="@style/App.SplashTheme.Dark" />

<activity
android:name=".ui.home.UrlBarActivity"
Expand All @@ -66,7 +64,7 @@

<activity
android:name=".ui.vnc.IntentReceiverActivity"
android:theme="@style/SplashTheme" />
android:theme="@style/App.SplashTheme.Dark" />

<activity android:name=".ui.prefs.PrefsActivity" />

Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/com/gaurav/avnc/ui/home/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package com.gaurav.avnc.ui.home
import android.app.ActivityOptions
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.Window
Expand Down Expand Up @@ -47,6 +48,7 @@ class HomeActivity : AppCompatActivity() {
private val tabs = ServerTabs(this)

override fun onCreate(savedInstanceState: Bundle?) {
setTheme(R.style.App_Theme)
super.onCreate(savedInstanceState)
window.requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS)

Expand All @@ -69,6 +71,7 @@ class HomeActivity : AppCompatActivity() {
viewModel.discovery.servers.observe(this) { updateDiscoveryBadge(it) }
viewModel.serverProfiles.observe(this) { updateShortcuts(it) }

setupSplashTheme()
showWelcomeMsg()
}

Expand Down Expand Up @@ -182,6 +185,22 @@ class HomeActivity : AppCompatActivity() {
}.isSuccess
}

/**
* Updates splash theme to match with app theme
*/
private fun setupSplashTheme() {
if (Build.VERSION.SDK_INT < 31)
return

viewModel.pref.ui.theme.observe(this) {
when (it) {
"light" -> splashScreen.setSplashScreenTheme(R.style.App_SplashTheme_Light)
"dark" -> splashScreen.setSplashScreenTheme(R.style.App_SplashTheme_Dark)
else -> splashScreen.setSplashScreenTheme(R.style.App_SplashTheme)
}
}
}

/************************************************************************************
* Shortcuts
************************************************************************************/
Expand Down
29 changes: 0 additions & 29 deletions app/src/main/java/com/gaurav/avnc/ui/home/SplashActivity.kt

This file was deleted.

2 changes: 1 addition & 1 deletion app/src/main/res/values-night/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<resources>

<style name="AppThemeBase" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<style name="App.BaseTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- This matches with appbar background at 4dp elevation-->
<item name="android:statusBarColor">#272727</item>
</style>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-v23/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<resources>

<style name="AppThemeBase" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<style name="App.BaseTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="android:windowLightStatusBar">true</item>
<!-- Surface color is White-->
<item name="android:statusBarColor">?colorSurface</item>
Expand Down
25 changes: 20 additions & 5 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,35 @@
<resources xmlns:tools="http://schemas.android.com/tools">

<!--
This is used by startup activities and is intentionally a dark theme.
This way we get a dark window on app start irrespective of the theme chosen by user.
When app is launched, Android shows a temporary window until the app is ready.
The background color for this temporary window is selected from the theme specified
for the activity in app manifest. E.g. it will be white for a Light theme.
If we specify DayNight theme in manifest for startup activity, but user has selected
dark theme in AVNC, they may see a white flash before activity starts drawing. To
workaround this, we use App.SplashTheme.Dark in the manifest to get a dark window,
but switch to main theme in Activity's onCreate().
Since API 31, Android provides SplashScreen API to change splash theme at runtime,
which we use to match the splash theme with user's prefs (see HomeActivity).
Note: Some custom ROMs don't respect/support the splash theme introduced in API 31.
On such devices, we still rely on the above mentioned workaround.
-->
<style name="SplashTheme" parent="Theme.MaterialComponents.NoActionBar" />
<style name="App.SplashTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar" />

<style name="App.SplashTheme.Light" parent="Theme.MaterialComponents.Light.NoActionBar" />

<style name="App.SplashTheme.Dark" parent="Theme.MaterialComponents.NoActionBar" />

<!--
This base theme is used for configuration-specific styling (e.g. night mode, API 23)
-->
<style name="AppThemeBase" parent="Theme.MaterialComponents.DayNight.NoActionBar" />
<style name="App.BaseTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar" />

<!--
This is the main theme.
-->
<style name="AppTheme" parent="AppThemeBase">
<style name="App.Theme" parent="App.BaseTheme">
<item name="appBarLayoutStyle">@style/Widget.MaterialComponents.AppBarLayout.Surface</item>
</style>

Expand Down

0 comments on commit 12f235f

Please sign in to comment.