Skip to content

Commit

Permalink
Redesigned components ui[6]
Browse files Browse the repository at this point in the history
  • Loading branch information
Tornaco committed Dec 23, 2024
1 parent 0046bb3 commit 741d4d4
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package github.tornaco.android.thanos.core.pm;

import static android.content.pm.PackageManager.GET_ACTIVITIES;
import static android.content.pm.PackageManager.GET_PROVIDERS;
import static android.content.pm.PackageManager.GET_RECEIVERS;
import static android.content.pm.PackageManager.GET_SERVICES;

import android.content.ComponentName;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ProviderInfo;
import android.content.pm.ServiceInfo;
import android.util.Log;

import com.elvishew.xlog.XLog;
import com.google.common.collect.Lists;

import java.util.ArrayList;

import github.tornaco.android.thanos.core.annotation.NonNull;

/**
* Created by guohao4 on 2017/11/17.
* Email: Tornaco@163.com
*/

public class ComponentUtil {

/**
* Those that has not service and broadcast and no activity.
*/
public static boolean isGreenPackage(PackageInfo packageInfo) {
return (packageInfo.receivers == null || packageInfo.receivers.length == 0)
&& (packageInfo.services == null || packageInfo.services.length == 0)
&& (packageInfo.activities == null || packageInfo.activities.length == 0);
}

@NonNull
public static ArrayList<ServiceInfo> getServices(Context context, String pkg) {
PackageManager pm = context.getPackageManager();
try {
PackageInfo packageInfo = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
packageInfo = pm.getPackageInfo(pkg, GET_SERVICES | PackageManager.MATCH_DISABLED_COMPONENTS);
} else {
packageInfo = pm.getPackageInfo(pkg, GET_SERVICES | PackageManager.GET_DISABLED_COMPONENTS);
}
if (packageInfo == null) return Lists.newArrayListWithCapacity(0);
ServiceInfo[] serviceInfoArray = packageInfo.services;
if (serviceInfoArray == null || serviceInfoArray.length == 0)
return Lists.newArrayListWithCapacity(0);
return Lists.newArrayList(serviceInfoArray);
} catch (Exception e) {
XLog.e("getServices: " + Log.getStackTraceString(e));
return Lists.newArrayListWithCapacity(0);
}
}

@NonNull
public static ArrayList<ActivityInfo> getActivities(Context context, String pkg) {
PackageManager pm = context.getPackageManager();
try {
PackageInfo packageInfo;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
packageInfo = pm.getPackageInfo(pkg, GET_ACTIVITIES | PackageManager.MATCH_DISABLED_COMPONENTS);
} else {
packageInfo = pm.getPackageInfo(pkg, GET_ACTIVITIES | PackageManager.GET_DISABLED_COMPONENTS);
}
if (packageInfo == null) return Lists.newArrayListWithCapacity(0);
ActivityInfo[] activityInfos = packageInfo.activities;
if (activityInfos == null || activityInfos.length == 0)
return Lists.newArrayListWithCapacity(0);
return Lists.newArrayList(activityInfos);
} catch (Exception e) {
XLog.e("getActivities: " + Log.getStackTraceString(e));
return Lists.newArrayListWithCapacity(0);
}
}

@NonNull
public static ArrayList<ActivityInfo> getBroadcasts(Context context, String pkg) {
PackageManager pm = context.getPackageManager();
try {
PackageInfo packageInfo = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
packageInfo = pm.getPackageInfo(pkg, GET_RECEIVERS | PackageManager.MATCH_DISABLED_COMPONENTS);
} else {
packageInfo = pm.getPackageInfo(pkg, GET_RECEIVERS | PackageManager.GET_DISABLED_COMPONENTS);
}
if (packageInfo == null) return Lists.newArrayListWithCapacity(0);
ActivityInfo[] activityInfos = packageInfo.receivers;
if (activityInfos == null || activityInfos.length == 0)
return Lists.newArrayListWithCapacity(0);
return Lists.newArrayList(activityInfos);
} catch (Exception e) {
XLog.e("getBroadcasts: " + Log.getStackTraceString(e));
return Lists.newArrayListWithCapacity(0);
}
}

@NonNull
public static ArrayList<ProviderInfo> getProviders(Context context, String pkg) {
PackageManager pm = context.getPackageManager();
try {
PackageInfo packageInfo = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
packageInfo = pm.getPackageInfo(pkg, GET_PROVIDERS | PackageManager.MATCH_DISABLED_COMPONENTS);
} else {
packageInfo = pm.getPackageInfo(pkg, GET_PROVIDERS | PackageManager.GET_DISABLED_COMPONENTS);
}
if (packageInfo == null) return Lists.newArrayListWithCapacity(0);
ProviderInfo[] providerInfos = packageInfo.providers;
if (providerInfos == null || providerInfos.length == 0)
return Lists.newArrayListWithCapacity(0);
return Lists.newArrayList(providerInfos);
} catch (Exception e) {
XLog.e("getProviders: " + Log.getStackTraceString(e));
return Lists.newArrayListWithCapacity(0);
}
}

public static ComponentName getComponentName(ActivityInfo activityInfo) {
return new ComponentName(activityInfo.packageName, activityInfo.name);
}

public static ComponentName getComponentName(ServiceInfo serviceInfo) {
return new ComponentName(serviceInfo.packageName, serviceInfo.name);
}

public static boolean isComponentDisabled(int enableSetting) {
return enableSetting != PackageManager.COMPONENT_ENABLED_STATE_ENABLED
&& enableSetting != PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
}

public static boolean isComponentEnabled(int enableSetting) {
return !isComponentDisabled(enableSetting);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ class SearchBarState {
}

fun closeSearchBar() {
inputKeyword("")
_showSearchBar = false
}

Expand Down
1 change: 1 addition & 0 deletions android/modules/module_component_manager/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ dependencies {

implementation(project(":third_party:recyclerview-fastscroll"))
implementation(project(":third_party:search"))
implementation(project(":third_party:remix"))

implementation(platform(libs.compose.bom))
implementation(libs.compose.ui)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,47 @@ class ComponentsActivity : ComposeThemeActivity() {
}
}
} else {
IconButton(onClick = {
searchBarState.showSearchBar()
}) {
Icon(
imageVector = Icons.Filled.Search,
contentDescription = "Search"
)
Row {
val filterDropdownState = rememberDropdownSelectorState()
Box {
IconButton(onClick = {
filterDropdownState.open()
}) {
Icon(
painterResource(github.tornaco.android.thanos.icon.remix.R.drawable.ic_remix_filter_fill),
contentDescription = "Filter"
)
}

DropdownSelector(
state = filterDropdownState,
items = listOf(
DropdownItem(
data = FilterState.All,
labelLines = listOf(stringResource(github.tornaco.android.thanos.res.R.string.all))
),
DropdownItem(
data = FilterState.Enabled,
labelLines = listOf(stringResource(github.tornaco.android.thanos.res.R.string.enabled))
),
DropdownItem(
data = FilterState.Disabled,
labelLines = listOf(stringResource(github.tornaco.android.thanos.res.R.string.disabled))
)
)
) {
viewModel.setFilter(it.data)
}
}

IconButton(onClick = {
searchBarState.showSearchBar()
}) {
Icon(
imageVector = Icons.Filled.Search,
contentDescription = "Search"
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import github.tornaco.android.thanos.common.UiState
import github.tornaco.android.thanos.core.app.ThanosManager
import github.tornaco.android.thanos.core.pm.AppInfo
import github.tornaco.android.thanos.core.pm.ComponentInfo
import github.tornaco.android.thanos.core.pm.ComponentUtil
import github.tornaco.android.thanos.core.pm.Pkg
import github.tornaco.thanos.module.component.manager.ComponentRule
import github.tornaco.thanos.module.component.manager.fallbackRule
Expand All @@ -28,6 +29,12 @@ import kotlinx.coroutines.withContext
import util.PinyinComparatorUtils
import java.util.UUID

enum class FilterState {
All,
Enabled,
Disabled,
}

data class ComponentGroup(
val rule: ComponentRule = fallbackRule,
val components: List<ComponentModel> = emptyList(),
Expand Down Expand Up @@ -59,16 +66,18 @@ abstract class ComponentsVM(

val selectState = MutableStateFlow(MultipleSelectState())
val batchOpState = MutableStateFlow(BatchOpState())
val filterState = MutableStateFlow(FilterState.All)

val components =
combineTransform<AppInfo, String, Long, UiState<List<ComponentGroup>>>(
combineTransform<AppInfo, String, FilterState, Long, UiState<List<ComponentGroup>>>(
_appInfo,
_searchQuery,
filterState,
_refresh,
transform = { appInfo, query, _ ->
transform = { appInfo, query, filterState, _ ->
emit(UiState.Loading)
kotlin.runCatching {
emit(UiState.Loaded(loadComponentsGroups(appInfo, query)))
emit(UiState.Loaded(loadComponentsGroups(appInfo, filterState, query)))
}.onFailure {
emit(UiState.Error(it))
}
Expand All @@ -86,6 +95,7 @@ abstract class ComponentsVM(

private suspend fun loadComponentsGroups(
appInfo: AppInfo,
filterState: FilterState,
query: String
): List<ComponentGroup> {
return withContext(Dispatchers.IO) {
Expand All @@ -99,6 +109,15 @@ abstract class ComponentsVM(
/* batchIndex = */ i
) ?: break
batch
.filter {
filterState == FilterState.All
|| filterState == FilterState.Enabled && ComponentUtil.isComponentEnabled(
it.enableSetting
)
|| filterState == FilterState.Disabled && ComponentUtil.isComponentDisabled(
it.enableSetting
)
}
.filter {
TextUtils.isEmpty(query) || it.name.lowercase().contains(query.lowercase())
}
Expand All @@ -116,6 +135,7 @@ abstract class ComponentsVM(
)
}
}

res.sort()

res.groupBy { it.componentRule }.toSortedMap { o1, o2 ->
Expand Down Expand Up @@ -149,6 +169,12 @@ abstract class ComponentsVM(
_refresh.update { System.currentTimeMillis() }
}

fun setFilter(filter: FilterState) {
filterState.update {
filter
}
}

fun expand(group: ComponentGroup, expand: Boolean) {
collapsedGroups.update {
if (expand) {
Expand Down

0 comments on commit 741d4d4

Please sign in to comment.