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.common.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 Log.v(TestauthConstants.LOG_TAG, "addAccount() type=" + accountType); 65 final Bundle bundle = new Bundle(); 66 67 final Account account = new Account(newUniqueUserName(), accountType); 68 69 // Create an account. 70 AccountManager.get(mContext).addAccountExplicitly(account, PASSWORD, null); 71 72 // And return it. 73 bundle.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); 74 bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); 75 return bundle; 76 } 77 78 /** 79 * Just return the user name as the authtoken. 80 */ 81 @Override 82 public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, 83 String authTokenType, Bundle loginOptions) { 84 Log.v(TestauthConstants.LOG_TAG, "getAuthToken() account=" + account); 85 final Bundle bundle = new Bundle(); 86 bundle.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); 87 bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); 88 bundle.putString(AccountManager.KEY_AUTHTOKEN, account.name); 89 90 return bundle; 91 } 92 93 @Override 94 public Bundle confirmCredentials( 95 AccountAuthenticatorResponse response, Account account, Bundle options) { 96 Log.v(TestauthConstants.LOG_TAG, "confirmCredentials()"); 97 return null; 98 } 99 100 @Override 101 public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) { 102 Log.v(TestauthConstants.LOG_TAG, "editProperties()"); 103 throw new UnsupportedOperationException(); 104 } 105 106 @Override 107 public String getAuthTokenLabel(String authTokenType) { 108 // null means we don't support multiple authToken types 109 Log.v(TestauthConstants.LOG_TAG, "getAuthTokenLabel()"); 110 return null; 111 } 112 113 @Override 114 public Bundle hasFeatures( 115 AccountAuthenticatorResponse response, Account account, String[] features) { 116 // This call is used to query whether the Authenticator supports 117 // specific features. We don't expect to get called, so we always 118 // return false (no) for any queries. 119 Log.v(TestauthConstants.LOG_TAG, "hasFeatures()"); 120 final Bundle result = new Bundle(); 121 result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false); 122 return result; 123 } 124 125 @Override 126 public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account, 127 String authTokenType, Bundle loginOptions) { 128 Log.v(TestauthConstants.LOG_TAG, "updateCredentials()"); 129 return null; 130 } 131} 132