Skip to content

Commit 5c09fe4

Browse files
committed
perf(startup): splash screen
Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
1 parent 50b8438 commit 5c09fe4

2 files changed

Lines changed: 58 additions & 28 deletions

File tree

app/src/androidTest/java/com/nmc/android/ui/LauncherActivityIT.kt

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,55 @@
66
*/
77
package com.nmc.android.ui
88

9+
import android.content.Intent
910
import android.view.View
10-
import androidx.test.core.app.launchActivity
1111
import androidx.test.ext.junit.runners.AndroidJUnit4
12+
import androidx.test.platform.app.InstrumentationRegistry
1213
import com.owncloud.android.AbstractIT
1314
import com.owncloud.android.R
1415
import org.junit.Assert.assertEquals
16+
import org.junit.Assert.assertTrue
1517
import org.junit.Test
1618
import org.junit.runner.RunWith
1719

1820
@RunWith(AndroidJUnit4::class)
1921
class LauncherActivityIT : AbstractIT() {
2022

23+
private val instrumentation get() = InstrumentationRegistry.getInstrumentation()
24+
2125
@Test
2226
fun testSplashScreenWithEmptyTitlesShouldHideTitles() {
23-
launchActivity<LauncherActivity>().onActivity { activity ->
24-
assertEquals(View.VISIBLE, activity.findViewById<View>(R.id.ivSplash).visibility)
25-
assertEquals(View.GONE, activity.findViewById<View>(R.id.splashScreenBold).visibility)
26-
assertEquals(View.GONE, activity.findViewById<View>(R.id.splashScreenNormal).visibility)
27-
}
27+
val activity = launchLauncherActivity()
28+
29+
assertEquals(View.VISIBLE, activity.findViewById<View>(R.id.ivSplash).visibility)
30+
assertEquals(View.GONE, activity.findViewById<View>(R.id.splashScreenBold).visibility)
31+
assertEquals(View.GONE, activity.findViewById<View>(R.id.splashScreenNormal).visibility)
2832
}
2933

3034
@Test
3135
fun testSplashScreenWithTitlesShouldShowTitles() {
32-
launchActivity<LauncherActivity>().onActivity { activity ->
33-
activity.setSplashTitles("Example", "Cloud")
36+
val activity = launchLauncherActivity()
37+
38+
instrumentation.runOnMainSync { activity.setSplashTitles("Example", "Cloud") }
39+
40+
assertEquals(View.VISIBLE, activity.findViewById<View>(R.id.ivSplash).visibility)
41+
assertEquals(View.VISIBLE, activity.findViewById<View>(R.id.splashScreenBold).visibility)
42+
assertEquals(View.VISIBLE, activity.findViewById<View>(R.id.splashScreenNormal).visibility)
43+
}
44+
45+
@Test
46+
fun testSplashScreenWithEmptyTitlesShouldNotDelayNextScreen() {
47+
val activity = launchLauncherActivity()
48+
49+
instrumentation.waitForIdleSync()
50+
51+
assertTrue(activity.isFinishing)
52+
}
53+
54+
private fun launchLauncherActivity(): LauncherActivity {
55+
val intent = Intent(targetContext, LauncherActivity::class.java)
56+
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
3457

35-
assertEquals(View.VISIBLE, activity.findViewById<View>(R.id.ivSplash).visibility)
36-
assertEquals(View.VISIBLE, activity.findViewById<View>(R.id.splashScreenBold).visibility)
37-
assertEquals(View.VISIBLE, activity.findViewById<View>(R.id.splashScreenNormal).visibility)
38-
}
58+
return instrumentation.startActivitySync(intent) as LauncherActivity
3959
}
4060
}

app/src/main/java/com/nmc/android/ui/LauncherActivity.kt

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import android.content.Intent
1212
import android.os.Bundle
1313
import android.os.Handler
1414
import android.os.Looper
15-
import android.text.TextUtils
1615
import android.view.View
1716
import androidx.annotation.VisibleForTesting
1817
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
@@ -25,6 +24,8 @@ import com.owncloud.android.ui.activity.BaseActivity
2524
import com.owncloud.android.ui.activity.FileDisplayActivity
2625
import com.owncloud.android.ui.activity.SettingsActivity
2726
import javax.inject.Inject
27+
import kotlin.time.Duration
28+
import kotlin.time.Duration.Companion.milliseconds
2829

2930
class LauncherActivity : BaseActivity() {
3031

@@ -56,30 +57,39 @@ class LauncherActivity : BaseActivity() {
5657
}
5758

5859
private fun updateTitleVisibility() {
59-
if (TextUtils.isEmpty(resources.getString(R.string.splashScreenBold))) {
60+
if (resources.getString(R.string.splashScreenBold).isEmpty()) {
6061
binding.splashScreenBold.visibility = View.GONE
6162
}
62-
if (TextUtils.isEmpty(resources.getString(R.string.splashScreenNormal))) {
63+
if (resources.getString(R.string.splashScreenNormal).isEmpty()) {
6364
binding.splashScreenNormal.visibility = View.GONE
6465
}
6566
}
6667

68+
private fun hasBrandedTitle(): Boolean = resources.getString(R.string.splashScreenBold).isNotEmpty() ||
69+
resources.getString(R.string.splashScreenNormal).isNotEmpty()
70+
6771
private fun scheduleSplashScreen() {
68-
Handler(Looper.getMainLooper()).postDelayed({
69-
if (user.isPresent) {
70-
if (MDMConfig.enforceProtection(this) && appPreferences.lockPreference == SettingsActivity.LOCK_NONE) {
71-
startActivity(Intent(this, SettingsActivity::class.java))
72-
} else {
73-
startActivity(Intent(this, FileDisplayActivity::class.java))
74-
}
75-
} else {
76-
startActivity(Intent(this, AuthenticatorActivity::class.java))
77-
}
78-
finish()
79-
}, SPLASH_DURATION)
72+
val duration = if (hasBrandedTitle()) SPLASH_DURATION else NO_SPLASH_DURATION
73+
74+
Handler(Looper.getMainLooper()).postDelayed({ openNextScreen() }, duration.inWholeMilliseconds)
75+
}
76+
77+
private fun openNextScreen() {
78+
val nextScreen = when {
79+
!user.isPresent -> AuthenticatorActivity::class.java
80+
81+
MDMConfig.enforceProtection(this) &&
82+
appPreferences.lockPreference == SettingsActivity.LOCK_NONE -> SettingsActivity::class.java
83+
84+
else -> FileDisplayActivity::class.java
85+
}
86+
87+
startActivity(Intent(this, nextScreen))
88+
finish()
8089
}
8190

8291
companion object {
83-
const val SPLASH_DURATION = 1500L
92+
private val SPLASH_DURATION = 1500.milliseconds
93+
private val NO_SPLASH_DURATION = Duration.ZERO
8494
}
8595
}

0 commit comments

Comments
 (0)