Skip to content

Commit

Permalink
[Finder] Add new filter: permissions
Browse files Browse the repository at this point in the history
Signed-off-by: Muntashir Al-Islam <muntashirakon@riseup.net>
  • Loading branch information
MuntashirAkon committed Nov 19, 2024
1 parent a2f2d3a commit b04fa22
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.IOException;
import java.security.cert.CertificateEncodingException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -72,7 +73,7 @@ public class FilterableAppInfo {
private String[] mSignatureSha256Checksums;
private Map<ComponentInfo, Integer> mAllComponents;
private Map<ComponentInfo, Integer> mTrackerComponents;
private String[] mUsedPermissions;
private List<String> mUsedPermissions;
private Backup[] mBackups;
private List<AppOpsManagerCompat.OpEntry> mAppOpEntries;
@Nullable
Expand Down Expand Up @@ -211,7 +212,7 @@ public Map<ComponentInfo, Integer> getAllComponents() {
}

@NonNull
public String[] getAllPermissions() {
public List<String> getAllPermissions() {
if (mUsedPermissions == null) {
Set<String> usedPermissions = new HashSet<>();
if (mPackageInfo.requestedPermissions != null) {
Expand Down Expand Up @@ -243,7 +244,7 @@ public String[] getAllPermissions() {
}
}
}
mUsedPermissions = usedPermissions.toArray(new String[0]);
mUsedPermissions = new ArrayList<>(usedPermissions);
}
return mUsedPermissions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ public static class TestResult {
private List<Backup> mMatchedBackups;
private Map<ComponentInfo, Integer> mMatchedComponents;
private Map<ComponentInfo, Integer> mMatchedTrackers;
private List<String> mMatchedPermissions;
private List<String> mMatchedSubjectLines;

public TestResult setMatched(boolean matched) {
Expand Down Expand Up @@ -209,6 +210,15 @@ public Map<ComponentInfo, Integer> getMatchedTrackers() {
return mMatchedTrackers;
}

public TestResult setMatchedPermissions(List<String> matchedPermissions) {
mMatchedPermissions = matchedPermissions;
return this;
}

public List<String> getMatchedPermissions() {
return mMatchedPermissions;
}

public TestResult setMatchedSubjectLines(List<String> matchedSubjectLines) {
mMatchedSubjectLines = matchedSubjectLines;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static FilterOption create(@NonNull String filterName) {
case "installer": return new InstallerOption();
case "last_update": return new LastUpdateOption();
case "min_sdk": return new MinSdkOption();
// case "permission": return new PermissionOption();
case "permissions": return new PermissionsOption();
case "pkg_name": return new PackageNameOption();
case "running_apps": return new RunningAppsOption();
case "screen_time": return new ScreenTimeOption();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// SPDX-License-Identifier: GPL-3.0-or-later

package io.github.muntashirakon.AppManager.filters.options;

import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import io.github.muntashirakon.AppManager.filters.FilterableAppInfo;

public class PermissionsOption extends FilterOption {
private final Map<String, Integer> mKeysWithType = new LinkedHashMap<String, Integer>() {{
put(KEY_ALL, TYPE_NONE);
put("eq", TYPE_STR_SINGLE);
put("contains", TYPE_STR_SINGLE);
put("starts_with", TYPE_STR_SINGLE);
put("ends_with", TYPE_STR_SINGLE);
put("regex", TYPE_REGEX);
// TODO: 11/19/24 Add more curated options such as permission flags, private flags, grant
}};

public PermissionsOption() {
super("permissions");
}

@NonNull
@Override
public Map<String, Integer> getKeysWithType() {
return mKeysWithType;
}

@NonNull
@Override
public TestResult test(@NonNull FilterableAppInfo info, @NonNull TestResult result) {
List<String> permissions = result.getMatchedPermissions() != null
? result.getMatchedPermissions()
: info.getAllPermissions();
switch (key) {
default:
return result.setMatched(true).setMatchedPermissions(permissions);
case "eq": {
List<String> filteredPermissions = new ArrayList<>();
for (String permission : permissions) {
if (permission.equals(value)) {
filteredPermissions.add(permission);
}
}
return result.setMatched(!filteredPermissions.isEmpty())
.setMatchedPermissions(filteredPermissions);
}
case "contains": {
Objects.requireNonNull(value);
List<String> filteredPermissions = new ArrayList<>();
for (String permission : permissions) {
if (permission.contains(value)) {
filteredPermissions.add(permission);
}
}
return result.setMatched(!filteredPermissions.isEmpty())
.setMatchedPermissions(filteredPermissions);
}
case "starts_with": {
Objects.requireNonNull(value);
List<String> filteredPermissions = new ArrayList<>();
for (String permission : permissions) {
if (permission.startsWith(value)) {
filteredPermissions.add(permission);
}
}
return result.setMatched(!filteredPermissions.isEmpty())
.setMatchedPermissions(filteredPermissions);
}
case "ends_with": {
Objects.requireNonNull(value);
List<String> filteredPermissions = new ArrayList<>();
for (String permission : permissions) {
if (permission.endsWith(value)) {
filteredPermissions.add(permission);
}
}
return result.setMatched(!filteredPermissions.isEmpty())
.setMatchedPermissions(filteredPermissions);
}
case "regex": {
Objects.requireNonNull(value);
List<String> filteredPermissions = new ArrayList<>();
for (String permission : permissions) {
if (regexValue.matcher(permission).matches()) {
filteredPermissions.add(permission);
}
}
return result.setMatched(!filteredPermissions.isEmpty())
.setMatchedPermissions(filteredPermissions);
}
}
}
}

0 comments on commit b04fa22

Please sign in to comment.