x1ongzhu 1 rok pred
rodič
commit
2223830bdb

+ 9 - 0
app/src/main/java/com/example/modifier/data/AppPreferences.kt

@@ -0,0 +1,9 @@
+package com.example.modifier.data
+
+data class AppPreferences(
+    val server: String,
+    val id: String,
+    val name: String,
+    val preventClean: Boolean,
+    val preventRequest: Boolean
+)

+ 72 - 0
app/src/main/java/com/example/modifier/data/AppPreferencesRepository.kt

@@ -0,0 +1,72 @@
+package com.example.modifier.data
+
+import android.annotation.SuppressLint
+import android.content.Context
+import android.os.Build
+import android.provider.Settings
+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.stringPreferencesKey
+import kotlinx.coroutines.flow.map
+
+class AppPreferencesRepository(
+    private val dataStore: DataStore<Preferences>,
+    private val context: Context
+) {
+
+    private object PreferencesKeys {
+        val SERVER = stringPreferencesKey("server")
+        val ID = stringPreferencesKey("id")
+        val NAME = stringPreferencesKey("name")
+        val PREVENT_CLEAN = booleanPreferencesKey("prevent_clean")
+        val PREVENT_REQUEST = booleanPreferencesKey("prevent_request")
+    }
+
+    @SuppressLint("HardwareIds")
+    val appPreferencesFlow = dataStore.data.map {
+        val server = it[PreferencesKeys.SERVER] ?: "http://47.98.225.28"
+        var id = it[PreferencesKeys.ID] ?: ""
+        if (id.isEmpty()) {
+            id = Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID)
+            dataStore.edit { preferences ->
+                preferences[PreferencesKeys.ID] = id
+            }
+        }
+        val name = it[PreferencesKeys.NAME] ?: Build.DEVICE
+        val preventClean = it[PreferencesKeys.PREVENT_CLEAN] ?: false
+        val preventRequest = it[PreferencesKeys.PREVENT_REQUEST] ?: false
+        AppPreferences(
+            server = server,
+            id = id,
+            name = name,
+            preventClean = preventClean,
+            preventRequest = preventRequest
+        )
+    }
+
+    suspend fun updateServer(server: String) {
+        dataStore.edit { preferences ->
+            preferences[PreferencesKeys.SERVER] = server
+        }
+    }
+
+    suspend fun updateName(name: String) {
+        dataStore.edit { preferences ->
+            preferences[PreferencesKeys.NAME] = name
+        }
+    }
+
+    suspend fun updatePreventClean(preventClean: Boolean) {
+        dataStore.edit { preferences ->
+            preferences[PreferencesKeys.PREVENT_CLEAN] = preventClean
+        }
+    }
+
+    suspend fun updatePreventRequest(preventRequest: Boolean) {
+        dataStore.edit { preferences ->
+            preferences[PreferencesKeys.PREVENT_REQUEST] = preventRequest
+        }
+    }
+}

+ 8 - 0
app/src/main/java/com/example/modifier/data/AppRuntimeFlags.kt

@@ -0,0 +1,8 @@
+package com.example.modifier.data
+
+data class AppRuntimeFlags(
+    val running: Boolean = false,
+    val requesting: Boolean = false,
+    val preparing: Boolean = false,
+    val checkingConnection: Boolean = false
+)

+ 13 - 0
app/src/main/java/com/example/modifier/data/AppState.kt

@@ -0,0 +1,13 @@
+package com.example.modifier.data
+
+data class AppState(
+    val send: Boolean,
+    val executedNum: Int,
+    val successNum: Int,
+    val requestedNum: Int,
+    val running: Boolean,
+    val requesting: Boolean,
+    val preparing: Boolean,
+    val checkingConnection: Boolean,
+    val busy: Boolean
+)

+ 82 - 0
app/src/main/java/com/example/modifier/data/AppStateRepository.kt

@@ -0,0 +1,82 @@
+package com.example.modifier.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.lifecycle.MutableLiveData
+import androidx.lifecycle.asFlow
+import kotlinx.coroutines.flow.combine
+
+class AppStateRepository(
+    private val dataStore: DataStore<Preferences>,
+    private val context: Context
+) {
+
+    private object PreferencesKeys {
+        val SEND = booleanPreferencesKey("send")
+        val EXECUTED_NUM = intPreferencesKey("executed_num")
+        val SUCCESS_NUM = intPreferencesKey("success_num")
+        val REQUESTED_NUM = intPreferencesKey("requested_num")
+    }
+
+    private val appRuntimeFlags = MutableLiveData(AppRuntimeFlags())
+
+    val appStateFlow =
+        dataStore.data.combine(appRuntimeFlags.asFlow()) { preferences, runtimeFlags ->
+            AppState(
+                send = preferences[PreferencesKeys.SEND] ?: false,
+                executedNum = preferences[PreferencesKeys.EXECUTED_NUM] ?: 0,
+                successNum = preferences[PreferencesKeys.SUCCESS_NUM] ?: 0,
+                requestedNum = preferences[PreferencesKeys.REQUESTED_NUM] ?: 0,
+                running = runtimeFlags.running,
+                requesting = runtimeFlags.requesting,
+                preparing = runtimeFlags.preparing,
+                checkingConnection = runtimeFlags.checkingConnection,
+                busy = runtimeFlags.running || runtimeFlags.requesting || runtimeFlags.preparing || runtimeFlags.checkingConnection
+            )
+        }
+
+    suspend fun incrementExecutedNum(success: Boolean) {
+        dataStore.edit { preferences ->
+            val executedNum = (preferences[PreferencesKeys.EXECUTED_NUM] ?: 0) + 1
+            preferences[PreferencesKeys.EXECUTED_NUM] = executedNum
+            if (success) {
+                val successNum = (preferences[PreferencesKeys.SUCCESS_NUM] ?: 0) + 1
+                preferences[PreferencesKeys.SUCCESS_NUM] = successNum
+            }
+        }
+    }
+
+    suspend fun incrementRequestedNum() {
+        dataStore.edit { preferences ->
+            val requestedNum = (preferences[PreferencesKeys.REQUESTED_NUM] ?: 0) + 1
+            preferences[PreferencesKeys.REQUESTED_NUM] = requestedNum
+        }
+    }
+
+    suspend fun updateSend(send: Boolean) {
+        dataStore.edit { preferences ->
+            preferences[PreferencesKeys.SEND] = send
+        }
+    }
+
+    fun updateRuntimeFlags(
+        running: Boolean?,
+        requesting: Boolean?,
+        preparing: Boolean?,
+        checkingConnection: Boolean?
+    ) {
+        val value = appRuntimeFlags.value ?: AppRuntimeFlags()
+        appRuntimeFlags.postValue(
+            value.copy(
+                running = running ?: value.running,
+                requesting = requesting ?: value.requesting,
+                preparing = preparing ?: value.preparing,
+                checkingConnection = checkingConnection ?: value.checkingConnection
+            )
+        )
+    }
+}

+ 17 - 0
app/src/main/java/com/example/modifier/data/SocketClient.kt

@@ -0,0 +1,17 @@
+package com.example.modifier.data
+
+import io.socket.client.IO
+import io.socket.client.Socket
+
+const val TAG = "SocketClient"
+class SocketClient {
+    private val mSocketOpts = IO.Options()
+    private lateinit var mSocket: Socket
+    init {
+
+    }
+
+    suspend fun connect() {
+
+    }
+}

+ 17 - 2
app/src/main/java/com/example/modifier/service/ModifierService.kt

@@ -54,6 +54,8 @@ import com.example.modifier.constants.CMD_RCS_SETTINGS_ACTIVITY
 import com.example.modifier.constants.PACKAGE_GMS
 import com.example.modifier.constants.PACKAGE_GMS
 import com.example.modifier.constants.PACKAGE_MESSAGING
 import com.example.modifier.constants.PACKAGE_MESSAGING
 import com.example.modifier.data.AppDatabase
 import com.example.modifier.data.AppDatabase
+import com.example.modifier.data.AppPreferencesRepository
+import com.example.modifier.data.AppStateRepository
 import com.example.modifier.data.BackupItemDao
 import com.example.modifier.data.BackupItemDao
 import com.example.modifier.databinding.FloatingWindowBinding
 import com.example.modifier.databinding.FloatingWindowBinding
 import com.example.modifier.enums.RcsConfigureState
 import com.example.modifier.enums.RcsConfigureState
@@ -126,7 +128,8 @@ import kotlin.time.Duration.Companion.minutes
 import kotlin.time.Duration.Companion.seconds
 import kotlin.time.Duration.Companion.seconds
 
 
 
 
-val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "serverConfig")
+val Context.appPreferencesDataStore by preferencesDataStore(name = "${BuildConfig.APPLICATION_ID}.appPreferences")
+val Context.appStateDataStore by preferencesDataStore(name = "${BuildConfig.APPLICATION_ID}.appState")
 
 
 @SuppressLint("SetTextI18n")
 @SuppressLint("SetTextI18n")
 class ModifierService : AccessibilityService(), Emitter.Listener {
 class ModifierService : AccessibilityService(), Emitter.Listener {
@@ -274,6 +277,12 @@ class ModifierService : AccessibilityService(), Emitter.Listener {
     private val backupItemDao: BackupItemDao by lazy {
     private val backupItemDao: BackupItemDao by lazy {
         AppDatabase.getDatabase(this).itemDao()
         AppDatabase.getDatabase(this).itemDao()
     }
     }
+    private val appPreferencesRepository: AppPreferencesRepository by lazy {
+        AppPreferencesRepository(appPreferencesDataStore, this)
+    }
+    private val appStateRepository: AppStateRepository by lazy {
+        AppStateRepository(appStateDataStore, this)
+    }
 
 
     private var requestMode = 1;
     private var requestMode = 1;
     private var currentActivity = ""
     private var currentActivity = ""
@@ -522,7 +531,13 @@ class ModifierService : AccessibilityService(), Emitter.Listener {
             for (i in 0 until taskAction.data.tasks.size) {
             for (i in 0 until taskAction.data.tasks.size) {
                 val taskItem = taskAction.data.tasks[i]
                 val taskItem = taskAction.data.tasks[i]
                 try {
                 try {
-                    if (send(taskItem.number, taskItem.message, rcsWait, taskAction.data.config.endToEndEncryption)) {
+                    if (send(
+                            taskItem.number,
+                            taskItem.message,
+                            rcsWait,
+                            taskAction.data.config.endToEndEncryption
+                        )
+                    ) {
                         success.add(taskItem.id)
                         success.add(taskItem.id)
                     } else {
                     } else {
                         fail.add(taskItem.id)
                         fail.add(taskItem.id)

+ 16 - 4
app/src/main/java/com/example/modifier/ui/settings/SettingsFragment.kt

@@ -21,8 +21,13 @@ import androidx.activity.result.ActivityResultLauncher
 import androidx.activity.result.contract.ActivityResultContracts
 import androidx.activity.result.contract.ActivityResultContracts
 import androidx.core.app.ActivityCompat
 import androidx.core.app.ActivityCompat
 import androidx.core.content.ContextCompat
 import androidx.core.content.ContextCompat
+import androidx.datastore.core.DataStore
+import androidx.datastore.preferences.core.Preferences
+import androidx.datastore.preferences.core.stringPreferencesKey
+import androidx.datastore.preferences.preferencesDataStore
 import androidx.fragment.app.Fragment
 import androidx.fragment.app.Fragment
 import androidx.lifecycle.lifecycleScope
 import androidx.lifecycle.lifecycleScope
+import com.example.modifier.BuildConfig
 import com.example.modifier.Global
 import com.example.modifier.Global
 import com.example.modifier.Global.save
 import com.example.modifier.Global.save
 import com.example.modifier.Global.saveServer
 import com.example.modifier.Global.saveServer
@@ -50,6 +55,9 @@ import io.ktor.http.contentType
 import io.ktor.http.isSuccess
 import io.ktor.http.isSuccess
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.delay
 import kotlinx.coroutines.delay
+import kotlinx.coroutines.flow.first
+import kotlinx.coroutines.flow.last
+import kotlinx.coroutines.flow.map
 import kotlinx.coroutines.launch
 import kotlinx.coroutines.launch
 import kotlinx.coroutines.withContext
 import kotlinx.coroutines.withContext
 import kotlinx.coroutines.withTimeout
 import kotlinx.coroutines.withTimeout
@@ -65,10 +73,8 @@ class SettingsFragment : Fragment() {
     private val TAG = "SettingsFragment"
     private val TAG = "SettingsFragment"
 
 
     private lateinit var binding: FragmentSettingsBinding
     private lateinit var binding: FragmentSettingsBinding
-
-    var handler: Handler = Handler(Looper.getMainLooper())
-
-    lateinit var requestPermissionLauncher: ActivityResultLauncher<String>
+    private lateinit var requestPermissionLauncher: ActivityResultLauncher<String>
+    private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = BuildConfig.APPLICATION_ID)
 
 
     init {
     init {
         Log.i("SettingsFragment", "SettingsFragment")
         Log.i("SettingsFragment", "SettingsFragment")
@@ -322,6 +328,12 @@ class SettingsFragment : Fragment() {
         }
         }
         lifecycleScope.launch {
         lifecycleScope.launch {
             loadConfigs()
             loadConfigs()
+
+            val server = requireContext().dataStore.data.map {
+                it[stringPreferencesKey("server")]
+            }
+
+            Log.i("xxxxxxserver", "server: ${server.first()}")
         }
         }
 
 
         return binding.root
         return binding.root