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            }
89        }
90    }
91
92    public byte[] getPayload() {
93        return mPayload;
94    }
95
96    public int getTimeout() {
97        return mTimeout;
98    }
99
100    public boolean getShouldReEnroll() {
101        return mShouldReEnroll;
102    }
103
104    public int getResponseCode() {
105        return mResponseCode;
106    }
107
108    private void setTimeout(int timeout) {
109        mTimeout = timeout;
110    }
111
112    private void setShouldReEnroll(boolean shouldReEnroll) {
113        mShouldReEnroll = shouldReEnroll;
114    }
115
116    private void setPayload(byte[] payload) {
117        mPayload = payload;
118    }
119
120}
121