| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.service.gatekeeper; |
| 18 |
|
| 19 |
import android.os.Parcel; |
| 20 |
import android.os.Parcelable; |
| 21 |
|
| 22 |
import com.android.internal.annotations.VisibleForTesting; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public final class GateKeeperResponse implements Parcelable { |
| 29 |
|
| 30 |
public static final int RESPONSE_ERROR = -1; |
| 31 |
public static final int RESPONSE_OK = 0; |
| 32 |
public static final int RESPONSE_RETRY = 1; |
| 33 |
|
| 34 |
public static final GateKeeperResponse ERROR = createGenericResponse(RESPONSE_ERROR); |
| 35 |
|
| 36 |
private final int mResponseCode; |
| 37 |
|
| 38 |
private int mTimeout; |
| 39 |
private byte[] mPayload; |
| 40 |
private boolean mShouldReEnroll; |
| 41 |
|
| 42 |
|
| 43 |
private GateKeeperResponse(int responseCode) { |
| 44 |
mResponseCode = responseCode; |
| 45 |
} |
| 46 |
|
| 47 |
@VisibleForTesting |
| 48 |
public static GateKeeperResponse createGenericResponse(int responseCode) { |
| 49 |
return new GateKeeperResponse(responseCode); |
| 50 |
} |
| 51 |
|
| 52 |
private static GateKeeperResponse createRetryResponse(int timeout) { |
| 53 |
GateKeeperResponse response = new GateKeeperResponse(RESPONSE_RETRY); |
| 54 |
response.mTimeout = timeout; |
| 55 |
return response; |
| 56 |
} |
| 57 |
|
| 58 |
@VisibleForTesting |
| 59 |
public static GateKeeperResponse createOkResponse(byte[] payload, boolean shouldReEnroll) { |
| 60 |
GateKeeperResponse response = new GateKeeperResponse(RESPONSE_OK); |
| 61 |
response.mPayload = payload; |
| 62 |
response.mShouldReEnroll = shouldReEnroll; |
| 63 |
return response; |
| 64 |
} |
| 65 |
|
| 66 |
@Override |
| 67 |
public int describeContents() { |
| 68 |
return 0; |
| 69 |
} |
| 70 |
|
| 71 |
public static final @android.annotation.NonNull Parcelable.Creator<GateKeeperResponse> CREATOR |
| 72 |
= new Parcelable.Creator<GateKeeperResponse>() { |
| 73 |
@Override |
| 74 |
public GateKeeperResponse createFromParcel(Parcel source) { |
| 75 |
int responseCode = source.readInt(); |
| 76 |
final GateKeeperResponse response; |
| 77 |
if (responseCode == RESPONSE_RETRY) { |
| 78 |
response = createRetryResponse(source.readInt()); |
| 79 |
} else if (responseCode == RESPONSE_OK) { |
| 80 |
final boolean shouldReEnroll = source.readInt() == 1; |
| 81 |
byte[] payload = null; |
| 82 |
int size = source.readInt(); |
| 83 |
if (size > 0) { |
| 84 |
payload = new byte[size]; |
| 85 |
source.readByteArray(payload); |
| 86 |
} |
| 87 |
response = createOkResponse(payload, shouldReEnroll); |
| 88 |
} else { |
| 89 |
response = createGenericResponse(responseCode); |
| 90 |
} |
| 91 |
return response; |
| 92 |
} |
| 93 |
|
| 94 |
@Override |
| 95 |
public GateKeeperResponse[] newArray(int size) { |
| 96 |
return new GateKeeperResponse[size]; |
| 97 |
} |
| 98 |
|
| 99 |
}; |
| 100 |
|
| 101 |
@Override |
| 102 |
public void writeToParcel(Parcel dest, int flags) { |
| 103 |
dest.writeInt(mResponseCode); |
| 104 |
if (mResponseCode == RESPONSE_RETRY) { |
| 105 |
dest.writeInt(mTimeout); |
| 106 |
} else if (mResponseCode == RESPONSE_OK) { |
| 107 |
dest.writeInt(mShouldReEnroll ? 1 : 0); |
| 108 |
if (mPayload != null && mPayload.length > 0) { |
| 109 |
dest.writeInt(mPayload.length); |
| 110 |
dest.writeByteArray(mPayload); |
| 111 |
} else { |
| 112 |
dest.writeInt(0); |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
public byte[] getPayload() { |
| 118 |
return mPayload; |
| 119 |
} |
| 120 |
|
| 121 |
public int getTimeout() { |
| 122 |
return mTimeout; |
| 123 |
} |
| 124 |
|
| 125 |
public boolean getShouldReEnroll() { |
| 126 |
return mShouldReEnroll; |
| 127 |
} |
| 128 |
|
| 129 |
public int getResponseCode() { |
| 130 |
return mResponseCode; |
| 131 |
} |
| 132 |
} |