Skip to content

Commit

Permalink
gosps: share common code between GosPackageState and GosPackageStatePm
Browse files Browse the repository at this point in the history
  • Loading branch information
muhomorr authored and thestinger committed Mar 24, 2023
1 parent 623ac1f commit ada89ee
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 64 deletions.
4 changes: 1 addition & 3 deletions core/api/system-current.txt
Expand Up @@ -3238,7 +3238,7 @@ package android.content.pm {
method public boolean hasDerivedFlag(int);
method public boolean hasDerivedFlags(int);
method public boolean hasFlag(int);
method public boolean hasFlags(int);
method public final boolean hasFlags(int);
method public void writeToParcel(@NonNull android.os.Parcel, int);
field @NonNull public static final android.os.Parcelable.Creator<android.content.pm.GosPackageState> CREATOR;
field public static final int DFLAGS_SET = 1; // 0x1
Expand All @@ -3259,8 +3259,6 @@ package android.content.pm {
field public static final int FLAG_ENABLE_COMPAT_VA_39_BIT = 8; // 0x8
field public static final int FLAG_STORAGE_SCOPES_ENABLED = 1; // 0x1
field public final int derivedFlags;
field public final int flags;
field @Nullable public final byte[] storageScopes;
}

public static class GosPackageState.Editor {
Expand Down
35 changes: 10 additions & 25 deletions core/java/android/content/pm/GosPackageState.java
Expand Up @@ -38,10 +38,7 @@
* @hide
*/
@SystemApi
public final class GosPackageState implements Parcelable {
public final int flags;
@Nullable
public final byte[] storageScopes;
public final class GosPackageState extends GosPackageStateBase implements Parcelable {
public final int derivedFlags; // derived from persistent state, but not persisted themselves

// packageName and userId are stored here for convenience, they don't get serialized
Expand Down Expand Up @@ -73,8 +70,7 @@ public final class GosPackageState implements Parcelable {

/** @hide */
public GosPackageState(int flags, @Nullable byte[] storageScopes, int derivedFlags) {
this.flags = flags;
this.storageScopes = storageScopes;
super(flags, storageScopes);
this.derivedFlags = derivedFlags;
}

Expand Down Expand Up @@ -152,10 +148,6 @@ public boolean hasFlag(int flag) {
return (flags & flag) != 0;
}

public boolean hasFlags(int flags) {
return (this.flags & flags) == flags;
}

public boolean hasDerivedFlag(int flag) {
return (derivedFlags & flag) != 0;
}
Expand Down Expand Up @@ -295,28 +287,21 @@ public static class Editor {
private boolean killUidAfterApply;

/**
* Don't call directly, use GosPackageState#edit or GosPackageStatePm#edit
* Don't call directly, use GosPackageState#edit or GosPackageStatePm#getEditor
*
* @hide
* */
public Editor(String packageName, int userId, int flags, byte[] storageScopes) {
public Editor(String packageName, int userId) {
this.packageName = packageName;
this.userId = userId;
this.flags = flags;
this.storageScopes = storageScopes;
}

/**
* Don't call directly, use GosPackageState#edit or GosPackageStatePm#edit
*
* @hide
* */
public Editor(String packageName, int userId) {
this(packageName, userId, 0, null);
}

Editor(GosPackageState s, String packageName, int userId) {
this(packageName, userId, s.flags, s.storageScopes);
/** @hide */
public Editor(GosPackageStateBase s, String packageName, int userId) {
this.packageName = packageName;
this.userId = userId;
this.flags = s.flags;
this.storageScopes = s.storageScopes;
}

@NonNull
Expand Down
48 changes: 48 additions & 0 deletions core/java/android/content/pm/GosPackageStateBase.java
@@ -0,0 +1,48 @@
package android.content.pm;

import android.annotation.Nullable;

import java.util.Arrays;

/**
* Common code between GosPackageState and GosPackageStatePm.
*
* @hide
*/
public abstract class GosPackageStateBase {
public final int flags;
@Nullable
public final byte[] storageScopes;

protected GosPackageStateBase(int flags, @Nullable byte[] storageScopes) {
this.flags = flags;
this.storageScopes = storageScopes;
}

public final boolean hasFlags(int flags) {
return (this.flags & flags) == flags;
}

@Override
public final int hashCode() {
return 31 * flags + Arrays.hashCode(storageScopes);
}

@Override
public final boolean equals(Object obj) {
if (!(obj instanceof GosPackageStateBase)) {
return false;
}

GosPackageStateBase o = (GosPackageStateBase) obj;
if (flags != o.flags) {
return false;
}

if (!Arrays.equals(storageScopes, o.storageScopes)) {
return false;
}

return true;
}
}
Expand Up @@ -18,12 +18,11 @@

import android.annotation.Nullable;
import android.content.pm.GosPackageState;
import android.content.pm.GosPackageStateBase;

import com.android.server.pm.Computer;
import com.android.server.pm.PackageManagerService;

import java.util.Arrays;

/**
* GrapheneOS-specific package state, stored in PackageUserState (per-user, removed during uninstallation).
*
Expand All @@ -48,21 +47,18 @@
*
* @hide
*/
public final class GosPackageStatePm {
public final int flags;
@Nullable
public final byte[] storageScopes;
public final class GosPackageStatePm extends GosPackageStateBase {

public GosPackageStatePm(int flags, @Nullable byte[] storageScopes) {
this.flags = flags;
this.storageScopes = storageScopes;
super(flags, storageScopes);
}

@Nullable
public static GosPackageStatePm get(PackageManagerService pm, String packageName, int userId) {
return get(pm.snapshotComputer(), packageName, userId);
}

@Nullable
public static GosPackageStatePm get(Computer snapshot, String packageName, int userId) {
PackageStateInternal psi = snapshot.getPackageStates().get(packageName);
if (psi == null) {
Expand All @@ -72,6 +68,7 @@ public static GosPackageStatePm get(Computer snapshot, String packageName, int u
return get(snapshot, psi, userId);
}

@Nullable
public static GosPackageStatePm get(Computer snapshot, PackageStateInternal psi, int userId) {
GosPackageStatePm res = psi.getUserStateOrDefault(userId).getGosPackageState();
if (res != null) {
Expand Down Expand Up @@ -114,36 +111,9 @@ public static GosPackageState.Editor getEditor(PackageManagerService pm, String
var ps = get(pm, packageName, userId);

if (ps != null) {
return new GosPackageState.Editor(packageName, userId, ps.flags, ps.storageScopes);
return new GosPackageState.Editor(ps, packageName, userId);
}

return new GosPackageState.Editor(packageName, userId);
}

public boolean hasFlags(int flags) {
return (this.flags & flags) == flags;
}

@Override
public int hashCode() {
return 31 * flags + Arrays.hashCode(storageScopes);
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof GosPackageStatePm)) {
return false;
}

GosPackageStatePm o = (GosPackageStatePm) obj;
if (flags != o.flags) {
return false;
}

if (!Arrays.equals(storageScopes, o.storageScopes)) {
return false;
}

return true;
}
}

0 comments on commit ada89ee

Please sign in to comment.