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 com.android.internal.widget;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22/**
23 * Response object for a ILockSettings credential verification request.
24 * @hide
25 */
26public final class VerifyCredentialResponse 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    public static final VerifyCredentialResponse OK = new VerifyCredentialResponse();
33    public static final VerifyCredentialResponse ERROR
34            = new VerifyCredentialResponse(RESPONSE_ERROR, 0, null);
35
36    private int mResponseCode;
37    private byte[] mPayload;
38    private int mTimeout;
39
40    public static final Parcelable.Creator<VerifyCredentialResponse> CREATOR
41            = new Parcelable.Creator<VerifyCredentialResponse>() {
42        @Override
43        public VerifyCredentialResponse createFromParcel(Parcel source) {
44            int responseCode = source.readInt();
45            VerifyCredentialResponse response = new VerifyCredentialResponse(responseCode, 0, null);
46            if (responseCode == RESPONSE_RETRY) {
47                response.setTimeout(source.readInt());
48            } else if (responseCode == RESPONSE_OK) {
49                int size = source.readInt();
50                if (size > 0) {
51                    byte[] payload = new byte[size];
52                    source.readByteArray(payload);
53                    response.setPayload(payload);
54                }
55            }
56            return response;
57        }
58
59        @Override
60        public VerifyCredentialResponse[] newArray(int size) {
61            return new VerifyCredentialResponse[size];
62        }
63
64    };
65
66    public VerifyCredentialResponse() {
67        mResponseCode = RESPONSE_OK;
68        mPayload = null;
69    }
70
71
72    public VerifyCredentialResponse(byte[] payload) {
73        mPayload = payload;
74        mResponseCode = RESPONSE_OK;
75    }
76
77    public VerifyCredentialResponse(int timeout) {
78        mTimeout = timeout;
79        mResponseCode = RESPONSE_RETRY;
80        mPayload = null;
81    }
82
83    private VerifyCredentialResponse(int responseCode, int timeout, byte[] payload) {
84        mResponseCode = responseCode;
85        mTimeout = timeout;
86        mPayload = payload;
87    }
88
89    @Override
90    public void writeToParcel(Parcel dest, int flags) {
91        dest.writeInt(mResponseCode);
92        if (mResponseCode == RESPONSE_RETRY) {
93            dest.writeInt(mTimeout);
94        } else if (mResponseCode == RESPONSE_OK) {
95            if (mPayload != null) {
96                dest.writeInt(mPayload.length);
97                dest.writeByteArray(mPayload);
98            }
99        }
100    }
101
102    @Override
103    public int describeContents() {
104        return 0;
105    }
106
107    public byte[] getPayload() {
108        return mPayload;
109    }
110
111    public int getTimeout() {
112        return mTimeout;
113    }
114
115    public int getResponseCode() {
116        return mResponseCode;
117    }
118
119    private void setTimeout(int timeout) {
120        mTimeout = timeout;
121    }
122
123    private void setPayload(byte[] payload) {
124        mPayload = payload;
125    }
126}
127