Skip to content
Closed
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: 2 additions & 0 deletions llm/android/LlamaDemo/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,14 @@ android {
dependencies {
implementation("androidx.core:core-ktx:1.9.0")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.1")
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.6.1")
implementation("androidx.activity:activity-compose:1.7.0")
implementation(platform("androidx.compose:compose-bom:2023.03.00"))
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3")
implementation("io.coil-kt:coil-compose:2.4.0")
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("androidx.camera:camera-core:1.3.0-rc02")
implementation("androidx.constraintlayout:constraintlayout:2.2.0-alpha12")
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.example.executorchllamademo

enum class AppearanceMode(val displayName: String) {
SYSTEM("System"),
LIGHT("Light"),
DARK("Dark");

companion object {
fun fromDisplayName(name: String): AppearanceMode {
return values().find { it.displayName == name } ?: SYSTEM
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ class DemoSharedPreferences(private val context: Context) {
) ?: ""
}

fun addMessages(messageAdapter: MessageAdapter) {
fun addMessages(messages: List<Message>) {
val editor = sharedPreferences.edit()
val gson = Gson()
val msgJSON = gson.toJson(messageAdapter.savedMessages)
val msgJSON = gson.toJson(messages)
editor.putString(context.getString(R.string.saved_messages_json_key), msgJSON)
editor.apply()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,75 +8,67 @@

package com.example.executorchllamademo

import android.app.AlertDialog
import android.os.Build
import android.os.Bundle
import android.widget.ImageButton
import android.widget.ListView
import androidx.appcompat.app.AppCompatActivity
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Surface
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.executorchllamademo.ui.screens.LogsScreen
import com.example.executorchllamademo.ui.theme.LlamaDemoTheme
import com.google.gson.Gson

class LogsActivity : AppCompatActivity() {
class LogsActivity : ComponentActivity() {

private lateinit var logsAdapter: LogsAdapter
private var appearanceMode by mutableStateOf(AppearanceMode.SYSTEM)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_logs)

if (Build.VERSION.SDK_INT >= 21) {
window.statusBarColor = ContextCompat.getColor(this, R.color.status_bar)
window.navigationBarColor = ContextCompat.getColor(this, R.color.nav_bar)
}

ViewCompat.setOnApplyWindowInsetsListener(requireViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}

setupLogs()
setupClearLogsButton()
}

override fun onResume() {
super.onResume()
logsAdapter.clear()
logsAdapter.addAll(ETLogging.getInstance().getLogs())
logsAdapter.notifyDataSetChanged()
}
loadAppearanceMode()

private fun setupLogs() {
val logsListView = requireViewById<ListView>(R.id.logsListView)
logsAdapter = LogsAdapter(this, R.layout.logs_message)
setContent {
val isDarkTheme = when (appearanceMode) {
AppearanceMode.LIGHT -> false
AppearanceMode.DARK -> true
AppearanceMode.SYSTEM -> isSystemInDarkTheme()
}

logsListView.adapter = logsAdapter
logsAdapter.addAll(ETLogging.getInstance().getLogs())
logsAdapter.notifyDataSetChanged()
LlamaDemoTheme(darkTheme = isDarkTheme) {
Surface(modifier = Modifier.fillMaxSize()) {
LogsScreen()
}
}
}
}

private fun setupClearLogsButton() {
val clearLogsButton = requireViewById<ImageButton>(R.id.clearLogsButton)
clearLogsButton.setOnClickListener {
AlertDialog.Builder(this)
.setTitle("Delete Logs History")
.setMessage("Do you really want to delete logs history?")
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(android.R.string.yes) { _, _ ->
// Clear the messageAdapter and sharedPreference
ETLogging.getInstance().clearLogs()
logsAdapter.clear()
logsAdapter.notifyDataSetChanged()
}
.setNegativeButton(android.R.string.no, null)
.show()
private fun loadAppearanceMode() {
val prefs = DemoSharedPreferences(this)
val settingsJson = prefs.getSettings()
if (settingsJson.isNotEmpty()) {
try {
val settings = Gson().fromJson(settingsJson, SettingsFields::class.java)
appearanceMode = settings.appearanceMode
} catch (e: Exception) {
Log.e("LogsActivity", "Error loading appearance mode", e)
}
}
}

override fun onDestroy() {
super.onDestroy()
ETLogging.getInstance().saveLogs()
override fun onResume() {
super.onResume()
loadAppearanceMode()
}
}

This file was deleted.

Loading