Skip to content

Commit 14831f9

Browse files
whhonecopybara-github
authored andcommitted
[gallery] add firebase integration
PiperOrigin-RevId: 776739977
1 parent 323124a commit 14831f9

File tree

6 files changed

+50
-1
lines changed

6 files changed

+50
-1
lines changed

Android/src/app/build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
plugins {
1818
alias(libs.plugins.android.application)
19+
// Note: set apply to true to enable google-services (requires google-services.json).
20+
alias(libs.plugins.google.services) apply false
1921
alias(libs.plugins.kotlin.android)
2022
alias(libs.plugins.kotlin.compose)
2123
alias(libs.plugins.kotlin.serialization)
@@ -95,6 +97,8 @@ dependencies {
9597
implementation(libs.protobuf.javalite)
9698
implementation(libs.hilt.android)
9799
implementation(libs.hilt.navigation.compose)
100+
implementation(platform(libs.firebase.bom))
101+
implementation(libs.firebase.analytics)
98102
kapt(libs.hilt.android.compiler)
99103
testImplementation(libs.junit)
100104
androidTestImplementation(libs.androidx.junit)

Android/src/app/src/main/AndroidManifest.xml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
3232
<uses-permission android:name="android.permission.RECORD_AUDIO" />
3333
<uses-permission android:name="android.permission.WAKE_LOCK"/>
34+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
3435

3536
<uses-feature
3637
android:name="android.hardware.camera"
@@ -87,6 +88,20 @@
8788
android:exported="false"
8889
tools:node="merge">
8990
</service>
91+
92+
<!-- For Firebase Analytics. -->
93+
<receiver
94+
android:name="com.google.android.gms.measurement.AppMeasurementReceiver"
95+
android:enabled="true"
96+
android:exported="false" />
97+
<service android:name="com.google.android.gms.measurement.AppMeasurementService"
98+
android:enabled="true"
99+
android:exported="false" />
100+
<service
101+
android:name="com.google.android.gms.measurement.AppMeasurementJobService"
102+
android:enabled="true"
103+
android:exported="false"
104+
android:permission="android.permission.BIND_JOB_SERVICE" />
90105
</application>
91106

92-
</manifest>
107+
</manifest>

Android/src/app/src/main/java/com/google/ai/edge/gallery/GalleryApplication.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import android.app.Application
2020
import com.google.ai.edge.gallery.common.writeLaunchInfo
2121
import com.google.ai.edge.gallery.data.DataStoreRepository
2222
import com.google.ai.edge.gallery.ui.theme.ThemeSettings
23+
import com.google.firebase.FirebaseApp
2324
import dagger.hilt.android.HiltAndroidApp
2425
import javax.inject.Inject
2526

@@ -35,5 +36,7 @@ class GalleryApplication : Application() {
3536

3637
// Load saved theme.
3738
ThemeSettings.themeOverride.value = dataStoreRepository.readTheme()
39+
40+
FirebaseApp.initializeApp(this)
3841
}
3942
}

Android/src/app/src/main/java/com/google/ai/edge/gallery/MainActivity.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package com.google.ai.edge.gallery
1818

1919
import android.os.Build
2020
import android.os.Bundle
21+
import android.util.Log
2122
import android.view.WindowManager
2223
import androidx.activity.ComponentActivity
2324
import androidx.activity.compose.setContent
@@ -27,12 +28,26 @@ import androidx.compose.material3.Surface
2728
import androidx.compose.ui.Modifier
2829
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
2930
import com.google.ai.edge.gallery.ui.theme.GalleryTheme
31+
import com.google.firebase.analytics.FirebaseAnalytics
32+
import com.google.firebase.analytics.ktx.analytics
33+
import com.google.firebase.ktx.Firebase
3034
import dagger.hilt.android.AndroidEntryPoint
3135

3236
@AndroidEntryPoint
3337
class MainActivity : ComponentActivity() {
3438

39+
private var firebaseAnalytics: FirebaseAnalytics? = null
40+
3541
override fun onCreate(savedInstanceState: Bundle?) {
42+
firebaseAnalytics =
43+
runCatching { Firebase.analytics }
44+
.onFailure { exception ->
45+
// Firebase.analytics can throw an exception if goolgle-services is not set up, e.g.,
46+
// missing google-services.json.
47+
Log.w(TAG, "Firebase Analytics is not available", exception)
48+
}
49+
.getOrNull()
50+
3651
installSplashScreen()
3752

3853
super.onCreate(savedInstanceState)
@@ -46,4 +61,8 @@ class MainActivity : ComponentActivity() {
4661
// Keep the screen on while the app is running for better demo experience.
4762
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
4863
}
64+
65+
companion object {
66+
private const val TAG = "AGMainActivity"
67+
}
4968
}

Android/src/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
// Top-level build file where you can add configuration options common to all sub-projects/modules.
1818
plugins {
1919
alias(libs.plugins.android.application) apply false
20+
alias(libs.plugins.google.services) apply false
2021
alias(libs.plugins.kotlin.android) apply false
2122
alias(libs.plugins.kotlin.compose) apply false
2223
alias(libs.plugins.hilt.application) apply false

Android/src/gradle/libs.versions.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ netOpenidAppauth = "0.11.1"
3131
splashscreen = "1.2.0-beta01"
3232
hilt = "2.56.2"
3333
hiltNavigation = "1.2.0"
34+
googleService = "4.4.3"
35+
firebaseBom = "33.16.0"
3436

3537
[libraries]
3638
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
@@ -73,6 +75,10 @@ hilt-android = { module = "com.google.dagger:hilt-android", version.ref = "hilt"
7375
hilt-navigation-compose = { module = "androidx.hilt:hilt-navigation-compose", version.ref = "hiltNavigation" }
7476
hilt-android-testing = { module = "com.google.dagger:hilt-android-testing", version.ref = "hilt" }
7577
hilt-android-compiler = { module = "com.google.dagger:hilt-android-compiler", version.ref = "hilt" }
78+
firebase-bom = { group = "com.google.firebase", name = "firebase-bom", version.ref = "firebaseBom" }
79+
# When using the Firebase BoM, you don't specify versions in Firebase
80+
# library dependencies.
81+
firebase-analytics = { group = "com.google.firebase", name = "firebase-analytics" }
7682

7783
[plugins]
7884
android-application = { id = "com.android.application", version.ref = "agp" }
@@ -81,3 +87,4 @@ kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "ko
8187
kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "serializationPlugin" }
8288
protobuf = {id = "com.google.protobuf", version.ref = "protobuf"}
8389
hilt-application = { id = "com.google.dagger.hilt.android", version.ref = "hilt" }
90+
google-services = { id = "com.google.gms.google-services", version.ref = "googleService" }

0 commit comments

Comments
 (0)