Search in sources :

Example 1 with RulesTypeSelectionDialogFragment

use of io.github.muntashirakon.AppManager.rules.RulesTypeSelectionDialogFragment in project AppManager by MuntashirAkon.

the class AppInfoFragment method onOptionsItemSelected.

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    int itemId = item.getItemId();
    if (itemId == R.id.action_refresh_detail) {
        refreshDetails();
    } else if (itemId == R.id.action_share_apk) {
        executor.submit(() -> {
            try {
                Path tmpApkSource = ApkUtils.getSharableApkFile(mPackageInfo);
                UiThreadHandler.run(() -> {
                    Context ctx = AppManager.getContext();
                    Intent intent = new Intent(Intent.ACTION_SEND).setType("application/*").putExtra(Intent.EXTRA_STREAM, FmProvider.getContentUri(tmpApkSource)).addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    ctx.startActivity(Intent.createChooser(intent, ctx.getString(R.string.share_apk)).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                });
            } catch (Exception e) {
                Log.e(TAG, e);
                displayLongToast(R.string.failed_to_extract_apk_file);
            }
        });
    } else if (itemId == R.id.action_backup) {
        if (mainModel == null)
            return true;
        BackupDialogFragment backupDialogFragment = new BackupDialogFragment();
        Bundle args = new Bundle();
        args.putParcelableArrayList(BackupDialogFragment.ARG_PACKAGE_PAIRS, new ArrayList<>(Collections.singleton(new UserPackagePair(mPackageName, mainModel.getUserHandle()))));
        backupDialogFragment.setArguments(args);
        backupDialogFragment.setOnActionBeginListener(mode -> showProgressIndicator(true));
        backupDialogFragment.setOnActionCompleteListener((mode, failedPackages) -> showProgressIndicator(false));
        backupDialogFragment.show(mActivity.getSupportFragmentManager(), BackupDialogFragment.TAG);
    } else if (itemId == R.id.action_view_settings) {
        startActivity(IntentUtils.getAppDetailsSettings(mPackageName));
    } else if (itemId == R.id.action_export_blocking_rules) {
        final String fileName = "app_manager_rules_export-" + DateUtils.formatDateTime(System.currentTimeMillis()) + ".am.tsv";
        export.launch(fileName, uri -> {
            if (uri == null || mainModel == null) {
                // Back button pressed.
                return;
            }
            RulesTypeSelectionDialogFragment dialogFragment = new RulesTypeSelectionDialogFragment();
            Bundle exportArgs = new Bundle();
            ArrayList<String> packages = new ArrayList<>();
            packages.add(mPackageName);
            exportArgs.putInt(RulesTypeSelectionDialogFragment.ARG_MODE, RulesTypeSelectionDialogFragment.MODE_EXPORT);
            exportArgs.putParcelable(RulesTypeSelectionDialogFragment.ARG_URI, uri);
            exportArgs.putStringArrayList(RulesTypeSelectionDialogFragment.ARG_PKG, packages);
            exportArgs.putIntArray(RulesTypeSelectionDialogFragment.ARG_USERS, new int[] { mainModel.getUserHandle() });
            dialogFragment.setArguments(exportArgs);
            dialogFragment.show(mActivity.getSupportFragmentManager(), RulesTypeSelectionDialogFragment.TAG);
        });
    } else if (itemId == R.id.action_open_in_termux) {
        if (PermissionUtils.hasTermuxPermission(mActivity)) {
            openInTermux();
        } else
            requestPerm.launch(TERMUX_PERM_RUN_COMMAND, granted -> {
                if (granted)
                    openInTermux();
            });
    } else if (itemId == R.id.action_run_in_termux) {
        if (PermissionUtils.hasTermuxPermission(mActivity)) {
            runInTermux();
        } else
            requestPerm.launch(TERMUX_PERM_RUN_COMMAND, granted -> {
                if (granted)
                    runInTermux();
            });
    } else if (itemId == R.id.action_magisk_hide) {
        if (mainModel == null)
            return true;
        displayMagiskHideDialog();
    } else if (itemId == R.id.action_magisk_denylist) {
        if (mainModel == null)
            return true;
        displayMagiskDenyListDialog();
    } else if (itemId == R.id.action_battery_opt) {
        if (hasDumpPermission()) {
            new MaterialAlertDialogBuilder(mActivity).setTitle(R.string.battery_optimization).setMessage(R.string.choose_what_to_do).setPositiveButton(R.string.enable, (dialog, which) -> {
                Runner.runCommand(new String[] { "dumpsys", "deviceidle", "whitelist", "-" + mPackageName });
                refreshDetails();
            }).setNegativeButton(R.string.disable, (dialog, which) -> {
                Runner.runCommand(new String[] { "dumpsys", "deviceidle", "whitelist", "+" + mPackageName });
                refreshDetails();
            }).show();
        } else {
            Log.e(TAG, "No DUMP permission.");
        }
    } else if (itemId == R.id.action_net_policy) {
        ArrayMap<Integer, String> netPolicyMap = NetworkPolicyManagerCompat.getAllReadablePolicies(mActivity);
        int[] polices = new int[netPolicyMap.size()];
        String[] policyStrings = new String[netPolicyMap.size()];
        boolean[] choices = new boolean[netPolicyMap.size()];
        AtomicInteger selectedPolicies = new AtomicInteger(NetworkPolicyManagerCompat.getUidPolicy(mApplicationInfo.uid));
        for (int i = 0; i < netPolicyMap.size(); ++i) {
            polices[i] = netPolicyMap.keyAt(i);
            policyStrings[i] = netPolicyMap.valueAt(i);
            if (selectedPolicies.get() == 0) {
                choices[i] = polices[i] == NetworkPolicyManager.POLICY_NONE;
            } else {
                choices[i] = (selectedPolicies.get() & polices[i]) != 0;
            }
        }
        new MaterialAlertDialogBuilder(mActivity).setTitle(R.string.net_policy).setMultiChoiceItems(policyStrings, choices, (dialog, which, isChecked) -> {
            int currentPolicies = selectedPolicies.get();
            if (isChecked)
                selectedPolicies.set(currentPolicies | polices[which]);
            else
                selectedPolicies.set(currentPolicies & ~polices[which]);
        }).setNegativeButton(R.string.cancel, null).setPositiveButton(R.string.save, (dialog, which) -> {
            try {
                NetworkPolicyManagerCompat.setUidPolicy(mApplicationInfo.uid, selectedPolicies.get());
                refreshDetails();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }).show();
    } else if (itemId == R.id.action_extract_icon) {
        String iconName = mPackageLabel + "_icon.png";
        export.launch(iconName, uri -> {
            if (uri == null) {
                // Back button pressed.
                return;
            }
            try {
                try (OutputStream outputStream = mActivity.getContentResolver().openOutputStream(uri)) {
                    if (outputStream == null) {
                        throw new IOException("Unable to open output stream.");
                    }
                    Bitmap bitmap = FileUtils.getBitmapFromDrawable(mApplicationInfo.loadIcon(mPackageManager));
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
                    outputStream.flush();
                    displayShortToast(R.string.saved_successfully);
                }
            } catch (IOException e) {
                Log.e(TAG, e);
                displayShortToast(R.string.saving_failed);
            }
        });
    } else if (itemId == R.id.action_add_to_profile) {
        HashMap<String, ProfileMetaManager> profilesMap = ProfileManager.getProfileMetadata();
        List<CharSequence> profileNames = new ArrayList<>(profilesMap.size());
        List<ProfileMetaManager> profiles = new ArrayList<>(profilesMap.size());
        ProfileMetaManager profileMetaManager;
        Spannable summary;
        for (String profileName : profilesMap.keySet()) {
            profileMetaManager = profilesMap.get(profileName);
            // noinspection ConstantConditions
            summary = TextUtils.joinSpannable(", ", profileMetaManager.getLocalisedSummaryOrComment(mActivity));
            profiles.add(profileMetaManager);
            profileNames.add(new SpannableStringBuilder(profileName).append("\n").append(getSecondaryText(mActivity, getSmallerText(summary))));
        }
        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) {
                if (metaManager.profile != null) {
                    try {
                        metaManager.profile.packages = ArrayUtils.appendElement(String.class, metaManager.profile.packages, mPackageName);
                        metaManager.writeProfile();
                    } catch (Throwable e) {
                        e.printStackTrace();
                    }
                }
            }
        }).show();
    } else
        return super.onOptionsItemSelected(item);
    return true;
}
Also used : Chip(com.google.android.material.chip.Chip) Arrays(java.util.Arrays) Bundle(android.os.Bundle) NonNull(androidx.annotation.NonNull) BackupDialogFragment(io.github.muntashirakon.AppManager.backup.BackupDialogFragment) BuildConfig(io.github.muntashirakon.AppManager.BuildConfig) ComponentRule(io.github.muntashirakon.AppManager.rules.struct.ComponentRule) ProxyFile(io.github.muntashirakon.io.ProxyFile) Uri(android.net.Uri) ImageView(android.widget.ImageView) Drawable(android.graphics.drawable.Drawable) LogViewerActivity(io.github.muntashirakon.AppManager.logcat.LogViewerActivity) Manifest(android.Manifest) UIUtils.getStyledKeyValue(io.github.muntashirakon.AppManager.utils.UIUtils.getStyledKeyValue) NoRootAccessibilityService(io.github.muntashirakon.AppManager.accessibility.NoRootAccessibilityService) UIUtils.displayLongToast(io.github.muntashirakon.AppManager.utils.UIUtils.displayLongToast) UIUtils.displayShortToast(io.github.muntashirakon.AppManager.utils.UIUtils.displayShortToast) Fragment(androidx.fragment.app.Fragment) ClipboardManager(android.content.ClipboardManager) UiThread(androidx.annotation.UiThread) ContextCompat(androidx.core.content.ContextCompat) R(io.github.muntashirakon.AppManager.R) ArrayMap(androidx.collection.ArrayMap) Runner(io.github.muntashirakon.AppManager.runner.Runner) PackageManagerCompat(io.github.muntashirakon.AppManager.servermanager.PackageManagerCompat) LinearProgressIndicator(com.google.android.material.progressindicator.LinearProgressIndicator) NetworkPolicyManager(android.net.NetworkPolicyManager) KeyStoreUtils(io.github.muntashirakon.AppManager.utils.KeyStoreUtils) FileUtils(io.github.muntashirakon.AppManager.utils.FileUtils) UIUtils(io.github.muntashirakon.AppManager.utils.UIUtils) Executors(java.util.concurrent.Executors) StringRes(androidx.annotation.StringRes) Nullable(androidx.annotation.Nullable) SearchableMultiChoiceDialogBuilder(io.github.muntashirakon.AppManager.types.SearchableMultiChoiceDialogBuilder) LangUtils(io.github.muntashirakon.AppManager.utils.LangUtils) UIUtils.getColoredText(io.github.muntashirakon.AppManager.utils.UIUtils.getColoredText) ActivityResultContracts(androidx.activity.result.contract.ActivityResultContracts) MagiskHide(io.github.muntashirakon.AppManager.magisk.MagiskHide) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) UiThreadHandler(io.github.muntashirakon.AppManager.utils.UiThreadHandler) PackageInstallerActivity(io.github.muntashirakon.AppManager.apk.installer.PackageInstallerActivity) RunnerUtils(io.github.muntashirakon.AppManager.runner.RunnerUtils) ServiceHelper(io.github.muntashirakon.AppManager.logcat.helper.ServiceHelper) SsaidSettings(io.github.muntashirakon.AppManager.ssaid.SsaidSettings) TextInputLayout(com.google.android.material.textfield.TextInputLayout) PackageSizeInfo(io.github.muntashirakon.AppManager.types.PackageSizeInfo) HIDDEN_API_ENFORCEMENT_DISABLED(io.github.muntashirakon.AppManager.servermanager.ApplicationInfoCompat.HIDDEN_API_ENFORCEMENT_DISABLED) WorkerThread(androidx.annotation.WorkerThread) RemoteException(android.os.RemoteException) ArrayList(java.util.ArrayList) PermissionUtils(io.github.muntashirakon.AppManager.utils.PermissionUtils) SpannableStringBuilder(android.text.SpannableStringBuilder) Path(io.github.muntashirakon.io.Path) UIUtils.getSecondaryText(io.github.muntashirakon.AppManager.utils.UIUtils.getSecondaryText) MenuInflater(android.view.MenuInflater) Menu(android.view.Menu) Settings(android.provider.Settings) SearchCriteria(io.github.muntashirakon.AppManager.logcat.struct.SearchCriteria) DateUtils(io.github.muntashirakon.AppManager.utils.DateUtils) Formatter(android.text.format.Formatter) NetworkPolicyManagerCompat(io.github.muntashirakon.AppManager.servermanager.NetworkPolicyManagerCompat) ViewModelProvider(androidx.lifecycle.ViewModelProvider) ComponentName(android.content.ComponentName) PackageInfoCompat(androidx.core.content.pm.PackageInfoCompat) IOException(java.io.IOException) AnyThread(androidx.annotation.AnyThread) WhatsNewDialogFragment(io.github.muntashirakon.AppManager.apk.whatsnew.WhatsNewDialogFragment) HIDDEN_API_ENFORCEMENT_JUST_WARN(io.github.muntashirakon.AppManager.servermanager.ApplicationInfoCompat.HIDDEN_API_ENFORCEMENT_JUST_WARN) Utils(io.github.muntashirakon.AppManager.utils.Utils) File(java.io.File) AccessibilityMultiplexer(io.github.muntashirakon.AppManager.accessibility.AccessibilityMultiplexer) FastScrollerBuilder(me.zhanghai.android.fastscroll.FastScrollerBuilder) UIUtils.getSmallerText(io.github.muntashirakon.AppManager.utils.UIUtils.getSmallerText) DigestUtils(io.github.muntashirakon.AppManager.utils.DigestUtils) AppDetailsActivity(io.github.muntashirakon.AppManager.details.AppDetailsActivity) IntentUtils(io.github.muntashirakon.AppManager.utils.IntentUtils) ComponentsBlocker(io.github.muntashirakon.AppManager.rules.compontents.ComponentsBlocker) PackageInstallerCompat(io.github.muntashirakon.AppManager.apk.installer.PackageInstallerCompat) PackageUtils(io.github.muntashirakon.AppManager.utils.PackageUtils) FeatureController(io.github.muntashirakon.AppManager.settings.FeatureController) PackageManager(android.content.pm.PackageManager) ProfileManager(io.github.muntashirakon.AppManager.profiles.ProfileManager) Spannable(android.text.Spannable) MaterialAlertDialogBuilder(com.google.android.material.dialog.MaterialAlertDialogBuilder) UIUtils.getTitleText(io.github.muntashirakon.AppManager.utils.UIUtils.getTitleText) ManifestViewerActivity(io.github.muntashirakon.AppManager.details.ManifestViewerActivity) Log(io.github.muntashirakon.AppManager.logs.Log) DrawableRes(androidx.annotation.DrawableRes) ScannerActivity(io.github.muntashirakon.AppManager.scanner.ScannerActivity) HIDDEN_API_ENFORCEMENT_DEFAULT(io.github.muntashirakon.AppManager.servermanager.ApplicationInfoCompat.HIDDEN_API_ENFORCEMENT_DEFAULT) AppManager(io.github.muntashirakon.AppManager.AppManager) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HIDDEN_API_ENFORCEMENT_ENABLED(io.github.muntashirakon.AppManager.servermanager.ApplicationInfoCompat.HIDDEN_API_ENFORCEMENT_ENABLED) GuardedBy(androidx.annotation.GuardedBy) Locale(java.util.Locale) View(android.view.View) Button(android.widget.Button) RecyclerView(androidx.recyclerview.widget.RecyclerView) AppUsageStatsManager(io.github.muntashirakon.AppManager.usage.AppUsageStatsManager) BetterActivityResult(io.github.muntashirakon.AppManager.utils.BetterActivityResult) ApkUtils(io.github.muntashirakon.AppManager.apk.ApkUtils) PermissionUtils.hasDumpPermission(io.github.muntashirakon.AppManager.utils.PermissionUtils.hasDumpPermission) AppDetailsViewModel(io.github.muntashirakon.AppManager.details.AppDetailsViewModel) Utils.openAsFolderInFM(io.github.muntashirakon.AppManager.utils.Utils.openAsFolderInFM) ViewGroup(android.view.ViewGroup) UserPackagePair(io.github.muntashirakon.AppManager.types.UserPackagePair) TERMUX_PERM_RUN_COMMAND(io.github.muntashirakon.AppManager.utils.PermissionUtils.TERMUX_PERM_RUN_COMMAND) List(java.util.List) TextView(android.widget.TextView) TextUtils(com.android.internal.util.TextUtils) SwipeRefreshLayout(io.github.muntashirakon.widget.SwipeRefreshLayout) ProfileMetaManager(io.github.muntashirakon.AppManager.profiles.ProfileMetaManager) ApplicationInfo(android.content.pm.ApplicationInfo) Signature(android.content.pm.Signature) ColorRes(androidx.annotation.ColorRes) AppDetailsItem(io.github.muntashirakon.AppManager.details.struct.AppDetailsItem) Typeface(android.graphics.Typeface) HIDDEN_API_ENFORCEMENT_BLACK(io.github.muntashirakon.AppManager.servermanager.ApplicationInfoCompat.HIDDEN_API_ENFORCEMENT_BLACK) Context(android.content.Context) TextInputEditText(com.google.android.material.textfield.TextInputEditText) AlertDialog(androidx.appcompat.app.AlertDialog) AppDetailsFragment(io.github.muntashirakon.AppManager.details.AppDetailsFragment) Pair(android.util.Pair) Intent(android.content.Intent) MainThread(androidx.annotation.MainThread) HashMap(java.util.HashMap) Ops(io.github.muntashirakon.AppManager.settings.Ops) PackageInfo(android.content.pm.PackageInfo) AtomicReference(java.util.concurrent.atomic.AtomicReference) FmProvider(io.github.muntashirakon.AppManager.fm.FmProvider) MenuItem(android.view.MenuItem) ClipData(android.content.ClipData) ApkFile(io.github.muntashirakon.AppManager.apk.ApkFile) MaterialButton(com.google.android.material.button.MaterialButton) SharedPrefsActivity(io.github.muntashirakon.AppManager.sharedpref.SharedPrefsActivity) ArrayUtils(io.github.muntashirakon.AppManager.utils.ArrayUtils) Build(android.os.Build) ExecutorService(java.util.concurrent.ExecutorService) OutputStream(java.io.OutputStream) LayoutInflater(android.view.LayoutInflater) ActivityCompat(androidx.core.app.ActivityCompat) RulesTypeSelectionDialogFragment(io.github.muntashirakon.AppManager.rules.RulesTypeSelectionDialogFragment) MagiskDenyList(io.github.muntashirakon.AppManager.magisk.MagiskDenyList) ScrollableDialogBuilder(io.github.muntashirakon.dialog.ScrollableDialogBuilder) ResolveInfo(android.content.pm.ResolveInfo) DialogTitleBuilder(io.github.muntashirakon.dialog.DialogTitleBuilder) Bitmap(android.graphics.Bitmap) MagiskProcess(io.github.muntashirakon.AppManager.magisk.MagiskProcess) Collections(java.util.Collections) MimeTypeMap(android.webkit.MimeTypeMap) ActivityResult(androidx.activity.result.ActivityResult) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) ProfileMetaManager(io.github.muntashirakon.AppManager.profiles.ProfileMetaManager) UserPackagePair(io.github.muntashirakon.AppManager.types.UserPackagePair) BackupDialogFragment(io.github.muntashirakon.AppManager.backup.BackupDialogFragment) Bitmap(android.graphics.Bitmap) RulesTypeSelectionDialogFragment(io.github.muntashirakon.AppManager.rules.RulesTypeSelectionDialogFragment) SearchableMultiChoiceDialogBuilder(io.github.muntashirakon.AppManager.types.SearchableMultiChoiceDialogBuilder) Path(io.github.muntashirakon.io.Path) Context(android.content.Context) Bundle(android.os.Bundle) Intent(android.content.Intent) IOException(java.io.IOException) MaterialAlertDialogBuilder(com.google.android.material.dialog.MaterialAlertDialogBuilder) RemoteException(android.os.RemoteException) IOException(java.io.IOException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RemoteException(android.os.RemoteException) Spannable(android.text.Spannable) SpannableStringBuilder(android.text.SpannableStringBuilder)

Aggregations

Manifest (android.Manifest)1 ClipData (android.content.ClipData)1 ClipboardManager (android.content.ClipboardManager)1 ComponentName (android.content.ComponentName)1 Context (android.content.Context)1 Intent (android.content.Intent)1 ApplicationInfo (android.content.pm.ApplicationInfo)1 PackageInfo (android.content.pm.PackageInfo)1 PackageManager (android.content.pm.PackageManager)1 ResolveInfo (android.content.pm.ResolveInfo)1 Signature (android.content.pm.Signature)1 Bitmap (android.graphics.Bitmap)1 Typeface (android.graphics.Typeface)1 Drawable (android.graphics.drawable.Drawable)1 NetworkPolicyManager (android.net.NetworkPolicyManager)1 Uri (android.net.Uri)1 Build (android.os.Build)1 Bundle (android.os.Bundle)1 RemoteException (android.os.RemoteException)1 Settings (android.provider.Settings)1