Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust to AssetManager#nativeSetConfiguration signature change in SDKs > T. #8037

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import static android.os.Build.VERSION_CODES.Q;
import static android.os.Build.VERSION_CODES.R;
import static android.os.Build.VERSION_CODES.S;
import static android.os.Build.VERSION_CODES.TIRAMISU;
import static java.util.stream.Collectors.toSet;
import static org.robolectric.shadow.api.Shadow.invokeConstructor;
import static org.robolectric.util.reflector.Reflector.reflector;

import android.annotation.NonNull;
import android.annotation.Nullable;
Expand Down Expand Up @@ -59,6 +61,8 @@
import org.robolectric.shadow.api.Shadow;
import org.robolectric.util.ReflectionHelpers;
import org.robolectric.util.ReflectionHelpers.ClassParameter;
import org.robolectric.util.reflector.Accessor;
import org.robolectric.util.reflector.ForType;

/** Shadow for {@link AppOpsManager}. */
@Implements(value = AppOpsManager.class, minSdk = KITKAT, looseSignatures = true)
Expand Down Expand Up @@ -146,14 +150,22 @@ public void setMode(int op, int uid, String packageName, int mode) {
for (Key key : entry.getValue()) {
if (op == key.getOpCode()
&& (key.getPackageName() == null || key.getPackageName().equals(packageName))) {
String[] sOpToString =
ReflectionHelpers.getStaticField(AppOpsManager.class, "sOpToString");
entry.getKey().onOpChanged(sOpToString[op], packageName);
entry.getKey().onOpChanged(getOpString(op), packageName);
}
}
}
}

protected String getOpString(int opCode) {
if (RuntimeEnvironment.getApiLevel() <= TIRAMISU) {
String[] sOpToString = ReflectionHelpers.getStaticField(AppOpsManager.class, "sOpToString");
return sOpToString[opCode];
} else {
Object[] sAppOpInfos = ReflectionHelpers.getStaticField(AppOpsManager.class, "sAppOpInfos");
return reflector(AppOpInfoReflector.class, sAppOpInfos[opCode]).getName();
}
}

/**
* Returns app op details for all packages for which one of {@link #setMode} methods was used to
* set the value of one of the given app ops (it does return those set to 'default' mode, while
Expand Down Expand Up @@ -633,4 +645,10 @@ public static void reset() {
ReflectionHelpers.setStaticField(AppOpsManager.class, "sOnOpNotedCallback", null);
}
}

@ForType(className = "android.app.AppOpInfo")
interface AppOpInfoReflector {
@Accessor("name")
String getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static android.os.Build.VERSION_CODES.Q;
import static android.os.Build.VERSION_CODES.R;
import static android.os.Build.VERSION_CODES.S;
import static android.os.Build.VERSION_CODES.TIRAMISU;
import static org.robolectric.res.android.ApkAssetsCookie.K_INVALID_COOKIE;
import static org.robolectric.res.android.ApkAssetsCookie.kInvalidCookie;
import static org.robolectric.res.android.Asset.SEEK_CUR;
Expand Down Expand Up @@ -584,7 +585,7 @@ protected static void nativeSetApkAssets(
// jint smallest_screen_width_dp, jint screen_width_dp,
// jint screen_height_dp, jint screen_layout, jint ui_mode,
// jint color_mode, jint major_version) {
@Implementation(minSdk = P)
@Implementation(minSdk = P, maxSdk = TIRAMISU)
protected static void nativeSetConfiguration(
long ptr,
int mcc,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static android.os.Build.VERSION_CODES.P;
import static android.os.Build.VERSION_CODES.Q;
import static android.os.Build.VERSION_CODES.R;
import static android.os.Build.VERSION_CODES.TIRAMISU;
import static org.robolectric.res.android.ApkAssetsCookie.K_INVALID_COOKIE;
import static org.robolectric.res.android.ApkAssetsCookie.kInvalidCookie;
import static org.robolectric.res.android.Asset.SEEK_CUR;
Expand Down Expand Up @@ -578,7 +579,7 @@ protected static void nativeSetApkAssets(
// jint smallest_screen_width_dp, jint screen_width_dp,
// jint screen_height_dp, jint screen_layout, jint ui_mode,
// jint color_mode, jint major_version) {
@Implementation(minSdk = P)
@Implementation(minSdk = P, maxSdk = TIRAMISU)
protected static void nativeSetConfiguration(
long ptr,
int mcc,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static android.os.Build.VERSION_CODES.O;
import static android.os.Build.VERSION_CODES.O_MR1;
import static android.os.Build.VERSION_CODES.P;
import static android.os.Build.VERSION_CODES.TIRAMISU;
import static org.robolectric.RuntimeEnvironment.castNativePtr;
import static org.robolectric.shadow.api.Shadow.invokeConstructor;
import static org.robolectric.util.ReflectionHelpers.ClassParameter.from;
Expand Down Expand Up @@ -697,7 +698,7 @@ public final void setConfiguration(
}

@HiddenApi
@Implementation(minSdk = VERSION_CODES.O)
@Implementation(minSdk = VERSION_CODES.O, maxSdk = TIRAMISU)
public void setConfiguration(
int mcc,
int mnc,
Expand Down