TestAccountType1Authenticator.java revision 81c4c8af206624f902efbd913fc3b26bb6842307
181c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin/*
281c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin * Copyright (C) 2017 The Android Open Source Project
381c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin *
481c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin * Licensed under the Apache License, Version 2.0 (the "License");
581c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin * you may not use this file except in compliance with the License.
681c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin * You may obtain a copy of the License at
781c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin *
881c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin *      http://www.apache.org/licenses/LICENSE-2.0
981c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin *
1081c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin * Unless required by applicable law or agreed to in writing, software
1181c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin * distributed under the License is distributed on an "AS IS" BASIS,
1281c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1381c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin * See the License for the specific language governing permissions and
1481c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin * limitations under the License.
1581c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin */
1681c4c8af206624f902efbd913fc3b26bb6842307Hongming Jinpackage com.android.server.accounts;
1781c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin
1881c4c8af206624f902efbd913fc3b26bb6842307Hongming Jinimport android.accounts.AbstractAccountAuthenticator;
1981c4c8af206624f902efbd913fc3b26bb6842307Hongming Jinimport android.accounts.Account;
2081c4c8af206624f902efbd913fc3b26bb6842307Hongming Jinimport android.accounts.AccountAuthenticatorResponse;
2181c4c8af206624f902efbd913fc3b26bb6842307Hongming Jinimport android.accounts.AccountManager;
2281c4c8af206624f902efbd913fc3b26bb6842307Hongming Jinimport android.accounts.NetworkErrorException;
2381c4c8af206624f902efbd913fc3b26bb6842307Hongming Jinimport android.content.Context;
2481c4c8af206624f902efbd913fc3b26bb6842307Hongming Jinimport android.content.Intent;
2581c4c8af206624f902efbd913fc3b26bb6842307Hongming Jinimport android.os.Bundle;
2681c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin
2781c4c8af206624f902efbd913fc3b26bb6842307Hongming Jinimport com.android.frameworks.servicestests.R;
2881c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin
2981c4c8af206624f902efbd913fc3b26bb6842307Hongming Jinimport java.util.concurrent.atomic.AtomicInteger;
3081c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin
3181c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin/**
3281c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin * This authenticator is to mock account authenticator to test AccountManagerService.
3381c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin */
3481c4c8af206624f902efbd913fc3b26bb6842307Hongming Jinpublic class TestAccountType1Authenticator extends AbstractAccountAuthenticator {
3581c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin    private final AtomicInteger mTokenCounter  = new AtomicInteger(0);
3681c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin
3781c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin    private final String mAccountType;
3881c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin    private final Context mContext;
3981c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin
4081c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin    public TestAccountType1Authenticator(Context context, String accountType) {
4181c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        super(context);
4281c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        mAccountType = accountType;
4381c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        mContext = context;
4481c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin    }
4581c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin
4681c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin    @Override
4781c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin    public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
48a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin        throw new UnsupportedOperationException(
49a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin                "editProperties is not yet supported by the TestAccountAuthenticator");
50a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin    }
51a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin
52a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin    @Override
53a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin    public Bundle addAccount(
54a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            AccountAuthenticatorResponse response,
55a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            String accountType,
56a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            String authTokenType,
5781c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            String[] requiredFeatures,
5881c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            Bundle options) throws NetworkErrorException {
5981c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        if (!mAccountType.equals(accountType)) {
6081c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            throw new IllegalArgumentException("Request to the wrong authenticator!");
6181c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        }
6281c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin
6381c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        Bundle result = new Bundle();
6481c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        result.putString(AccountManager.KEY_ACCOUNT_NAME, "test_account@test.com");
6581c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        result.putString(AccountManager.KEY_ACCOUNT_TYPE, mAccountType);
6681c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        return result;
6781c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin    }
6881c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin
69a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin    @Override
70a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin    public Bundle confirmCredentials(
71a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            AccountAuthenticatorResponse response,
72a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            Account account,
73a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            Bundle options) throws NetworkErrorException {
7481c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        throw new UnsupportedOperationException(
7581c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin                "confirmCredentials is not yet supported by the TestAccountAuthenticator");
76a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin    }
77a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin
78a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin    @Override
79a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin    public Bundle getAuthToken(
80a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            AccountAuthenticatorResponse response,
81a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            Account account,
82a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            String authTokenType,
83a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            Bundle options) throws NetworkErrorException {
84a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin        throw new UnsupportedOperationException(
85a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin                "getAuthToken is not yet supported by the TestAccountAuthenticator");
86a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin    }
87a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin
88a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin    @Override
89a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin    public String getAuthTokenLabel(String authTokenType) {
90a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin        throw new UnsupportedOperationException(
91a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin                "getAuthTokenLabel is not yet supported by the TestAccountAuthenticator");
92a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin    }
93a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin
94a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin    @Override
95a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin    public Bundle updateCredentials(
96a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            AccountAuthenticatorResponse response,
97a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            Account account,
98a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            String authTokenType,
99a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            Bundle options) throws NetworkErrorException {
100a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin        if (!mAccountType.equals(account.type)) {
10181c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            throw new IllegalArgumentException("Request to the wrong authenticator!");
10281c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        }
10381c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        Bundle result = new Bundle();
10481c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
10581c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
10681c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        return result;
10781c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin    }
10881c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin
109a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin    @Override
110a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin    public Bundle hasFeatures(
111a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            AccountAuthenticatorResponse response,
112a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            Account account,
113a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            String[] features) throws NetworkErrorException {
114a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin        throw new UnsupportedOperationException(
115a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin                "hasFeatures is not yet supported by the TestAccountAuthenticator");
116a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin    }
117a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin
118a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin    @Override
119a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin    public Bundle startAddAccountSession(
120a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            AccountAuthenticatorResponse response,
121a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            String accountType,
122a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            String authTokenType,
123a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            String[] requiredFeatures,
124a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            Bundle options) throws NetworkErrorException {
125a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin        if (!mAccountType.equals(accountType)) {
126a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            throw new IllegalArgumentException("Request to the wrong authenticator!");
127a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin        }
128a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin
129a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin        String accountName = null;
130a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin        Bundle sessionBundle = null;
131a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin        String password = null;
132a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin        if (options != null) {
133a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            accountName = options.getString(AccountManagerServiceTestFixtures.KEY_ACCOUNT_NAME);
134a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            sessionBundle = options.getBundle(
135a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin                    AccountManagerServiceTestFixtures.KEY_ACCOUNT_SESSION_BUNDLE);
136a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            password = options.getString(AccountManagerServiceTestFixtures.KEY_ACCOUNT_PASSWORD);
137a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin        }
138a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin
139a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin        Bundle result = new Bundle();
140a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin        if (accountName.equals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS)) {
14181c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            // fill bundle with a success result.
14281c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            result.putBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE, sessionBundle);
14381c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            result.putString(AccountManager.KEY_ACCOUNT_STATUS_TOKEN,
14481c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin                    AccountManagerServiceTestFixtures.ACCOUNT_STATUS_TOKEN);
14581c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            result.putString(AccountManager.KEY_PASSWORD, password);
14681c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            result.putString(AccountManager.KEY_AUTHTOKEN,
14781c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin                    Integer.toString(mTokenCounter.incrementAndGet()));
14881c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        } else if (accountName.equals(
149a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin                AccountManagerServiceTestFixtures.ACCOUNT_NAME_INTERVENE)) {
150a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            // Specify data to be returned by the eventual activity.
151a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            Intent eventualActivityResultData = new Intent();
152a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            eventualActivityResultData.putExtra(AccountManager.KEY_AUTHTOKEN,
153a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin                    Integer.toString(mTokenCounter.incrementAndGet()));
154a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            eventualActivityResultData.putExtra(AccountManager.KEY_ACCOUNT_STATUS_TOKEN,
155a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin                    AccountManagerServiceTestFixtures.ACCOUNT_STATUS_TOKEN);
156a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            eventualActivityResultData.putExtra(AccountManager.KEY_PASSWORD, password);
157a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            eventualActivityResultData.putExtra(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE,
158a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin                    sessionBundle);
159a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            // Fill result with Intent.
160a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            Intent intent = new Intent(mContext, AccountAuthenticatorDummyActivity.class);
161a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            intent.putExtra(AccountManagerServiceTestFixtures.KEY_RESULT,
162a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin                    eventualActivityResultData);
163a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            intent.putExtra(AccountManagerServiceTestFixtures.KEY_CALLBACK, response);
164a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin
165a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            result.putParcelable(AccountManager.KEY_INTENT, intent);
166a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin        } else {
167a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            // fill with error
168a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            fillResultWithError(result, options);
169a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin        }
170a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin
171a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin        return result;
172a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin    }
173a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin
174a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin    @Override
175a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin    public Bundle startUpdateCredentialsSession(
176a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            AccountAuthenticatorResponse response,
177a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            Account account,
178a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            String authTokenType,
179a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            Bundle options)
180a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            throws NetworkErrorException {
181a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin
182a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin        if (!mAccountType.equals(account.type)) {
183a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            throw new IllegalArgumentException("Request to the wrong authenticator!");
184a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin        }
185a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin
186a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin        String accountName = null;
187a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin        Bundle sessionBundle = null;
188a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin        if (options != null) {
189a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            accountName = options.getString(AccountManagerServiceTestFixtures.KEY_ACCOUNT_NAME);
190a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            sessionBundle = options.getBundle(
19181c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            AccountManagerServiceTestFixtures.KEY_ACCOUNT_SESSION_BUNDLE);
19281c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        }
19381c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin
19481c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        Bundle result = new Bundle();
195a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin        if (accountName.equals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS)) {
19681c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            // fill bundle with a success result.
19781c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            result.putBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE, sessionBundle);
19881c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            result.putString(AccountManager.KEY_ACCOUNT_STATUS_TOKEN,
19981c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin                    AccountManagerServiceTestFixtures.ACCOUNT_STATUS_TOKEN);
20081c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            result.putString(AccountManager.KEY_PASSWORD, "doesn't matter");
20181c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            result.putString(AccountManager.KEY_AUTHTOKEN,
20281c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin                    Integer.toString(mTokenCounter.incrementAndGet()));
20381c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        } else if (accountName.equals(
20481c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin                AccountManagerServiceTestFixtures.ACCOUNT_NAME_INTERVENE)) {
20581c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            // Specify data to be returned by the eventual activity.
20681c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            Intent eventualActivityResultData = new Intent();
20781c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            eventualActivityResultData.putExtra(AccountManager.KEY_AUTHTOKEN,
208a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin                    Integer.toString(mTokenCounter.incrementAndGet()));
209a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            eventualActivityResultData.putExtra(AccountManager.KEY_ACCOUNT_STATUS_TOKEN,
210a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin                    AccountManagerServiceTestFixtures.ACCOUNT_STATUS_TOKEN);
211a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            eventualActivityResultData.putExtra(AccountManager.KEY_PASSWORD,
212a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin                    AccountManagerServiceTestFixtures.ACCOUNT_PASSWORD);
213a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            eventualActivityResultData.putExtra(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE,
214a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin                    sessionBundle);
215a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            // Fill result with Intent.
216a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            Intent intent = new Intent(mContext, AccountAuthenticatorDummyActivity.class);
217a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            intent.putExtra(AccountManagerServiceTestFixtures.KEY_RESULT,
218a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin                    eventualActivityResultData);
219a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            intent.putExtra(AccountManagerServiceTestFixtures.KEY_CALLBACK, response);
220a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin
221a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            result.putParcelable(AccountManager.KEY_INTENT, intent);
222a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin        } else {
223a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            // fill with error
224a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            fillResultWithError(result, options);
225a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin        }
226a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin        return result;
227a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin    }
228a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin
229a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin    @Override
230a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin    public Bundle finishSession(AccountAuthenticatorResponse response,
231a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            String accountType,
232a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            Bundle sessionBundle) throws NetworkErrorException {
23381c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin
23481c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        if (!mAccountType.equals(accountType)) {
23581c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            throw new IllegalArgumentException("Request to the wrong authenticator!");
23681c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        }
23781c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin
23881c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        String accountName = null;
23981c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        if (sessionBundle != null) {
24081c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            accountName = sessionBundle.getString(
241a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            AccountManagerServiceTestFixtures.KEY_ACCOUNT_NAME);
242a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin        }
2438c505dbacb65ac8bd11b1061657c1a3b5abe1be2Hongming Jin
244a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin        Bundle result = new Bundle();
245f29d549c2d16f377fe11261b98d77d2c9db1070csunjian        if (accountName.equals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS)) {
246f29d549c2d16f377fe11261b98d77d2c9db1070csunjian            // fill bundle with a success result.
2478c505dbacb65ac8bd11b1061657c1a3b5abe1be2Hongming Jin            result.putString(AccountManager.KEY_ACCOUNT_NAME,
2488c505dbacb65ac8bd11b1061657c1a3b5abe1be2Hongming Jin                    AccountManagerServiceTestFixtures.ACCOUNT_NAME);
2498c505dbacb65ac8bd11b1061657c1a3b5abe1be2Hongming Jin            result.putString(AccountManager.KEY_ACCOUNT_TYPE,
250a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin                    AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1);
251a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            result.putString(AccountManager.KEY_AUTHTOKEN,
252a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin                    Integer.toString(mTokenCounter.incrementAndGet()));
253a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin        } else if (accountName.equals(
254a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin                AccountManagerServiceTestFixtures.ACCOUNT_NAME_INTERVENE)) {
255a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            // Specify data to be returned by the eventual activity.
256a0c4d1d6bfd026de36a542d6d5cd82cd12740ee5Hongming Jin            Intent eventualActivityResultData = new Intent();
25781c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            eventualActivityResultData.putExtra(AccountManager.KEY_ACCOUNT_NAME,
25881c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin                    AccountManagerServiceTestFixtures.ACCOUNT_NAME);
25981c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            eventualActivityResultData.putExtra(AccountManager.KEY_ACCOUNT_TYPE,
26081c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin                    AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1);
26181c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            eventualActivityResultData.putExtra(AccountManager.KEY_AUTHTOKEN,
26281c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin                    Integer.toString(mTokenCounter.incrementAndGet()));
26381c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin
26481c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            // Fill result with Intent.
26581c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            Intent intent = new Intent(mContext, AccountAuthenticatorDummyActivity.class);
26681c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            intent.putExtra(AccountManagerServiceTestFixtures.KEY_RESULT,
26781c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin                    eventualActivityResultData);
26881c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            intent.putExtra(AccountManagerServiceTestFixtures.KEY_CALLBACK, response);
26981c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin
27081c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            result.putParcelable(AccountManager.KEY_INTENT, intent);
27181c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        } else {
27281c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            // fill with error
27381c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            fillResultWithError(result, sessionBundle);
27481c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        }
27581c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        return result;
27681c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin    }
27781c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin
27881c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin    @Override
27981c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin    public Bundle isCredentialsUpdateSuggested(
28081c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            final AccountAuthenticatorResponse response,
28181c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            Account account,
28281c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            String statusToken) throws NetworkErrorException {
28381c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin
28481c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        Bundle result = new Bundle();
28581c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        if (account.name.equals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS)) {
28681c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            // fill bundle with a success result.
28781c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true);
28881c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        } else {
28981c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            // fill with error
29081c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            fillResultWithError(
29181c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin                    result, AccountManager.ERROR_CODE_INVALID_RESPONSE, "Default Error Message");
29281c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        }
29381c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin
29481c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        response.onResult(result);
29581c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        return null;
29681c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin    }
29781c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin
29881c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin    private void fillResultWithError(Bundle result, Bundle options) {
29981c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        int errorCode = AccountManager.ERROR_CODE_INVALID_RESPONSE;
30081c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        String errorMsg = "Default Error Message";
30181c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        if (options != null) {
30281c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            errorCode = options.getInt(AccountManager.KEY_ERROR_CODE);
30381c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin            errorMsg = options.getString(AccountManager.KEY_ERROR_MESSAGE);
30481c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        }
30581c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        fillResultWithError(result, errorCode, errorMsg);
30681c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin    }
30781c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin
30881c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin    private void fillResultWithError(Bundle result, int errorCode, String errorMsg) {
30981c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        result.putInt(AccountManager.KEY_ERROR_CODE, errorCode);
31081c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin        result.putString(AccountManager.KEY_ERROR_MESSAGE, errorMsg);
31181c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin    }
31281c4c8af206624f902efbd913fc3b26bb6842307Hongming Jin}