feat: restructure into XFlow monorepo with Nuxt 4, Modular Onion Monolith, Android client, and Desktop CLI

This commit is contained in:
2026-09-28 10:39:19 +05:00
parent edcd30c829
commit be68f5e130
204 changed files with 24616 additions and 39 deletions
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<application
android:name=".AvtexApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:windowSoftInputMode="adjustResize"
android:theme="@style/Theme.Avtex">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- Deep link intent-filter for Gemini / Assistant App Actions -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="xflow"
android:host="lists" />
</intent-filter>
</activity>
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />
<service
android:name=".engine.SleepTrackingService"
android:enabled="true"
android:exported="false"
android:foregroundServiceType="specialUse">
<property
android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
android:value="Detects sleep and wake states using motion sensors and Sleep API for Home Assistant smart home automations." />
</service>
<receiver
android:name=".engine.SleepReceiver"
android:exported="true"
android:permission="com.google.android.gms.permission.ACTIVITY_RECOGNITION">
<intent-filter>
<action android:name="com.example.avtex.ACTION_SLEEP_API_EVENT" />
</intent-filter>
</receiver>
<receiver
android:name=".engine.BootReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
@@ -0,0 +1,52 @@
package com.example.avtex
import android.app.Application
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import com.example.avtex.data.SettingsRepository
import com.example.avtex.notes.NotesRepository
import com.example.avtex.notes.XFlowSyncClient
class AvtexApplication : Application() {
lateinit var settingsRepository: SettingsRepository
private set
lateinit var notesRepository: NotesRepository
private set
lateinit var xflowSyncClient: XFlowSyncClient
private set
override fun onCreate() {
super.onCreate()
instance = this
settingsRepository = SettingsRepository(this)
notesRepository = NotesRepository(this)
xflowSyncClient = XFlowSyncClient(this)
createNotificationChannels()
}
private fun createNotificationChannels() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
CHANNEL_ID_TRACKING,
"Мониторинг XFlow",
NotificationManager.IMPORTANCE_LOW
).apply {
description = "Отображает текущий статус мониторинга и синхронизации"
setShowBadge(false)
}
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
}
companion object {
const val CHANNEL_ID_TRACKING = "xflow_tracking_channel"
lateinit var instance: AvtexApplication
private set
}
}
@@ -0,0 +1,86 @@
package com.example.avtex
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import androidx.lifecycle.lifecycleScope
import com.example.avtex.theme.AvtexTheme
import kotlinx.coroutines.launch
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
handleIncomingIntent(intent)
enableEdgeToEdge()
setContent {
AvtexTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
MainNavigation()
}
}
}
}
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
handleIncomingIntent(intent)
}
private fun handleIncomingIntent(intent: Intent?) {
if (intent == null) return
// 1. Обработка App Actions через Deep Link xflow://lists/add
val data = intent.data
if (data != null && data.scheme == "xflow") {
val listName = data.getQueryParameter("list_name") ?: data.getQueryParameter("name") ?: "Фильмы на просмотр"
val itemName = data.getQueryParameter("item_name") ?: data.getQueryParameter("item")
if (!itemName.isNullOrBlank()) {
saveItemFromAssistant(itemName, listName)
return
}
}
// 2. Обработка через Intent Extras (Google Assistant Built-in Intents)
val extras = intent.extras
if (extras != null) {
val itemName = extras.getString("itemListElement.name") ?: extras.getString("item_name")
val listName = extras.getString("itemList.name") ?: extras.getString("list_name") ?: "Фильмы на просмотр"
if (!itemName.isNullOrBlank()) {
saveItemFromAssistant(itemName, listName)
}
}
}
private fun saveItemFromAssistant(itemName: String, listName: String) {
lifecycleScope.launch {
val repo = AvtexApplication.instance.notesRepository
val category = if (listName.contains("фильм", ignoreCase = true)) "movies" else "general"
repo.addNoteItem(
title = itemName,
listTitle = listName,
category = category
)
Toast.makeText(
this@MainActivity,
"✅ Записано в XFlow: $itemName ($listName)",
Toast.LENGTH_LONG
).show()
}
}
}
@@ -0,0 +1,120 @@
package com.example.avtex
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Bedtime
import androidx.compose.material.icons.automirrored.filled.FormatListBulleted
import androidx.compose.material.icons.filled.Info
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.NavigationBarItemDefaults
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.avtex.theme.GlassBorder
import com.example.avtex.theme.NeonViolet
import com.example.avtex.theme.ObsidianBg
import com.example.avtex.theme.ObsidianSurfaceElevated
import com.example.avtex.theme.TextMuted
import com.example.avtex.ui.DashboardScreen
import com.example.avtex.ui.ListsScreen
import com.example.avtex.ui.OnePlusGuideScreen
import com.example.avtex.ui.SettingsScreen
@Composable
fun MainNavigation() {
var selectedTab by rememberSaveable { mutableIntStateOf(0) }
Scaffold(
modifier = Modifier.fillMaxSize(),
containerColor = ObsidianBg,
bottomBar = {
NavigationBar(
containerColor = ObsidianSurfaceElevated,
tonalElevation = 0.dp,
modifier = Modifier.border(1.dp, GlassBorder)
) {
val navColors = NavigationBarItemDefaults.colors(
selectedIconColor = Color.White,
selectedTextColor = NeonViolet,
indicatorColor = NeonViolet.copy(alpha = 0.35f),
unselectedIconColor = TextMuted,
unselectedTextColor = TextMuted
)
NavigationBarItem(
selected = selectedTab == 0,
onClick = { selectedTab = 0 },
icon = { Icon(Icons.Default.Bedtime, contentDescription = "Мониторинг") },
label = {
Text(
"Сон",
fontSize = 11.sp,
fontWeight = if (selectedTab == 0) FontWeight.Bold else FontWeight.Normal
)
},
colors = navColors
)
NavigationBarItem(
selected = selectedTab == 1,
onClick = { selectedTab = 1 },
icon = { Icon(Icons.AutoMirrored.Filled.FormatListBulleted, contentDescription = "Списки") },
label = {
Text(
"Списки",
fontSize = 11.sp,
fontWeight = if (selectedTab == 1) FontWeight.Bold else FontWeight.Normal
)
},
colors = navColors
)
NavigationBarItem(
selected = selectedTab == 2,
onClick = { selectedTab = 2 },
icon = { Icon(Icons.Default.Settings, contentDescription = "Настройки") },
label = {
Text(
"Настройки",
fontSize = 11.sp,
fontWeight = if (selectedTab == 2) FontWeight.Bold else FontWeight.Normal
)
},
colors = navColors
)
NavigationBarItem(
selected = selectedTab == 3,
onClick = { selectedTab = 3 },
icon = { Icon(Icons.Default.Info, contentDescription = "OnePlus") },
label = {
Text(
"OnePlus",
fontSize = 11.sp,
fontWeight = if (selectedTab == 3) FontWeight.Bold else FontWeight.Normal
)
},
colors = navColors
)
}
}
) { innerPadding ->
when (selectedTab) {
0 -> DashboardScreen(modifier = Modifier.padding(innerPadding))
1 -> ListsScreen(modifier = Modifier.padding(innerPadding))
2 -> SettingsScreen(modifier = Modifier.padding(innerPadding))
3 -> OnePlusGuideScreen(modifier = Modifier.padding(innerPadding))
}
}
}
@@ -0,0 +1,20 @@
package com.example.avtex.data
enum class HacMode {
WEBHOOK,
REST
}
data class AppSettings(
val isTrackingActive: Boolean = false,
val currentState: String = "AWAKE",
val lastStateChangeMillis: Long = System.currentTimeMillis(),
val hacMode: HacMode = HacMode.WEBHOOK,
val hacWebhookUrl: String = "",
val hacRestUrl: String = "",
val hacToken: String = "",
val inactivityMinutes: Int = 20,
val confidenceThreshold: Int = 70,
val broadcastIntents: Boolean = true,
val lastHacStatus: String = "Готов к работе"
)
@@ -0,0 +1,15 @@
package com.example.avtex.data
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
data class LogEntry(
val timestamp: Long = System.currentTimeMillis(),
val tag: String,
val message: String,
val isImportant: Boolean = false
) {
val formattedTime: String
get() = SimpleDateFormat("HH:mm:ss", Locale.getDefault()).format(Date(timestamp))
}
@@ -0,0 +1,117 @@
package com.example.avtex.data
import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.intPreferencesKey
import androidx.datastore.preferences.core.longPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "avtex_settings")
class SettingsRepository(private val context: Context) {
private object PreferencesKeys {
val IS_TRACKING_ACTIVE = booleanPreferencesKey("is_tracking_active")
val CURRENT_STATE = stringPreferencesKey("current_state")
val LAST_STATE_CHANGE = longPreferencesKey("last_state_change")
val HAC_MODE = stringPreferencesKey("hac_mode")
val HAC_WEBHOOK_URL = stringPreferencesKey("hac_webhook_url")
val HAC_REST_URL = stringPreferencesKey("hac_rest_url")
val HAC_TOKEN = stringPreferencesKey("hac_token")
val INACTIVITY_MINUTES = intPreferencesKey("inactivity_minutes")
val CONFIDENCE_THRESHOLD = intPreferencesKey("confidence_threshold")
val BROADCAST_INTENTS = booleanPreferencesKey("broadcast_intents")
val LAST_HAC_STATUS = stringPreferencesKey("last_hac_status")
}
private val _logsFlow = MutableStateFlow<List<LogEntry>>(emptyList())
val logsFlow: StateFlow<List<LogEntry>> = _logsFlow.asStateFlow()
fun addLog(tag: String, message: String, isImportant: Boolean = false) {
val newEntry = LogEntry(tag = tag, message = message, isImportant = isImportant)
val current = _logsFlow.value.toMutableList()
current.add(0, newEntry)
if (current.size > 100) {
current.removeAt(current.size - 1)
}
_logsFlow.value = current
}
val settingsFlow: Flow<AppSettings> = context.dataStore.data.map { prefs ->
val modeStr = prefs[PreferencesKeys.HAC_MODE] ?: HacMode.WEBHOOK.name
val mode = try {
HacMode.valueOf(modeStr)
} catch (_: Exception) {
HacMode.WEBHOOK
}
AppSettings(
isTrackingActive = prefs[PreferencesKeys.IS_TRACKING_ACTIVE] ?: false,
currentState = prefs[PreferencesKeys.CURRENT_STATE] ?: "AWAKE",
lastStateChangeMillis = prefs[PreferencesKeys.LAST_STATE_CHANGE] ?: System.currentTimeMillis(),
hacMode = mode,
hacWebhookUrl = prefs[PreferencesKeys.HAC_WEBHOOK_URL] ?: "",
hacRestUrl = prefs[PreferencesKeys.HAC_REST_URL] ?: "",
hacToken = prefs[PreferencesKeys.HAC_TOKEN] ?: "",
inactivityMinutes = prefs[PreferencesKeys.INACTIVITY_MINUTES] ?: 20,
confidenceThreshold = prefs[PreferencesKeys.CONFIDENCE_THRESHOLD] ?: 70,
broadcastIntents = prefs[PreferencesKeys.BROADCAST_INTENTS] ?: true,
lastHacStatus = prefs[PreferencesKeys.LAST_HAC_STATUS] ?: "Готов к работе"
)
}
suspend fun getSettings(): AppSettings = settingsFlow.first()
suspend fun updateTrackingActive(active: Boolean) {
context.dataStore.edit { it[PreferencesKeys.IS_TRACKING_ACTIVE] = active }
}
suspend fun updateCurrentState(state: String, timestamp: Long = System.currentTimeMillis()) {
context.dataStore.edit {
it[PreferencesKeys.CURRENT_STATE] = state
it[PreferencesKeys.LAST_STATE_CHANGE] = timestamp
}
}
suspend fun updateHacSettings(
mode: HacMode,
webhookUrl: String,
restUrl: String,
token: String
) {
context.dataStore.edit {
it[PreferencesKeys.HAC_MODE] = mode.name
it[PreferencesKeys.HAC_WEBHOOK_URL] = webhookUrl
it[PreferencesKeys.HAC_REST_URL] = restUrl
it[PreferencesKeys.HAC_TOKEN] = token
}
}
suspend fun updateDetectionSensitivity(
inactivityMinutes: Int,
confidenceThreshold: Int,
broadcastIntents: Boolean
) {
context.dataStore.edit {
it[PreferencesKeys.INACTIVITY_MINUTES] = inactivityMinutes
it[PreferencesKeys.CONFIDENCE_THRESHOLD] = confidenceThreshold
it[PreferencesKeys.BROADCAST_INTENTS] = broadcastIntents
}
}
suspend fun updateHacStatus(status: String) {
context.dataStore.edit {
it[PreferencesKeys.LAST_HAC_STATUS] = status
}
}
}
@@ -0,0 +1,39 @@
package com.example.avtex.engine
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import android.util.Log
import com.example.avtex.AvtexApplication
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class BootReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
Log.d(TAG, "Boot completed event received")
val repo = AvtexApplication.instance.settingsRepository
CoroutineScope(Dispatchers.Default).launch {
val settings = repo.getSettings()
if (settings.isTrackingActive) {
Log.d(TAG, "Restarting SleepTrackingService after reboot")
val serviceIntent = Intent(context, SleepTrackingService::class.java).apply {
action = SleepTrackingService.ACTION_START_TRACKING
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(serviceIntent)
} else {
context.startService(serviceIntent)
}
}
}
}
}
companion object {
private const val TAG = "BootReceiver"
}
}
@@ -0,0 +1,54 @@
package com.example.avtex.engine
import android.content.Context
import android.hardware.Sensor
import android.hardware.SensorManager
import android.hardware.TriggerEvent
import android.hardware.TriggerEventListener
import android.util.Log
class SignificantMotionTrigger(
private val context: Context,
private val onMotionDetected: () -> Unit
) {
private val sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager
private val motionSensor: Sensor? = sensorManager.getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION)
private var isArmed = false
private val triggerListener = object : TriggerEventListener() {
override fun onTrigger(event: TriggerEvent?) {
Log.d(TAG, "Hardware Significant Motion Triggered!")
isArmed = false
onMotionDetected()
}
}
val isAvailable: Boolean
get() = motionSensor != null
fun arm(): Boolean {
if (motionSensor == null) {
Log.w(TAG, "Significant motion sensor not available")
return false
}
if (isArmed) {
sensorManager.cancelTriggerSensor(triggerListener, motionSensor)
}
isArmed = sensorManager.requestTriggerSensor(triggerListener, motionSensor)
Log.d(TAG, "Significant motion sensor armed: $isArmed")
return isArmed
}
fun disarm() {
if (motionSensor != null && isArmed) {
sensorManager.cancelTriggerSensor(triggerListener, motionSensor)
isArmed = false
Log.d(TAG, "Significant motion sensor disarmed")
}
}
companion object {
private const val TAG = "SignificantMotion"
}
}
@@ -0,0 +1,54 @@
package com.example.avtex.engine
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import com.example.avtex.AvtexApplication
import com.google.android.gms.location.SleepClassifyEvent
import com.google.android.gms.location.SleepSegmentEvent
class SleepReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (SleepClassifyEvent.hasEvents(intent)) {
val events = SleepClassifyEvent.extractEvents(intent)
for (event in events) {
val confidence = event.confidence
val motion = event.motion
val light = event.light
Log.d(TAG, "SleepClassifyEvent: confidence=$confidence, motion=$motion, light=$light")
val now = System.currentTimeMillis()
if (now - lastClassifyLogTimestamp >= MIN_CLASSIFY_LOG_INTERVAL_MS) {
lastClassifyLogTimestamp = now
AvtexApplication.instance.settingsRepository.addLog(
tag = "SleepAPI",
message = "Классификация (раз в час): уверенность $confidence%, движение $motion/6, свет $light/6"
)
}
SleepTrackingService.onSleepClassifyReceived(context, confidence, motion, light)
}
}
if (SleepSegmentEvent.hasEvents(intent)) {
val events = SleepSegmentEvent.extractEvents(intent)
for (event in events) {
Log.d(TAG, "SleepSegmentEvent: ${event.startTimeMillis} to ${event.endTimeMillis} (status=${event.status})")
AvtexApplication.instance.settingsRepository.addLog(
tag = "SleepSegment",
message = "Сегмент сна завершен: ${event.startTimeMillis} - ${event.endTimeMillis}",
isImportant = true
)
SleepTrackingService.onSleepSegmentReceived(context, event.startTimeMillis, event.endTimeMillis, event.status)
}
}
}
companion object {
private const val TAG = "SleepReceiver"
const val ACTION_SLEEP_API_EVENT = "com.example.avtex.ACTION_SLEEP_API_EVENT"
private const val MIN_CLASSIFY_LOG_INTERVAL_MS = 60 * 60 * 1000L // Минимум 1 раз в час
@Volatile
private var lastClassifyLogTimestamp: Long = 0L
}
}
@@ -0,0 +1,6 @@
package com.example.avtex.engine
enum class SleepState {
AWAKE,
ASLEEP
}
@@ -0,0 +1,373 @@
package com.example.avtex.engine
import android.Manifest
import android.app.Notification
import android.app.PendingIntent
import android.app.Service
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.os.Build
import android.os.IBinder
import android.os.PowerManager
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat
import com.example.avtex.AvtexApplication
import com.example.avtex.MainActivity
import com.example.avtex.hac.HacClient
import com.google.android.gms.location.ActivityRecognition
import com.google.android.gms.location.SleepSegmentRequest
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
class SleepTrackingService : Service() {
private val serviceScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
private lateinit var significantMotionTrigger: SignificantMotionTrigger
private lateinit var hacClient: HacClient
private var screenReceiver: BroadcastReceiver? = null
private var inactivityJob: Job? = null
private var sleepApiPendingIntent: PendingIntent? = null
private var currentState: SleepState = SleepState.AWAKE
private var stateChangeTimestamp: Long = System.currentTimeMillis()
private val settingsRepository get() = AvtexApplication.instance.settingsRepository
override fun onCreate() {
super.onCreate()
Log.d(TAG, "SleepTrackingService onCreate")
hacClient = HacClient(this)
significantMotionTrigger = SignificantMotionTrigger(this) {
handleWakeupDetected(source = "significant_motion")
}
registerScreenReceiver()
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val action = intent?.action ?: ACTION_START_TRACKING
Log.d(TAG, "onStartCommand action: $action")
when (action) {
ACTION_START_TRACKING -> {
startForegroundServiceWithNotification()
initializeTracking()
}
ACTION_STOP_TRACKING -> {
stopTracking()
stopForeground(STOP_FOREGROUND_REMOVE)
stopSelf()
}
ACTION_MANUAL_TOGGLE -> {
val newState = if (currentState == SleepState.AWAKE) SleepState.ASLEEP else SleepState.AWAKE
transitionToState(newState, source = "manual_toggle", confidence = 100)
}
ACTION_TEST_HAC -> {
serviceScope.launch {
val settings = settingsRepository.getSettings()
val isAsleep = currentState == SleepState.ASLEEP
settingsRepository.addLog("HAC_TEST", "Отправка тестового запроса в Home Assistant...")
val result = hacClient.sendSleepEvent(settings, isAsleep, source = "manual_test")
result.onSuccess { msg ->
settingsRepository.addLog("HAC_TEST", "Тест успешен: $msg", isImportant = true)
settingsRepository.updateHacStatus("Тест успешен: $msg")
}.onFailure { err ->
settingsRepository.addLog("HAC_TEST", "Ошибка теста: ${err.localizedMessage}", isImportant = true)
settingsRepository.updateHacStatus("Ошибка: ${err.localizedMessage}")
}
}
}
}
return START_STICKY
}
private fun startForegroundServiceWithNotification() {
val notification = buildNotification(
title = "Avtex: Мониторинг активности",
text = "Статус: ${if (currentState == SleepState.ASLEEP) "Спит 🌙" else "Бодрствует ☀️"}"
)
startForeground(NOTIFICATION_ID, notification)
}
private fun initializeTracking() {
serviceScope.launch {
val settings = settingsRepository.getSettings()
currentState = if (settings.currentState == "ASLEEP") SleepState.ASLEEP else SleepState.AWAKE
stateChangeTimestamp = settings.lastStateChangeMillis
settingsRepository.updateTrackingActive(true)
settingsRepository.addLog("Engine", "Служба мониторинга сна запущена", isImportant = true)
updateNotification()
subscribeToSleepApi()
if (currentState == SleepState.ASLEEP) {
significantMotionTrigger.arm()
}
}
}
private fun stopTracking() {
serviceScope.launch {
settingsRepository.updateTrackingActive(false)
settingsRepository.addLog("Engine", "Служба мониторинга остановлена", isImportant = true)
significantMotionTrigger.disarm()
unsubscribeFromSleepApi()
inactivityJob?.cancel()
}
}
private fun registerScreenReceiver() {
screenReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
when (intent.action) {
Intent.ACTION_SCREEN_OFF -> {
Log.d(TAG, "Screen OFF event received")
handleScreenOff()
}
Intent.ACTION_SCREEN_ON -> {
Log.d(TAG, "Screen ON event received")
}
Intent.ACTION_USER_PRESENT -> {
Log.d(TAG, "User unlocked device")
handleWakeupDetected(source = "screen_unlocked")
}
}
}
}
val filter = IntentFilter().apply {
addAction(Intent.ACTION_SCREEN_OFF)
addAction(Intent.ACTION_SCREEN_ON)
addAction(Intent.ACTION_USER_PRESENT)
}
registerReceiver(screenReceiver, filter)
}
private fun handleScreenOff() {
if (currentState == SleepState.AWAKE) {
inactivityJob?.cancel()
inactivityJob = serviceScope.launch {
val settings = settingsRepository.getSettings()
val minutes = settings.inactivityMinutes
Log.d(TAG, "Starting inactivity countdown: $minutes minutes")
settingsRepository.addLog("Engine", "Экран выключен. Ожидание сна ($minutes мин)...")
delay(minutes * 60 * 1000L)
// If still awake and not interrupted, assume sleep
if (currentState == SleepState.AWAKE) {
settingsRepository.addLog("Engine", "Время покоя истекло ($minutes мин). Переход в режим сна.", isImportant = true)
transitionToState(SleepState.ASLEEP, source = "inactivity_timer", confidence = 90)
}
}
}
}
private fun handleWakeupDetected(source: String) {
inactivityJob?.cancel()
if (currentState == SleepState.ASLEEP) {
Log.d(TAG, "Wakeup detected from source: $source")
transitionToState(SleepState.AWAKE, source = source, confidence = 100)
}
}
private fun transitionToState(newState: SleepState, source: String, confidence: Int) {
if (currentState == newState) return
currentState = newState
stateChangeTimestamp = System.currentTimeMillis()
serviceScope.launch {
val isAsleep = (newState == SleepState.ASLEEP)
val stateName = if (isAsleep) "ASLEEP" else "AWAKE"
settingsRepository.updateCurrentState(stateName, stateChangeTimestamp)
if (isAsleep) {
// When falling asleep, arm significant motion trigger for instant wake detection
val armed = significantMotionTrigger.arm()
settingsRepository.addLog(
"Engine",
"Зафиксировано засыпание ($source). Датчик движения взведен: $armed",
isImportant = true
)
} else {
// When waking up, disarm trigger
significantMotionTrigger.disarm()
settingsRepository.addLog(
"Engine",
"Зафиксировано пробуждение ($source).",
isImportant = true
)
}
updateNotification()
// Acquire temporary WakeLock to ensure HTTP request reaches HAC on OnePlus
val powerManager = getSystemService(Context.POWER_SERVICE) as PowerManager
val wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Avtex:HacTransmissionWakeLock")
wakeLock.acquire(15000L) // max 15 seconds
try {
val settings = settingsRepository.getSettings()
val result = hacClient.sendSleepEvent(settings, isAsleep, source, confidence)
result.onSuccess { msg ->
Log.d(TAG, "HAC send success: $msg")
settingsRepository.addLog("HAC", "Отправлено в Home Assistant: $msg")
settingsRepository.updateHacStatus("Отправлено в ${formatTime(stateChangeTimestamp)}: $msg")
}.onFailure { err ->
Log.e(TAG, "HAC send error", err)
settingsRepository.addLog("HAC", "Ошибка отправки в Home Assistant: ${err.localizedMessage}", isImportant = true)
settingsRepository.updateHacStatus("Ошибка: ${err.localizedMessage}")
}
} finally {
if (wakeLock.isHeld) {
wakeLock.release()
}
}
}
}
private fun subscribeToSleepApi() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACTIVITY_RECOGNITION) != PackageManager.PERMISSION_GRANTED) {
Log.w(TAG, "ACTIVITY_RECOGNITION permission not granted for Sleep API")
settingsRepository.addLog("SleepAPI", "Нет разрешения на активность. Используются аппаратные датчики движения.")
return
}
try {
val intent = Intent(this, SleepReceiver::class.java).apply {
action = SleepReceiver.ACTION_SLEEP_API_EVENT
}
val flags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
} else {
PendingIntent.FLAG_UPDATE_CURRENT
}
sleepApiPendingIntent = PendingIntent.getBroadcast(this, REQUEST_CODE_SLEEP_API, intent, flags)
val request = SleepSegmentRequest.getDefaultSleepSegmentRequest()
val task = ActivityRecognition.getClient(this).requestSleepSegmentUpdates(sleepApiPendingIntent!!, request)
task.addOnSuccessListener {
Log.d(TAG, "Successfully subscribed to Google Sleep API")
settingsRepository.addLog("SleepAPI", "Google Sleep API успешно подключен")
}
task.addOnFailureListener { e ->
Log.e(TAG, "Failed to subscribe to Google Sleep API", e)
settingsRepository.addLog("SleepAPI", "Не удалось подключить Google Sleep API: ${e.localizedMessage}")
}
} catch (e: Exception) {
Log.e(TAG, "Exception subscribing to Sleep API", e)
}
}
private fun unsubscribeFromSleepApi() {
sleepApiPendingIntent?.let { pi ->
try {
ActivityRecognition.getClient(this).removeSleepSegmentUpdates(pi)
Log.d(TAG, "Unsubscribed from Sleep API")
} catch (e: Exception) {
Log.e(TAG, "Error unsubscribing from Sleep API", e)
}
}
}
private fun buildNotification(title: String, text: String): Notification {
val openAppIntent = Intent(this, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
}
val flags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
} else {
PendingIntent.FLAG_UPDATE_CURRENT
}
val pendingIntent = PendingIntent.getActivity(this, 0, openAppIntent, flags)
return NotificationCompat.Builder(this, AvtexApplication.CHANNEL_ID_TRACKING)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(android.R.drawable.ic_lock_idle_charging)
.setContentIntent(pendingIntent)
.setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.build()
}
private fun updateNotification() {
val stateText = if (currentState == SleepState.ASLEEP) "Спит 🌙" else "Бодрствует ☀️"
val timeFormatted = formatTime(stateChangeTimestamp)
val notification = buildNotification(
title = "Avtex: $stateText",
text = "Состояние с $timeFormatted"
)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as android.app.NotificationManager
notificationManager.notify(NOTIFICATION_ID, notification)
}
private fun formatTime(millis: Long): String {
return SimpleDateFormat("HH:mm", Locale.getDefault()).format(Date(millis))
}
override fun onDestroy() {
Log.d(TAG, "SleepTrackingService onDestroy")
screenReceiver?.let {
unregisterReceiver(it)
screenReceiver = null
}
significantMotionTrigger.disarm()
unsubscribeFromSleepApi()
serviceScope.cancel()
super.onDestroy()
}
override fun onBind(intent: Intent?): IBinder? = null
companion object {
private const val TAG = "SleepTrackingService"
private const val NOTIFICATION_ID = 1001
private const val REQUEST_CODE_SLEEP_API = 2001
const val ACTION_START_TRACKING = "com.example.avtex.ACTION_START_TRACKING"
const val ACTION_STOP_TRACKING = "com.example.avtex.ACTION_STOP_TRACKING"
const val ACTION_MANUAL_TOGGLE = "com.example.avtex.ACTION_MANUAL_TOGGLE"
const val ACTION_TEST_HAC = "com.example.avtex.ACTION_TEST_HAC"
@Volatile
private var instance: SleepTrackingService? = null
fun onSleepClassifyReceived(context: Context, confidence: Int, motion: Int, light: Int) {
// If confidence is high and device is in stationary motion
val repo = AvtexApplication.instance.settingsRepository
CoroutineScope(Dispatchers.Default).launch {
val settings = repo.getSettings()
if (settings.isTrackingActive && settings.currentState == "AWAKE") {
if (confidence >= settings.confidenceThreshold && motion <= 2) {
Log.d(TAG, "SleepClassify indicates sleep: confidence=$confidence, motion=$motion")
// Send intent to service to transition state
val intent = Intent(context, SleepTrackingService::class.java).apply {
action = ACTION_MANUAL_TOGGLE // or specific intent
}
// Only trigger if actually confident
}
}
}
}
fun onSleepSegmentReceived(context: Context, start: Long, end: Long, status: Int) {
Log.d(TAG, "SleepSegment completed: start=$start, end=$end, status=$status")
}
}
}
@@ -0,0 +1,168 @@
package com.example.avtex.hac
import android.content.Context
import android.content.Intent
import com.example.avtex.data.AppSettings
import com.example.avtex.data.HacMode
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import java.util.concurrent.TimeUnit
class HacClient(private val context: Context) {
private val httpClient = OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.build()
private val jsonMediaType = "application/json; charset=utf-8".toMediaType()
suspend fun sendSleepEvent(
settings: AppSettings,
isAsleep: Boolean,
source: String,
confidence: Int = 100
): Result<String> = withContext(Dispatchers.IO) {
val stateStr = if (isAsleep) "asleep" else "awake"
val eventStr = if (isAsleep) "sleep_started" else "sleep_ended"
val timestamp = System.currentTimeMillis()
// 1. Send Local Broadcast if enabled
if (settings.broadcastIntents) {
val broadcastIntent = Intent(ACTION_SLEEP_STATE_CHANGED).apply {
putExtra("state", stateStr)
putExtra("event", eventStr)
putExtra("is_asleep", isAsleep)
putExtra("source", source)
putExtra("confidence", confidence)
putExtra("timestamp", timestamp)
setPackage(context.packageName)
}
context.sendBroadcast(broadcastIntent)
}
// 2. Send HTTP Request based on configured mode
when (settings.hacMode) {
HacMode.WEBHOOK -> {
if (settings.hacWebhookUrl.isBlank()) {
return@withContext Result.failure(IllegalArgumentException("Webhook URL не задан в настройках"))
}
sendWebhook(
url = settings.hacWebhookUrl.trim(),
event = eventStr,
state = stateStr,
confidence = confidence,
source = source,
timestamp = timestamp
)
}
HacMode.REST -> {
if (settings.hacRestUrl.isBlank()) {
return@withContext Result.failure(IllegalArgumentException("REST API URL не задан в настройках"))
}
sendRest(
url = settings.hacRestUrl.trim(),
token = settings.hacToken.trim(),
isAsleep = isAsleep,
confidence = confidence,
source = source,
timestamp = timestamp
)
}
}
}
private fun sendWebhook(
url: String,
event: String,
state: String,
confidence: Int,
source: String,
timestamp: Long
): Result<String> {
return try {
val json = JSONObject().apply {
put("event", event)
put("state", state)
put("confidence", confidence)
put("source", source)
put("timestamp", timestamp)
put("time_iso", SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX", Locale.US).format(Date(timestamp)))
}
val body = json.toString().toRequestBody(jsonMediaType)
val request = Request.Builder()
.url(url)
.post(body)
.build()
val response = httpClient.newCall(request).execute()
response.use {
if (it.isSuccessful) {
Result.success("Webhook OK (${it.code})")
} else {
Result.failure(Exception("Ошибка сервера: HTTP ${it.code} ${it.message}"))
}
}
} catch (e: Exception) {
Result.failure(e)
}
}
private fun sendRest(
url: String,
token: String,
isAsleep: Boolean,
confidence: Int,
source: String,
timestamp: Long
): Result<String> {
return try {
val binaryState = if (isAsleep) "on" else "off"
val attributes = JSONObject().apply {
put("device_class", "occupancy")
put("friendly_name", "Сон пользователя (Avtex)")
put("confidence", confidence)
put("source", source)
put("last_updated", SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX", Locale.US).format(Date(timestamp)))
}
val payload = JSONObject().apply {
put("state", binaryState)
put("attributes", attributes)
}
val body = payload.toString().toRequestBody(jsonMediaType)
val requestBuilder = Request.Builder()
.url(url)
.post(body)
if (token.isNotBlank()) {
requestBuilder.header("Authorization", "Bearer $token")
}
val response = httpClient.newCall(requestBuilder.build()).execute()
response.use {
if (it.isSuccessful) {
Result.success("REST OK (${it.code})")
} else {
Result.failure(Exception("Ошибка REST API: HTTP ${it.code} ${it.message}"))
}
}
} catch (e: Exception) {
Result.failure(e)
}
}
companion object {
const val ACTION_SLEEP_STATE_CHANGED = "com.example.avtex.SLEEP_STATE_CHANGED"
}
}
@@ -0,0 +1,24 @@
package com.example.avtex.notes
data class NoteItem(
val id: String,
val listId: String,
val title: String,
val description: String = "",
val isCompleted: Boolean = false,
val status: String = "todo", // "want_to_watch", "watched", "todo", "completed"
val priority: String = "normal",
val metadata: String = "{}",
val createdAt: Long = System.currentTimeMillis()
)
data class NoteList(
val id: String,
val title: String,
val category: String = "general", // "movies", "shopping", "tasks", "general"
val description: String = "",
val color: String? = null,
val icon: String? = null,
val items: List<NoteItem> = emptyList(),
val createdAt: Long = System.currentTimeMillis()
)
@@ -0,0 +1,189 @@
package com.example.avtex.notes
import android.content.ContentValues
import android.content.Context
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.util.UUID
class NotesRepository(context: Context) {
private val dbHelper = XFlowDatabaseHelper(context)
private val scope = CoroutineScope(Dispatchers.IO)
private val _listsFlow = MutableStateFlow<List<NoteList>>(emptyList())
val listsFlow: StateFlow<List<NoteList>> = _listsFlow.asStateFlow()
init {
reload()
}
fun reload() {
scope.launch {
val lists = loadListsFromDb()
_listsFlow.value = lists
}
}
private suspend fun loadListsFromDb(): List<NoteList> = withContext(Dispatchers.IO) {
val db = dbHelper.readableDatabase
val lists = mutableListOf<NoteList>()
val cursorLists = db.query(
XFlowDatabaseHelper.TABLE_LISTS,
null, null, null, null, null, "created_at DESC"
)
cursorLists.use { cLists ->
while (cLists.moveToNext()) {
val listId = cLists.getString(cLists.getColumnIndexOrThrow("id"))
val title = cLists.getString(cLists.getColumnIndexOrThrow("title"))
val category = cLists.getString(cLists.getColumnIndexOrThrow("category"))
val description = cLists.getString(cLists.getColumnIndexOrThrow("description")) ?: ""
val color = cLists.getString(cLists.getColumnIndexOrThrow("color"))
val icon = cLists.getString(cLists.getColumnIndexOrThrow("icon"))
val createdAt = cLists.getLong(cLists.getColumnIndexOrThrow("created_at"))
// Загружаем элементы списка
val items = mutableListOf<NoteItem>()
val cursorItems = db.query(
XFlowDatabaseHelper.TABLE_ITEMS,
null, "list_id = ?", arrayOf(listId), null, null, "created_at DESC"
)
cursorItems.use { cItems ->
while (cItems.moveToNext()) {
val itemId = cItems.getString(cItems.getColumnIndexOrThrow("id"))
val itemTitle = cItems.getString(cItems.getColumnIndexOrThrow("title"))
val itemDesc = cItems.getString(cItems.getColumnIndexOrThrow("description")) ?: ""
val isCompleted = cItems.getInt(cItems.getColumnIndexOrThrow("is_completed")) == 1
val status = cItems.getString(cItems.getColumnIndexOrThrow("status"))
val priority = cItems.getString(cItems.getColumnIndexOrThrow("priority"))
val metadata = cItems.getString(cItems.getColumnIndexOrThrow("metadata")) ?: "{}"
val itemCreatedAt = cItems.getLong(cItems.getColumnIndexOrThrow("created_at"))
items.add(
NoteItem(
id = itemId,
listId = listId,
title = itemTitle,
description = itemDesc,
isCompleted = isCompleted,
status = status,
priority = priority,
metadata = metadata,
createdAt = itemCreatedAt
)
)
}
}
lists.add(
NoteList(
id = listId,
title = title,
category = category,
description = description,
color = color,
icon = icon,
items = items,
createdAt = createdAt
)
)
}
}
lists
}
suspend fun addNoteItem(
title: String,
listTitle: String = "Фильмы на просмотр",
category: String = "movies",
description: String = ""
): NoteItem = withContext(Dispatchers.IO) {
val db = dbHelper.writableDatabase
// Ищем список по названию
var targetListId: String? = null
val c = db.query(
XFlowDatabaseHelper.TABLE_LISTS,
arrayOf("id"), "LOWER(title) = LOWER(?)", arrayOf(listTitle.trim()),
null, null, null
)
c.use {
if (it.moveToFirst()) {
targetListId = it.getString(0)
}
}
// Если список не найден, создаем его
if (targetListId == null) {
val newListId = "list-" + UUID.randomUUID().toString()
val cvList = ContentValues().apply {
put("id", newListId)
put("title", listTitle.trim())
put("category", category)
put("description", "Список $listTitle")
put("created_at", System.currentTimeMillis())
}
db.insert(XFlowDatabaseHelper.TABLE_LISTS, null, cvList)
targetListId = newListId
}
val newItemId = "item-" + UUID.randomUUID().toString()
val now = System.currentTimeMillis()
val cvItem = ContentValues().apply {
put("id", newItemId)
put("list_id", targetListId)
put("title", title.trim())
put("description", description)
put("is_completed", 0)
put("status", if (category == "movies") "want_to_watch" else "todo")
put("priority", "normal")
put("metadata", "{}")
put("created_at", now)
}
db.insert(XFlowDatabaseHelper.TABLE_ITEMS, null, cvItem)
reload()
NoteItem(
id = newItemId,
listId = targetListId,
title = title.trim(),
description = description,
isCompleted = false,
status = if (category == "movies") "want_to_watch" else "todo",
createdAt = now
)
}
suspend fun toggleItem(item: NoteItem) = withContext(Dispatchers.IO) {
val db = dbHelper.writableDatabase
val newCompleted = !item.isCompleted
val newStatus = if (newCompleted) {
if (item.status == "want_to_watch") "watched" else "completed"
} else {
if (item.status == "watched") "want_to_watch" else "todo"
}
val cv = ContentValues().apply {
put("is_completed", if (newCompleted) 1 else 0)
put("status", newStatus)
}
db.update(XFlowDatabaseHelper.TABLE_ITEMS, cv, "id = ?", arrayOf(item.id))
reload()
}
suspend fun deleteItem(itemId: String) = withContext(Dispatchers.IO) {
val db = dbHelper.writableDatabase
db.delete(XFlowDatabaseHelper.TABLE_ITEMS, "id = ?", arrayOf(itemId))
reload()
}
}
@@ -0,0 +1,72 @@
package com.example.avtex.notes
import android.content.ContentValues
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
class XFlowDatabaseHelper(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) {
override fun onCreate(db: SQLiteDatabase) {
db.execSQL("""
CREATE TABLE $TABLE_LISTS (
id TEXT PRIMARY KEY,
title TEXT NOT NULL,
category TEXT NOT NULL,
description TEXT,
color TEXT,
icon TEXT,
created_at INTEGER NOT NULL
)
""".trimIndent())
db.execSQL("""
CREATE TABLE $TABLE_ITEMS (
id TEXT PRIMARY KEY,
list_id TEXT NOT NULL,
title TEXT NOT NULL,
description TEXT,
is_completed INTEGER NOT NULL DEFAULT 0,
status TEXT NOT NULL,
priority TEXT NOT NULL,
metadata TEXT,
created_at INTEGER NOT NULL,
FOREIGN KEY(list_id) REFERENCES $TABLE_LISTS(id) ON DELETE CASCADE
)
""".trimIndent())
// Создаем дефолтные списки для удобства пользователя
val now = System.currentTimeMillis()
val cvMovies = ContentValues().apply {
put("id", "list-movies-default")
put("title", "Фильмы на просмотр")
put("category", "movies")
put("description", "Список фильмов и сериалов")
put("created_at", now)
}
db.insert(TABLE_LISTS, null, cvMovies)
val cvShopping = ContentValues().apply {
put("id", "list-shopping-default")
put("title", "Покупки")
put("category", "shopping")
put("description", "Список покупок")
put("created_at", now)
}
db.insert(TABLE_LISTS, null, cvShopping)
}
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
db.execSQL("DROP TABLE IF EXISTS $TABLE_ITEMS")
db.execSQL("DROP TABLE IF EXISTS $TABLE_LISTS")
onCreate(db)
}
companion object {
private const val DATABASE_NAME = "xflow_notes.db"
private const val DATABASE_VERSION = 1
const val TABLE_LISTS = "lists"
const val TABLE_ITEMS = "items"
}
}
@@ -0,0 +1,65 @@
package com.example.avtex.notes
import android.content.Context
import android.util.Log
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject
import java.util.concurrent.TimeUnit
class XFlowSyncClient(private val context: Context) {
private val httpClient = OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.build()
private val jsonMediaType = "application/json; charset=utf-8".toMediaType()
suspend fun syncItemToServer(
serverUrl: String,
title: String,
listTitle: String,
category: String,
ownerId: String = "default-user"
): Result<String> = withContext(Dispatchers.IO) {
if (serverUrl.isBlank()) {
return@withContext Result.failure(IllegalArgumentException("URL сервера XFlow не указан"))
}
try {
val endpoint = "${serverUrl.trimEnd('/')}/api/v1/lists/items"
val json = JSONObject().apply {
put("title", title)
put("listTitle", listTitle)
put("category", category)
put("ownerId", ownerId)
}
val request = Request.Builder()
.url(endpoint)
.post(json.toString().toRequestBody(jsonMediaType))
.build()
val response = httpClient.newCall(request).execute()
response.use {
if (it.isSuccessful) {
Result.success("Синхронизировано с XFlow")
} else {
Result.failure(Exception("HTTP ${it.code}: ${it.message}"))
}
}
} catch (e: Exception) {
Log.e(TAG, "Sync to XFlow server failed", e)
Result.failure(e)
}
}
companion object {
private const val TAG = "XFlowSyncClient"
}
}
@@ -0,0 +1,34 @@
package com.example.avtex.theme
import androidx.compose.ui.graphics.Color
// Obsidian & Midnight Backgrounds
val ObsidianBg = Color(0xFF090A0F)
val ObsidianSurface = Color(0xFF11131E)
val ObsidianSurfaceElevated = Color(0xFF181B2A)
val ObsidianSurfaceHigh = Color(0xFF21253A)
val GlassBorder = Color(0x1FFFFFFF)
// Sleep Neon Accents (Night / Sleep)
val NeonViolet = Color(0xFFA855F7)
val NeonPurple = Color(0xFF8B5CF6)
val ElectricIndigo = Color(0xFF6366F1)
val DeepIndigo = Color(0xFF1E1B4B)
val LavenderGlow = Color(0xFFE9D5FF)
// Wake Solar Accents (Day / Awake)
val SolarAmber = Color(0xFFF59E0B)
val SunsetOrange = Color(0xFFF97316)
val MorningGold = Color(0xFFFCD34D)
val DeepSolar = Color(0xFF451A03)
// Status & Indicators
val EmeraldMint = Color(0xFF10B981)
val EmeraldGlow = Color(0xFF34D399)
val CoralRed = Color(0xFFEF4444)
val CyanAccent = Color(0xFF06B6D4)
// Typography
val TextPrimary = Color(0xFFF8FAFC)
val TextSecondary = Color(0xFF94A3B8)
val TextMuted = Color(0xFF64748B)
@@ -0,0 +1,38 @@
package com.example.avtex.theme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.darkColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
private val AvtexDarkColorScheme = darkColorScheme(
primary = NeonViolet,
onPrimary = Color.White,
primaryContainer = DeepIndigo,
onPrimaryContainer = LavenderGlow,
secondary = SolarAmber,
onSecondary = Color.Black,
secondaryContainer = DeepSolar,
onSecondaryContainer = MorningGold,
tertiary = CyanAccent,
background = ObsidianBg,
onBackground = TextPrimary,
surface = ObsidianSurface,
onSurface = TextPrimary,
surfaceVariant = ObsidianSurfaceElevated,
onSurfaceVariant = TextSecondary,
surfaceContainerHighest = ObsidianSurfaceHigh,
outline = TextMuted,
outlineVariant = GlassBorder
)
@Composable
fun AvtexTheme(
content: @Composable () -> Unit
) {
MaterialTheme(
colorScheme = AvtexDarkColorScheme,
typography = Typography,
content = content
)
}
@@ -0,0 +1,36 @@
package com.example.avtex.theme
import androidx.compose.material3.Typography
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.sp
// Set of Material typography styles to start with
val Typography =
Typography(
bodyLarge =
TextStyle(
fontFamily = FontFamily.Default,
fontWeight = FontWeight.Normal,
fontSize = 16.sp,
lineHeight = 24.sp,
letterSpacing = 0.5.sp,
)
/* Other default text styles to override
titleLarge = TextStyle(
fontFamily = FontFamily.Default,
fontWeight = FontWeight.Normal,
fontSize = 22.sp,
lineHeight = 28.sp,
letterSpacing = 0.sp
),
labelSmall = TextStyle(
fontFamily = FontFamily.Default,
fontWeight = FontWeight.Medium,
fontSize = 11.sp,
lineHeight = 16.sp,
letterSpacing = 0.5.sp
)
*/
)
@@ -0,0 +1,728 @@
package com.example.avtex.ui
import android.Manifest
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.RepeatMode
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.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.Send
import androidx.compose.material.icons.filled.Bedtime
import androidx.compose.material.icons.filled.Bolt
import androidx.compose.material.icons.filled.CheckCircle
import androidx.compose.material.icons.filled.Hub
import androidx.compose.material.icons.filled.PlayArrow
import androidx.compose.material.icons.filled.Psychology
import androidx.compose.material.icons.filled.Refresh
import androidx.compose.material.icons.filled.Sensors
import androidx.compose.material.icons.filled.Stop
import androidx.compose.material.icons.filled.WbSunny
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ElevatedCard
import androidx.compose.material3.FilledTonalButton
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Switch
import androidx.compose.material3.SwitchDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.scale
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.content.ContextCompat
import com.example.avtex.AvtexApplication
import com.example.avtex.R
import com.example.avtex.data.AppSettings
import com.example.avtex.data.LogEntry
import com.example.avtex.engine.SleepTrackingService
import com.example.avtex.theme.CyanAccent
import com.example.avtex.theme.DeepIndigo
import com.example.avtex.theme.DeepSolar
import com.example.avtex.theme.ElectricIndigo
import com.example.avtex.theme.EmeraldGlow
import com.example.avtex.theme.EmeraldMint
import com.example.avtex.theme.GlassBorder
import com.example.avtex.theme.LavenderGlow
import com.example.avtex.theme.MorningGold
import com.example.avtex.theme.NeonPurple
import com.example.avtex.theme.NeonViolet
import com.example.avtex.theme.ObsidianBg
import com.example.avtex.theme.ObsidianSurface
import com.example.avtex.theme.ObsidianSurfaceElevated
import com.example.avtex.theme.ObsidianSurfaceHigh
import com.example.avtex.theme.SolarAmber
import com.example.avtex.theme.SunsetOrange
import com.example.avtex.theme.TextMuted
import com.example.avtex.theme.TextPrimary
import com.example.avtex.theme.TextSecondary
import java.util.concurrent.TimeUnit
@Composable
fun DashboardScreen(modifier: Modifier = Modifier) {
val context = LocalContext.current
val repository = remember { AvtexApplication.instance.settingsRepository }
val settings by repository.settingsFlow.collectAsState(initial = AppSettings())
val logs by repository.logsFlow.collectAsState(initial = emptyList())
val isAsleep = settings.currentState == "ASLEEP"
// Permission launcher for Notifications and Activity Recognition
val permissionsLauncher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.RequestMultiplePermissions()
) { permissions ->
val notifGranted = permissions[Manifest.permission.POST_NOTIFICATIONS] ?: true
val actGranted = permissions[Manifest.permission.ACTIVITY_RECOGNITION] ?: true
if (notifGranted && actGranted) {
startTrackingService(context)
}
}
fun onToggleTracking(enable: Boolean) {
if (enable) {
val neededPermissions = mutableListOf<String>()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&
ContextCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
neededPermissions.add(Manifest.permission.POST_NOTIFICATIONS)
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q &&
ContextCompat.checkSelfPermission(context, Manifest.permission.ACTIVITY_RECOGNITION) != PackageManager.PERMISSION_GRANTED) {
neededPermissions.add(Manifest.permission.ACTIVITY_RECOGNITION)
}
if (neededPermissions.isNotEmpty()) {
permissionsLauncher.launch(neededPermissions.toTypedArray())
} else {
startTrackingService(context)
}
} else {
stopTrackingService(context)
}
}
LazyColumn(
modifier = modifier
.fillMaxSize()
.background(ObsidianBg),
contentPadding = PaddingValues(16.dp),
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
// 1. Futuristic Brand Header
item {
BrandHeader(isTrackingActive = settings.isTrackingActive)
}
// 2. Dynamic Ambient Hero State Card
item {
DynamicHeroCard(
isAsleep = isAsleep,
isTrackingActive = settings.isTrackingActive,
lastStateChangeMillis = settings.lastStateChangeMillis
)
}
// 3. Engine Metrics Grid
item {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.spacedBy(10.dp)
) {
MetricGlassCard(
modifier = Modifier.weight(1f),
icon = Icons.Default.Sensors,
title = "Датчик движения",
value = if (isAsleep) "Взведен ⚡" else "Активен",
subtitle = "Significant Motion",
accentColor = if (isAsleep) NeonViolet else SolarAmber
)
MetricGlassCard(
modifier = Modifier.weight(1f),
icon = Icons.Default.Psychology,
title = "Google Sleep API",
value = "${settings.confidenceThreshold}%",
subtitle = "Порог нейросети",
accentColor = CyanAccent
)
MetricGlassCard(
modifier = Modifier.weight(1f),
icon = Icons.Default.Hub,
title = "Home Assistant",
value = settings.hacMode.name,
subtitle = "HAC протокол",
accentColor = EmeraldMint
)
}
}
// 4. Master Monitoring Glass Card
item {
Box(
modifier = Modifier
.fillMaxWidth()
.clip(RoundedCornerShape(20.dp))
.background(ObsidianSurfaceElevated)
.border(1.dp, GlassBorder, RoundedCornerShape(20.dp))
.padding(18.dp)
) {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Column(modifier = Modifier.weight(1f)) {
Row(verticalAlignment = Alignment.CenterVertically) {
Box(
modifier = Modifier
.size(8.dp)
.clip(CircleShape)
.background(if (settings.isTrackingActive) EmeraldGlow else TextMuted)
)
Spacer(modifier = Modifier.width(8.dp))
Text(
text = "Фоновый мониторинг сна",
fontWeight = FontWeight.Bold,
fontSize = 16.sp,
color = TextPrimary
)
}
Spacer(modifier = Modifier.height(4.dp))
Text(
text = if (settings.isTrackingActive) "Служба активна в фоне • нулевой расход батареи"
else "Служба остановлена • нажмите для запуска",
fontSize = 12.sp,
color = if (settings.isTrackingActive) EmeraldMint else TextMuted
)
}
Switch(
checked = settings.isTrackingActive,
onCheckedChange = { onToggleTracking(it) },
colors = SwitchDefaults.colors(
checkedThumbColor = Color.White,
checkedTrackColor = NeonViolet,
uncheckedThumbColor = TextMuted,
uncheckedTrackColor = ObsidianSurfaceHigh
)
)
}
}
}
// 5. Quick Action Control Buttons
item {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.spacedBy(10.dp)
) {
FilledTonalButton(
onClick = {
val intent = Intent(context, SleepTrackingService::class.java).apply {
action = SleepTrackingService.ACTION_MANUAL_TOGGLE
}
context.startService(intent)
},
modifier = Modifier
.weight(1f)
.height(48.dp),
shape = RoundedCornerShape(14.dp),
colors = ButtonDefaults.filledTonalButtonColors(
containerColor = ObsidianSurfaceElevated,
contentColor = TextPrimary
)
) {
Icon(
imageVector = Icons.Default.Refresh,
contentDescription = null,
modifier = Modifier.size(18.dp),
tint = if (isAsleep) SolarAmber else NeonViolet
)
Spacer(modifier = Modifier.width(8.dp))
Text(text = "Переключить", fontSize = 13.sp, fontWeight = FontWeight.Medium)
}
Button(
onClick = {
val intent = Intent(context, SleepTrackingService::class.java).apply {
action = SleepTrackingService.ACTION_TEST_HAC
}
context.startService(intent)
},
modifier = Modifier
.weight(1f)
.height(48.dp),
shape = RoundedCornerShape(14.dp),
colors = ButtonDefaults.buttonColors(
containerColor = NeonViolet,
contentColor = Color.White
)
) {
Icon(
imageVector = Icons.AutoMirrored.Filled.Send,
contentDescription = null,
modifier = Modifier.size(18.dp)
)
Spacer(modifier = Modifier.width(8.dp))
Text(text = "Тест в HAC", fontSize = 13.sp, fontWeight = FontWeight.SemiBold)
}
}
}
// 6. HAC Live Connection Banner
item {
Box(
modifier = Modifier
.fillMaxWidth()
.clip(RoundedCornerShape(16.dp))
.background(ObsidianSurface)
.border(1.dp, GlassBorder, RoundedCornerShape(16.dp))
.padding(14.dp)
) {
Row(verticalAlignment = Alignment.CenterVertically) {
Box(
modifier = Modifier
.clip(CircleShape)
.background(EmeraldMint.copy(alpha = 0.15f))
.padding(8.dp)
) {
Icon(
imageVector = Icons.Default.Bolt,
contentDescription = null,
tint = EmeraldMint,
modifier = Modifier.size(18.dp)
)
}
Spacer(modifier = Modifier.width(12.dp))
Column(modifier = Modifier.weight(1f)) {
Text(
text = "Home Assistant Status",
fontSize = 12.sp,
fontWeight = FontWeight.Bold,
color = TextSecondary
)
Text(
text = settings.lastHacStatus,
fontSize = 13.sp,
fontWeight = FontWeight.Medium,
color = TextPrimary
)
}
}
}
}
// 7. Audit Log Header
item {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(top = 4.dp),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = "ТЕЛЕМЕТРИЯ И СОБЫТИЯ",
fontSize = 12.sp,
fontWeight = FontWeight.Bold,
letterSpacing = 1.2.sp,
color = TextMuted
)
Text(
text = "${logs.size} событий",
fontSize = 11.sp,
color = TextMuted
)
}
}
if (logs.isEmpty()) {
item {
Box(
modifier = Modifier
.fillMaxWidth()
.clip(RoundedCornerShape(14.dp))
.background(ObsidianSurface)
.padding(24.dp),
contentAlignment = Alignment.Center
) {
Text(
text = "Событий пока нет. Включите мониторинг для сбора данных.",
fontSize = 13.sp,
color = TextMuted
)
}
}
} else {
items(logs) { log ->
FuturisticLogRow(log)
}
}
}
}
@Composable
private fun BrandHeader(isTrackingActive: Boolean) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 4.dp),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Row(verticalAlignment = Alignment.CenterVertically) {
Image(
painter = painterResource(id = R.drawable.app_logo),
contentDescription = "Avtex Logo",
modifier = Modifier
.size(42.dp)
.clip(RoundedCornerShape(12.dp))
.border(1.dp, GlassBorder, RoundedCornerShape(12.dp))
)
Spacer(modifier = Modifier.width(12.dp))
Column {
Text(
text = "AVTEX",
fontSize = 20.sp,
fontWeight = FontWeight.Black,
letterSpacing = 2.sp,
color = TextPrimary
)
Text(
text = "SMART SLEEP ENGINE",
fontSize = 9.sp,
fontWeight = FontWeight.Bold,
letterSpacing = 1.5.sp,
color = NeonViolet
)
}
}
Box(
modifier = Modifier
.clip(RoundedCornerShape(20.dp))
.background(if (isTrackingActive) EmeraldMint.copy(alpha = 0.15f) else ObsidianSurfaceElevated)
.border(
1.dp,
if (isTrackingActive) EmeraldMint.copy(alpha = 0.3f) else GlassBorder,
RoundedCornerShape(20.dp)
)
.padding(horizontal = 12.dp, vertical = 6.dp)
) {
Row(verticalAlignment = Alignment.CenterVertically) {
Box(
modifier = Modifier
.size(6.dp)
.clip(CircleShape)
.background(if (isTrackingActive) EmeraldGlow else TextMuted)
)
Spacer(modifier = Modifier.width(6.dp))
Text(
text = if (isTrackingActive) "ONLINE" else "STANDBY",
fontSize = 10.sp,
fontWeight = FontWeight.Bold,
letterSpacing = 1.sp,
color = if (isTrackingActive) EmeraldMint else TextMuted
)
}
}
}
}
@Composable
private fun DynamicHeroCard(
isAsleep: Boolean,
isTrackingActive: Boolean,
lastStateChangeMillis: Long
) {
val durationText = remember(lastStateChangeMillis) {
val diff = System.currentTimeMillis() - lastStateChangeMillis
val hours = TimeUnit.MILLISECONDS.toHours(diff)
val minutes = TimeUnit.MILLISECONDS.toMinutes(diff) % 60
if (hours > 0) "$hours ч $minutes мин" else "$minutes мин"
}
// Breathing glow animation
val infiniteTransition = rememberInfiniteTransition(label = "pulse")
val glowScale by infiniteTransition.animateFloat(
initialValue = 0.99f,
targetValue = 1.01f,
animationSpec = infiniteRepeatable(
animation = tween(2500),
repeatMode = RepeatMode.Reverse
),
label = "glowScale"
)
val gradientColors = if (isAsleep) {
listOf(
Color(0xFF0F0B24),
Color(0xFF1E1446),
Color(0xFF2A1560),
Color(0xFF4C1D95)
)
} else {
listOf(
Color(0xFF2E1005),
Color(0xFF4A1A05),
Color(0xFF7C2D12),
Color(0xFFB45309)
)
}
val accentBorderColor = if (isAsleep) NeonViolet.copy(alpha = 0.4f) else SolarAmber.copy(alpha = 0.4f)
Card(
modifier = Modifier
.fillMaxWidth()
.height(210.dp)
.scale(glowScale)
.border(1.dp, accentBorderColor, RoundedCornerShape(26.dp)),
shape = RoundedCornerShape(26.dp),
elevation = CardDefaults.cardElevation(defaultElevation = 8.dp)
) {
Box(
modifier = Modifier
.fillMaxSize()
.background(Brush.linearGradient(gradientColors))
.padding(22.dp)
) {
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.SpaceBetween
) {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Box(
modifier = Modifier
.clip(CircleShape)
.background(Color.White.copy(alpha = 0.12f))
.border(1.dp, Color.White.copy(alpha = 0.2f), CircleShape)
.padding(14.dp)
) {
Icon(
imageVector = if (isAsleep) Icons.Default.Bedtime else Icons.Default.WbSunny,
contentDescription = null,
tint = if (isAsleep) LavenderGlow else MorningGold,
modifier = Modifier.size(34.dp)
)
}
Box(
modifier = Modifier
.clip(RoundedCornerShape(12.dp))
.background(
if (isTrackingActive) EmeraldMint.copy(alpha = 0.25f)
else Color.White.copy(alpha = 0.1f)
)
.border(
1.dp,
if (isTrackingActive) EmeraldMint.copy(alpha = 0.5f)
else Color.White.copy(alpha = 0.15f),
RoundedCornerShape(12.dp)
)
.padding(horizontal = 12.dp, vertical = 6.dp)
) {
Text(
text = if (isTrackingActive) "МОНИТОРИНГ АКТИВЕН" else "ПАУЗА",
color = Color.White,
fontSize = 11.sp,
fontWeight = FontWeight.Bold,
letterSpacing = 0.8.sp
)
}
}
Column {
Text(
text = if (isAsleep) "Спит" else "Бодрствует",
color = Color.White,
fontSize = 36.sp,
fontWeight = FontWeight.Black,
letterSpacing = (-0.5).sp
)
Spacer(modifier = Modifier.height(2.dp))
Text(
text = if (isAsleep) "Датчик движения взведен • $durationText в покое"
else "Активность зафиксирована • $durationText с пробуждения",
color = Color.White.copy(alpha = 0.85f),
fontSize = 13.sp,
fontWeight = FontWeight.Medium
)
}
}
}
}
}
@Composable
private fun MetricGlassCard(
modifier: Modifier = Modifier,
icon: ImageVector,
title: String,
value: String,
subtitle: String,
accentColor: Color
) {
Box(
modifier = modifier
.clip(RoundedCornerShape(16.dp))
.background(ObsidianSurfaceElevated)
.border(1.dp, GlassBorder, RoundedCornerShape(16.dp))
.padding(12.dp)
) {
Column(verticalArrangement = Arrangement.SpaceBetween) {
Box(
modifier = Modifier
.clip(RoundedCornerShape(8.dp))
.background(accentColor.copy(alpha = 0.15f))
.padding(6.dp)
) {
Icon(
imageVector = icon,
contentDescription = null,
tint = accentColor,
modifier = Modifier.size(16.dp)
)
}
Spacer(modifier = Modifier.height(8.dp))
Text(
text = value,
fontSize = 13.sp,
fontWeight = FontWeight.Bold,
color = TextPrimary
)
Text(
text = title,
fontSize = 10.sp,
fontWeight = FontWeight.Medium,
color = TextMuted,
lineHeight = 12.sp
)
}
}
}
@Composable
private fun FuturisticLogRow(log: LogEntry) {
val tagColor = when (log.tag) {
"Engine" -> NeonViolet
"SleepAPI" -> CyanAccent
"SleepSegment" -> ElectricIndigo
"SignificantMotion" -> SolarAmber
"HAC", "HAC_TEST" -> EmeraldMint
else -> TextSecondary
}
Box(
modifier = Modifier
.fillMaxWidth()
.clip(RoundedCornerShape(12.dp))
.background(if (log.isImportant) ObsidianSurfaceHigh else ObsidianSurface)
.border(
1.dp,
if (log.isImportant) tagColor.copy(alpha = 0.4f) else GlassBorder,
RoundedCornerShape(12.dp)
)
.padding(horizontal = 14.dp, vertical = 10.dp)
) {
Row(
modifier = Modifier.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = log.formattedTime,
fontSize = 11.sp,
color = TextMuted,
fontFamily = FontFamily.Monospace,
fontWeight = FontWeight.SemiBold
)
Spacer(modifier = Modifier.width(10.dp))
Box(
modifier = Modifier
.clip(RoundedCornerShape(6.dp))
.background(tagColor.copy(alpha = 0.15f))
.border(1.dp, tagColor.copy(alpha = 0.3f), RoundedCornerShape(6.dp))
.padding(horizontal = 6.dp, vertical = 2.dp)
) {
Text(
text = log.tag,
fontSize = 10.sp,
color = tagColor,
fontWeight = FontWeight.Bold,
fontFamily = FontFamily.Monospace
)
}
Spacer(modifier = Modifier.width(10.dp))
Text(
text = log.message,
fontSize = 12.sp,
color = if (log.isImportant) TextPrimary else TextSecondary,
fontWeight = if (log.isImportant) FontWeight.Medium else FontWeight.Normal,
modifier = Modifier.weight(1f)
)
}
}
}
private fun startTrackingService(context: Context) {
val intent = Intent(context, SleepTrackingService::class.java).apply {
action = SleepTrackingService.ACTION_START_TRACKING
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent)
} else {
context.startService(intent)
}
}
private fun stopTrackingService(context: Context) {
val intent = Intent(context, SleepTrackingService::class.java).apply {
action = SleepTrackingService.ACTION_STOP_TRACKING
}
context.startService(intent)
}
@@ -0,0 +1,407 @@
package com.example.avtex.ui
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Check
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.filled.Movie
import androidx.compose.material.icons.filled.Refresh
import androidx.compose.material.icons.filled.ShoppingCart
import androidx.compose.material.icons.filled.TaskAlt
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Checkbox
import androidx.compose.material3.CheckboxDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.OutlinedTextFieldDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.avtex.AvtexApplication
import com.example.avtex.notes.NoteItem
import com.example.avtex.notes.NoteList
import com.example.avtex.theme.CyanAccent
import com.example.avtex.theme.ElectricIndigo
import com.example.avtex.theme.EmeraldMint
import com.example.avtex.theme.GlassBorder
import com.example.avtex.theme.NeonViolet
import com.example.avtex.theme.ObsidianBg
import com.example.avtex.theme.ObsidianSurface
import com.example.avtex.theme.ObsidianSurfaceElevated
import com.example.avtex.theme.ObsidianSurfaceHigh
import com.example.avtex.theme.SolarAmber
import com.example.avtex.theme.TextMuted
import com.example.avtex.theme.TextPrimary
import com.example.avtex.theme.TextSecondary
import kotlinx.coroutines.launch
@Composable
fun ListsScreen(modifier: Modifier = Modifier) {
val repository = remember { AvtexApplication.instance.notesRepository }
val lists by repository.listsFlow.collectAsState()
val coroutineScope = rememberCoroutineScope()
var selectedCategory by remember { mutableStateOf("all") }
var newItemText by remember { mutableStateOf("") }
var isAddingActive by remember { mutableStateOf(false) }
val categories = listOf(
"all" to "Все",
"movies" to "🎬 Фильмы",
"shopping" to "🛒 Покупки",
"tasks" to "⚡ Дела"
)
val filteredLists = remember(lists, selectedCategory) {
if (selectedCategory == "all") lists else lists.filter { it.category == selectedCategory }
}
LazyColumn(
modifier = modifier
.fillMaxSize()
.background(ObsidianBg)
.padding(horizontal = 16.dp),
contentPadding = PaddingValues(top = 16.dp, bottom = 32.dp),
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
// Header
item {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Column {
Text(
text = "XFlow Заметки & Списки",
fontSize = 22.sp,
fontWeight = FontWeight.Bold,
color = TextPrimary
)
Text(
text = "Мульти-записная книжка • Голос Gemini • Синхронизация",
fontSize = 12.sp,
color = TextMuted
)
}
IconButton(
onClick = { repository.reload() },
modifier = Modifier
.size(38.dp)
.clip(CircleShape)
.background(ObsidianSurfaceElevated)
.border(1.dp, GlassBorder, CircleShape)
) {
Icon(
imageVector = Icons.Default.Refresh,
contentDescription = "Обновить",
tint = CyanAccent,
modifier = Modifier.size(18.dp)
)
}
}
}
// Category Filter Chips
item {
LazyRow(
horizontalArrangement = Arrangement.spacedBy(8.dp),
modifier = Modifier.fillMaxWidth()
) {
items(categories) { (key, label) ->
val isSelected = selectedCategory == key
Box(
modifier = Modifier
.clip(RoundedCornerShape(12.dp))
.background(if (isSelected) NeonViolet.copy(alpha = 0.25f) else ObsidianSurfaceElevated)
.border(
1.dp,
if (isSelected) NeonViolet else GlassBorder,
RoundedCornerShape(12.dp)
)
.clickable { selectedCategory = key }
.padding(horizontal = 14.dp, vertical = 8.dp)
) {
Text(
text = label,
fontSize = 12.sp,
fontWeight = if (isSelected) FontWeight.Bold else FontWeight.Normal,
color = if (isSelected) TextPrimary else TextSecondary
)
}
}
}
}
// Quick Add Item Bar
item {
Column(
modifier = Modifier
.fillMaxWidth()
.clip(RoundedCornerShape(16.dp))
.background(ObsidianSurface)
.border(1.dp, GlassBorder, RoundedCornerShape(16.dp))
.padding(12.dp)
) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
OutlinedTextField(
value = newItemText,
onValueChange = { newItemText = it },
placeholder = {
Text(
if (selectedCategory == "movies") "Название фильма на просмотр..." else "Новая запись...",
fontSize = 13.sp,
color = TextMuted
)
},
colors = OutlinedTextFieldDefaults.colors(
focusedBorderColor = NeonViolet,
unfocusedBorderColor = GlassBorder,
focusedTextColor = TextPrimary,
unfocusedTextColor = TextPrimary,
cursorColor = NeonViolet
),
singleLine = true,
modifier = Modifier.weight(1f),
shape = RoundedCornerShape(12.dp)
)
Button(
onClick = {
if (newItemText.isNotBlank()) {
coroutineScope.launch {
val targetCategory = if (selectedCategory == "all") "movies" else selectedCategory
val listTitle = when (targetCategory) {
"movies" -> "Фильмы на просмотр"
"shopping" -> "Покупки"
else -> "Общий список"
}
repository.addNoteItem(
title = newItemText.trim(),
listTitle = listTitle,
category = targetCategory
)
newItemText = ""
}
}
},
colors = ButtonDefaults.buttonColors(containerColor = NeonViolet),
shape = RoundedCornerShape(12.dp),
modifier = Modifier.height(52.dp)
) {
Icon(Icons.Default.Add, contentDescription = "Добавить", tint = Color.White)
}
}
}
}
// Lists and Items
if (filteredLists.isEmpty()) {
item {
Box(
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 40.dp),
contentAlignment = Alignment.Center
) {
Text(
"Нет записей. Добавьте первую заметку или скажите Gemini!",
fontSize = 13.sp,
color = TextMuted
)
}
}
} else {
items(filteredLists, key = { it.id }) { list ->
ListCard(
list = list,
onToggleItem = { item ->
coroutineScope.launch { repository.toggleItem(item) }
},
onDeleteItem = { item ->
coroutineScope.launch { repository.deleteItem(item.id) }
}
)
}
}
}
}
@Composable
private fun ListCard(
list: NoteList,
onToggleItem: (NoteItem) -> Unit,
onDeleteItem: (NoteItem) -> Unit
) {
val accentColor = when (list.category) {
"movies" -> CyanAccent
"shopping" -> EmeraldMint
"tasks" -> SolarAmber
else -> NeonViolet
}
Column(
modifier = Modifier
.fillMaxWidth()
.clip(RoundedCornerShape(16.dp))
.background(ObsidianSurface)
.border(1.dp, GlassBorder, RoundedCornerShape(16.dp))
.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(10.dp)
) {
// List Title Header
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
Box(
modifier = Modifier
.size(8.dp)
.clip(CircleShape)
.background(accentColor)
)
Text(
text = list.title,
fontSize = 15.sp,
fontWeight = FontWeight.SemiBold,
color = TextPrimary
)
}
Text(
text = "${list.items.count { !it.isCompleted }} активных",
fontSize = 11.sp,
color = TextMuted
)
}
// Items list
if (list.items.isEmpty()) {
Text(
text = "Список пуст",
fontSize = 12.sp,
color = TextMuted,
modifier = Modifier.padding(start = 16.dp)
)
} else {
list.items.forEach { item ->
ItemRow(
item = item,
accentColor = accentColor,
onToggle = { onToggleItem(item) },
onDelete = { onDeleteItem(item) }
)
}
}
}
}
@Composable
private fun ItemRow(
item: NoteItem,
accentColor: Color,
onToggle: () -> Unit,
onDelete: () -> Unit
) {
Row(
modifier = Modifier
.fillMaxWidth()
.clip(RoundedCornerShape(10.dp))
.background(ObsidianSurfaceElevated)
.border(1.dp, GlassBorder.copy(alpha = 0.5f), RoundedCornerShape(10.dp))
.clickable { onToggle() }
.padding(horizontal = 10.dp, vertical = 6.dp),
verticalAlignment = Alignment.CenterVertically
) {
Checkbox(
checked = item.isCompleted,
onCheckedChange = { onToggle() },
colors = CheckboxDefaults.colors(
checkedColor = accentColor,
uncheckedColor = TextMuted,
checkmarkColor = ObsidianBg
),
modifier = Modifier.size(24.dp)
)
Spacer(modifier = Modifier.width(8.dp))
Column(modifier = Modifier.weight(1f)) {
Text(
text = item.title,
fontSize = 14.sp,
color = if (item.isCompleted) TextMuted else TextPrimary,
textDecoration = if (item.isCompleted) TextDecoration.LineThrough else TextDecoration.None
)
if (item.status == "want_to_watch") {
Text(
text = "🎬 Буду смотреть",
fontSize = 10.sp,
color = CyanAccent
)
} else if (item.status == "watched") {
Text(
text = "✔ Просмотрено",
fontSize = 10.sp,
color = EmeraldMint
)
}
}
IconButton(
onClick = onDelete,
modifier = Modifier.size(28.dp)
) {
Icon(
imageVector = Icons.Default.Delete,
contentDescription = "Удалить",
tint = TextMuted,
modifier = Modifier.size(15.dp)
)
}
}
}
@@ -0,0 +1,329 @@
package com.example.avtex.ui
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.PowerManager
import android.provider.Settings
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.BatteryAlert
import androidx.compose.material.icons.filled.CheckCircle
import androidx.compose.material.icons.filled.Lock
import androidx.compose.material.icons.filled.PowerSettingsNew
import androidx.compose.material.icons.filled.Shield
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.avtex.theme.CyanAccent
import com.example.avtex.theme.EmeraldGlow
import com.example.avtex.theme.EmeraldMint
import com.example.avtex.theme.GlassBorder
import com.example.avtex.theme.NeonViolet
import com.example.avtex.theme.ObsidianBg
import com.example.avtex.theme.ObsidianSurface
import com.example.avtex.theme.ObsidianSurfaceElevated
import com.example.avtex.theme.SolarAmber
import com.example.avtex.theme.TextMuted
import com.example.avtex.theme.TextPrimary
import com.example.avtex.theme.TextSecondary
@Composable
fun OnePlusGuideScreen(modifier: Modifier = Modifier) {
val context = LocalContext.current
val powerManager = remember { context.getSystemService(Context.POWER_SERVICE) as PowerManager }
val isIgnoringBatteryOptimizations = remember {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
powerManager.isIgnoringBatteryOptimizations(context.packageName)
} else true
}
LazyColumn(
modifier = modifier
.fillMaxSize()
.background(ObsidianBg),
contentPadding = PaddingValues(16.dp),
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
item {
Column(modifier = Modifier.padding(vertical = 4.dp)) {
Text(
text = "Оптимизация для OnePlus",
fontSize = 24.sp,
fontWeight = FontWeight.Bold,
color = TextPrimary
)
Text(
text = "Настройка OxygenOS / ColorOS для работы в фоновом режиме",
fontSize = 13.sp,
color = TextMuted
)
}
}
// 1. Battery Optimization Step
item {
OnePlusStepCard(
step = "01",
icon = Icons.Default.BatteryAlert,
title = "Отключить оптимизацию батареи",
status = if (isIgnoringBatteryOptimizations) "АКТИВНО" else "ТРЕБУЕТСЯ",
statusColor = if (isIgnoringBatteryOptimizations) EmeraldMint else SolarAmber,
description = "OxygenOS замораживает фоновые службы при выключении экрана. Для бесперебойной работы установите режим «Не оптимизировать» (Unrestricted).",
actionLabel = "Открыть настройки батареи",
onAction = {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
try {
val intent = Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS).apply {
data = Uri.parse("package:${context.packageName}")
}
context.startActivity(intent)
} catch (_: Exception) {
val intent = Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS)
context.startActivity(intent)
}
}
}
)
}
// 2. Auto-Launch & Background Activity Step
item {
OnePlusStepCard(
step = "02",
icon = Icons.Default.PowerSettingsNew,
title = "Автозапуск и фоновая активность",
status = "РЕКОМЕНДУЕТСЯ",
statusColor = CyanAccent,
description = "В свойствах приложения перейдите в «Использование батареи» и включите пункты:\n• «Разрешить работу в фоновом режиме»\n• «Разрешить автозапуск» (Auto-launch).",
actionLabel = "Свойства приложения в системе",
onAction = {
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply {
data = Uri.parse("package:${context.packageName}")
}
context.startActivity(intent)
}
)
}
// 3. Lock in Recents Step
item {
Box(
modifier = Modifier
.fillMaxWidth()
.clip(RoundedCornerShape(22.dp))
.background(ObsidianSurfaceElevated)
.border(1.dp, GlassBorder, RoundedCornerShape(22.dp))
.padding(18.dp)
) {
Column {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Row(verticalAlignment = Alignment.CenterVertically) {
Box(
modifier = Modifier
.clip(RoundedCornerShape(10.dp))
.background(NeonViolet.copy(alpha = 0.15f))
.padding(8.dp)
) {
Icon(
imageVector = Icons.Default.Lock,
contentDescription = null,
tint = NeonViolet,
modifier = Modifier.size(20.dp)
)
}
Spacer(modifier = Modifier.width(10.dp))
Text(
text = "Закрепить в недавних",
fontWeight = FontWeight.Bold,
fontSize = 16.sp,
color = TextPrimary
)
}
Text(
text = "ШАГ 03",
fontSize = 11.sp,
fontWeight = FontWeight.Bold,
fontFamily = FontFamily.Monospace,
color = NeonViolet
)
}
Spacer(modifier = Modifier.height(10.dp))
Text(
text = "Откройте экран недавних приложений (свайп снизу вверх с удержанием), нажмите три точки над карточкой Avtex и выберите «Закрепить» (иконка замочка 🔒). Это предотвратит выгрузку приложения при очистке памяти.",
fontSize = 13.sp,
color = TextSecondary,
lineHeight = 18.sp
)
}
}
}
// 4. Hardware Sensor Hub Assurance
item {
Box(
modifier = Modifier
.fillMaxWidth()
.clip(RoundedCornerShape(20.dp))
.background(ObsidianSurface)
.border(1.dp, GlassBorder, RoundedCornerShape(20.dp))
.padding(16.dp)
) {
Row(verticalAlignment = Alignment.CenterVertically) {
Box(
modifier = Modifier
.clip(CircleShape)
.background(EmeraldMint.copy(alpha = 0.15f))
.padding(10.dp)
) {
Icon(
imageVector = Icons.Default.Shield,
contentDescription = null,
tint = EmeraldMint,
modifier = Modifier.size(22.dp)
)
}
Spacer(modifier = Modifier.width(14.dp))
Column {
Text(
text = "Аппаратная энергоэффективность",
fontWeight = FontWeight.Bold,
fontSize = 14.sp,
color = TextPrimary
)
Spacer(modifier = Modifier.height(2.dp))
Text(
text = "Датчик Significant Motion работает на отдельном микроконтроллере (Sensor Hub). Батарея OnePlus не разряжается даже при круглосуточном отслеживании.",
fontSize = 12.sp,
color = TextMuted,
lineHeight = 16.sp
)
}
}
}
}
}
}
@Composable
private fun OnePlusStepCard(
step: String,
icon: ImageVector,
title: String,
status: String,
statusColor: Color,
description: String,
actionLabel: String,
onAction: () -> Unit
) {
Box(
modifier = Modifier
.fillMaxWidth()
.clip(RoundedCornerShape(22.dp))
.background(ObsidianSurfaceElevated)
.border(1.dp, GlassBorder, RoundedCornerShape(22.dp))
.padding(18.dp)
) {
Column {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Row(modifier = Modifier.weight(1f), verticalAlignment = Alignment.CenterVertically) {
Box(
modifier = Modifier
.clip(RoundedCornerShape(10.dp))
.background(statusColor.copy(alpha = 0.15f))
.padding(8.dp)
) {
Icon(
imageVector = icon,
contentDescription = null,
tint = statusColor,
modifier = Modifier.size(20.dp)
)
}
Spacer(modifier = Modifier.width(10.dp))
Text(
text = title,
fontWeight = FontWeight.Bold,
fontSize = 15.sp,
color = TextPrimary
)
}
Box(
modifier = Modifier
.clip(RoundedCornerShape(8.dp))
.background(statusColor.copy(alpha = 0.15f))
.border(1.dp, statusColor.copy(alpha = 0.3f), RoundedCornerShape(8.dp))
.padding(horizontal = 8.dp, vertical = 3.dp)
) {
Text(
text = status,
fontSize = 10.sp,
fontWeight = FontWeight.Bold,
fontFamily = FontFamily.Monospace,
color = statusColor
)
}
}
Spacer(modifier = Modifier.height(10.dp))
Text(
text = description,
fontSize = 13.sp,
color = TextSecondary,
lineHeight = 18.sp
)
Spacer(modifier = Modifier.height(14.dp))
OutlinedButton(
onClick = onAction,
shape = RoundedCornerShape(12.dp),
modifier = Modifier.fillMaxWidth(),
border = androidx.compose.foundation.BorderStroke(1.dp, GlassBorder),
colors = ButtonDefaults.outlinedButtonColors(
containerColor = ObsidianSurface,
contentColor = TextPrimary
)
) {
Text(text = actionLabel, fontSize = 13.sp, fontWeight = FontWeight.Medium)
}
}
}
}
@@ -0,0 +1,469 @@
package com.example.avtex.ui
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Check
import androidx.compose.material.icons.filled.Dns
import androidx.compose.material.icons.filled.Link
import androidx.compose.material.icons.filled.NotificationsActive
import androidx.compose.material.icons.filled.Save
import androidx.compose.material.icons.filled.Tune
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.OutlinedTextFieldDefaults
import androidx.compose.material3.Slider
import androidx.compose.material3.SliderDefaults
import androidx.compose.material3.Switch
import androidx.compose.material3.SwitchDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.avtex.AvtexApplication
import com.example.avtex.data.AppSettings
import com.example.avtex.data.HacMode
import com.example.avtex.theme.CyanAccent
import com.example.avtex.theme.EmeraldMint
import com.example.avtex.theme.GlassBorder
import com.example.avtex.theme.NeonViolet
import com.example.avtex.theme.ObsidianBg
import com.example.avtex.theme.ObsidianSurface
import com.example.avtex.theme.ObsidianSurfaceElevated
import com.example.avtex.theme.ObsidianSurfaceHigh
import com.example.avtex.theme.TextMuted
import com.example.avtex.theme.TextPrimary
import com.example.avtex.theme.TextSecondary
import kotlinx.coroutines.launch
import kotlin.math.roundToInt
@Composable
fun SettingsScreen(modifier: Modifier = Modifier) {
val repository = remember { AvtexApplication.instance.settingsRepository }
val settings by repository.settingsFlow.collectAsState(initial = AppSettings())
val coroutineScope = rememberCoroutineScope()
var selectedMode by remember(settings.hacMode) { mutableStateOf(settings.hacMode) }
var webhookUrl by remember(settings.hacWebhookUrl) { mutableStateOf(settings.hacWebhookUrl) }
var restUrl by remember(settings.hacRestUrl) { mutableStateOf(settings.hacRestUrl) }
var token by remember(settings.hacToken) { mutableStateOf(settings.hacToken) }
var inactivityMinutes by remember(settings.inactivityMinutes) { mutableFloatStateOf(settings.inactivityMinutes.toFloat()) }
var confidenceThreshold by remember(settings.confidenceThreshold) { mutableFloatStateOf(settings.confidenceThreshold.toFloat()) }
var broadcastIntents by remember(settings.broadcastIntents) { mutableStateOf(settings.broadcastIntents) }
var isSaved by remember { mutableStateOf(false) }
LazyColumn(
modifier = modifier
.fillMaxSize()
.background(ObsidianBg),
contentPadding = PaddingValues(16.dp),
verticalArrangement = Arrangement.spacedBy(18.dp)
) {
// 1. Header
item {
Column(modifier = Modifier.padding(vertical = 4.dp)) {
Text(
text = "Конфигурация",
fontSize = 24.sp,
fontWeight = FontWeight.Bold,
color = TextPrimary
)
Text(
text = "Интеграция с Home Assistant и калибровка сенсоров",
fontSize = 13.sp,
color = TextMuted
)
}
}
// 2. HAC Connection Glass Card
item {
Box(
modifier = Modifier
.fillMaxWidth()
.clip(RoundedCornerShape(22.dp))
.background(ObsidianSurfaceElevated)
.border(1.dp, GlassBorder, RoundedCornerShape(22.dp))
.padding(18.dp)
) {
Column {
Row(verticalAlignment = Alignment.CenterVertically) {
Box(
modifier = Modifier
.clip(RoundedCornerShape(10.dp))
.background(NeonViolet.copy(alpha = 0.15f))
.padding(8.dp)
) {
Icon(
imageVector = Icons.Default.Dns,
contentDescription = null,
tint = NeonViolet,
modifier = Modifier.size(20.dp)
)
}
Spacer(modifier = Modifier.width(10.dp))
Column {
Text(
text = "Связь с Home Assistant (HAC)",
fontWeight = FontWeight.Bold,
fontSize = 16.sp,
color = TextPrimary
)
Text(
text = "Отправка событий сна в умный дом",
fontSize = 11.sp,
color = TextMuted
)
}
}
Spacer(modifier = Modifier.height(16.dp))
// Mode Selector Tabs
Row(
modifier = Modifier
.fillMaxWidth()
.clip(RoundedCornerShape(14.dp))
.background(ObsidianSurface)
.border(1.dp, GlassBorder, RoundedCornerShape(14.dp))
.padding(4.dp)
) {
TabSelectorItem(
modifier = Modifier.weight(1f),
title = "Webhook URL",
isSelected = selectedMode == HacMode.WEBHOOK,
onClick = { selectedMode = HacMode.WEBHOOK }
)
TabSelectorItem(
modifier = Modifier.weight(1f),
title = "REST State API",
isSelected = selectedMode == HacMode.REST,
onClick = { selectedMode = HacMode.REST }
)
}
Spacer(modifier = Modifier.height(16.dp))
if (selectedMode == HacMode.WEBHOOK) {
OutlinedTextField(
value = webhookUrl,
onValueChange = { webhookUrl = it; isSaved = false },
label = { Text("Webhook URL") },
placeholder = { Text("http://192.168.1.50:8123/api/webhook/avtex_sleep") },
modifier = Modifier.fillMaxWidth(),
shape = RoundedCornerShape(14.dp),
singleLine = true,
colors = customTextFieldColors()
)
Spacer(modifier = Modifier.height(6.dp))
Text(
text = "Работает как с локальным адресом, так и через Nabu Casa / VPN.",
fontSize = 11.sp,
color = TextMuted
)
} else {
OutlinedTextField(
value = restUrl,
onValueChange = { restUrl = it; isSaved = false },
label = { Text("REST State URL") },
placeholder = { Text("http://192.168.1.50:8123/api/states/binary_sensor.phone_sleep") },
modifier = Modifier.fillMaxWidth(),
shape = RoundedCornerShape(14.dp),
singleLine = true,
colors = customTextFieldColors()
)
Spacer(modifier = Modifier.height(10.dp))
OutlinedTextField(
value = token,
onValueChange = { token = it; isSaved = false },
label = { Text("Long-Lived Access Token") },
placeholder = { Text("Токен доступа Home Assistant") },
modifier = Modifier.fillMaxWidth(),
shape = RoundedCornerShape(14.dp),
singleLine = true,
colors = customTextFieldColors()
)
}
Spacer(modifier = Modifier.height(16.dp))
// Broadcast toggle
Row(
modifier = Modifier
.fillMaxWidth()
.clip(RoundedCornerShape(14.dp))
.background(ObsidianSurface)
.padding(12.dp),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Row(modifier = Modifier.weight(1f), verticalAlignment = Alignment.CenterVertically) {
Icon(
imageVector = Icons.Default.NotificationsActive,
contentDescription = null,
tint = CyanAccent,
modifier = Modifier.size(20.dp)
)
Spacer(modifier = Modifier.width(10.dp))
Column {
Text(
text = "Локальный Broadcast Intent",
fontWeight = FontWeight.SemiBold,
fontSize = 13.sp,
color = TextPrimary
)
Text(
text = "Отправка интента для Tasker / Companion App",
fontSize = 11.sp,
color = TextMuted
)
}
}
Switch(
checked = broadcastIntents,
onCheckedChange = { broadcastIntents = it; isSaved = false },
colors = SwitchDefaults.colors(
checkedThumbColor = Color.White,
checkedTrackColor = CyanAccent
)
)
}
}
}
}
// 3. Sensor Tuning Glass Card
item {
Box(
modifier = Modifier
.fillMaxWidth()
.clip(RoundedCornerShape(22.dp))
.background(ObsidianSurfaceElevated)
.border(1.dp, GlassBorder, RoundedCornerShape(22.dp))
.padding(18.dp)
) {
Column {
Row(verticalAlignment = Alignment.CenterVertically) {
Box(
modifier = Modifier
.clip(RoundedCornerShape(10.dp))
.background(CyanAccent.copy(alpha = 0.15f))
.padding(8.dp)
) {
Icon(
imageVector = Icons.Default.Tune,
contentDescription = null,
tint = CyanAccent,
modifier = Modifier.size(20.dp)
)
}
Spacer(modifier = Modifier.width(10.dp))
Column {
Text(
text = "Параметры детектора сна",
fontWeight = FontWeight.Bold,
fontSize = 16.sp,
color = TextPrimary
)
Text(
text = "Калибровка порогов перехода в сон",
fontSize = 11.sp,
color = TextMuted
)
}
}
Spacer(modifier = Modifier.height(16.dp))
// Inactivity Slider
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween
) {
Text(
text = "Таймаут покоя до сна",
fontSize = 13.sp,
fontWeight = FontWeight.Medium,
color = TextSecondary
)
Text(
text = "${inactivityMinutes.roundToInt()} мин",
fontSize = 13.sp,
fontWeight = FontWeight.Bold,
color = NeonViolet
)
}
Text(
text = "Если экран выключен и нет движения указанное время, фиксируется сон.",
fontSize = 11.sp,
color = TextMuted
)
Slider(
value = inactivityMinutes,
onValueChange = { inactivityMinutes = it; isSaved = false },
valueRange = 5f..60f,
steps = 10,
colors = SliderDefaults.colors(
thumbColor = NeonViolet,
activeTrackColor = NeonViolet,
inactiveTrackColor = ObsidianSurfaceHigh
)
)
Spacer(modifier = Modifier.height(12.dp))
// Confidence Slider
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween
) {
Text(
text = "Порог уверенности Sleep API",
fontSize = 13.sp,
fontWeight = FontWeight.Medium,
color = TextSecondary
)
Text(
text = "${confidenceThreshold.roundToInt()}%",
fontSize = 13.sp,
fontWeight = FontWeight.Bold,
color = CyanAccent
)
}
Text(
text = "Минимальная оценка уверенности сна от алгоритма Google Play Services.",
fontSize = 11.sp,
color = TextMuted
)
Slider(
value = confidenceThreshold,
onValueChange = { confidenceThreshold = it; isSaved = false },
valueRange = 50f..95f,
steps = 8,
colors = SliderDefaults.colors(
thumbColor = CyanAccent,
activeTrackColor = CyanAccent,
inactiveTrackColor = ObsidianSurfaceHigh
)
)
}
}
}
// 4. Save Button
item {
Button(
onClick = {
coroutineScope.launch {
repository.updateHacSettings(
mode = selectedMode,
webhookUrl = webhookUrl,
restUrl = restUrl,
token = token
)
repository.updateDetectionSensitivity(
inactivityMinutes = inactivityMinutes.roundToInt(),
confidenceThreshold = confidenceThreshold.roundToInt(),
broadcastIntents = broadcastIntents
)
isSaved = true
}
},
modifier = Modifier
.fillMaxWidth()
.height(52.dp),
shape = RoundedCornerShape(16.dp),
colors = ButtonDefaults.buttonColors(
containerColor = if (isSaved) EmeraldMint else NeonViolet,
contentColor = Color.White
)
) {
Icon(
imageVector = if (isSaved) Icons.Default.Check else Icons.Default.Save,
contentDescription = null,
modifier = Modifier.size(18.dp)
)
Spacer(modifier = Modifier.width(8.dp))
Text(
text = if (isSaved) "Настройки применены!" else "Сохранить конфигурацию",
fontWeight = FontWeight.SemiBold,
fontSize = 14.sp
)
}
}
}
}
@Composable
private fun TabSelectorItem(
modifier: Modifier = Modifier,
title: String,
isSelected: Boolean,
onClick: () -> Unit
) {
Box(
modifier = modifier
.clip(RoundedCornerShape(10.dp))
.background(if (isSelected) NeonViolet.copy(alpha = 0.25f) else Color.Transparent)
.border(
1.dp,
if (isSelected) NeonViolet.copy(alpha = 0.5f) else Color.Transparent,
RoundedCornerShape(10.dp)
)
.clickable { onClick() }
.padding(vertical = 10.dp),
contentAlignment = Alignment.Center
) {
Text(
text = title,
fontSize = 12.sp,
fontWeight = if (isSelected) FontWeight.Bold else FontWeight.Medium,
color = if (isSelected) Color.White else TextMuted
)
}
}
@Composable
private fun customTextFieldColors() = OutlinedTextFieldDefaults.colors(
focusedContainerColor = ObsidianSurface,
unfocusedContainerColor = ObsidianSurface,
focusedBorderColor = NeonViolet,
unfocusedBorderColor = GlassBorder,
focusedLabelColor = NeonViolet,
unfocusedLabelColor = TextMuted,
focusedTextColor = TextPrimary,
unfocusedTextColor = TextPrimary,
cursorColor = NeonViolet
)
Binary file not shown.

After

Width:  |  Height:  |  Size: 266 KiB

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="108dp"
android:height="108dp"
android:viewportWidth="108"
android:viewportHeight="108">
<path
android:fillColor="#0D0E1A"
android:pathData="M0,0h108v108h-108z" />
</vector>
Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@drawable/ic_launcher_background" />
<foreground android:drawable="@drawable/ic_launcher_foreground" />
<monochrome android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon>
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@drawable/ic_launcher_background" />
<foreground android:drawable="@drawable/ic_launcher_foreground" />
<monochrome android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon>
Binary file not shown.

After

Width:  |  Height:  |  Size: 688 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 866 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 434 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 570 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

@@ -0,0 +1,3 @@
<resources>
<string name="app_name">XFlow</string>
</resources>
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Avtex" parent="android:Theme.Material.Light.NoActionBar" />
</resources>
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?><!--
Sample backup rules file; uncomment and customize as necessary.
See https://developer.android.com/guide/topics/data/autobackup
for details.
Note: This file is ignored for devices older than API 31
See https://developer.android.com/about/versions/12/backup-restore
-->
<full-backup-content>
<!--
<include domain="sharedpref" path="."/>
<exclude domain="sharedpref" path="device.xml"/>
-->
</full-backup-content>
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?><!--
Sample data extraction rules file; uncomment and customize as necessary.
See https://developer.android.com/about/versions/12/backup-restore#xml-changes
for details.
-->
<data-extraction-rules>
<cloud-backup>
<!-- TODO: Use <include> and <exclude> to control what is backed up.
<include .../>
<exclude .../>
-->
</cloud-backup>
<!--
<device-transfer>
<include .../>
<exclude .../>
</device-transfer>
-->
</data-extraction-rules>
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Добавление элемента в список через Gemini / Google Assistant -->
<capability android:name="actions.intent.ADD_ITEM_TO_LIST">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="com.example.avtex"
android:targetClass="com.example.avtex.MainActivity"
android:data="xflow://lists/add">
<parameter
android:name="itemList.name"
android:key="list_name" />
<parameter
android:name="itemListElement.name"
android:key="item_name" />
</intent>
</capability>
<!-- Создание списка через Gemini / Google Assistant -->
<capability android:name="actions.intent.CREATE_ITEM_LIST">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="com.example.avtex"
android:targetClass="com.example.avtex.MainActivity"
android:data="xflow://lists/create">
<parameter
android:name="itemList.name"
android:key="list_name" />
</intent>
</capability>
<!-- Просмотр списка -->
<capability android:name="actions.intent.GET_ITEM_LIST">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="com.example.avtex"
android:targetClass="com.example.avtex.MainActivity"
android:data="xflow://lists/view">
<parameter
android:name="itemList.name"
android:key="list_name" />
</intent>
</capability>
</shortcuts>
@@ -0,0 +1,34 @@
package com.example.avtex
import com.example.avtex.data.AppSettings
import com.example.avtex.data.HacMode
import com.example.avtex.data.LogEntry
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
class AppSettingsTest {
@Test
fun defaultSettings_areCorrect() {
val settings = AppSettings()
assertEquals(false, settings.isTrackingActive)
assertEquals("AWAKE", settings.currentState)
assertEquals(HacMode.WEBHOOK, settings.hacMode)
assertEquals(20, settings.inactivityMinutes)
assertEquals(70, settings.confidenceThreshold)
assertEquals(true, settings.broadcastIntents)
}
@Test
fun logEntry_formatsTimeProperly() {
val entry = LogEntry(
timestamp = 1726830000000L,
tag = "TestTag",
message = "Test message"
)
assertTrue(entry.formattedTime.matches(Regex("\\d{2}:\\d{2}:\\d{2}")))
assertEquals("TestTag", entry.tag)
assertEquals("Test message", entry.message)
}
}