GateKeeperResponse.java revision e74cae8f7c3e6b12f2bf2b75427ee8f5b53eca3c
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.service.gatekeeper;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22/**
23 * Response object for a GateKeeper verification request.
24 * @hide
25 */
26public final class GateKeeperResponse implements Parcelable {
27
28    public static final int RESPONSE_ERROR = -1;
29    public static final int RESPONSE_OK = 0;
30    public static final int RESPONSE_RETRY = 1;
31
32    private final int mResponseCode;
33
34    private int mTimeout;
35    private byte[] mPayload;
36    private boolean mShouldReEnroll;
37
38    private GateKeeperResponse(int responseCode) {
39        mResponseCode = responseCode;
40    }
41
42    private GateKeeperResponse(int responseCode, int timeout) {
43        mResponseCode = responseCode;
44    }
45
46    @Override
47    public int describeContents() {
48        return 0;
49    }
50
51    public static final Parcelable.Creator<GateKeeperResponse> CREATOR
52            = new Parcelable.Creator<GateKeeperResponse>() {
53        @Override
54        public GateKeeperResponse createFromParcel(Parcel source) {
55            int responseCode = source.readInt();
56            GateKeeperResponse response = new GateKeeperResponse(responseCode);
57            if (responseCode == RESPONSE_RETRY) {
58                response.setTimeout(source.readInt());
59            } else if (responseCode == RESPONSE_OK) {
60                response.setShouldReEnroll(source.readInt() == 1);
61                int size = source.readInt();
62                if (size > 0) {
63                    byte[] payload = new byte[size];
64                    source.readByteArray(payload);
65                    response.setPayload(payload);
66                }
67            }
68            return response;
69        }
70
71        @Override
72        public GateKeeperResponse[] newArray(int size) {
73            return new GateKeeperResponse[size];
74        }
75
76    };
77
78    @Override
79    public void writeToParcel(Parcel dest, int flags) {
80        dest.writeInt(mResponseCode);
81        if (mResponseCode == RESPONSE_RETRY) {
82            dest.writeInt(mTimeout);
83        } else if (mResponseCode == RESPONSE_OK) {
84            dest.writeInt(mShouldReEnroll ? 1 : 0);
85            if (mPayload != null) {
86                dest.writeInt(mPayload.length);
87                dest.writeByteArray(mPayload);
88            } else {
89                dest.writeInt(0);
90            }
91        }
92    }
93
94    public byte[] getPayload() {
95        return mPayload;
96    }
97
98    public int getTimeout() {
99        return mTimeout;
100    }
101
102    public boolean getShouldReEnroll() {
103        return mShouldReEnroll;
104    }
105
106    public int getResponseCode() {
107        return mResponseCode;
108    }
109
110    private void setTimeout(int timeout) {
111        mTimeout = timeout;
112    }
113
114    private void setShouldReEnroll(boolean shouldReEnroll) {
115        mShouldReEnroll = shouldReEnroll;
116    }
117
118    private void setPayload(byte[] payload) {
119        mPayload = payload;
120    }
121
122}
123