Skip to content

Commit

Permalink
[AppInfo] Attempt to fix displaying wrong info regarding blocked trac…
Browse files Browse the repository at this point in the history
…kers

Signed-off-by: Muntashir Al-Islam <[email protected]>
  • Loading branch information
MuntashirAkon committed Sep 15, 2023
1 parent 2c42e62 commit b5b0041
Showing 1 changed file with 17 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import static io.github.muntashirakon.AppManager.compat.PackageManagerCompat.MATCH_DISABLED_COMPONENTS;
import static io.github.muntashirakon.AppManager.compat.PackageManagerCompat.MATCH_UNINSTALLED_PACKAGES;

import android.annotation.SuppressLint;
import android.app.AppOpsManager;
import android.content.ComponentName;
import android.content.Context;
Expand All @@ -26,6 +25,7 @@
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

Expand Down Expand Up @@ -71,9 +71,6 @@ public final class ComponentsBlocker extends RulesStorageManager {
: "/data/system/ifw";
}

@SuppressLint("StaticFieldLeak")
private static ComponentsBlocker sInstance;

/**
* Get a new or existing IMMUTABLE instance of {@link ComponentsBlocker}. The existing instance
* will only be returned if the existing instance has the same package name as the original.
Expand Down Expand Up @@ -132,23 +129,18 @@ public static ComponentsBlocker getMutableInstance(@NonNull String packageName,
@NonNull
public static ComponentsBlocker getInstance(@NonNull String packageName, int userHandle, boolean reloadFromDisk) {
Objects.requireNonNull(packageName);
if (sInstance == null) {
sInstance = new ComponentsBlocker(packageName, userHandle);
} else if (reloadFromDisk || !Objects.equals(sInstance.packageName, packageName)) {
sInstance.close();
sInstance = new ComponentsBlocker(packageName, userHandle);
}
ComponentsBlocker blocker = new ComponentsBlocker(packageName, userHandle);
if (reloadFromDisk && SelfPermissions.canBlockByIFW()) {
sInstance.retrieveDisabledComponents();
sInstance.invalidateComponents();
blocker.retrieveDisabledComponents();
blocker.invalidateComponents();
}
sInstance.readOnly = true;
return sInstance;
blocker.readOnly = true;
return blocker;
}

@NonNull
private final AtomicExtendedFile mRulesFile;
@NonNull
@Nullable
private Set<String> mComponents;
@Nullable
private PackageInfo mPackageInfo;
Expand All @@ -165,14 +157,14 @@ private ComponentsBlocker(@NonNull String packageName, int userHandle) {
} catch (Throwable e) {
Log.e(TAG, e.getMessage(), e);
}
mComponents = PackageUtils.collectComponentClassNames(mPackageInfo).keySet();
mComponents = mPackageInfo != null ? PackageUtils.collectComponentClassNames(mPackageInfo).keySet() : null;
}

/**
* Reload package components
*/
public void reloadComponents() {
mComponents = PackageUtils.collectComponentClassNames(mPackageInfo).keySet();
mComponents = mPackageInfo != null ? PackageUtils.collectComponentClassNames(mPackageInfo).keySet() : null;
}

/**
Expand Down Expand Up @@ -524,7 +516,10 @@ public boolean applyAppOpsAndPerms() {
* Check if the components are up-to-date and remove the ones that are not up-to-date.
*/
private void validateComponents() {
// Validate components
if (mComponents == null) {
// No validation required
return;
}
List<ComponentRule> allEntries = getAllComponents();
for (ComponentRule entry : allEntries) {
if (!mComponents.contains(entry.name)) {
Expand All @@ -540,10 +535,11 @@ private void validateComponents() {
*/
public int invalidateComponents() {
int invalidated = 0;
boolean canCheckExistence = mComponents != null;
List<ComponentRule> allEntries = getAllComponents();
for (ComponentRule entry : allEntries) {
// First check if it actually exists
if (!mComponents.contains(entry.name)) {
if (canCheckExistence && !mComponents.contains(entry.name)) {
removeEntry(entry);
++invalidated;
continue;
Expand Down Expand Up @@ -590,9 +586,9 @@ private void retrieveDisabledComponents() {
}
try (InputStream rulesStream = mRulesFile.openRead()) {
HashMap<String, RuleType> components = ComponentUtils.readIFWRules(rulesStream, packageName);
for (String componentName : components.keySet()) {
for (Map.Entry<String, RuleType> component : components.entrySet()) {
// Override existing rule for the component if it exists
setComponent(componentName, components.get(componentName), ComponentRule.COMPONENT_BLOCKED_IFW_DISABLE);
setComponent(component.getKey(), component.getValue(), ComponentRule.COMPONENT_BLOCKED_IFW_DISABLE);
}
Log.d(TAG, "Retrieved components for package %s", packageName);
} catch (IOException | RemoteException ignored) {
Expand Down

0 comments on commit b5b0041

Please sign in to comment.