-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
971a602
commit 890518f
Showing
44 changed files
with
1,170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
*.iml | ||
.gradle | ||
/local.properties | ||
/.idea/caches | ||
/.idea/libraries | ||
/.idea/modules.xml | ||
/.idea/workspace.xml | ||
/.idea/navEditor.xml | ||
/.idea/assetWizardSettings.xml | ||
.DS_Store | ||
/build | ||
/captures | ||
.externalNativeBuild | ||
.cxx | ||
local.properties |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
plugins { | ||
alias(libs.plugins.android.application) | ||
alias(libs.plugins.jetbrains.kotlin.android) | ||
alias(libs.plugins.google.android.libraries.mapsplatform.secrets.gradle.plugin) | ||
} | ||
|
||
android { | ||
namespace = "com.example.geminiapp" | ||
compileSdk = 34 | ||
|
||
defaultConfig { | ||
applicationId = "com.example.geminiapp" | ||
minSdk = 26 | ||
targetSdk = 34 | ||
versionCode = 1 | ||
versionName = "1.0" | ||
|
||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" | ||
vectorDrawables { | ||
useSupportLibrary = true | ||
} | ||
} | ||
|
||
buildTypes { | ||
release { | ||
isMinifyEnabled = false | ||
proguardFiles( | ||
getDefaultProguardFile("proguard-android-optimize.txt"), | ||
"proguard-rules.pro" | ||
) | ||
} | ||
} | ||
compileOptions { | ||
sourceCompatibility = JavaVersion.VERSION_1_8 | ||
targetCompatibility = JavaVersion.VERSION_1_8 | ||
} | ||
kotlinOptions { | ||
jvmTarget = "1.8" | ||
} | ||
buildFeatures { | ||
compose = true | ||
buildConfig = true | ||
} | ||
composeOptions { | ||
kotlinCompilerExtensionVersion = "1.5.1" | ||
} | ||
packaging { | ||
resources { | ||
excludes += "/META-INF/{AL2.0,LGPL2.1}" | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
|
||
implementation(libs.androidx.core.ktx) | ||
implementation(libs.androidx.lifecycle.runtime.ktx) | ||
implementation(libs.androidx.lifecycle.viewmodel.compose) | ||
implementation(libs.androidx.activity.compose) | ||
implementation(platform(libs.androidx.compose.bom)) | ||
implementation(libs.androidx.ui) | ||
implementation(libs.androidx.ui.graphics) | ||
implementation(libs.androidx.ui.tooling.preview) | ||
implementation(libs.androidx.material3) | ||
implementation(libs.generativeai) | ||
testImplementation(libs.junit) | ||
androidTestImplementation(libs.androidx.junit) | ||
androidTestImplementation(libs.androidx.espresso.core) | ||
androidTestImplementation(platform(libs.androidx.compose.bom)) | ||
androidTestImplementation(libs.androidx.ui.test.junit4) | ||
debugImplementation(libs.androidx.ui.tooling) | ||
debugImplementation(libs.androidx.ui.test.manifest) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
24 changes: 24 additions & 0 deletions
24
GeminiApp/app/src/androidTest/java/com/example/geminiapp/ExampleInstrumentedTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.example.geminiapp | ||
|
||
import androidx.test.platform.app.InstrumentationRegistry | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
|
||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
import org.junit.Assert.* | ||
|
||
/** | ||
* Instrumented test, which will execute on an Android device. | ||
* | ||
* See [testing documentation](http://d.android.com/tools/testing). | ||
*/ | ||
@RunWith(AndroidJUnit4::class) | ||
class ExampleInstrumentedTest { | ||
@Test | ||
fun useAppContext() { | ||
// Context of the app under test. | ||
val appContext = InstrumentationRegistry.getInstrumentation().targetContext | ||
assertEquals("com.example.geminiapp", appContext.packageName) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools"> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:dataExtractionRules="@xml/data_extraction_rules" | ||
android:fullBackupContent="@xml/backup_rules" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/Theme.GeminiApp" | ||
tools:targetApi="31"> | ||
<activity | ||
android:name=".MainActivity" | ||
android:exported="true" | ||
android:label="@string/app_name" | ||
android:theme="@style/Theme.GeminiApp"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
</application> | ||
|
||
</manifest> |
147 changes: 147 additions & 0 deletions
147
GeminiApp/app/src/main/java/com/example/geminiapp/BakingScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package com.example.geminiapp | ||
|
||
import android.graphics.BitmapFactory | ||
import androidx.compose.foundation.BorderStroke | ||
import androidx.compose.foundation.Image | ||
import androidx.compose.foundation.border | ||
import androidx.compose.foundation.clickable | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.requiredSize | ||
import androidx.compose.foundation.lazy.LazyRow | ||
import androidx.compose.foundation.lazy.itemsIndexed | ||
import androidx.compose.foundation.rememberScrollState | ||
import androidx.compose.foundation.verticalScroll | ||
import androidx.compose.material3.Button | ||
import androidx.compose.material3.CircularProgressIndicator | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.TextField | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableIntStateOf | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.saveable.rememberSaveable | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.res.painterResource | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.text.style.TextAlign | ||
import androidx.compose.ui.unit.dp | ||
import androidx.lifecycle.viewmodel.compose.viewModel | ||
|
||
val images = arrayOf( | ||
R.drawable.baked_goods_1, | ||
R.drawable.baked_goods_2, | ||
R.drawable.baked_goods_3 | ||
) | ||
val imageDescriptions = arrayOf( | ||
R.string.image1_description, | ||
R.string.image2_description, | ||
R.string.image3_description | ||
) | ||
|
||
@Composable | ||
fun BakingScreen( | ||
bakingViewModel: BakingViewModel = viewModel() | ||
) { | ||
val selectedImage = remember { mutableIntStateOf(0) } | ||
val placeholderPrompt = stringResource(R.string.prompt_placeholder) | ||
val placeholderResult = stringResource(R.string.results_placeholder) | ||
var prompt by rememberSaveable { mutableStateOf(placeholderPrompt) } | ||
var result by rememberSaveable { mutableStateOf(placeholderResult) } | ||
val uiState by bakingViewModel.uiState.collectAsState() | ||
val context = LocalContext.current | ||
|
||
Column( | ||
modifier = Modifier.fillMaxSize() | ||
) { | ||
Text( | ||
text = stringResource(R.string.baking_title), | ||
style = MaterialTheme.typography.titleLarge, | ||
modifier = Modifier.padding(16.dp) | ||
) | ||
|
||
LazyRow( | ||
modifier = Modifier.fillMaxWidth() | ||
) { | ||
itemsIndexed(images) { index, image -> | ||
var imageModifier = Modifier | ||
.padding(start = 8.dp, end = 8.dp) | ||
.requiredSize(200.dp) | ||
.clickable { | ||
selectedImage.intValue = index | ||
} | ||
if (index == selectedImage.intValue) { | ||
imageModifier = | ||
imageModifier.border(BorderStroke(4.dp, MaterialTheme.colorScheme.primary)) | ||
} | ||
Image( | ||
painter = painterResource(image), | ||
contentDescription = stringResource(imageDescriptions[index]), | ||
modifier = imageModifier | ||
) | ||
} | ||
} | ||
|
||
Row( | ||
modifier = Modifier.padding(all = 16.dp) | ||
) { | ||
TextField( | ||
value = prompt, | ||
label = { Text(stringResource(R.string.label_prompt)) }, | ||
onValueChange = { prompt = it }, | ||
modifier = Modifier | ||
.weight(0.8f) | ||
.padding(end = 16.dp) | ||
.align(Alignment.CenterVertically) | ||
) | ||
|
||
Button( | ||
onClick = { | ||
val bitmap = BitmapFactory.decodeResource( | ||
context.resources, | ||
images[selectedImage.intValue] | ||
) | ||
bakingViewModel.sendPrompt(bitmap, prompt) | ||
}, | ||
enabled = prompt.isNotEmpty(), | ||
modifier = Modifier | ||
.align(Alignment.CenterVertically) | ||
) { | ||
Text(text = stringResource(R.string.action_go)) | ||
} | ||
} | ||
|
||
if (uiState is UiState.Loading) { | ||
CircularProgressIndicator(modifier = Modifier.align(Alignment.CenterHorizontally)) | ||
} else { | ||
var textColor = MaterialTheme.colorScheme.onSurface | ||
if (uiState is UiState.Error) { | ||
textColor = MaterialTheme.colorScheme.error | ||
result = (uiState as UiState.Error).errorMessage | ||
} else if (uiState is UiState.Success) { | ||
textColor = MaterialTheme.colorScheme.onSurface | ||
result = (uiState as UiState.Success).outputText | ||
} | ||
val scrollState = rememberScrollState() | ||
Text( | ||
text = result, | ||
textAlign = TextAlign.Start, | ||
color = textColor, | ||
modifier = Modifier | ||
.align(Alignment.CenterHorizontally) | ||
.padding(16.dp) | ||
.fillMaxSize() | ||
.verticalScroll(scrollState) | ||
) | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
GeminiApp/app/src/main/java/com/example/geminiapp/BakingViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.example.geminiapp | ||
|
||
import android.graphics.Bitmap | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.google.ai.client.generativeai.GenerativeModel | ||
import com.google.ai.client.generativeai.type.content | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.launch | ||
|
||
class BakingViewModel : ViewModel() { | ||
private val _uiState: MutableStateFlow<UiState> = | ||
MutableStateFlow(UiState.Initial) | ||
val uiState: StateFlow<UiState> = | ||
_uiState.asStateFlow() | ||
|
||
private val generativeModel = GenerativeModel( | ||
modelName = "gemini-pro-vision", | ||
apiKey = BuildConfig.apiKey | ||
) | ||
|
||
fun sendPrompt( | ||
bitmap: Bitmap, | ||
prompt: String | ||
) { | ||
_uiState.value = UiState.Loading | ||
|
||
viewModelScope.launch(Dispatchers.IO) { | ||
try { | ||
val response = generativeModel.generateContent( | ||
content { | ||
image(bitmap) | ||
text(prompt) | ||
} | ||
) | ||
response.text?.let { outputContent -> | ||
_uiState.value = UiState.Success(outputContent) | ||
} | ||
} catch (e: Exception) { | ||
_uiState.value = UiState.Error(e.localizedMessage ?: "") | ||
} | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
GeminiApp/app/src/main/java/com/example/geminiapp/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.example.geminiapp | ||
|
||
import android.os.Bundle | ||
import androidx.activity.ComponentActivity | ||
import androidx.activity.compose.setContent | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Surface | ||
import androidx.compose.ui.Modifier | ||
import com.example.geminiapp.ui.theme.GeminiAppTheme | ||
|
||
class MainActivity : ComponentActivity() { | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContent { | ||
GeminiAppTheme { | ||
// A surface container using the 'background' color from the theme | ||
Surface( | ||
modifier = Modifier.fillMaxSize(), | ||
color = MaterialTheme.colorScheme.background, | ||
) { | ||
BakingScreen() | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.