1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.contacts.tests.testauth;
18
19import android.accounts.AbstractAccountAuthenticator;
20import android.accounts.Account;
21import android.accounts.AccountAuthenticatorResponse;
22import android.accounts.AccountManager;
23import android.content.Context;
24import android.content.SharedPreferences;
25import android.os.Bundle;
26import android.preference.PreferenceManager;
27import android.util.Log;
28
29/**
30 * Simple authenticator.  It has no "login" dialogs/activities.  When you add a new account, it'll
31 * just create a new account with a unique name.
32 */
33class TestAuthenticator extends AbstractAccountAuthenticator {
34    private static final String PASSWORD = "xxx"; // any string will do.
35
36    // To remember the last user-ID.
37    private static final String PREF_KEY_LAST_USER_ID = "TestAuthenticator.PREF_KEY_LAST_USER_ID";
38
39    private final Context mContext;
40
41    public TestAuthenticator(Context context) {
42        super(context);
43        mContext = context.getApplicationContext();
44    }
45
46    /**
47     * @return a new, unique username.
48     */
49    private String newUniqueUserName() {
50        final SharedPreferences prefs =
51                PreferenceManager.getDefaultSharedPreferences(mContext);
52        final int nextId = prefs.getInt(PREF_KEY_LAST_USER_ID, 0) + 1;
53        prefs.edit().putInt(PREF_KEY_LAST_USER_ID, nextId).apply();
54
55        return "User-" + nextId;
56    }
57
58    /**
59     * Create a new account with the name generated by {@link #newUniqueUserName()}.
60     */
61    @Override
62    public Bundle addAccount(AccountAuthenticatorResponse response, String accountType,
63            String authTokenType, String[] requiredFeatures, Bundle options) {
64        if (Log.isLoggable(TestauthConstants.LOG_TAG, Log.VERBOSE)) {
65            Log.v(TestauthConstants.LOG_TAG, "addAccount() type=" + accountType);
66        }
67        final Bundle bundle = new Bundle();
68
69        final Account account = new Account(newUniqueUserName(), accountType);
70
71        // Create an account.
72        AccountManager.get(mContext).addAccountExplicitly(account, PASSWORD, null);
73
74        // And return it.
75        bundle.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
76        bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
77        return bundle;
78    }
79
80    /**
81     * Just return the user name as the authtoken.
82     */
83    @Override
84    public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account,
85            String authTokenType, Bundle loginOptions) {
86        if (Log.isLoggable(TestauthConstants.LOG_TAG, Log.VERBOSE)) {
87            Log.v(TestauthConstants.LOG_TAG, "getAuthToken() account=" + account);
88        }
89        final Bundle bundle = new Bundle();
90        bundle.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
91        bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
92        bundle.putString(AccountManager.KEY_AUTHTOKEN, account.name);
93
94        return bundle;
95    }
96
97    @Override
98    public Bundle confirmCredentials(
99            AccountAuthenticatorResponse response, Account account, Bundle options) {
100        if (Log.isLoggable(TestauthConstants.LOG_TAG, Log.VERBOSE)) {
101            Log.v(TestauthConstants.LOG_TAG, "confirmCredentials()");
102        }
103        return null;
104    }
105
106    @Override
107    public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
108        if (Log.isLoggable(TestauthConstants.LOG_TAG, Log.VERBOSE)) {
109            Log.v(TestauthConstants.LOG_TAG, "editProperties()");
110        }
111        throw new UnsupportedOperationException();
112    }
113
114    @Override
115    public String getAuthTokenLabel(String authTokenType) {
116        // null means we don't support multiple authToken types
117        if (Log.isLoggable(TestauthConstants.LOG_TAG, Log.VERBOSE)) {
118            Log.v(TestauthConstants.LOG_TAG, "getAuthTokenLabel()");
119        }
120        return null;
121    }
122
123    @Override
124    public Bundle hasFeatures(
125            AccountAuthenticatorResponse response, Account account, String[] features) {
126        // This call is used to query whether the Authenticator supports
127        // specific features. We don't expect to get called, so we always
128        // return false (no) for any queries.
129        if (Log.isLoggable(TestauthConstants.LOG_TAG, Log.VERBOSE)) {
130            Log.v(TestauthConstants.LOG_TAG, "hasFeatures()");
131        }
132        final Bundle result = new Bundle();
133        result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false);
134        return result;
135    }
136
137    @Override
138    public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account,
139            String authTokenType, Bundle loginOptions) {
140        if (Log.isLoggable(TestauthConstants.LOG_TAG, Log.VERBOSE)) {
141            Log.v(TestauthConstants.LOG_TAG, "updateCredentials()");
142        }
143        return null;
144    }
145}
146