Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit 9beb208

Browse files
committed
Implement App Startup
1 parent 6e6963a commit 9beb208

File tree

8 files changed

+129
-29
lines changed

8 files changed

+129
-29
lines changed

buildSrc/src/main/java/Libs.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ object Libs {
1818
const val ACTIVITY_COMPOSE = "androidx.activity:activity-compose"
1919
const val ACTIVITY_KTX = "androidx.activity:activity-ktx"
2020
const val APPCOMPAT = "androidx.appcompat:appcompat"
21+
const val APP_STARTUP = "androidx.startup:startup-runtime"
2122
const val ARCH_TESTING = "androidx.arch.core:core-testing"
2223
const val ARCORE = "com.google.ar:core"
2324
const val BENCHMARK = "androidx.benchmark:benchmark-junit4"

depconstraints/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ plugins {
2222
val appcompat = "1.1.0"
2323
val activity = "1.2.0-rc01"
2424
val activityCompose = "1.3.0-alpha03"
25+
val appStartup = "1.1.0-beta01"
2526
val cardview = "1.0.0"
2627
val archTesting = "2.0.0"
2728
val arcore = "1.7.0"
@@ -76,6 +77,7 @@ dependencies {
7677
api("${Libs.ACTIVITY_COMPOSE}:$activityCompose")
7778
api("${Libs.ACTIVITY_KTX}:$activity")
7879
api("${Libs.APPCOMPAT}:$appcompat")
80+
api("${Libs.APP_STARTUP}:$appStartup")
7981
api("${Libs.CARDVIEW}:$cardview")
8082
api("${Libs.ARCH_TESTING}:$archTesting")
8183
api("${Libs.ARCORE}:$arcore")

mobile/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ dependencies {
204204
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
205205

206206
implementation(Libs.CORE_KTX)
207+
implementation(Libs.APP_STARTUP)
207208

208209
// UI
209210
implementation(Libs.ACTIVITY_KTX)

mobile/src/main/AndroidManifest.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,23 @@
8787
android:exported="false" />
8888
<receiver android:name=".shared.notifications.CancelNotificationBroadcastReceiver"
8989
android:exported="false" />
90+
91+
<provider
92+
android:name="androidx.startup.InitializationProvider"
93+
android:authorities="${applicationId}.androidx-startup"
94+
android:exported="false"
95+
tools:node="merge">
96+
<meta-data
97+
android:name="com.google.samples.apps.iosched.util.initializers.AndroidThreeTenInitializer"
98+
android:value="androidx.startup" />
99+
<meta-data
100+
android:name="com.google.samples.apps.iosched.util.initializers.StrictModeInitializer"
101+
android:value="androidx.startup" />
102+
<!-- Initalize TimberInitializer after the Application's onCreate is called -->
103+
<meta-data
104+
android:name="com.google.samples.apps.iosched.util.initializers.TimberInitializer"
105+
tools:node="remove" />
106+
</provider>
90107
</application>
91108

92109
</manifest>

mobile/src/main/java/com/google/samples/apps/iosched/MainApplication.kt

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,10 @@
1717
package com.google.samples.apps.iosched
1818

1919
import android.app.Application
20-
import android.os.StrictMode
21-
import android.os.StrictMode.ThreadPolicy.Builder
20+
import androidx.startup.AppInitializer
2221
import com.google.samples.apps.iosched.shared.analytics.AnalyticsHelper
23-
import com.google.samples.apps.iosched.util.CrashlyticsTree
24-
import com.jakewharton.threetenabp.AndroidThreeTen
22+
import com.google.samples.apps.iosched.util.initializers.TimberInitializer
2523
import dagger.hilt.android.HiltAndroidApp
26-
import timber.log.Timber
2724
import javax.inject.Inject
2825

2926
/**
@@ -36,30 +33,8 @@ class MainApplication : Application() {
3633
@Inject lateinit var analyticsHelper: AnalyticsHelper
3734

3835
override fun onCreate() {
39-
// ThreeTenBP for times and dates, called before super to be available for objects
40-
AndroidThreeTen.init(this)
41-
42-
// Enable strict mode before Dagger creates graph
43-
if (BuildConfig.DEBUG) {
44-
enableStrictMode()
45-
}
4636
super.onCreate()
47-
48-
if (BuildConfig.DEBUG) {
49-
Timber.plant(Timber.DebugTree())
50-
} else {
51-
Timber.plant(CrashlyticsTree())
52-
}
53-
}
54-
55-
private fun enableStrictMode() {
56-
StrictMode.setThreadPolicy(
57-
Builder()
58-
.detectDiskReads()
59-
.detectDiskWrites()
60-
.detectNetwork()
61-
.penaltyLog()
62-
.build()
63-
)
37+
AppInitializer.getInstance(this)
38+
.initializeComponent(TimberInitializer::class.java)
6439
}
6540
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.samples.apps.iosched.util.initializers
18+
19+
import android.content.Context
20+
import androidx.startup.Initializer
21+
import com.jakewharton.threetenabp.AndroidThreeTen
22+
23+
class AndroidThreeTenInitializer : Initializer<Unit> {
24+
25+
override fun create(context: Context) {
26+
AndroidThreeTen.init(context)
27+
}
28+
29+
override fun dependencies(): List<Class<out Initializer<*>>> = emptyList()
30+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.samples.apps.iosched.util.initializers
18+
19+
import android.content.Context
20+
import android.os.StrictMode
21+
import android.os.StrictMode.ThreadPolicy
22+
import androidx.startup.Initializer
23+
24+
class StrictModeInitializer : Initializer<Unit> {
25+
26+
override fun create(context: Context) {
27+
StrictMode.setThreadPolicy(
28+
ThreadPolicy.Builder()
29+
.detectDiskReads()
30+
.detectDiskWrites()
31+
.detectNetwork()
32+
.penaltyLog()
33+
.build()
34+
)
35+
}
36+
37+
override fun dependencies(): List<Class<out Initializer<*>>> = emptyList()
38+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.samples.apps.iosched.util.initializers
18+
19+
import android.content.Context
20+
import androidx.startup.Initializer
21+
import com.google.samples.apps.iosched.BuildConfig
22+
import com.google.samples.apps.iosched.util.CrashlyticsTree
23+
import timber.log.Timber
24+
25+
class TimberInitializer : Initializer<Unit> {
26+
27+
override fun create(context: Context) {
28+
if (BuildConfig.DEBUG) {
29+
Timber.plant(Timber.DebugTree())
30+
} else {
31+
Timber.plant(CrashlyticsTree())
32+
}
33+
}
34+
35+
override fun dependencies(): List<Class<out Initializer<*>>> = emptyList()
36+
}

0 commit comments

Comments
 (0)