UI: move existing UI src files into `legacy` package

This commit is contained in:
Han Yin 2025-04-11 10:21:44 -07:00
parent 37f3e1c415
commit 44a522dbc8
7 changed files with 31 additions and 21 deletions

View File

@ -16,7 +16,7 @@
> >
<activity <activity
android:name=".MainActivity" android:name=".legacy.LegacyActivity"
android:exported="true" android:exported="true"
android:theme="@style/Theme.LlamaAndroid"> android:theme="@style/Theme.LlamaAndroid">
<intent-filter> <intent-filter>

View File

@ -1,4 +1,4 @@
package com.example.llama package com.example.llama.legacy
import android.app.DownloadManager import android.app.DownloadManager
import android.net.Uri import android.net.Uri
@ -31,7 +31,7 @@ data class Downloadable(val name: String, val source: Uri, val destination: File
@JvmStatic @JvmStatic
@Composable @Composable
fun Button(viewModel: MainViewModel, dm: DownloadManager, item: Downloadable) { fun Button(viewModel: LegacyViewModel, dm: DownloadManager, item: Downloadable) {
var status: State by remember { var status: State by remember {
mutableStateOf( mutableStateOf(
if (item.destination.exists()) Downloaded(item) if (item.destination.exists()) Downloaded(item)

View File

@ -1,4 +1,4 @@
package com.example.llama package com.example.llama.legacy
import android.app.ActivityManager import android.app.ActivityManager
import android.app.DownloadManager import android.app.DownloadManager
@ -30,21 +30,23 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.core.content.getSystemService import androidx.core.content.getSystemService
import com.example.llama.ui.theme.LlamaAndroidTheme import com.example.llama.legacy.theme.LlamaAndroidTheme
import java.io.File import java.io.File
class MainActivity( class LegacyActivity(
activityManager: ActivityManager? = null, activityManager: ActivityManager? = null,
downloadManager: DownloadManager? = null, downloadManager: DownloadManager? = null,
clipboardManager: ClipboardManager? = null, clipboardManager: ClipboardManager? = null,
): ComponentActivity() { ) : ComponentActivity() {
private val tag: String? = this::class.simpleName private val TAG: String? = this::class.simpleName
private val activityManager by lazy { activityManager ?: getSystemService<ActivityManager>()!! } private val activityManager by lazy { activityManager ?: getSystemService<ActivityManager>()!! }
private val downloadManager by lazy { downloadManager ?: getSystemService<DownloadManager>()!! } private val downloadManager by lazy { downloadManager ?: getSystemService<DownloadManager>()!! }
private val clipboardManager by lazy { clipboardManager ?: getSystemService<ClipboardManager>()!! } private val clipboardManager by lazy {
clipboardManager ?: getSystemService<ClipboardManager>()!!
}
private val viewModel: MainViewModel by viewModels() private val viewModel: LegacyViewModel by viewModels()
// Get a MemoryInfo object for the device's current memory status. // Get a MemoryInfo object for the device's current memory status.
private fun availableMemory(): ActivityManager.MemoryInfo { private fun availableMemory(): ActivityManager.MemoryInfo {
@ -86,6 +88,11 @@ class MainActivity(
Uri.parse("https://huggingface.co/TheBloke/phi-2-dpo-GGUF/resolve/main/phi-2-dpo.Q3_K_M.gguf?download=true"), Uri.parse("https://huggingface.co/TheBloke/phi-2-dpo-GGUF/resolve/main/phi-2-dpo.Q3_K_M.gguf?download=true"),
File(extFilesDir, "phi-2-dpo.Q3_K_M.gguf") File(extFilesDir, "phi-2-dpo.Q3_K_M.gguf")
), ),
Downloadable(
"Phi 4 mini instruct (Q4_0, 2.33 GiB)",
Uri.parse("https://huggingface.co/bartowski/microsoft_Phi-4-mini-instruct-GGUF/resolve/main/microsoft_Phi-4-mini-instruct-Q4_0.gguf?download=true"),
File(extFilesDir, "microsoft_Phi-4-mini-instruct-Q4_0.gguf")
),
) )
setContent { setContent {
@ -110,7 +117,7 @@ class MainActivity(
@Composable @Composable
fun MainCompose( fun MainCompose(
viewModel: MainViewModel, viewModel: LegacyViewModel,
clipboard: ClipboardManager, clipboard: ClipboardManager,
dm: DownloadManager, dm: DownloadManager,
models: List<Downloadable> models: List<Downloadable>
@ -129,11 +136,13 @@ fun MainCompose(
} }
} }
} }
OutlinedTextField( OutlinedTextField(
value = viewModel.message, value = viewModel.message,
onValueChange = { viewModel.updateMessage(it) }, onValueChange = { viewModel.updateMessage(it) },
label = { Text("Message") }, label = { Text("Message") },
) )
Row { Row {
Button({ viewModel.send() }) { Text("Send") } Button({ viewModel.send() }) { Text("Send") }
Button({ viewModel.bench(8, 4, 1) }) { Text("Bench") } Button({ viewModel.bench(8, 4, 1) }) { Text("Bench") }
@ -143,12 +152,11 @@ fun MainCompose(
clipboard.setPrimaryClip(ClipData.newPlainText("", it)) clipboard.setPrimaryClip(ClipData.newPlainText("", it))
} }
}) { Text("Copy") } }) { Text("Copy") }
Button({ viewModel.unload() }) { Text("Unload") }
} }
Column { models.forEach {
for (model in models) { Downloadable.Button(viewModel, dm, it)
Downloadable.Button(viewModel, dm, model)
}
} }
} }
} }

View File

@ -1,4 +1,4 @@
package com.example.llama package com.example.llama.legacy
import android.llama.cpp.LLamaAndroid import android.llama.cpp.LLamaAndroid
import android.util.Log import android.util.Log
@ -10,7 +10,9 @@ import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
class MainViewModel(private val llamaAndroid: LLamaAndroid = LLamaAndroid.instance()): ViewModel() { class LegacyViewModel(
private val llamaAndroid: LLamaAndroid = LLamaAndroid.instance()
): ViewModel() {
companion object { companion object {
@JvmStatic @JvmStatic
private val NanosPerSecond = 1_000_000_000.0 private val NanosPerSecond = 1_000_000_000.0
@ -46,7 +48,7 @@ class MainViewModel(private val llamaAndroid: LLamaAndroid = LLamaAndroid.instan
viewModelScope.launch { viewModelScope.launch {
// TODO-hyin: implement format message // TODO-hyin: implement format message
llamaAndroid.sendUserPrompt(message = actualText) llamaAndroid.sendUserPrompt(message = text)
.catch { .catch {
Log.e(tag, "send() failed", it) Log.e(tag, "send() failed", it)
messages += it.message!! messages += it.message!!

View File

@ -1,4 +1,4 @@
package com.example.llama.ui.theme package com.example.llama.legacy.theme
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color

View File

@ -1,4 +1,4 @@
package com.example.llama.ui.theme package com.example.llama.legacy.theme
import android.app.Activity import android.app.Activity
import android.os.Build import android.os.Build

View File

@ -1,4 +1,4 @@
package com.example.llama.ui.theme package com.example.llama.legacy.theme
import androidx.compose.material3.Typography import androidx.compose.material3.Typography
import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.TextStyle