Skip to content

Commit

Permalink
[AppOps] Fix problem with extra modes on some ROMs
Browse files Browse the repository at this point in the history
MODE_ALLOW and MODE_IGNORED should be common in all Android ROMs. Hence, AppOpsService#setMode(int, int, String, int) still uses op constants
  • Loading branch information
MuntashirAkon committed Jul 8, 2020
1 parent ba1690f commit ef6cb29
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -297,13 +297,13 @@ public View getView(int position, View convertView, ViewGroup parent) {
}
}).start());
new Thread(() -> {
int mode = AppOpsManager.MODE_DEFAULT;
String mode = AppOpsManager.modeToName(AppOpsManager.MODE_DEFAULT);
try {
mode = new AppOpsService(mActivity).checkOperation(AppOpsManager.OP_RUN_IN_BACKGROUND, applicationInfo.uid, applicationInfo.packageName);
} catch (Exception ignore) {}
int finalMode = mode;
String finalMode = mode;
mActivity.runOnUiThread(() -> {
if (finalMode != AppOpsManager.MODE_IGNORED) {
if (!finalMode.equals(AppOpsManager.modeToName(AppOpsManager.MODE_IGNORED))) {
holder.disableBackgroundRunBtn.setVisibility(View.VISIBLE);
holder.disableBackgroundRunBtn.setOnClickListener(v -> new Thread(() -> {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1913,14 +1913,14 @@ public PackageOps[] newArray(int size) {
public static final class OpEntry implements Parcelable {
private final int mOp;
private final Boolean mRunning;
private final @Mode int mMode;
private final @NonNull String mMode;
private final long mAccessTime;
private final long mRejectTime;
private final long mDuration;
private final @Nullable String mProxyUid;
private final @Nullable String mProxyPackageName;

public OpEntry(int op, boolean running, @Mode int mode,
public OpEntry(int op, boolean running, @NonNull String mode,
long accessTime, long rejectTime,
long duration, @Nullable String proxyUid,
@Nullable String proxyPackageName) {
Expand All @@ -1934,7 +1934,7 @@ public OpEntry(int op, boolean running, @Mode int mode,
mProxyPackageName = proxyPackageName;
}

public OpEntry(int op, @Mode int mode) {
public OpEntry(int op, @NonNull String mode) {
mOp = op;
mMode = mode;
mRunning = false;
Expand All @@ -1958,10 +1958,9 @@ String getOpStr() {
}

/**
* @return this entry's current mode, such as {@link #MODE_ALLOWED}.
* @return this entry's current mode string value, such as allow and ignore.
*/
public @Mode
int getMode() {
public String getMode() {
return mMode;
}

Expand Down Expand Up @@ -2015,7 +2014,7 @@ public String toString() {
return "OpEntry{" +
"mOp=" + opToName(mOp) +
", mRunning=" + mRunning +
", mMode=" + modeToName(mMode) +
", mMode=" + mMode +
", mAccessTime=" + mAccessTime +
", mRejectTime=" + mRejectTime +
", mDuration=" + mDuration +
Expand All @@ -2030,7 +2029,7 @@ public int describeContents() {
@Override
public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeInt(mOp);
dest.writeInt(mMode);
dest.writeString(mMode);
dest.writeValue(mRunning);
dest.writeLong(mAccessTime);
dest.writeLong(mRejectTime);
Expand All @@ -2041,7 +2040,7 @@ public void writeToParcel(@NonNull Parcel dest, int flags) {

OpEntry(@NonNull Parcel source) {
mOp = source.readInt();
mMode = source.readInt();
mMode = Objects.requireNonNull(source.readString());
mRunning = (Boolean) source.readValue(getClass().getClassLoader());
mAccessTime = source.readLong();
mRejectTime = source.readLong();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ public AppOpsService(Context context) {
* an invalid operation name or mode name or there's an error parsing the output
*/
@Override
@AppOpsManager.Mode
public int checkOperation(int op, int uid, @Nullable String packageName)
public String checkOperation(int op, int uid, @Nullable String packageName)
throws Exception {
String opStr = AppOpsManager.opToName(op);
if (packageName != null)
Expand All @@ -68,7 +67,7 @@ else if (uid >= 0)
String line2 = output.get(1);
if (line2.startsWith("Default mode:")) {
opModeOut = line2.substring(DEFAULT_MODE_SKIP);
return strModeToMode(opModeOut);
return opModeOut;
} else return parseOpName(line2).getMode();
}
} catch (IndexOutOfBoundsException e) {
Expand Down Expand Up @@ -105,7 +104,9 @@ public List<AppOpsManager.PackageOps> getOpsForPackage(int uid, String packageNa
}
List<AppOpsManager.OpEntry> opEntries = new ArrayList<>();
for(String line: lines) {
opEntries.add(parseOpName(line));
try {
opEntries.add(parseOpName(line));
} catch (Exception ignored) {}
}
AppOpsManager.PackageOps packageOps = new AppOpsManager.PackageOps(packageName, uid, opEntries);
packageOpsList.add(packageOps);
Expand Down Expand Up @@ -188,8 +189,8 @@ private static AppOpsManager.OpEntry parseOpName(@NonNull String line) throws Ex
if (matcher.find()) {
if (matcher.group(1) == null && matcher.group(2) == null)
throw new Exception("Op name or mode cannot be empty");
int op = strOpToOp(matcher.group(1));
@AppOpsManager.Mode int mode = strModeToMode(matcher.group(2));
int op = strOpToOp(matcher.group(1)); // FIXME: make it string as there could be many custom mode
String mode = matcher.group(2);
boolean running = matcher.group(15) != null;
long accessTime = getTime(matcher, 3);
long rejectTime = getTime(matcher, 9);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import java.util.List;

interface IAppOpsService {
@AppOpsManager.Mode
int checkOperation(int op, int uid, String packageName) throws Exception;
String checkOperation(int op, int uid, String packageName) throws Exception;
List<AppOpsManager.PackageOps> getOpsForPackage(int uid, String packageName, int[] ops) throws Exception;
void setMode(int op, int uid, String packageName, int mode) throws Exception;
void resetAllModes(int reqUserId, String reqPackageName) throws Exception;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1134,7 +1134,7 @@ private View getAppOpsView(ViewGroup viewGroup, View convertView, int index) {
}
// Op Switch
viewHolder.toggleSwitch.setVisibility(View.VISIBLE);
if (opEntry.getMode() == AppOpsManager.MODE_ALLOWED) {
if (opEntry.getMode().equals(AppOpsManager.modeToName(AppOpsManager.MODE_ALLOWED))) {
// op granted
viewHolder.toggleSwitch.setChecked(true);
} else {
Expand All @@ -1150,7 +1150,7 @@ private View getAppOpsView(ViewGroup viewGroup, View convertView, int index) {
}
// TODO: Use AppOpsManager.getOpsForPackage() instead
AppOpsManager.OpEntry opEntry1 = new AppOpsManager.OpEntry(opEntry.getOp(),
opEntry.isRunning(), AppOpsManager.MODE_ALLOWED, opEntry.getTime(),
opEntry.isRunning(), AppOpsManager.modeToName(AppOpsManager.MODE_ALLOWED), opEntry.getTime(),
opEntry.getRejectTime(), opEntry.getDuration(),
opEntry.getProxyUid(), opEntry.getProxyPackageName());
AppDetailsItem appDetailsItem = new AppDetailsItem(opEntry1);
Expand All @@ -1171,7 +1171,7 @@ private View getAppOpsView(ViewGroup viewGroup, View convertView, int index) {
}
// TODO: Use AppOpsManager.getOpsForPackage() instead
AppOpsManager.OpEntry opEntry1 = new AppOpsManager.OpEntry(opEntry.getOp(),
opEntry.isRunning(), AppOpsManager.MODE_IGNORED, opEntry.getTime(),
opEntry.isRunning(), AppOpsManager.modeToName(AppOpsManager.MODE_IGNORED), opEntry.getTime(),
opEntry.getRejectTime(), opEntry.getDuration(),
opEntry.getProxyUid(), opEntry.getProxyPackageName());
AppDetailsItem appDetailsItem = new AppDetailsItem(opEntry1);
Expand Down
4 changes: 2 additions & 2 deletions app/version.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#Wed Jul 08 12:52:09 BDT 2020
#Wed Jul 08 13:20:42 BDT 2020
VERSION_NAME=2.5.7
VERSION_CODE=261
VERSION_CODE=263

0 comments on commit ef6cb29

Please sign in to comment.