Skip to content

Commit

Permalink
Fix remote settings` bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshine0523 committed Aug 30, 2023
1 parent a84601d commit 1f1e22f
Show file tree
Hide file tree
Showing 26 changed files with 253 additions and 181 deletions.
35 changes: 5 additions & 30 deletions app/src/main/java/com/sunshine/freeform/room/DatabaseRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<String>?> {
Expand All @@ -45,7 +44,7 @@ class DatabaseRepository(context: Context) {
freeFormAppsDao.update(entity)
}

fun getAllFreeFormWithoutLiveData() : List<String>? {
fun getAllFreeFormWithoutLiveData() : List<FreeFormAppsEntity>? {
return freeFormAppsDao.getAllWithoutLiveData()
}

Expand All @@ -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<List<NotificationAppsEntity>?> {
return notificationAppsDao.getAll()
}

fun getAllNotificationByFlow(): Flow<List<NotificationAppsEntity>?> {
return notificationAppsDao.getAllByFlow()
}

fun deleteAllNotification() {
notificationAppsDao.deleteAll()
}

init {
val database = getDatabase(context)
freeFormAppsDao = database.freeFormAppsDao
notificationAppsDao = database.notificationAppsDao
}
}
12 changes: 6 additions & 6 deletions app/src/main/java/com/sunshine/freeform/room/FreeFormAppsDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<FreeFormAppsEntity>?>
Expand All @@ -29,8 +29,8 @@ interface FreeFormAppsDao {
@Query("SELECT packageName FROM FreeFormAppsEntity")
fun getAllName() : LiveData<List<String>?>

@Query("SELECT packageName FROM FreeFormAppsEntity")
fun getAllWithoutLiveData() : List<String>?
@Query("SELECT * FROM FreeFormAppsEntity")
fun getAllWithoutLiveData() : List<FreeFormAppsEntity>?

@Query("SELECT COUNT(*) FROM FreeFormAppsEntity")
fun getCount(): Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/com/sunshine/freeform/room/MyDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sunshine.freeform
package com.sunshine.freeform.ui

import android.annotation.SuppressLint
import android.content.ComponentName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<AppInfo>()
val appListLiveData: LiveData<List<AppInfo>>
get() = _appList
Expand All @@ -44,16 +52,33 @@ class AppListViewModel(private val application: Application): AndroidViewModel(a
getAppList()
}

/**
* use in IO
*/
private fun getAllFreeFormApps(): List<FreeFormAppsEntity> {
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
))
}
}
Expand All @@ -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
}
Expand Down
Loading

0 comments on commit 1f1e22f

Please sign in to comment.