Skip to content

Commit 8cb484b

Browse files
Misc code clean up (#43)
* General code clean up * VertexAI API provider for video summarization Use VertexAI API provider for video summarization sample
1 parent 2e0c14c commit 8cb484b

File tree

10 files changed

+90
-128
lines changed

10 files changed

+90
-128
lines changed

ai-catalog/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
android:supportsRtl="true"
1212
android:theme="@style/Theme.AISampleCatalog"
1313
tools:targetApi="31"
14-
android:name=".CatalogApp"
14+
android:name=".CatalogApplication"
1515
android:enableOnBackInvokedCallback="true"
1616
>
1717
<activity

ai-catalog/app/src/main/java/com/android/ai/catalog/CatalogApp.kt renamed to ai-catalog/app/src/main/java/com/android/ai/catalog/CatalogApplication.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ import android.app.Application
1919
import dagger.hilt.android.HiltAndroidApp
2020

2121
@HiltAndroidApp
22-
class CatalogApp: Application()
22+
class CatalogApplication: Application()

ai-catalog/app/src/main/java/com/android/ai/catalog/MainActivity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import androidx.activity.compose.setContent
2323
import androidx.activity.enableEdgeToEdge
2424
import androidx.compose.foundation.layout.fillMaxSize
2525
import androidx.compose.ui.Modifier
26-
import com.android.ai.catalog.ui.CatalogScreen
26+
import com.android.ai.catalog.ui.CatalogApp
2727
import com.android.ai.catalog.ui.theme.AISampleCatalogTheme
2828
import dagger.hilt.android.AndroidEntryPoint
2929

@@ -34,7 +34,7 @@ class MainActivity : ComponentActivity() {
3434
enableEdgeToEdge()
3535
setContent {
3636
AISampleCatalogTheme {
37-
CatalogScreen(modifier = Modifier.fillMaxSize())
37+
CatalogApp(modifier = Modifier.fillMaxSize())
3838
}
3939
}
4040
}

ai-catalog/app/src/main/java/com/android/ai/catalog/ui/CatalogScreen.kt renamed to ai-catalog/app/src/main/java/com/android/ai/catalog/ui/CatalogApp.kt

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,17 @@ import androidx.navigation.compose.NavHost
5555
import androidx.navigation.compose.composable
5656
import androidx.navigation.compose.rememberNavController
5757
import com.android.ai.catalog.R
58-
import com.android.ai.catalog.ui.domain.SampleCatalog
5958
import com.android.ai.catalog.ui.domain.SampleCatalogItem
6059
import com.google.firebase.FirebaseApp
6160
import kotlinx.serialization.Serializable
61+
import androidx.core.net.toUri
62+
import com.android.ai.catalog.ui.domain.sampleCatalog
6263

6364
@OptIn(ExperimentalMaterial3Api::class)
6465
@Composable
65-
fun CatalogScreen(modifier: Modifier = Modifier) {
66+
fun CatalogApp(modifier: Modifier = Modifier) {
6667
val context = LocalContext.current
6768
val navController = rememberNavController()
68-
val catalog = SampleCatalog(
69-
context
70-
)
7169

7270
var isDialogOpened by remember { mutableStateOf(false) }
7371

@@ -92,7 +90,7 @@ fun CatalogScreen(modifier: Modifier = Modifier) {
9290
LazyColumn(
9391
modifier = Modifier.padding(innerPadding)
9492
) {
95-
items(catalog.list) {
93+
items(sampleCatalog) {
9694
CatalogListItem(catalogItem = it) {
9795
if (it.needsFirebase && !isFirebaseInitialized()) {
9896
isDialogOpened = true
@@ -105,7 +103,7 @@ fun CatalogScreen(modifier: Modifier = Modifier) {
105103
}
106104
}
107105

108-
catalog.list.forEach {
106+
sampleCatalog.forEach {
109107
val catalogItem = it
110108
composable(catalogItem.route) {
111109
catalogItem.sampleEntryScreen()
@@ -115,11 +113,12 @@ fun CatalogScreen(modifier: Modifier = Modifier) {
115113

116114

117115
if (isDialogOpened) {
118-
firebaseRequiredAlert(
116+
FirebaseRequiredAlert(
119117
onDismiss = { isDialogOpened = false },
120118
onOpenFirebaseDocClick = {
121119
isDialogOpened = false
122-
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://firebase.google.com/docs/vertex-ai/get-started#no-existing-firebase"))
120+
val intent = Intent(Intent.ACTION_VIEW,
121+
"https://firebase.google.com/docs/vertex-ai/get-started#no-existing-firebase".toUri())
123122
context.startActivity(intent)
124123
}
125124
)
@@ -131,6 +130,7 @@ fun CatalogListItem(
131130
catalogItem: SampleCatalogItem,
132131
onButtonClick: () -> Unit
133132
) {
133+
val context = LocalContext.current
134134
ElevatedCard(
135135
modifier = Modifier.padding(18.dp),
136136
onClick = {
@@ -146,11 +146,11 @@ fun CatalogListItem(
146146
.padding(bottom = 8.dp),
147147
fontSize = 20.sp,
148148
fontWeight = FontWeight.Bold,
149-
text = catalogItem.title
149+
text = context.getString(catalogItem.title)
150150
)
151151
Text(
152152
modifier = Modifier.padding(bottom = 8.dp),
153-
text = catalogItem.description,
153+
text = context.getString(catalogItem.description),
154154
)
155155
Row {
156156
Spacer(Modifier.weight(1f))
@@ -182,7 +182,7 @@ fun CatalogListItem(
182182
object HomeScreen
183183

184184
@Composable
185-
fun firebaseRequiredAlert(
185+
fun FirebaseRequiredAlert(
186186
onDismiss: () -> Unit = {},
187187
onOpenFirebaseDocClick: () -> Unit = {}
188188
) {
@@ -221,12 +221,7 @@ fun firebaseRequiredAlert(
221221
fun isFirebaseInitialized(): Boolean {
222222
return try {
223223
val firebaseApp = FirebaseApp.getInstance()
224-
Log.d("CatalogScreen", "firebaseApp.options.projectId: ${firebaseApp.options.projectId}")
225-
if (firebaseApp.options.projectId == "mock_project") {
226-
Log.e("CatalogScreen", "Firebase is not initialized")
227-
return false
228-
}
229-
return true
224+
return firebaseApp.options.projectId != "mock_project"
230225
} catch (e: IllegalStateException) {
231226
Log.e("CatalogScreen", "Firebase is not initialized")
232227
return false

ai-catalog/app/src/main/java/com/android/ai/catalog/ui/domain/SampleCatalog.kt

Lines changed: 67 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
package com.android.ai.catalog.ui.domain
1919

20-
import android.content.Context
20+
import androidx.annotation.StringRes
2121
import androidx.compose.runtime.Composable
2222
import androidx.compose.ui.graphics.Color
2323
import com.android.ai.catalog.R
@@ -30,81 +30,76 @@ import com.android.ai.samples.genai_writing_assistance.GenAIWritingAssistanceScr
3030
import com.android.ai.samples.imagen.ImagenScreen
3131
import com.android.ai.samples.magicselfie.MagicSelfieScreen
3232

33-
class SampleCatalog(
34-
context: Context
35-
) {
36-
37-
val list = listOf(
38-
SampleCatalogItem(
39-
title = context.getString(R.string.gemini_multimodal_sample_title),
40-
description = context.getString(R.string.gemini_multimodal_sample_description),
41-
route = "GeminiMultimodalScreen",
42-
sampleEntryScreen = { GeminiMultimodalScreen() },
43-
tags = listOf(SampleTags.GEMINI_2_0_FLASH, SampleTags.FIREBASE),
44-
needsFirebase = true
45-
),
46-
SampleCatalogItem(
47-
title = context.getString(R.string.gemini_chatbot_sample_title),
48-
description = context.getString(R.string.gemini_chatbot_sample_description),
49-
route = "GeminiChitchatScreen",
50-
sampleEntryScreen = { GeminiChatbotScreen() },
51-
tags = listOf(SampleTags.GEMINI_2_0_FLASH, SampleTags.FIREBASE),
52-
needsFirebase = true
53-
),
54-
SampleCatalogItem(
55-
title = context.getString(R.string.genai_summarization_sample_title),
56-
description = context.getString(R.string.genai_summarization_sample_description),
57-
route = "GenAISummarizationScreen",
58-
sampleEntryScreen = { GenAISummarizationScreen() },
59-
tags = listOf(SampleTags.GEMINI_NANO, SampleTags.ML_KIT)
60-
),
61-
SampleCatalogItem(
62-
title = context.getString(R.string.genai_image_description_sample_title),
63-
description = context.getString(R.string.genai_image_description_sample_description),
64-
route = "GenAIImageDescriptionScreen",
65-
sampleEntryScreen = { GenAIImageDescriptionScreen() },
66-
tags = listOf(SampleTags.GEMINI_NANO, SampleTags.ML_KIT)
67-
),
68-
SampleCatalogItem(
69-
title = context.getString(R.string.genai_writing_assistance_sample_title),
70-
description = context.getString(R.string.genai_writing_assistance_sample_description),
71-
route = "GenAIWritingAssistanceScreen",
72-
sampleEntryScreen = { GenAIWritingAssistanceScreen() },
73-
tags = listOf(SampleTags.GEMINI_NANO, SampleTags.ML_KIT)
74-
),
75-
SampleCatalogItem(
76-
title = context.getString(R.string.imagen_sample_title),
77-
description = context.getString(R.string.imagen_sample_description),
78-
route = "ImagenImageGenerationScreen",
79-
sampleEntryScreen = { ImagenScreen() },
80-
tags = listOf(SampleTags.IMAGEN, SampleTags.FIREBASE),
81-
needsFirebase = true
82-
),
83-
SampleCatalogItem(
84-
title = context.getString(R.string.magic_selfie_sample_title),
85-
description = context.getString(R.string.magic_selfie_sample_description),
86-
route = "MagicSelfieScreen",
87-
sampleEntryScreen = { MagicSelfieScreen() },
88-
tags = listOf(SampleTags.IMAGEN, SampleTags.FIREBASE, SampleTags.ML_KIT),
89-
needsFirebase = true
90-
),
91-
SampleCatalogItem(
92-
title = context.getString(R.string.gemini_video_summarization_sample_title),
93-
description = context.getString(R.string.gemini_video_summarization_sample_description),
94-
route = "VideoSummarizationScreen",
95-
sampleEntryScreen = { VideoSummarizationScreen() },
96-
tags = listOf(SampleTags.GEMINI_2_0_FLASH, SampleTags.FIREBASE, SampleTags.MEDIA3),
97-
needsFirebase = true
98-
),
33+
val sampleCatalog = listOf<SampleCatalogItem>(
34+
SampleCatalogItem(
35+
title = R.string.gemini_multimodal_sample_title,
36+
description = R.string.gemini_multimodal_sample_description,
37+
route = "GeminiMultimodalScreen",
38+
sampleEntryScreen = { GeminiMultimodalScreen() },
39+
tags = listOf(SampleTags.GEMINI_2_0_FLASH, SampleTags.FIREBASE),
40+
needsFirebase = true
41+
),
42+
SampleCatalogItem(
43+
title = R.string.gemini_chatbot_sample_title,
44+
description = R.string.gemini_chatbot_sample_description,
45+
route = "GeminiChitchatScreen",
46+
sampleEntryScreen = { GeminiChatbotScreen() },
47+
tags = listOf(SampleTags.GEMINI_2_0_FLASH, SampleTags.FIREBASE),
48+
needsFirebase = true
49+
),
50+
SampleCatalogItem(
51+
title = R.string.genai_summarization_sample_title,
52+
description = R.string.genai_summarization_sample_description,
53+
route = "GenAISummarizationScreen",
54+
sampleEntryScreen = { GenAISummarizationScreen() },
55+
tags = listOf(SampleTags.GEMINI_NANO, SampleTags.ML_KIT)
56+
),
57+
SampleCatalogItem(
58+
title = R.string.genai_image_description_sample_title,
59+
description = R.string.genai_image_description_sample_description,
60+
route = "GenAIImageDescriptionScreen",
61+
sampleEntryScreen = { GenAIImageDescriptionScreen() },
62+
tags = listOf(SampleTags.GEMINI_NANO, SampleTags.ML_KIT)
63+
),
64+
SampleCatalogItem(
65+
title = R.string.genai_writing_assistance_sample_title,
66+
description = R.string.genai_writing_assistance_sample_description,
67+
route = "GenAIWritingAssistanceScreen",
68+
sampleEntryScreen = { GenAIWritingAssistanceScreen() },
69+
tags = listOf(SampleTags.GEMINI_NANO, SampleTags.ML_KIT)
70+
),
71+
SampleCatalogItem(
72+
title = R.string.imagen_sample_title,
73+
description = R.string.imagen_sample_description,
74+
route = "ImagenImageGenerationScreen",
75+
sampleEntryScreen = { ImagenScreen() },
76+
tags = listOf(SampleTags.IMAGEN, SampleTags.FIREBASE),
77+
needsFirebase = true
78+
),
79+
SampleCatalogItem(
80+
title = R.string.magic_selfie_sample_title,
81+
description = R.string.magic_selfie_sample_description,
82+
route = "MagicSelfieScreen",
83+
sampleEntryScreen = { MagicSelfieScreen() },
84+
tags = listOf(SampleTags.IMAGEN, SampleTags.FIREBASE, SampleTags.ML_KIT),
85+
needsFirebase = true
86+
),
87+
SampleCatalogItem(
88+
title = R.string.gemini_video_summarization_sample_title,
89+
description = R.string.gemini_video_summarization_sample_description,
90+
route = "VideoSummarizationScreen",
91+
sampleEntryScreen = { VideoSummarizationScreen() },
92+
tags = listOf(SampleTags.GEMINI_2_0_FLASH, SampleTags.FIREBASE, SampleTags.MEDIA3),
93+
needsFirebase = true
94+
),
9995

100-
// To create a new sample entry, add a new SampleCatalogItem here.
101-
)
96+
// To create a new sample entry, add a new SampleCatalogItem here.
97+
)
10298

103-
}
10499

105100
data class SampleCatalogItem(
106-
val title: String,
107-
val description: String,
101+
@StringRes val title: Int,
102+
@StringRes val description: Int,
108103
val route: String,
109104
val sampleEntryScreen: @Composable () -> Unit,
110105
val tags: List<SampleTags> = emptyList(),

ai-catalog/app/src/main/java/com/android/ai/catalog/ui/theme/Theme.kt

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,11 @@ private val LightColorScheme = lightColorScheme(
3737
primary = Purple40,
3838
secondary = PurpleGrey40,
3939
tertiary = Pink40
40-
41-
/* Other default colors to override
42-
background = Color(0xFFFFFBFE),
43-
surface = Color(0xFFFFFBFE),
44-
onPrimary = Color.White,
45-
onSecondary = Color.White,
46-
onTertiary = Color.White,
47-
onBackground = Color(0xFF1C1B1F),
48-
onSurface = Color(0xFF1C1B1F),
49-
*/
5040
)
5141

5242
@Composable
5343
fun AISampleCatalogTheme(
5444
darkTheme: Boolean = isSystemInDarkTheme(),
55-
// Dynamic color is available on Android 12+
5645
dynamicColor: Boolean = true,
5746
content: @Composable () -> Unit
5847
) {

ai-catalog/app/src/main/java/com/android/ai/catalog/ui/theme/Type.kt

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import androidx.compose.ui.text.font.FontFamily
2323
import androidx.compose.ui.text.font.FontWeight
2424
import androidx.compose.ui.unit.sp
2525

26-
// Set of Material typography styles to start with
2726
val Typography = Typography(
2827
bodyLarge = TextStyle(
2928
fontFamily = FontFamily.Default,
@@ -32,20 +31,4 @@ val Typography = Typography(
3231
lineHeight = 24.sp,
3332
letterSpacing = 0.5.sp
3433
)
35-
/* Other default text styles to override
36-
titleLarge = TextStyle(
37-
fontFamily = FontFamily.Default,
38-
fontWeight = FontWeight.Normal,
39-
fontSize = 22.sp,
40-
lineHeight = 28.sp,
41-
letterSpacing = 0.sp
42-
),
43-
labelSmall = TextStyle(
44-
fontFamily = FontFamily.Default,
45-
fontWeight = FontWeight.Medium,
46-
fontSize = 11.sp,
47-
lineHeight = 16.sp,
48-
letterSpacing = 0.5.sp
49-
)
50-
*/
5134
)

ai-catalog/app/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<string name="imagen_sample_title">Image generation with Imagen</string>
1616
<string name="imagen_sample_description">Generate images with Imagen, Google image generation model</string>
1717
<string name="magic_selfie_sample_title">Magic Selfie with Imagen and ML Kit</string>
18-
<string name="magic_selfie_sample_description">Change the background of you selfies with Imagen and the ML Kit Segmentation API</string>
18+
<string name="magic_selfie_sample_description">Change the background of your selfies with Imagen and the ML Kit Segmentation API</string>
1919
<string name="gemini_video_summarization_sample_title">Video Summarization with Gemini and Firebase</string>
2020
<string name="gemini_video_summarization_sample_description">"Generate a summary of a video (from a cloud URL or Youtube) with Gemini API powered by Firebase"</string>
2121
<string name="firebase_required">Firebase Required</string>

ai-catalog/samples/gemini-chatbot/src/main/java/com/android/ai/samples/geminichatbot/GeminiChatbotScreen.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ import androidx.hilt.navigation.compose.hiltViewModel
6969
fun GeminiChatbotScreen (
7070
viewModel: GeminiChatbotViewModel = hiltViewModel()
7171
) {
72-
73-
val context = LocalContext.current
7472
val topAppBarState = rememberTopAppBarState()
7573
val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior(topAppBarState)
7674
val messages by viewModel.messageList.collectAsState()
@@ -90,7 +88,7 @@ fun GeminiChatbotScreen (
9088
Text(text = stringResource(id = R.string.geminichatbot_title_bar))
9189
},
9290
actions = {
93-
SeeCodeButton(context)
91+
SeeCodeButton()
9492
}
9593
)
9694
}
@@ -189,8 +187,10 @@ fun MessageBubble(
189187
}
190188

191189
@Composable
192-
fun SeeCodeButton(context: Context) {
190+
fun SeeCodeButton() {
191+
val context = LocalContext.current
193192
val githubLink = "https://github.com/android/ai-samples/tree/main/ai-catalog/samples/gemini-chatbot"
193+
194194
Button(
195195
onClick = {
196196
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(githubLink))

ai-catalog/samples/gemini-video-summarization/src/main/java/com/android/ai/samples/geminivideosummary/viewmodel/VideoSummarizationViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class VideoSummarizationViewModel @Inject constructor() : ViewModel() {
5151
_outputText.value = OutputTextState.Loading
5252

5353
try {
54-
val generativeModel = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel("gemini-2.0-flash")
54+
val generativeModel = Firebase.ai(backend = GenerativeBackend.vertexAI()).generativeModel("gemini-2.0-flash")
5555

5656
val requestContent = content {
5757
fileData(videoSource.toString(), "video/mp4")

0 commit comments

Comments
 (0)