Skip to content

Commit

Permalink
[AppInfo] Add “New profile” option in the “Add to profile” dialog
Browse files Browse the repository at this point in the history
Signed-off-by: Muntashir Al-Islam <[email protected]>
  • Loading branch information
MuntashirAkon committed Aug 18, 2023
1 parent 39499c4 commit 2f4da4c
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,31 @@
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;

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;
Expand Down Expand Up @@ -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<ProfileMetaManager> profiles = ProfileManager.getProfileMetadata();
List<CharSequence> 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<AlertDialog> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<ProfileMetaManager> profiles = ProfileManager.getProfileMetadata();
List<CharSequence> 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))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<ProfileMetaManager> profiles = ProfileManager.getProfileMetadata();
List<CharSequence> 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<AlertDialog> 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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ProfileMetaManager> profiles = ProfileManager.getProfileMetadata();
List<CharSequence> 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<AlertDialog> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -189,7 +188,7 @@ public Profile getProfile() {
return mProfile;
}

public void appendPackages(@NonNull Collection<String> packageList) {
public void appendPackages(@NonNull String[] packageList) {
List<String> uniquePackages = new ArrayList<>();
for (String newPackage : packageList) {
if (!ArrayUtils.contains(mProfile.packages, newPackage)) {
Expand Down

0 comments on commit 2f4da4c

Please sign in to comment.