From 2f4da4ce3b5a0b0fed260580be50150ccd96963d Mon Sep 17 00:00:00 2001 From: Muntashir Al-Islam Date: Fri, 18 Aug 2023 18:43:50 +0600 Subject: [PATCH] =?UTF-8?q?[AppInfo]=20Add=20=E2=80=9CNew=20profile?= =?UTF-8?q?=E2=80=9D=20option=20in=20the=20=E2=80=9CAdd=20to=20profile?= =?UTF-8?q?=E2=80=9D=20dialog?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Muntashir Al-Islam --- .../AppManager/debloat/DebloaterActivity.java | 64 +------------ .../details/info/AppInfoFragment.java | 26 +----- .../AppManager/main/MainActivity.java | 58 +----------- .../profiles/AddToProfileDialogFragment.java | 89 +++++++++++++++++++ .../profiles/ProfileMetaManager.java | 3 +- 5 files changed, 101 insertions(+), 139 deletions(-) create mode 100644 app/src/main/java/io/github/muntashirakon/AppManager/profiles/AddToProfileDialogFragment.java diff --git a/app/src/main/java/io/github/muntashirakon/AppManager/debloat/DebloaterActivity.java b/app/src/main/java/io/github/muntashirakon/AppManager/debloat/DebloaterActivity.java index 05a93e69762..c873e7582d2 100644 --- a/app/src/main/java/io/github/muntashirakon/AppManager/debloat/DebloaterActivity.java +++ b/app/src/main/java/io/github/muntashirakon/AppManager/debloat/DebloaterActivity.java @@ -7,15 +7,12 @@ import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; -import android.text.SpannableStringBuilder; -import android.text.TextUtils; import android.view.Menu; import android.view.MenuItem; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.ActionBar; -import androidx.appcompat.app.AlertDialog; import androidx.core.content.ContextCompat; import androidx.lifecycle.ViewModelProvider; import androidx.recyclerview.widget.LinearLayoutManager; @@ -23,30 +20,18 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder; import com.google.android.material.progressindicator.LinearProgressIndicator; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.atomic.AtomicReference; - import io.github.muntashirakon.AppManager.BaseActivity; import io.github.muntashirakon.AppManager.R; import io.github.muntashirakon.AppManager.batchops.BatchOpsManager; import io.github.muntashirakon.AppManager.batchops.BatchOpsService; import io.github.muntashirakon.AppManager.misc.AdvancedSearchView; -import io.github.muntashirakon.AppManager.profiles.AppsProfileActivity; -import io.github.muntashirakon.AppManager.profiles.ProfileManager; -import io.github.muntashirakon.AppManager.profiles.ProfileMetaManager; +import io.github.muntashirakon.AppManager.profiles.AddToProfileDialogFragment; import io.github.muntashirakon.AppManager.utils.StoragePermission; import io.github.muntashirakon.AppManager.utils.UIUtils; -import io.github.muntashirakon.dialog.DialogTitleBuilder; -import io.github.muntashirakon.dialog.SearchableMultiChoiceDialogBuilder; -import io.github.muntashirakon.dialog.TextInputDialogBuilder; import io.github.muntashirakon.multiselection.MultiSelectionActionsView; import io.github.muntashirakon.widget.MultiSelectionView; import io.github.muntashirakon.widget.RecyclerView; -import static io.github.muntashirakon.AppManager.utils.UIUtils.getSecondaryText; -import static io.github.muntashirakon.AppManager.utils.UIUtils.getSmallerText; - public class DebloaterActivity extends BaseActivity implements MultiSelectionView.OnSelectionChangeListener, MultiSelectionActionsView.OnItemSelectedListener, AdvancedSearchView.OnQueryTextListener { DebloaterViewModel viewModel; @@ -164,50 +149,9 @@ public boolean onNavigationItemSelected(@NonNull MenuItem item) { handleBatchOp(BatchOpsManager.OP_UNBLOCK_TRACKERS)) .show(); } else if (id == R.id.action_add_to_profile) { - List profiles = ProfileManager.getProfileMetadata(); - List profileNames = new ArrayList<>(profiles.size()); - for (ProfileMetaManager profileMetaManager : profiles) { - profileNames.add(new SpannableStringBuilder(profileMetaManager.getProfileName()).append("\n") - .append(getSecondaryText(this, getSmallerText(profileMetaManager.toLocalizedString(this))))); - } - AtomicReference dialogRef = new AtomicReference<>(); - DialogTitleBuilder titleBuilder = new DialogTitleBuilder(this) - .setTitle(R.string.add_to_profile) - .setEndIconContentDescription(R.string.new_profile) - .setEndIcon(R.drawable.ic_add, v -> new TextInputDialogBuilder(this, R.string.input_profile_name) - .setTitle(R.string.new_profile) - .setHelperText(R.string.input_profile_name_description) - .setNegativeButton(R.string.cancel, null) - .setPositiveButton(R.string.go, (dialog, which, profName, isChecked) -> { - if (!TextUtils.isEmpty(profName)) { - //noinspection ConstantConditions - startActivity(AppsProfileActivity.getNewProfileIntent(this, profName.toString(), - viewModel.getSelectedPackages().keySet().toArray(new String[0]))); - mMultiSelectionView.cancel(); - if (dialogRef.get() != null) { - dialogRef.get().dismiss(); - } - } - }) - .show()); - AlertDialog alertDialog = new SearchableMultiChoiceDialogBuilder<>(this, profiles, profileNames) - .setTitle(titleBuilder.build()) - .setNegativeButton(R.string.cancel, null) - .setPositiveButton(R.string.add, (dialog, which, selectedItems) -> { - for (ProfileMetaManager metaManager : selectedItems) { - try { - metaManager.appendPackages(viewModel.getSelectedPackages().keySet()); - mMultiSelectionView.cancel(); - metaManager.writeProfile(); - } catch (Throwable e) { - e.printStackTrace(); - } - } - UIUtils.displayShortToast(R.string.done); - }) - .create(); - dialogRef.set(alertDialog); - alertDialog.show(); + AddToProfileDialogFragment dialog = AddToProfileDialogFragment.getInstance(viewModel.getSelectedPackages() + .keySet().toArray(new String[0])); + dialog.show(getSupportFragmentManager(), AddToProfileDialogFragment.TAG); } else return false; return true; } diff --git a/app/src/main/java/io/github/muntashirakon/AppManager/details/info/AppInfoFragment.java b/app/src/main/java/io/github/muntashirakon/AppManager/details/info/AppInfoFragment.java index 9689ace4a5f..8d70c44d494 100644 --- a/app/src/main/java/io/github/muntashirakon/AppManager/details/info/AppInfoFragment.java +++ b/app/src/main/java/io/github/muntashirakon/AppManager/details/info/AppInfoFragment.java @@ -13,7 +13,6 @@ import static io.github.muntashirakon.AppManager.utils.UIUtils.getBitmapFromDrawable; import static io.github.muntashirakon.AppManager.utils.UIUtils.getColoredText; import static io.github.muntashirakon.AppManager.utils.UIUtils.getDimmedBitmap; -import static io.github.muntashirakon.AppManager.utils.UIUtils.getSecondaryText; import static io.github.muntashirakon.AppManager.utils.UIUtils.getSmallerText; import static io.github.muntashirakon.AppManager.utils.UIUtils.getStyledKeyValue; import static io.github.muntashirakon.AppManager.utils.Utils.openAsFolderInFM; @@ -123,8 +122,7 @@ import io.github.muntashirakon.AppManager.magisk.MagiskDenyList; import io.github.muntashirakon.AppManager.magisk.MagiskHide; import io.github.muntashirakon.AppManager.magisk.MagiskProcess; -import io.github.muntashirakon.AppManager.profiles.ProfileManager; -import io.github.muntashirakon.AppManager.profiles.ProfileMetaManager; +import io.github.muntashirakon.AppManager.profiles.AddToProfileDialogFragment; import io.github.muntashirakon.AppManager.rules.RulesTypeSelectionDialogFragment; import io.github.muntashirakon.AppManager.rules.compontents.ComponentsBlocker; import io.github.muntashirakon.AppManager.rules.struct.ComponentRule; @@ -523,26 +521,8 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) { .setNegativeButton(R.string.cancel, null) .show(); } else if (itemId == R.id.action_add_to_profile) { - List profiles = ProfileManager.getProfileMetadata(); - List profileNames = new ArrayList<>(profiles.size()); - for (ProfileMetaManager profileMetaManager : profiles) { - profileNames.add(new SpannableStringBuilder(profileMetaManager.getProfileName()).append("\n") - .append(getSecondaryText(mActivity, getSmallerText(profileMetaManager.toLocalizedString(mActivity))))); - } - new SearchableMultiChoiceDialogBuilder<>(mActivity, profiles, profileNames) - .setTitle(R.string.add_to_profile) - .setNegativeButton(R.string.cancel, null) - .setPositiveButton(R.string.add, (dialog, which, selectedItems) -> { - for (ProfileMetaManager metaManager : selectedItems) { - try { - metaManager.appendPackages(Collections.singletonList(mPackageName)); - metaManager.writeProfile(); - } catch (Throwable e) { - e.printStackTrace(); - } - } - }) - .show(); + AddToProfileDialogFragment dialog = AddToProfileDialogFragment.getInstance(new String[]{mPackageName}); + dialog.show(getChildFragmentManager(), AddToProfileDialogFragment.TAG); } else if (itemId == R.id.action_optimize) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && (SelfPermissions.isSystemOrRootOrShell() || BuildConfig.APPLICATION_ID.equals(mInstallerPackageName))) { diff --git a/app/src/main/java/io/github/muntashirakon/AppManager/main/MainActivity.java b/app/src/main/java/io/github/muntashirakon/AppManager/main/MainActivity.java index ccef6f6f0f3..08d999b0917 100644 --- a/app/src/main/java/io/github/muntashirakon/AppManager/main/MainActivity.java +++ b/app/src/main/java/io/github/muntashirakon/AppManager/main/MainActivity.java @@ -2,9 +2,6 @@ package io.github.muntashirakon.AppManager.main; -import static io.github.muntashirakon.AppManager.utils.UIUtils.getSecondaryText; -import static io.github.muntashirakon.AppManager.utils.UIUtils.getSmallerText; - import android.annotation.SuppressLint; import android.content.BroadcastReceiver; import android.content.Context; @@ -13,7 +10,6 @@ import android.content.pm.PackageManager; import android.net.Uri; import android.os.Bundle; -import android.text.SpannableStringBuilder; import android.text.TextUtils; import android.view.Gravity; import android.view.Menu; @@ -26,7 +22,6 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.ActionBar; -import androidx.appcompat.app.AlertDialog; import androidx.collection.ArrayMap; import androidx.core.content.ContextCompat; import androidx.lifecycle.ViewModelProvider; @@ -44,7 +39,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.concurrent.atomic.AtomicReference; import io.github.muntashirakon.AppManager.BaseActivity; import io.github.muntashirakon.AppManager.BuildConfig; @@ -63,9 +57,7 @@ import io.github.muntashirakon.AppManager.misc.HelpActivity; import io.github.muntashirakon.AppManager.misc.LabsActivity; import io.github.muntashirakon.AppManager.oneclickops.OneClickOpsActivity; -import io.github.muntashirakon.AppManager.profiles.AppsProfileActivity; -import io.github.muntashirakon.AppManager.profiles.ProfileManager; -import io.github.muntashirakon.AppManager.profiles.ProfileMetaManager; +import io.github.muntashirakon.AppManager.profiles.AddToProfileDialogFragment; import io.github.muntashirakon.AppManager.profiles.ProfilesActivity; import io.github.muntashirakon.AppManager.rules.RulesTypeSelectionDialogFragment; import io.github.muntashirakon.AppManager.runningapps.RunningAppsActivity; @@ -80,11 +72,9 @@ import io.github.muntashirakon.AppManager.utils.StoragePermission; import io.github.muntashirakon.AppManager.utils.UIUtils; import io.github.muntashirakon.dialog.AlertDialogBuilder; -import io.github.muntashirakon.dialog.DialogTitleBuilder; import io.github.muntashirakon.dialog.ScrollableDialogBuilder; import io.github.muntashirakon.dialog.SearchableMultiChoiceDialogBuilder; import io.github.muntashirakon.dialog.SearchableSingleChoiceDialogBuilder; -import io.github.muntashirakon.dialog.TextInputDialogBuilder; import io.github.muntashirakon.io.Paths; import io.github.muntashirakon.multiselection.MultiSelectionActionsView; import io.github.muntashirakon.util.UiUtils; @@ -437,49 +427,9 @@ public boolean onNavigationItemSelected(@NonNull MenuItem item) { } else if (id == R.id.action_uninstall) { handleBatchOpWithWarning(BatchOpsManager.OP_UNINSTALL); } else if (id == R.id.action_add_to_profile) { - List profiles = ProfileManager.getProfileMetadata(); - List profileNames = new ArrayList<>(profiles.size()); - for (ProfileMetaManager profileMetaManager : profiles) { - profileNames.add(new SpannableStringBuilder(profileMetaManager.getProfileName()).append("\n") - .append(getSecondaryText(this, getSmallerText(profileMetaManager.toLocalizedString(this))))); - } - AtomicReference dialogRef = new AtomicReference<>(); - DialogTitleBuilder titleBuilder = new DialogTitleBuilder(this) - .setTitle(R.string.add_to_profile) - .setEndIconContentDescription(R.string.new_profile) - .setEndIcon(R.drawable.ic_add, v -> new TextInputDialogBuilder(this, R.string.input_profile_name) - .setTitle(R.string.new_profile) - .setHelperText(R.string.input_profile_name_description) - .setNegativeButton(R.string.cancel, null) - .setPositiveButton(R.string.go, (dialog, which, profName, isChecked) -> { - if (!TextUtils.isEmpty(profName)) { - //noinspection ConstantConditions - startActivity(AppsProfileActivity.getNewProfileIntent(this, profName.toString(), - viewModel.getSelectedPackages().keySet().toArray(new String[0]))); - mMultiSelectionView.cancel(); - if (dialogRef.get() != null) { - dialogRef.get().dismiss(); - } - } - }) - .show()); - AlertDialog alertDialog = new SearchableMultiChoiceDialogBuilder<>(this, profiles, profileNames) - .setTitle(titleBuilder.build()) - .setNegativeButton(R.string.cancel, null) - .setPositiveButton(R.string.add, (dialog, which, selectedItems) -> { - for (ProfileMetaManager metaManager : selectedItems) { - try { - metaManager.appendPackages(viewModel.getSelectedPackages().keySet()); - metaManager.writeProfile(); - } catch (Throwable e) { - e.printStackTrace(); - } - } - UIUtils.displayShortToast(R.string.done); - }) - .create(); - dialogRef.set(alertDialog); - alertDialog.show(); + AddToProfileDialogFragment dialog = AddToProfileDialogFragment.getInstance(viewModel.getSelectedPackages() + .keySet().toArray(new String[0])); + dialog.show(getSupportFragmentManager(), AddToProfileDialogFragment.TAG); } else { return false; } diff --git a/app/src/main/java/io/github/muntashirakon/AppManager/profiles/AddToProfileDialogFragment.java b/app/src/main/java/io/github/muntashirakon/AppManager/profiles/AddToProfileDialogFragment.java new file mode 100644 index 00000000000..66d581c2601 --- /dev/null +++ b/app/src/main/java/io/github/muntashirakon/AppManager/profiles/AddToProfileDialogFragment.java @@ -0,0 +1,89 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package io.github.muntashirakon.AppManager.profiles; + +import static io.github.muntashirakon.AppManager.utils.UIUtils.getSecondaryText; +import static io.github.muntashirakon.AppManager.utils.UIUtils.getSmallerText; + +import android.app.Dialog; +import android.os.Bundle; +import android.text.SpannableStringBuilder; +import android.text.TextUtils; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AlertDialog; +import androidx.fragment.app.DialogFragment; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicReference; + +import io.github.muntashirakon.AppManager.R; +import io.github.muntashirakon.AppManager.utils.UIUtils; +import io.github.muntashirakon.dialog.DialogTitleBuilder; +import io.github.muntashirakon.dialog.SearchableMultiChoiceDialogBuilder; +import io.github.muntashirakon.dialog.TextInputDialogBuilder; + +public class AddToProfileDialogFragment extends DialogFragment { + public static final String TAG = AddToProfileDialogFragment.class.getSimpleName(); + + private static final String ARG_PKGS = "pkgs"; + + public static AddToProfileDialogFragment getInstance(@NonNull String[] packages) { + AddToProfileDialogFragment fragment = new AddToProfileDialogFragment(); + Bundle args = new Bundle(); + args.putStringArray(ARG_PKGS, packages); + fragment.setArguments(args); + return fragment; + } + + @NonNull + @Override + public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { + String[] packages = requireArguments().getStringArray(ARG_PKGS); + List profiles = ProfileManager.getProfileMetadata(); + List profileNames = new ArrayList<>(profiles.size()); + for (ProfileMetaManager profileMetaManager : profiles) { + profileNames.add(new SpannableStringBuilder(profileMetaManager.getProfileName()).append("\n") + .append(getSecondaryText(requireContext(), getSmallerText(profileMetaManager + .toLocalizedString(requireContext()))))); + } + AtomicReference dialogRef = new AtomicReference<>(); + DialogTitleBuilder titleBuilder = new DialogTitleBuilder(requireContext()) + .setTitle(R.string.add_to_profile) + .setEndIconContentDescription(R.string.new_profile) + .setEndIcon(R.drawable.ic_add, v -> new TextInputDialogBuilder(requireContext(), R.string.input_profile_name) + .setTitle(R.string.new_profile) + .setHelperText(R.string.input_profile_name_description) + .setNegativeButton(R.string.cancel, null) + .setPositiveButton(R.string.go, (dialog, which, profName, isChecked) -> { + if (!TextUtils.isEmpty(profName)) { + //noinspection ConstantConditions + startActivity(AppsProfileActivity.getNewProfileIntent(requireContext(), profName.toString(), + packages)); + if (dialogRef.get() != null) { + dialogRef.get().dismiss(); + } + } + }) + .show()); + AlertDialog alertDialog = new SearchableMultiChoiceDialogBuilder<>(requireContext(), profiles, profileNames) + .setTitle(titleBuilder.build()) + .setNegativeButton(R.string.cancel, null) + .setPositiveButton(R.string.add, (dialog, which, selectedItems) -> { + for (ProfileMetaManager metaManager : selectedItems) { + try { + metaManager.appendPackages(packages); + metaManager.writeProfile(); + } catch (Throwable e) { + e.printStackTrace(); + } + } + UIUtils.displayShortToast(R.string.done); + }) + .create(); + dialogRef.set(alertDialog); + return alertDialog; + } +} diff --git a/app/src/main/java/io/github/muntashirakon/AppManager/profiles/ProfileMetaManager.java b/app/src/main/java/io/github/muntashirakon/AppManager/profiles/ProfileMetaManager.java index 7df2c158368..663d73bf479 100644 --- a/app/src/main/java/io/github/muntashirakon/AppManager/profiles/ProfileMetaManager.java +++ b/app/src/main/java/io/github/muntashirakon/AppManager/profiles/ProfileMetaManager.java @@ -21,7 +21,6 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.util.ArrayList; -import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Objects; @@ -189,7 +188,7 @@ public Profile getProfile() { return mProfile; } - public void appendPackages(@NonNull Collection packageList) { + public void appendPackages(@NonNull String[] packageList) { List uniquePackages = new ArrayList<>(); for (String newPackage : packageList) { if (!ArrayUtils.contains(mProfile.packages, newPackage)) {