diff --git a/app/src/main/java/com/sunshine/freeform/room/DatabaseRepository.kt b/app/src/main/java/com/sunshine/freeform/room/DatabaseRepository.kt index c8d8eae..f7befaa 100644 --- a/app/src/main/java/com/sunshine/freeform/room/DatabaseRepository.kt +++ b/app/src/main/java/com/sunshine/freeform/room/DatabaseRepository.kt @@ -13,16 +13,15 @@ import java.lang.Exception class DatabaseRepository(context: Context) { private val freeFormAppsDao: FreeFormAppsDao - private val notificationAppsDao: NotificationAppsDao - fun insertFreeForm(packageName: String, userId: Int) { + fun insertFreeForm(packageName: String, activityName: String, userId: Int) { try { - freeFormAppsDao.insert(packageName, userId) + freeFormAppsDao.insert(packageName, activityName, userId) }catch (e: Exception) { } } - fun deleteFreeForm(packageName: String, userId: Int) { - freeFormAppsDao.delete(packageName, userId) + fun deleteFreeForm(packageName: String, activityName: String, userId: Int) { + freeFormAppsDao.delete(packageName, activityName, userId) } fun getAllFreeFormName(): LiveData?> { @@ -45,7 +44,7 @@ class DatabaseRepository(context: Context) { freeFormAppsDao.update(entity) } - fun getAllFreeFormWithoutLiveData() : List? { + fun getAllFreeFormWithoutLiveData() : List? { return freeFormAppsDao.getAllWithoutLiveData() } @@ -57,32 +56,8 @@ class DatabaseRepository(context: Context) { freeFormAppsDao.deleteList(freeFormAppsEntityList) } - fun insertNotification(packageName: String, userId: Int) { - try { - notificationAppsDao.insert(packageName, userId) - }catch (e: Exception) {} - - } - - fun deleteNotification(packageName: String, userId: Int) { - notificationAppsDao.delete(packageName, userId) - } - - fun getAllNotification(): LiveData?> { - return notificationAppsDao.getAll() - } - - fun getAllNotificationByFlow(): Flow?> { - return notificationAppsDao.getAllByFlow() - } - - fun deleteAllNotification() { - notificationAppsDao.deleteAll() - } - init { val database = getDatabase(context) freeFormAppsDao = database.freeFormAppsDao - notificationAppsDao = database.notificationAppsDao } } \ No newline at end of file diff --git a/app/src/main/java/com/sunshine/freeform/room/FreeFormAppsDao.kt b/app/src/main/java/com/sunshine/freeform/room/FreeFormAppsDao.kt index 1c4108b..3b87dcc 100644 --- a/app/src/main/java/com/sunshine/freeform/room/FreeFormAppsDao.kt +++ b/app/src/main/java/com/sunshine/freeform/room/FreeFormAppsDao.kt @@ -14,11 +14,11 @@ import kotlinx.coroutines.flow.Flow @Dao interface FreeFormAppsDao { - @Query("INSERT INTO FreeFormAppsEntity(packageName, userId) VALUES(:packageName, :userId)") - fun insert(packageName: String, userId: Int) + @Query("INSERT INTO FreeFormAppsEntity(packageName, activityName, userId) VALUES(:packageName, :activityName, :userId)") + fun insert(packageName: String, activityName: String, userId: Int) - @Query("DELETE FROM FreeFormAppsEntity WHERE packageName = :packageName and userId = :userId") - fun delete(packageName: String, userId: Int) + @Query("DELETE FROM FreeFormAppsEntity WHERE packageName = :packageName and activityName = :activityName and userId = :userId") + fun delete(packageName: String, activityName: String, userId: Int) @Query("SELECT * FROM FreeFormAppsEntity") fun getAll() : LiveData?> @@ -29,8 +29,8 @@ interface FreeFormAppsDao { @Query("SELECT packageName FROM FreeFormAppsEntity") fun getAllName() : LiveData?> - @Query("SELECT packageName FROM FreeFormAppsEntity") - fun getAllWithoutLiveData() : List? + @Query("SELECT * FROM FreeFormAppsEntity") + fun getAllWithoutLiveData() : List? @Query("SELECT COUNT(*) FROM FreeFormAppsEntity") fun getCount(): Int diff --git a/app/src/main/java/com/sunshine/freeform/room/FreeFormAppsEntity.kt b/app/src/main/java/com/sunshine/freeform/room/FreeFormAppsEntity.kt index 8b18b14..93e05b1 100644 --- a/app/src/main/java/com/sunshine/freeform/room/FreeFormAppsEntity.kt +++ b/app/src/main/java/com/sunshine/freeform/room/FreeFormAppsEntity.kt @@ -15,7 +15,7 @@ class FreeFormAppsEntity( @PrimaryKey(autoGenerate = true) val sortNum: Int, var packageName: String, - //20210604新增用户标识 + var activityName: String, var userId: Int = 0 ) { override fun toString(): String { diff --git a/app/src/main/java/com/sunshine/freeform/room/MyDatabase.kt b/app/src/main/java/com/sunshine/freeform/room/MyDatabase.kt index 9f3a5e1..6cea957 100644 --- a/app/src/main/java/com/sunshine/freeform/room/MyDatabase.kt +++ b/app/src/main/java/com/sunshine/freeform/room/MyDatabase.kt @@ -11,10 +11,9 @@ import androidx.sqlite.db.SupportSQLiteDatabase * @author sunshine * @date 2021/1/31 */ -@Database(entities = [FreeFormAppsEntity::class, NotificationAppsEntity::class], version = 5, exportSchema = false) +@Database(entities = [FreeFormAppsEntity::class], version = 5, exportSchema = false) abstract class MyDatabase : RoomDatabase() { abstract val freeFormAppsDao: FreeFormAppsDao - abstract val notificationAppsDao: NotificationAppsDao companion object { private var database: MyDatabase? = null diff --git a/app/src/main/java/com/sunshine/freeform/room/NotificationAppsDao.kt b/app/src/main/java/com/sunshine/freeform/room/NotificationAppsDao.kt deleted file mode 100644 index c6c64d6..0000000 --- a/app/src/main/java/com/sunshine/freeform/room/NotificationAppsDao.kt +++ /dev/null @@ -1,29 +0,0 @@ -package com.sunshine.freeform.room - -import androidx.lifecycle.LiveData -import androidx.room.Dao -import androidx.room.Query -import kotlinx.coroutines.flow.Flow - -/** - * @author sunshine - * @date 2021/1/31 - */ -@Dao -interface NotificationAppsDao { - - @Query("INSERT INTO NotificationAppsEntity(packageName, userId) VALUES(:packageName, :userId)") - fun insert(packageName: String, userId: Int) - - @Query("DELETE FROM NotificationAppsEntity WHERE packageName = :packageName and userId = :userId") - fun delete(packageName: String, userId: Int) - - @Query("SELECT * FROM NotificationAppsEntity") - fun getAll() : LiveData?> - - @Query("SELECT * FROM NotificationAppsEntity") - fun getAllByFlow() : Flow?> - - @Query("DELETE FROM NotificationAppsEntity") - fun deleteAll() -} \ No newline at end of file diff --git a/app/src/main/java/com/sunshine/freeform/room/NotificationAppsEntity.kt b/app/src/main/java/com/sunshine/freeform/room/NotificationAppsEntity.kt deleted file mode 100644 index 9406a70..0000000 --- a/app/src/main/java/com/sunshine/freeform/room/NotificationAppsEntity.kt +++ /dev/null @@ -1,21 +0,0 @@ -package com.sunshine.freeform.room - -import androidx.room.Entity -import androidx.room.PrimaryKey - - -/** - * @author sunshine - * @date 2021/1/31 - * 开启小窗数据库实体类 - */ -@Entity -class NotificationAppsEntity( -// @PrimaryKey(autoGenerate = true) -// val id: Int, - @PrimaryKey - val packageName: String, - //20210604新增用户标识 - val userId: Int = 0 -) - diff --git a/app/src/main/java/com/sunshine/freeform/ProcessSurfaceView.kt b/app/src/main/java/com/sunshine/freeform/ui/ProcessSurfaceView.kt similarity index 99% rename from app/src/main/java/com/sunshine/freeform/ProcessSurfaceView.kt rename to app/src/main/java/com/sunshine/freeform/ui/ProcessSurfaceView.kt index 0b22c9f..9ab17de 100644 --- a/app/src/main/java/com/sunshine/freeform/ProcessSurfaceView.kt +++ b/app/src/main/java/com/sunshine/freeform/ui/ProcessSurfaceView.kt @@ -1,4 +1,4 @@ -package com.sunshine.freeform +package com.sunshine.freeform.ui import android.annotation.SuppressLint import android.content.ComponentName diff --git a/app/src/main/java/com/sunshine/freeform/ui/app_list/AppInfo.kt b/app/src/main/java/com/sunshine/freeform/ui/app_list/AppInfo.kt index 59244de..b3f892b 100644 --- a/app/src/main/java/com/sunshine/freeform/ui/app_list/AppInfo.kt +++ b/app/src/main/java/com/sunshine/freeform/ui/app_list/AppInfo.kt @@ -7,5 +7,7 @@ data class AppInfo( val label: String, val icon: Drawable, val componentName: ComponentName, - val userId: Int + val userId: Int, + // is add to freeform app, use for FreeformAppActivity + var isFreeformApp: Boolean = false ) diff --git a/app/src/main/java/com/sunshine/freeform/ui/app_list/AppListActivity.kt b/app/src/main/java/com/sunshine/freeform/ui/app_list/AppListActivity.kt index a720b15..33a753e 100644 --- a/app/src/main/java/com/sunshine/freeform/ui/app_list/AppListActivity.kt +++ b/app/src/main/java/com/sunshine/freeform/ui/app_list/AppListActivity.kt @@ -76,43 +76,6 @@ class AppListActivity : ComponentActivity() { } } -@Composable -fun SearchWidget(textStyle: TextStyle = TextStyle.Default, viewModel: AppListViewModel) { - var text by remember { mutableStateOf("")} - Box { - BasicTextField( - value = text, - onValueChange = { - text = it - viewModel.filterApp(text) - }, - textStyle = textStyle, - modifier = Modifier - .padding(20.dp) - .background(MaterialTheme.colorScheme.primaryContainer, CircleShape) - .height(60.dp) - .fillMaxWidth(), - decorationBox = { - Row(verticalAlignment = Alignment.CenterVertically, - modifier = Modifier.padding(horizontal = 8.dp)) { - Box(modifier = Modifier - .padding(horizontal = 10.dp) - .weight(1f), - contentAlignment = Alignment.CenterStart) { - if (text.isEmpty()) { - Text( - text = stringResource(id = R.string.search_app), - style = textStyle - ) - } - it() - } - } - } - ) - } -} - @Composable fun ListWidget(viewModel: AppListViewModel) { val appList by viewModel.appListLiveData.observeAsState(ArrayList()) diff --git a/app/src/main/java/com/sunshine/freeform/ui/app_list/AppListViewModel.kt b/app/src/main/java/com/sunshine/freeform/ui/app_list/AppListViewModel.kt index eaee30a..f3a68a4 100644 --- a/app/src/main/java/com/sunshine/freeform/ui/app_list/AppListViewModel.kt +++ b/app/src/main/java/com/sunshine/freeform/ui/app_list/AppListViewModel.kt @@ -2,16 +2,23 @@ package com.sunshine.freeform.ui.app_list import android.app.Application import android.content.Context +import android.content.pm.ApplicationInfo import android.content.pm.LauncherActivityInfo import android.content.pm.LauncherApps import android.os.UserHandle import android.os.UserManager +import android.util.Log import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.viewModelScope import com.sunshine.freeform.MiFreeform +import com.sunshine.freeform.room.DatabaseRepository +import com.sunshine.freeform.room.FreeFormAppsEntity +import com.sunshine.freeform.utils.contains import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.launch import kotlin.math.max import kotlin.math.min @@ -21,6 +28,7 @@ import kotlin.math.min * @since 2023/8/25 */ class AppListViewModel(private val application: Application): AndroidViewModel(application) { + private val repository = DatabaseRepository(application) private val allAppList = ArrayList() val appListLiveData: LiveData> get() = _appList @@ -44,16 +52,33 @@ class AppListViewModel(private val application: Application): AndroidViewModel(a getAppList() } + /** + * use in IO + */ + private fun getAllFreeFormApps(): List { + return repository.getAllFreeFormWithoutLiveData() ?: ArrayList() + } + private fun getAppList() { viewModelScope.launch(Dispatchers.IO) { + val freeformList = getAllFreeFormApps() userManager.userProfiles.forEach { userHandle -> val list = launcherApps.getActivityList(null, userHandle) list.forEach {info -> + val packageName = info.componentName.packageName + val activityName = info.componentName.className + val userId = com.sunshine.freeform.systemapi.UserHandle.getUserId(userHandle) + val isFreeformApp = freeformList.contains(FreeFormAppsEntity(-1, packageName, activityName, userId)) { info1, info2 -> + info1.packageName == info2.packageName && + info1.activityName == info2.activityName && + info1.userId == info2.userId + } allAppList.add(AppInfo( - info.label.toString(), + "${info.label}${if (com.sunshine.freeform.systemapi.UserHandle.getUserId(userHandle) != 0) -com.sunshine.freeform.systemapi.UserHandle.getUserId(userHandle) else ""}", info.applicationInfo.loadIcon(application.packageManager), info.componentName, - com.sunshine.freeform.systemapi.UserHandle.getUserId(userHandle) + userId, + isFreeformApp )) } } @@ -73,6 +98,14 @@ class AppListViewModel(private val application: Application): AndroidViewModel(a } } + fun addFreeformApp(packageName: String, activityName: String, userId: Int) { + repository.insertFreeForm(packageName, activityName, userId) + } + + fun removeFreeformApp(packageName: String, activityName: String, userId: Int) { + repository.deleteFreeForm(packageName, activityName, userId) + } + fun closeActivity() { _finishActivity.value = true } diff --git a/app/src/main/java/com/sunshine/freeform/ui/app_list/FreeformAppActivity.kt b/app/src/main/java/com/sunshine/freeform/ui/app_list/FreeformAppActivity.kt index 24d6225..e7b5755 100644 --- a/app/src/main/java/com/sunshine/freeform/ui/app_list/FreeformAppActivity.kt +++ b/app/src/main/java/com/sunshine/freeform/ui/app_list/FreeformAppActivity.kt @@ -3,10 +3,23 @@ package com.sunshine.freeform.ui.app_list import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent +import androidx.compose.foundation.Image +import androidx.compose.foundation.background +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.Row +import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.WindowInsets 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.material3.ExperimentalMaterial3Api import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Scaffold @@ -14,10 +27,22 @@ import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.material3.TopAppBar import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.livedata.observeAsState +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp import androidx.core.view.WindowCompat +import com.google.accompanist.drawablepainter.rememberDrawablePainter import com.sunshine.freeform.R import com.sunshine.freeform.ui.theme.MiFreeformTheme @@ -51,7 +76,16 @@ fun ScaffoldWidget(viewModel: AppListViewModel) { contentWindowInsets = WindowInsets(0.dp, 0.dp, 0.dp, 0.dp) ) { Box(modifier = Modifier.padding(it)) { - + Column { + SearchWidget( + textStyle = TextStyle( + color = Color(0,0,0,128), + fontSize = 20.sp + ), + viewModel = viewModel + ) + FreeformAppListWidget(viewModel = viewModel) + } } } } @@ -64,4 +98,55 @@ fun TopBarWidget() { Text(text = stringResource(id = R.string.title_activity_freeform_app)) }, ) +} + +@Composable +fun FreeformAppListWidget(viewModel: AppListViewModel) { + val appList by viewModel.appListLiveData.observeAsState(ArrayList()) + LazyColumn(content = { + items(appList) { appInfo -> + FreeformAppListItem(appInfo = appInfo, viewModel = viewModel) + } + }) +} + +@Composable +fun FreeformAppListItem(appInfo: AppInfo, viewModel: AppListViewModel) { + var bg by remember { mutableStateOf(Color.Transparent) } + val selectedBg = MaterialTheme.colorScheme.primaryContainer + if (appInfo.isFreeformApp) bg = selectedBg + Box(modifier = Modifier.fillMaxSize().background(bg)) { + Row( + verticalAlignment = Alignment.CenterVertically, + modifier = Modifier + .fillMaxWidth() + .height(100.dp) + .padding(horizontal = 16.dp) + .clickable { + bg = if (appInfo.isFreeformApp) { + viewModel.removeFreeformApp(appInfo.componentName.packageName, appInfo.componentName.className, appInfo.userId) + Color.Transparent + } else { + viewModel.addFreeformApp(appInfo.componentName.packageName, appInfo.componentName.className, appInfo.userId) + selectedBg + } + appInfo.isFreeformApp = !appInfo.isFreeformApp + } + ) { + Image( + painter = rememberDrawablePainter(drawable = appInfo.icon), + contentDescription = null, + modifier = Modifier + .size(60.dp), + contentScale = ContentScale.Crop + ) + Spacer(modifier = Modifier.width(8.dp)) + Text( + text = appInfo.label, + style = MaterialTheme.typography.bodyLarge, + overflow = TextOverflow.Ellipsis, + maxLines = 1 + ) + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/sunshine/freeform/ui/app_list/SearchView.kt b/app/src/main/java/com/sunshine/freeform/ui/app_list/SearchView.kt new file mode 100644 index 0000000..13b02f8 --- /dev/null +++ b/app/src/main/java/com/sunshine/freeform/ui/app_list/SearchView.kt @@ -0,0 +1,64 @@ +package com.sunshine.freeform.ui.app_list + +import androidx.compose.foundation.background +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.shape.CircleShape +import androidx.compose.foundation.text.BasicTextField +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.unit.dp +import com.sunshine.freeform.R + +/** + * @author KindBrave + * @since 2023/8/29 + */ +@Composable +fun SearchWidget(textStyle: TextStyle = TextStyle.Default, viewModel: AppListViewModel) { + var text by remember { mutableStateOf("") } + Box { + BasicTextField( + value = text, + onValueChange = { + text = it + viewModel.filterApp(text) + }, + textStyle = textStyle, + modifier = Modifier + .padding(20.dp) + .background(MaterialTheme.colorScheme.primaryContainer, CircleShape) + .height(60.dp) + .fillMaxWidth(), + decorationBox = { + Row(verticalAlignment = Alignment.CenterVertically, + modifier = Modifier.padding(horizontal = 8.dp)) { + Box(modifier = Modifier + .padding(horizontal = 10.dp) + .weight(1f), + contentAlignment = Alignment.CenterStart) { + if (text.isEmpty()) { + Text( + text = stringResource(id = R.string.search_app), + style = textStyle + ) + } + it() + } + } + } + ) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/sunshine/freeform/ui/floating/ChooseAppFloatingAdapter.kt b/app/src/main/java/com/sunshine/freeform/ui/floating/ChooseAppFloatingAdapter.kt index d54cea4..cc3169e 100644 --- a/app/src/main/java/com/sunshine/freeform/ui/floating/ChooseAppFloatingAdapter.kt +++ b/app/src/main/java/com/sunshine/freeform/ui/floating/ChooseAppFloatingAdapter.kt @@ -10,6 +10,7 @@ import android.content.pm.PackageManager import android.content.pm.ResolveInfo import android.os.UserHandle import android.os.UserManager +import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -110,7 +111,7 @@ class ChooseAppFloatingAdapter( holder.appName.text = getLabel(appInfo, apps[position - 1].userId) holder.click.setOnClickListener { MiFreeformServiceManager.createDisplay( - ComponentName(appInfo.packageName, appInfo.className), + ComponentName(apps[position - 1].packageName, apps[position - 1].activityName), apps[position - 1].userId, viewModel.getIntSp("freeform_width", (viewModel.screenWidth * 0.8).roundToInt()), viewModel.getIntSp("freeform_height", (viewModel.screenHeight * 0.5).roundToInt()), diff --git a/app/src/main/java/com/sunshine/freeform/ui/main/RemoteSettings.kt b/app/src/main/java/com/sunshine/freeform/ui/main/RemoteSettings.kt index 976ef73..e24fb67 100644 --- a/app/src/main/java/com/sunshine/freeform/ui/main/RemoteSettings.kt +++ b/app/src/main/java/com/sunshine/freeform/ui/main/RemoteSettings.kt @@ -1,5 +1,8 @@ package com.sunshine.freeform.ui.main +import androidx.annotation.Keep + +@Keep data class RemoteSettings( var enableSideBar: Boolean = false ) \ No newline at end of file diff --git a/app/src/main/java/com/sunshine/freeform/ui/main/SettingViewModel.kt b/app/src/main/java/com/sunshine/freeform/ui/main/SettingViewModel.kt index 2dffe25..f12a201 100644 --- a/app/src/main/java/com/sunshine/freeform/ui/main/SettingViewModel.kt +++ b/app/src/main/java/com/sunshine/freeform/ui/main/SettingViewModel.kt @@ -23,6 +23,7 @@ class SettingViewModel(private val application: Application) : AndroidViewModel( val screenHeight = max(application.resources.displayMetrics.widthPixels, application.resources.displayMetrics.heightPixels) val screenDensityDpi = application.resources.displayMetrics.densityDpi + private val remoteSetting = Gson().fromJson(MiFreeformServiceManager.getSetting(), RemoteSettings::class.java) ?: RemoteSettings() private val _enableSideBar = MutableLiveData() val enableSideBar: LiveData get() = _enableSideBar private val _freeformWidth = MutableLiveData() @@ -33,7 +34,7 @@ class SettingViewModel(private val application: Application) : AndroidViewModel( val freeformDensityDpi: LiveData get() = _freeformDensityDpi init { - val remoteSetting = Gson().fromJson(MiFreeformServiceManager.getSetting(), RemoteSettings::class.java) ?: RemoteSettings() + _enableSideBar.postValue(remoteSetting.enableSideBar) _freeformWidth.postValue(sp.getInt("freeform_width", (screenWidth * 0.8).roundToInt())) _freeformHeight.postValue(sp.getInt("freeform_height", (screenHeight * 0.5).roundToInt())) @@ -42,9 +43,8 @@ class SettingViewModel(private val application: Application) : AndroidViewModel( fun saveRemoteSidebar(enableSideBar: Boolean) { _enableSideBar.postValue(enableSideBar) - if (enableSideBar) { - MiFreeformServiceManager - } + remoteSetting.enableSideBar = enableSideBar + MiFreeformServiceManager.setSetting(remoteSetting) } fun setFreeformWidth(width: Int) { diff --git a/app/src/main/java/com/sunshine/freeform/utils/Extension.kt b/app/src/main/java/com/sunshine/freeform/utils/Extension.kt new file mode 100644 index 0000000..3478edf --- /dev/null +++ b/app/src/main/java/com/sunshine/freeform/utils/Extension.kt @@ -0,0 +1,14 @@ +package com.sunshine.freeform.utils + +/** + * @author KindBrave + * @since 2023/8/29 + */ +fun List.contains(element: T, predicate: (T, T) -> Boolean): Boolean { + for (item in this) { + if (predicate(item, element)) { + return true + } + } + return false +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml deleted file mode 100644 index 755aa72..0000000 --- a/app/src/main/res/layout/activity_main.xml +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/freeform-server/proguard-rules.pro b/freeform-server/proguard-rules.pro index df672e7..11959d4 100644 --- a/freeform-server/proguard-rules.pro +++ b/freeform-server/proguard-rules.pro @@ -22,4 +22,6 @@ -keepclasseswithmembers class com.sunshine.freeform.Main { public static void main(java.lang.String[]); -} \ No newline at end of file +} + +-keep class io.sunshine0523.freeform.util.Settings {*;} \ No newline at end of file diff --git a/freeform-server/src/main/assets/system/.placeholder b/freeform-server/src/main/assets/system/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/freeform-server/src/main/assets/system/app/.placeholder b/freeform-server/src/main/assets/system/app/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/freeform-server/src/main/java/io/sunshine0523/freeform/ZygoteMain.java b/freeform-server/src/main/java/io/sunshine0523/freeform/ZygoteMain.java index cecf059..3587066 100644 --- a/freeform-server/src/main/java/io/sunshine0523/freeform/ZygoteMain.java +++ b/freeform-server/src/main/java/io/sunshine0523/freeform/ZygoteMain.java @@ -24,7 +24,6 @@ public static void main() { ServiceManagerProxy.install(new ServiceManagerProxy.Interceptor() { @Override public Binder addService(final String name, final Binder service) { - Log.e(TAG, name + " " + service); if (name.equals(DISPLAY)) { Log.i(TAG, "find display service: " + service); try { diff --git a/freeform-server/src/main/java/io/sunshine0523/freeform/service/MiFreeformUIService.java b/freeform-server/src/main/java/io/sunshine0523/freeform/service/MiFreeformUIService.java index b93c886..e38c766 100644 --- a/freeform-server/src/main/java/io/sunshine0523/freeform/service/MiFreeformUIService.java +++ b/freeform-server/src/main/java/io/sunshine0523/freeform/service/MiFreeformUIService.java @@ -104,12 +104,8 @@ public String getSettings() { } @Override - public void setSideBarStatus(boolean show) { - sideBarService.setSideBarStatus(show); + public void setSettings(String settings) { + DataHelper.INSTANCE.saveSettings(settings, this.sideBarService); } - @Override - public void updateSideBarColor(int color) { - - } } diff --git a/freeform-server/src/main/java/io/sunshine0523/freeform/service/SideBarService.java b/freeform-server/src/main/java/io/sunshine0523/freeform/service/SideBarService.java index db405b6..507b012 100644 --- a/freeform-server/src/main/java/io/sunshine0523/freeform/service/SideBarService.java +++ b/freeform-server/src/main/java/io/sunshine0523/freeform/service/SideBarService.java @@ -3,15 +3,17 @@ import android.content.Context; import android.graphics.Color; import android.os.Handler; +import android.util.Log; import io.sunshine0523.freeform.ui.sidebar.SideBarWindow; +import io.sunshine0523.freeform.util.DataChangeListener; import io.sunshine0523.freeform.util.DataHelper; import io.sunshine0523.freeform.util.Settings; -public class SideBarService { +public class SideBarService implements DataChangeListener { private final Context context; private final Handler uiHandler; - private final Settings settings; + private Settings settings; private SideBarWindow sideBarWindow = null; SideBarService(Context context, Handler uiHandler, Settings settings) { this.context = context; @@ -27,10 +29,14 @@ public class SideBarService { }).start(); } + @Override + public void onChanged() { + settings = DataHelper.INSTANCE.getSettings(); + setSideBarStatus(settings.getEnableSideBar()); + } + public void setSideBarStatus(boolean show) { if (null != sideBarWindow) sideBarWindow.destroy(); if (show) sideBarWindow = new SideBarWindow(context, uiHandler); - settings.setEnableSideBar(show); - DataHelper.INSTANCE.saveSettings(settings); } } diff --git a/freeform-server/src/main/java/io/sunshine0523/freeform/ui/freeform/FreeformWindow.kt b/freeform-server/src/main/java/io/sunshine0523/freeform/ui/freeform/FreeformWindow.kt index 67f2c67..0901abe 100644 --- a/freeform-server/src/main/java/io/sunshine0523/freeform/ui/freeform/FreeformWindow.kt +++ b/freeform-server/src/main/java/io/sunshine0523/freeform/ui/freeform/FreeformWindow.kt @@ -26,7 +26,7 @@ import kotlin.math.roundToInt class FreeformWindow( val uiHandler: Handler, val context: Context, - val appConfig: AppConfig, + private val appConfig: AppConfig, val freeformConfig: FreeformConfig, private val uiConfig: UIConfig, ): TextureView.SurfaceTextureListener, IMiFreeformDisplayCallback.Stub(), View.OnTouchListener { diff --git a/freeform-server/src/main/java/io/sunshine0523/freeform/util/DataHelper.kt b/freeform-server/src/main/java/io/sunshine0523/freeform/util/DataHelper.kt index 0c27139..b59c0fe 100644 --- a/freeform-server/src/main/java/io/sunshine0523/freeform/util/DataHelper.kt +++ b/freeform-server/src/main/java/io/sunshine0523/freeform/util/DataHelper.kt @@ -42,4 +42,16 @@ object DataHelper { dataFile.writeText(gson.toJson(settings)) } } + + fun saveSettings(settings: String, listener: DataChangeListener) { + runCatching { + this.settings = gson.fromJson(settings, Settings::class.java) ?: this.settings + dataFile.writeText(settings) + listener.onChanged() + } + } +} + +interface DataChangeListener { + fun onChanged() } \ No newline at end of file diff --git a/freeform-server/src/main/java/io/sunshine0523/freeform/util/DimenUtils.kt b/freeform-server/src/main/java/io/sunshine0523/freeform/util/DimenUtils.kt deleted file mode 100644 index 382aa20..0000000 --- a/freeform-server/src/main/java/io/sunshine0523/freeform/util/DimenUtils.kt +++ /dev/null @@ -1,6 +0,0 @@ -package io.sunshine0523.freeform.util - -import android.content.res.Resources -import kotlin.math.round - -val Number.dp get() = round(toFloat() * Resources.getSystem().displayMetrics.density).toInt()