Skip to content

Commit

Permalink
Select all in this page
Browse files Browse the repository at this point in the history
  • Loading branch information
Tornaco committed Oct 23, 2024
1 parent e32876c commit b9ccd23
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,8 @@
<string name="common_menu_title_batch_select">اختيار الدفعة</string>
<string name="common_menu_title_select_all">الكل</string>
<string name="common_menu_title_un_select_all">لا على الاطلاق</string>
<string name="common_menu_title_select_all_in_this_page">Yes in this page</string>
<string name="common_menu_title_un_select_all_in_this_page">No in this page</string>
<string name="common_menu_title_search">بحث</string>
<string name="common_menu_title_rename">إعادة تسمية</string>
<string name="common_menu_title_remove">إزالة</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@
<string name="common_menu_title_batch_select">批量选择</string>
<string name="common_menu_title_select_all">全部</string>
<string name="common_menu_title_un_select_all">全不</string>
<string name="common_menu_title_select_all_in_this_page">当前页全部</string>
<string name="common_menu_title_un_select_all_in_this_page">当前页全不</string>
<string name="common_menu_title_search">搜索</string>
<string name="common_menu_title_rename">重命名</string>
<string name="common_menu_title_remove">移除</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@
<string name="common_menu_title_batch_select">批量选择</string>
<string name="common_menu_title_select_all">全部</string>
<string name="common_menu_title_un_select_all">全不</string>
<string name="common_menu_title_select_all_in_this_page">当前页全部</string>
<string name="common_menu_title_un_select_all_in_this_page">当前页全不</string>
<string name="common_menu_title_search">搜索</string>
<string name="common_menu_title_remove">移除</string>
<string name="common_menu_title_uninstall">卸载</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,8 @@
<string name="common_menu_title_batch_select">Batch select</string>
<string name="common_menu_title_select_all">All yes</string>
<string name="common_menu_title_un_select_all">All no</string>
<string name="common_menu_title_select_all_in_this_page">Yes in this page</string>
<string name="common_menu_title_un_select_all_in_this_page">No in this page</string>
<string name="common_menu_title_search">search</string>
<string name="common_menu_title_rename">Rename</string>
<string name="common_menu_title_remove">Remove</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@

<style name="TextAppearance.TileSubTitle" parent="TextAppearance.Small">
<item name="android:textColor">?android:attr/textColorPrimary</item>
<item name="android:textSize">13sp</item>
<item name="android:textSize">12sp</item>
<!-- Medium-->
<item name="android:fontWeight">500</item>
</style>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package github.tornaco.thanos.module.component.manager

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.Menu
Expand All @@ -12,6 +13,7 @@ import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.elvishew.xlog.XLog
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.miguelcatalan.materialsearchview.MaterialSearchView
Expand All @@ -24,6 +26,8 @@ import github.tornaco.android.thanos.theme.ThemeActivity
import github.tornaco.android.thanos.widget.ModernProgressDialog
import github.tornaco.thanos.module.component.manager.databinding.ModuleComponentManagerComponentListActivityBinding
import github.tornaco.thanos.module.component.manager.model.ComponentModel
import kotlin.math.max
import kotlin.math.min

abstract class ComponentListActivity : ThemeActivity() {

Expand Down Expand Up @@ -174,7 +178,7 @@ abstract class ComponentListActivity : ThemeActivity() {
.setPositiveButton(
android.R.string.ok
) { _, _ ->
onRequestSelectAll(true)
onRequestSelectAll(viewModel.componentModels, true)
}.show()
return true
}
Expand All @@ -184,19 +188,53 @@ abstract class ComponentListActivity : ThemeActivity() {
.setMessage(github.tornaco.android.thanos.res.R.string.common_dialog_message_are_you_sure)
.setPositiveButton(
android.R.string.ok
) { _, _ -> onRequestSelectAll(false) }
) { _, _ -> onRequestSelectAll(viewModel.componentModels, false) }
.show()
return true
}
if (github.tornaco.thanos.module.component.manager.R.id.action_select_all_in_this_page == item.itemId) {
MaterialAlertDialogBuilder(thisActivity())
.setTitle(github.tornaco.android.thanos.res.R.string.common_menu_title_select_all_in_this_page)
.setMessage(github.tornaco.android.thanos.res.R.string.common_dialog_message_are_you_sure)
.setPositiveButton(
android.R.string.ok
) { _, _ -> onRequestSelectAllInThisPage(binding.componentListView, true) }
.show()
return true
}
if (github.tornaco.thanos.module.component.manager.R.id.action_un_select_all_in_this_page == item.itemId) {
MaterialAlertDialogBuilder(thisActivity())
.setTitle(github.tornaco.android.thanos.res.R.string.common_menu_title_un_select_all_in_this_page)
.setMessage(github.tornaco.android.thanos.res.R.string.common_dialog_message_are_you_sure)
.setPositiveButton(
android.R.string.ok
) { _, _ -> onRequestSelectAllInThisPage(binding.componentListView, false) }
.show()
return true
}
return super.onOptionsItemSelected(item)
}

private fun onRequestSelectAll(isSelectAll: Boolean) {
private fun onRequestSelectAllInThisPage(recyclerView: RecyclerView, isSelectAll: Boolean) {
val models = viewModel.componentModels
val lm = recyclerView.layoutManager as LinearLayoutManager
val f = max(0, lm.findFirstVisibleItemPosition())
val l = min(models.size, lm.findLastVisibleItemPosition())
XLog.d("onRequestSelectAllInThisPage $f - $l")
val items = viewModel.componentModels.subList(f, l)
XLog.d("onRequestSelectAllInThisPage ${models.map { it.componentName.className }}")
XLog.d("onRequestSelectAllInThisPage ${items.map { it.componentName.className }}")
onRequestSelectAll(items, isSelectAll)
}

@SuppressLint("NotifyDataSetChanged")
private fun onRequestSelectAll(modelList: List<ComponentModel>, isSelectAll: Boolean) {
val progressDialog = ModernProgressDialog(thisActivity())
progressDialog.setTitle(getString(github.tornaco.android.thanos.res.R.string.common_text_wait_a_moment))
progressDialog.show()
viewModel.selectAll(
appInfo,
modelList,
isSelectAll,
{
runOnUiThread {
Expand All @@ -207,7 +245,7 @@ abstract class ComponentListActivity : ThemeActivity() {
runOnUiThread {
XLog.d("onRequestSelectAll, onComplete")
progressDialog.dismiss()
viewModel.start()
binding.componentListView.adapter?.notifyDataSetChanged()
}
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,23 @@ private void loadComponents() {

protected abstract ComponentsLoader onCreateLoader();

public void toggleComponentState(AppInfo appInfo, @Nullable ComponentModel componentModel, boolean checked) {
if (componentModel == null) return;
public boolean toggleComponentState(AppInfo appInfo, @Nullable ComponentModel componentModel, boolean checked) {
if (componentModel == null) return false;
ThanosManager thanox = ThanosManager.from(getApplication());
int newState = checked ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
if (newState == componentModel.getEnableSetting()) {
return false;
}
if (thanox.isServiceInstalled()) {
componentModel.setEnableSetting(newState);
thanox.getPkgManager().setComponentEnabledSetting(
appInfo.getUserId(),
componentModel.getComponentName(),
newState
, 0 /* Kill it */);
return true;
}
return false;
}

private void registerEventReceivers() {
Expand All @@ -121,21 +126,23 @@ public void clearSearchText() {
loadComponents();
}

public void selectAll(AppInfo appInfo, boolean enabled, Consumer<String> onUpdate, Runnable onComplete) {
int totalCount = componentModels.size();
public void selectAll(AppInfo appInfo, List<ComponentModel> modelList,
boolean enabled, Consumer<String> onUpdate,
Runnable onComplete) {
int totalCount = modelList.size();
ThanosManager.from(getApplication()).getActivityManager().forceStopPackage(Pkg.fromAppInfo(appInfo), "ComponentList UI selectAll");

// Wait 1s.
disposables.add(Completable.fromAction(() -> {
for (int i = 0; i < componentModels.size(); i++) {
ComponentModel componentModel = componentModels.get(i);
for (int i = 0; i < modelList.size(); i++) {
ComponentModel componentModel = modelList.get(i);
onUpdate.accept((i + 1) + "/" + totalCount);
toggleComponentState(appInfo, componentModel, enabled);
try {
// Maybe a short delay will make it safer.
Thread.sleep(100);
} catch (InterruptedException ignored) {
if (toggleComponentState(appInfo, componentModel, enabled)) {
try {
// Maybe a short delay will make it safer.
Thread.sleep(100);
} catch (InterruptedException ignored) {

}
}
}
}).subscribeOn(Schedulers.io())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,24 @@
android:layout_alignParentStart="true"
android:layout_marginTop="@dimen/common_view_margin_small"
android:layout_marginEnd="@dimen/common_view_margin_default"
android:layout_toStartOf="@id/state_container"
android:ellipsize="middle"
android:fadingEdge="horizontal"
android:text="@{component.label}"
android:textAppearance="@style/TextAppearance.TileTitle"
tools:text="Service Name" />

<com.google.android.material.button.MaterialButton
android:id="@+id/badge1"
style="@style/MaterialButtonBadged"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/common_view_margin_small"
android:layout_marginEnd="@dimen/common_view_margin_default"
android:layout_toEndOf="@id/label"
android:text="@string/module_component_manager_disabled_by_thanox"
android:textSize="@dimen/list_item_main_text_size_ext_small"
android:visibility="@{component.disabledByThanox ? View.VISIBLE : View.GONE}" />


<TextView
android:id="@+id/name"
Expand Down Expand Up @@ -126,17 +137,6 @@
android:layout_height="wrap_content"
android:orientation="vertical">

<com.google.android.material.button.MaterialButton
android:id="@+id/badge1"
style="@style/MaterialButtonBadged"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/common_view_margin_small"
android:layout_marginEnd="@dimen/common_view_margin_default"
android:text="@string/module_component_manager_disabled_by_thanox"
android:textSize="@dimen/list_item_main_text_size_ext_small"
android:visibility="@{component.disabledByThanox ? View.VISIBLE : View.GONE}" />

<com.google.android.material.button.MaterialButton
android:id="@+id/badge2"
style="@style/MaterialButtonBadged"
Expand All @@ -146,6 +146,7 @@
android:layout_marginEnd="@dimen/common_view_margin_default"
android:text="@string/module_component_manager_component_running"
android:textSize="@dimen/list_item_main_text_size_ext_small"
tools:visibility="gone"
android:visibility="@{component.running ? View.VISIBLE : View.GONE}" />

</LinearLayout>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_show_feature_desc"
android:icon="@drawable/module_common_ic_outline_info_24"
android:title="@string/module_component_manager_disabled_by_thanox"
app:showAsAction="always" />

<item
android:id="@+id/action_show_feature_desc"
android:icon="@drawable/module_common_ic_outline_info_24"
android:title="@string/module_component_manager_disabled_by_thanox"
app:showAsAction="always" />
android:id="@+id/action_component_search"
android:icon="@drawable/module_common_ic_search_2_line"
android:title="@string/common_menu_title_search"
app:showAsAction="ifRoom" />

<item
android:id="@+id/action_component_search"
android:icon="@drawable/module_common_ic_search_2_line"
android:title="@string/common_menu_title_search"
app:showAsAction="ifRoom" />
android:id="@+id/action_select_all_in_this_page"
android:title="@string/common_menu_title_select_all_in_this_page"
app:showAsAction="never" />

<item
android:id="@+id/action_select_all"
android:title="@string/common_menu_title_select_all"
app:showAsAction="never" />
android:id="@+id/action_un_select_all_in_this_page"
android:title="@string/common_menu_title_un_select_all_in_this_page"
app:showAsAction="never" />

<item
android:id="@+id/action_un_select_all"
android:title="@string/common_menu_title_un_select_all"
app:showAsAction="never" />
android:id="@+id/action_select_all"
android:title="@string/common_menu_title_select_all"
app:showAsAction="never" />

<item
android:id="@+id/action_un_select_all"
android:title="@string/common_menu_title_un_select_all"
app:showAsAction="never" />
</menu>

0 comments on commit b9ccd23

Please sign in to comment.