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;
}
Aggregations