Search in sources :

Example 1 with RuleEntry

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);
        }
    }
}
Also used : UriGrantRule(io.github.muntashirakon.AppManager.rules.struct.UriGrantRule) PermissionRule(io.github.muntashirakon.AppManager.rules.struct.PermissionRule) MagiskDenyListRule(io.github.muntashirakon.AppManager.rules.struct.MagiskDenyListRule) PseudoRules(io.github.muntashirakon.AppManager.rules.PseudoRules) AppOpsService(io.github.muntashirakon.AppManager.appops.AppOpsService) MagiskHideRule(io.github.muntashirakon.AppManager.rules.struct.MagiskHideRule) INotificationManager(android.app.INotificationManager) SsaidSettings(io.github.muntashirakon.AppManager.ssaid.SsaidSettings) Permission(io.github.muntashirakon.AppManager.permission.Permission) UriManager(io.github.muntashirakon.AppManager.uri.UriManager) RuleEntry(io.github.muntashirakon.AppManager.rules.struct.RuleEntry) ComponentName(android.content.ComponentName)

Example 2 with RuleEntry

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();
    }
}
Also used : RuleEntry(io.github.muntashirakon.AppManager.rules.struct.RuleEntry) WorkerThread(androidx.annotation.WorkerThread) GuardedBy(androidx.annotation.GuardedBy)

Example 3 with RuleEntry

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();
            }
        }
    }
}
Also used : RuleEntry(io.github.muntashirakon.AppManager.rules.struct.RuleEntry) GuardedBy(androidx.annotation.GuardedBy)

Example 4 with RuleEntry

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));
}
Also used : NotificationListenerRule(io.github.muntashirakon.AppManager.rules.struct.NotificationListenerRule) PermissionRule(io.github.muntashirakon.AppManager.rules.struct.PermissionRule) RuleEntry(io.github.muntashirakon.AppManager.rules.struct.RuleEntry) ComponentRule(io.github.muntashirakon.AppManager.rules.struct.ComponentRule) Test(org.junit.Test)

Example 5 with RuleEntry

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();
    }
}
Also used : RuleEntry(io.github.muntashirakon.AppManager.rules.struct.RuleEntry) WorkerThread(androidx.annotation.WorkerThread) GuardedBy(androidx.annotation.GuardedBy)

Aggregations

RuleEntry (io.github.muntashirakon.AppManager.rules.struct.RuleEntry)6 GuardedBy (androidx.annotation.GuardedBy)3 PermissionRule (io.github.muntashirakon.AppManager.rules.struct.PermissionRule)3 WorkerThread (androidx.annotation.WorkerThread)2 ComponentRule (io.github.muntashirakon.AppManager.rules.struct.ComponentRule)2 MagiskDenyListRule (io.github.muntashirakon.AppManager.rules.struct.MagiskDenyListRule)2 MagiskHideRule (io.github.muntashirakon.AppManager.rules.struct.MagiskHideRule)2 NotificationListenerRule (io.github.muntashirakon.AppManager.rules.struct.NotificationListenerRule)2 Test (org.junit.Test)2 INotificationManager (android.app.INotificationManager)1 ComponentName (android.content.ComponentName)1 AppOpsService (io.github.muntashirakon.AppManager.appops.AppOpsService)1 Permission (io.github.muntashirakon.AppManager.permission.Permission)1 PseudoRules (io.github.muntashirakon.AppManager.rules.PseudoRules)1 BatteryOptimizationRule (io.github.muntashirakon.AppManager.rules.struct.BatteryOptimizationRule)1 NetPolicyRule (io.github.muntashirakon.AppManager.rules.struct.NetPolicyRule)1 SsaidRule (io.github.muntashirakon.AppManager.rules.struct.SsaidRule)1 UriGrantRule (io.github.muntashirakon.AppManager.rules.struct.UriGrantRule)1 SsaidSettings (io.github.muntashirakon.AppManager.ssaid.SsaidSettings)1 UriManager (io.github.muntashirakon.AppManager.uri.UriManager)1