Skip to content

Commit

Permalink
Future proof ShadowCompanionDeviceManager for AssociationInfo change.
Browse files Browse the repository at this point in the history
Use AssociationInfoBuilder in ShadowCompanionDeviceManager to prepare
for future AssociationInfo constructor signature change.

PiperOrigin-RevId: 515782617
  • Loading branch information
brettchabot authored and Copybara-Service committed Mar 15, 2023
1 parent 607d811 commit 6d07c97
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 18 deletions.
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();
}
}
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
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
Expand Up @@ -7,7 +7,6 @@
import android.companion.AssociationRequest;
import android.companion.CompanionDeviceManager;
import android.content.ComponentName;
import android.net.MacAddress;
import android.os.Build.VERSION_CODES;
import android.os.Handler;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -129,17 +128,18 @@ protected List<AssociationInfo> getMyAssociations() {

/** Convert {@link RoboAssociationInfo} to actual {@link AssociationInfo}. */
private AssociationInfo createAssociationInfo(RoboAssociationInfo info) {
return new AssociationInfo(
info.id(),
info.userId(),
info.packageName(),
MacAddress.fromString(info.deviceMacAddress()),
info.displayName(),
info.deviceProfile(),
info.selfManaged(),
info.notifyOnDeviceNearby(),
info.timeApprovedMs(),
info.lastTimeConnectedMs());
return AssociationInfoBuilder.newBuilder()
.setId(info.id())
.setUserId(info.userId())
.setPackageName(info.packageName())
.setDeviceMacAddress(info.deviceMacAddress())
.setDisplayName(info.displayName())
.setDeviceProfile(info.deviceProfile())
.setSelfManaged(info.selfManaged())
.setNotifyOnDeviceNearby(info.notifyOnDeviceNearby())
.setApprovedMs(info.timeApprovedMs())
.setLastTimeConnectedMs(info.lastTimeConnectedMs())
.build();
}

private RoboAssociationInfo createShadowAssociationInfo(AssociationInfo info) {
Expand Down
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

0 comments on commit 6d07c97

Please sign in to comment.