Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@

-keepattributes Signature
-keepattributes *Annotation*
-keepattributes InnerClasses
-keepattributes InnerClasses
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ interface RemoteConfigDataSource {
fun useImagen(): Boolean

fun getFineTunedModelName(): String

fun getImageGenerationEditsModelName(): String
}

@Singleton
Expand Down Expand Up @@ -94,4 +96,8 @@ class RemoteConfigDataSourceImpl @Inject constructor() : RemoteConfigDataSource
override fun getFineTunedModelName(): String {
return remoteConfig.getString("fine_tuned_model_name")
}

override fun getImageGenerationEditsModelName(): String {
return remoteConfig.getString("image_generation_model_edits")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import android.annotation.SuppressLint
import android.content.Context
import androidx.startup.Initializer
import com.google.firebase.appcheck.FirebaseAppCheck
import com.google.firebase.appcheck.ktx.appCheck
import com.google.firebase.appcheck.appCheck
import com.google.firebase.appcheck.playintegrity.PlayIntegrityAppCheckProviderFactory
import com.google.firebase.ktx.Firebase
import com.google.firebase.Firebase

/**
* Initialize [FirebaseAppCheck] using the App Startup Library.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import com.google.firebase.ai.type.ImagenPersonFilterLevel
import com.google.firebase.ai.type.ImagenSafetyFilterLevel
import com.google.firebase.ai.type.ImagenSafetySettings
import com.google.firebase.ai.type.PublicPreviewAPI
import com.google.firebase.ai.type.ResponseModality
import com.google.firebase.ai.type.SafetySetting
import com.google.firebase.ai.type.Schema
import com.google.firebase.ai.type.asImageOrNull
Expand All @@ -51,14 +52,18 @@ interface FirebaseAiDataSource {
suspend fun generateDescriptivePromptFromImage(image: Bitmap): ValidatedDescription
suspend fun generateImageFromPromptAndSkinTone(prompt: String, skinTone: String): Bitmap
suspend fun generatePrompt(prompt: String): GeneratedPrompt
suspend fun generateImageWithEdit(image: Bitmap, backgroundPrompt: String): Bitmap
}

@OptIn(PublicPreviewAPI::class)
@Singleton
class FirebaseAiDataSourceImpl @Inject constructor(
private val remoteConfigDataSource: RemoteConfigDataSource,
) : FirebaseAiDataSource {
private fun createGenerativeTextModel(jsonSchema: Schema, temperature: Float? = null): GenerativeModel {
private fun createGenerativeTextModel(
jsonSchema: Schema,
temperature: Float? = null,
): GenerativeModel {
return Firebase.ai(backend = GenerativeBackend.vertexAI()).generativeModel(
modelName = remoteConfigDataSource.textModelName(),
generationConfig = generationConfig {
Expand Down Expand Up @@ -139,6 +144,7 @@ class FirebaseAiDataSourceImpl @Inject constructor(
image,
)
}

private fun createFineTunedModel(): GenerativeModel {
return Firebase.ai.generativeModel(
remoteConfigDataSource.getFineTunedModelName(),
Expand All @@ -152,7 +158,10 @@ class FirebaseAiDataSourceImpl @Inject constructor(
)
}

override suspend fun generateImageFromPromptAndSkinTone(prompt: String, skinTone: String): Bitmap {
override suspend fun generateImageFromPromptAndSkinTone(
prompt: String,
skinTone: String,
): Bitmap {
val basePromptTemplate = remoteConfigDataSource.promptImageGenerationWithSkinTone()
val imageGenerationPrompt = basePromptTemplate
.replace("{prompt}", prompt)
Expand Down Expand Up @@ -237,6 +246,29 @@ class FirebaseAiDataSourceImpl @Inject constructor(
return executePromptGeneration(generativeModel, prompt)
}

override suspend fun generateImageWithEdit(
image: Bitmap,
backgroundPrompt: String,
): Bitmap {
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(
modelName = remoteConfigDataSource.getImageGenerationEditsModelName(),
generationConfig = generationConfig {
responseModalities = listOf(
ResponseModality.TEXT,
ResponseModality.IMAGE,
)
},
)
val prompt = content {
text(backgroundPrompt)
image(image)
}
val response = model.generateContent(prompt)
val image = response.candidates.firstOrNull()
?.content?.parts?.firstNotNullOfOrNull { it.asImageOrNull() }
return image ?: throw IllegalStateException("Could not extract image from model response")
}

private suspend fun executePromptGeneration(
generativeModel: GenerativeModel,
prompt: String,
Expand Down
4 changes: 4 additions & 0 deletions core/network/src/main/res/xml/remote_config_defaults.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
limitations under the License.
-->
<defaults>
<entry>
<key>image_generation_model_edits</key>
<value>gemini-2.0-flash-preview-image-generation</value>
</entry>
<entry>
<key>use_imagen</key>
<value>true</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,11 @@ class TestFirebaseAiDataSource(val promptOutput: List<String>) : FirebaseAiDataS
override suspend fun generatePrompt(prompt: String): GeneratedPrompt {
return GeneratedPrompt(true, promptOutput)
}

override suspend fun generateImageWithEdit(
image: Bitmap,
backgroundPrompt: String,
): Bitmap {
return createBitmap(1, 1)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,8 @@ class TestRemoteConfigDataSource(private val useGeminiNano: Boolean) : RemoteCon
override fun getFineTunedModelName(): String {
return "test-fine-tuned-model"
}

override fun getImageGenerationEditsModelName(): String {
return "test_image_model"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,11 @@ class FakeImageGenerationRepository : ImageGenerationRepository {
if (exceptionToThrow != null) throw exceptionToThrow!!
return imageUri
}

override suspend fun generateImageWithEdit(
image: Bitmap,
editPrompt: String,
): Bitmap {
return createBitmap(1, 1)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.developers.androidify.theme.transitions

import androidx.compose.animation.core.InfiniteRepeatableSpec
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.animation.core.tween
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.ColorScheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawWithCache
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.drawOutline
import androidx.compose.ui.unit.dp
import kotlin.math.max

private val ColorScheme.highlightLoading: Color
get() =
Color(0xFFAB9FF6)

@Composable
fun Modifier.loadingShimmerOverlay(
visible: Boolean,
highlightColor: Color = MaterialTheme.colorScheme.highlightLoading,
clipShape: Shape = RoundedCornerShape(8.dp),
animationSpec: InfiniteRepeatableSpec<Float> = infiniteRepeatable(
animation = tween(1500),
),
): Modifier {
var highlightProgress: Float by remember { mutableFloatStateOf(0f) }
if (visible) {
val infiniteTransition = rememberInfiniteTransition()
highlightProgress = infiniteTransition.animateFloat(
initialValue = 0f,
targetValue = 1f,
animationSpec = animationSpec,
).value
}
return drawWithCache {
if (visible) {
val brush = Brush.radialGradient(
colors = listOf(
highlightColor.copy(alpha = 0f),
highlightColor.copy(alpha = 0.8f),
highlightColor.copy(alpha = 0f),
),
center = Offset(x = 0f, y = 0f),
radius = (max(size.width, size.height) * highlightProgress * 2.5f).coerceAtLeast(0.01f),
)
onDrawWithContent {
drawContent()
val outline = clipShape.createOutline(size, layoutDirection, this)

drawOutline(
outline = outline,
brush = brush,
)
}
} else {
onDrawWithContent {
drawContent()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ interface ImageGenerationRepository {
suspend fun saveImage(imageBitmap: Bitmap): Uri
suspend fun saveImageToExternalStorage(imageBitmap: Bitmap): Uri
suspend fun saveImageToExternalStorage(imageUri: Uri): Uri
suspend fun generateImageWithEdit(image: Bitmap, editPrompt: String): Bitmap
}

@Singleton
Expand Down Expand Up @@ -124,4 +125,8 @@ internal class ImageGenerationRepositoryImpl @Inject constructor(
throw NoInternetException()
}
}

override suspend fun generateImageWithEdit(image: Bitmap, editPrompt: String): Bitmap {
return firebaseAiDataSource.generateImageWithEdit(image, editPrompt)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,24 @@ import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import coil3.compose.rememberAsyncImagePainter
import com.android.developers.androidify.results.R
import com.android.developers.androidify.theme.AndroidifyTheme

@Composable
Expand All @@ -41,7 +50,8 @@ fun BackgroundTool(
singleLine: Boolean = false,
) {
GenericTool(
modifier = modifier.wrapContentSize(),
modifier = modifier.wrapContentSize()
.verticalScroll(rememberScrollState()),
tools = backgroundOptions,
singleLine = singleLine,
selectedOption = selectedOption,
Expand Down Expand Up @@ -73,6 +83,24 @@ fun BackgroundTool(
.clip(MaterialTheme.shapes.small),
)
}
if (tool.aiBackground) {
Box(
Modifier
.padding(2.dp)
.align(Alignment.BottomEnd)
.size(16.dp)
.background(
color = MaterialTheme.colorScheme.secondaryContainer,
shape = RoundedCornerShape(size = 24.dp),
),
contentAlignment = Alignment.Center,
) {
Icon(
imageVector = ImageVector.vectorResource(R.drawable.spark),
contentDescription = null,
)
}
}
}
},
)
Expand All @@ -88,6 +116,16 @@ private fun BackgroundToolPreview() {
BackgroundOption.Plain,
BackgroundOption.Lightspeed,
BackgroundOption.IO,
BackgroundOption.MusicLover,
BackgroundOption.PoolMaven,
BackgroundOption.SoccerFanatic,
BackgroundOption.StarGazer,
BackgroundOption.FitnessBuff,
BackgroundOption.Fandroid,
BackgroundOption.GreenThumb,
BackgroundOption.Gamer,
BackgroundOption.Jetsetter,
BackgroundOption.Chef
),
selectedOption = BackgroundOption.Lightspeed,
onBackgroundOptionSelected = {},
Expand Down
Loading