1/*
2 * Copyright (C) 2007 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.google.android.googleapps;
18
19import android.content.Intent;
20import android.os.Parcel;
21import android.os.Parcelable;
22
23/**
24 * "Result" container class returned by the GoogleLoginService
25 * {@link IGoogleLoginService#blockingGetCredentials} or
26 * {@link IGoogleLoginService#retryGetCredentials} request.
27 * <p>
28 * A single credentials request can <b>either</b>
29 * <ol>
30 * <li> instantly return the credentials you asked for
31 *      (ie. an authentication token, which is a String)
32 * <p> <b>or</b>
33 * <li> return an Intent which you (the caller) need to run yourself
34 *      using {@link android.app.Activity#startActivityForResult},
35 *      which will then return the credentials you asked
36 *      for to your <code>onActivityResult</code> method.
37 *      <p>
38 *      (Generally, that Intent will bring up a UI for the user to
39 *      enter their username and password.  The IGoogleLoginService
40 *      interface doesn't guarantee anything about what that Intent
41 *      actually does, though.)
42 * </ol>
43 * This class can encapsulate either result.  Call <code>getCredentialsString</code> or
44 * <code>getCredentialsIntent</code> on the returned object; one will return a non-null
45 * value.
46 */
47public class GoogleLoginCredentialsResult implements Parcelable {
48    private String mAccount;
49    private String mCredentialsString;
50    private Intent mCredentialsIntent;
51
52    /**
53     * Gets the account for which you asked for credentials.
54     */
55    public String getAccount() {
56        return mAccount;
57    }
58
59    /**
60     * Gets the authentication token representing the credentials you asked for.
61     * <p>
62     * Either this or <code>getCredentials</code> will return a non-null value on the object
63     * returned from a credentials request.
64     */
65    public String getCredentialsString() {
66        return mCredentialsString;
67    }
68
69    /**
70     * Gets the Intent which the caller needs to run in order
71     * to get the requested credentials.
72     * <p>
73     * With a GoogleLoginCredentialsResult object returned by the
74     * GoogleLoginService <code>getCredentials</code> method, either this or
75     * <code>getCredentialsString</code> (but not both) will return non-null.
76     */
77    // TODO: better name?  "getCredentialsLookupIntent"?
78    public Intent getCredentialsIntent() {
79        return mCredentialsIntent;
80    }
81
82    /**
83     * {@hide}
84     * Create an empty GoogleLoginCredentialsResult.
85     */
86    public GoogleLoginCredentialsResult() {
87        mCredentialsString = null;
88        mCredentialsIntent = null;
89        mAccount = null;
90    }
91
92    /** {@hide} */
93    public void setCredentialsString(String s) {
94        mCredentialsString = s;
95    }
96
97    /** {@hide} */
98    public void setCredentialsIntent(Intent intent) {
99        mCredentialsIntent = intent;
100    }
101
102    /** {@hide} */
103    public void setAccount(String account) {
104        mAccount = account;
105    }
106
107    //
108    // Parcelable interface
109    //
110
111    public int describeContents() {
112        return (mCredentialsIntent != null) ? mCredentialsIntent.describeContents() : 0;
113    }
114
115    public void writeToParcel(Parcel out, int flags) {
116        out.writeString(mAccount);
117        out.writeString(mCredentialsString);
118        if (mCredentialsIntent != null) {
119            out.writeInt(1);
120            mCredentialsIntent.writeToParcel(out, 0);
121        } else {
122            out.writeInt(0);
123        }
124    }
125
126    public static final Parcelable.Creator<GoogleLoginCredentialsResult> CREATOR
127        = new Parcelable.Creator<GoogleLoginCredentialsResult>() {
128            public GoogleLoginCredentialsResult createFromParcel(Parcel in) {
129                return new GoogleLoginCredentialsResult(in);
130            }
131
132            public GoogleLoginCredentialsResult[] newArray(int size) {
133                return new GoogleLoginCredentialsResult[size];
134            }
135        };
136
137    private GoogleLoginCredentialsResult(Parcel in) {
138        readFromParcel(in);
139    }
140
141    public void readFromParcel(Parcel in) {
142        mAccount = in.readString();
143        mCredentialsString = in.readString();
144
145        int hasIntent = in.readInt();
146        mCredentialsIntent = null;
147        if (hasIntent == 1) {
148            mCredentialsIntent = new Intent();
149            mCredentialsIntent.readFromParcel(in);
150            mCredentialsIntent.setExtrasClassLoader(getClass().getClassLoader());
151        }
152    }
153}
154