use of io.github.muntashirakon.AppManager.rules.struct.RuleEntry in project AppManager by MuntashirAkon.
the class RestoreOp method restoreExtras.
private synchronized void restoreExtras() throws BackupException {
if (!isInstalled) {
throw new BackupException("Misc restore is requested but the app isn't installed.");
}
PseudoRules rules = new PseudoRules(packageName, userHandle);
// Backward compatibility for restoring permissions
loadMiscRules(rules);
// Apply rules
List<RuleEntry> entries = rules.getAll();
AppOpsService appOpsService = new AppOpsService();
INotificationManager notificationManager = INotificationManager.Stub.asInterface(ProxyBinder.getService(Context.NOTIFICATION_SERVICE));
boolean magiskHideAvailable = MagiskHide.available();
for (RuleEntry entry : entries) {
try {
switch(entry.type) {
case APP_OP:
appOpsService.setMode(Integer.parseInt(entry.name), packageInfo.applicationInfo.uid, packageName, ((AppOpRule) entry).getMode());
break;
case NET_POLICY:
NetworkPolicyManagerCompat.setUidPolicy(packageInfo.applicationInfo.uid, ((NetPolicyRule) entry).getPolicies());
break;
case PERMISSION:
{
PermissionRule permissionRule = (PermissionRule) entry;
Permission permission = permissionRule.getPermission(true);
permission.setAppOpAllowed(permission.getAppOp() != OP_NONE && appOpsService.checkOperation(permission.getAppOp(), packageInfo.applicationInfo.uid, packageName) == AppOpsManager.MODE_ALLOWED);
if (permissionRule.isGranted()) {
PermUtils.grantPermission(packageInfo, permission, appOpsService, true, true);
} else {
PermUtils.revokePermission(packageInfo, permission, appOpsService, true);
}
break;
}
case BATTERY_OPT:
Runner.runCommand(new String[] { "dumpsys", "deviceidle", "whitelist", "+" + packageName });
break;
case MAGISK_HIDE:
{
MagiskHideRule magiskHideRule = (MagiskHideRule) entry;
if (magiskHideAvailable) {
if (magiskHideRule.isHidden()) {
MagiskHide.add(packageName, magiskHideRule.getProcessName());
} else {
MagiskHide.remove(packageName, magiskHideRule.getProcessName());
}
} else {
// Fall-back to Magisk DenyList
if (magiskHideRule.isHidden()) {
MagiskDenyList.add(packageName, magiskHideRule.getProcessName());
} else {
MagiskDenyList.remove(packageName, magiskHideRule.getProcessName());
}
}
break;
}
case MAGISK_DENY_LIST:
{
MagiskDenyListRule magiskDenyListRule = (MagiskDenyListRule) entry;
if (magiskDenyListRule.isDenied()) {
MagiskDenyList.add(packageName, magiskDenyListRule.getProcessName());
} else {
MagiskDenyList.remove(packageName, magiskDenyListRule.getProcessName());
}
break;
}
case NOTIFICATION:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
notificationManager.setNotificationListenerAccessGrantedForUser(new ComponentName(packageName, entry.name), userHandle, true);
}
break;
case URI_GRANT:
UriManager.UriGrant uriGrant = ((UriGrantRule) entry).getUriGrant();
UriManager.UriGrant newUriGrant = new UriManager.UriGrant(uriGrant.sourceUserId, userHandle, uriGrant.userHandle, uriGrant.sourcePkg, uriGrant.targetPkg, uriGrant.uri, uriGrant.prefix, uriGrant.modeFlags, uriGrant.createdTime);
UriManager uriManager = new UriManager();
uriManager.grantUri(newUriGrant);
uriManager.writeGrantedUriPermissions();
break;
case SSAID:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
new SsaidSettings(packageName, packageInfo.applicationInfo.uid).setSsaid(((SsaidRule) entry).getSsaid());
}
break;
}
} catch (Throwable e) {
// There are several reason restoring these things go wrong, especially when
// downgrading from an Android to another. It's better to simply suppress these
// exceptions instead of causing a failure or worse, a crash
Log.e(TAG, e);
}
}
}
use of io.github.muntashirakon.AppManager.rules.struct.RuleEntry in project AppManager by MuntashirAkon.
the class AppDetailsViewModel method removeRules.
@WorkerThread
@GuardedBy("blockerLocker")
public void removeRules(List<? extends RuleEntry> entries, boolean forceApply) {
if (mIsExternalApk)
return;
synchronized (mBlockerLocker) {
waitForBlockerOrExit();
mBlocker.setMutable();
for (RuleEntry entry : entries) {
String componentName = entry.name;
if (mBlocker.hasComponentName(componentName)) {
// Remove from the list
mBlocker.removeComponent(componentName);
}
}
// Apply rules if global blocking enable or already applied
if (forceApply || (Boolean) AppPref.get(AppPref.PrefKey.PREF_GLOBAL_BLOCKING_ENABLED_BOOL) || (mRuleApplicationStatus.getValue() != null && RULE_APPLIED == mRuleApplicationStatus.getValue())) {
mBlocker.applyRules(true);
}
// Set new status
setRuleApplicationStatus();
// Commit changes
mBlocker.commit();
mBlocker.setReadOnly();
// Update UI
reloadComponents();
}
}
use of io.github.muntashirakon.AppManager.rules.struct.RuleEntry in project AppManager by MuntashirAkon.
the class RulesStorageManager method removeEntries.
@GuardedBy("entries")
protected void removeEntries(String name, RuleType type) {
synchronized (mEntries) {
Iterator<RuleEntry> entryIterator = mEntries.iterator();
RuleEntry entry;
while (entryIterator.hasNext()) {
entry = entryIterator.next();
if (entry.name.equals(name) && entry.type.equals(type)) {
entryIterator.remove();
}
}
}
}
use of io.github.muntashirakon.AppManager.rules.struct.RuleEntry in project AppManager by MuntashirAkon.
the class PseudoRulesTest method interUniquenessTest.
@Test
public void interUniquenessTest() {
rules.setComponent(".component", RuleType.ACTIVITY, ComponentRule.COMPONENT_BLOCKED_IFW_DISABLE);
rules.setComponent(".component", RuleType.PROVIDER, ComponentRule.COMPONENT_BLOCKED_IFW_DISABLE);
rules.setComponent(".component", RuleType.SERVICE, ComponentRule.COMPONENT_BLOCKED_IFW_DISABLE);
rules.setComponent(".component", RuleType.RECEIVER, ComponentRule.COMPONENT_BLOCKED_IFW_DISABLE);
rules.setPermission(".component", true, 4);
rules.setNotificationListener(".component", true);
List<RuleEntry> ruleEntries = rules.getAll();
assertEquals(6, ruleEntries.size());
assertEquals(new ComponentRule(PACKAGE_NAME, ".component", RuleType.ACTIVITY, ComponentRule.COMPONENT_BLOCKED_IFW_DISABLE), ruleEntries.get(0));
assertEquals(new ComponentRule(PACKAGE_NAME, ".component", RuleType.PROVIDER, ComponentRule.COMPONENT_BLOCKED_IFW_DISABLE), ruleEntries.get(1));
assertEquals(new ComponentRule(PACKAGE_NAME, ".component", RuleType.SERVICE, ComponentRule.COMPONENT_BLOCKED_IFW_DISABLE), ruleEntries.get(2));
assertEquals(new ComponentRule(PACKAGE_NAME, ".component", RuleType.RECEIVER, ComponentRule.COMPONENT_BLOCKED_IFW_DISABLE), ruleEntries.get(3));
assertEquals(new PermissionRule(PACKAGE_NAME, ".component", true, 4), ruleEntries.get(4));
assertEquals(new NotificationListenerRule(PACKAGE_NAME, ".component", true), ruleEntries.get(5));
}
use of io.github.muntashirakon.AppManager.rules.struct.RuleEntry in project AppManager by MuntashirAkon.
the class AppDetailsViewModel method addRules.
@WorkerThread
@GuardedBy("blockerLocker")
public void addRules(List<? extends RuleEntry> entries, boolean forceApply) {
if (mIsExternalApk)
return;
synchronized (mBlockerLocker) {
waitForBlockerOrExit();
mBlocker.setMutable();
for (RuleEntry entry : entries) {
String componentName = entry.name;
if (mBlocker.hasComponentName(componentName)) {
// Remove from the list
mBlocker.removeComponent(componentName);
}
// Add to the list (again)
mBlocker.addComponent(componentName, entry.type);
}
// Apply rules if global blocking enable or already applied
if (forceApply || (Boolean) AppPref.get(AppPref.PrefKey.PREF_GLOBAL_BLOCKING_ENABLED_BOOL) || (mRuleApplicationStatus.getValue() != null && RULE_APPLIED == mRuleApplicationStatus.getValue())) {
mBlocker.applyRules(true);
}
// Set new status
setRuleApplicationStatus();
// Commit changes
mBlocker.commit();
mBlocker.setReadOnly();
// Update UI
reloadComponents();
}
}
Aggregations