Home | History | Annotate | Download | only in stub
      1 /*
      2  * Copyright (C) 2016 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 
     17 package com.android.cts.stub;
     18 
     19 import android.accounts.AbstractAccountAuthenticator;
     20 import android.accounts.Account;
     21 import android.accounts.AccountAuthenticatorResponse;
     22 import android.accounts.AccountManager;
     23 import android.accounts.NetworkErrorException;
     24 import android.app.Service;
     25 import android.content.Context;
     26 import android.content.Intent;
     27 import android.os.Bundle;
     28 import android.os.IBinder;
     29 
     30 public class StubAuthenticator extends Service {
     31     public static final String TOKEN_TYPE_REMOVE_ACCOUNTS = "TOKEN_TYPE_REMOVE_ACCOUNTS";
     32 
     33     private static long sNumAccountsAdded;
     34     private Authenticator mAuthenticator;
     35 
     36     @Override
     37     public void onCreate() {
     38         mAuthenticator = new Authenticator(this);
     39     }
     40 
     41     @Override
     42     public IBinder onBind(Intent intent) {
     43         return mAuthenticator.getIBinder();
     44     }
     45 
     46     public class Authenticator extends AbstractAccountAuthenticator {
     47         public Authenticator(Context context) {
     48             super(context);
     49             removeAccounts();
     50         }
     51 
     52         @Override
     53         public Bundle editProperties(AccountAuthenticatorResponse response,
     54                 String accountType) {
     55             return null;
     56         }
     57 
     58         @Override
     59         public Bundle addAccount(AccountAuthenticatorResponse response,
     60                 String accountType, String tokenType, String[] strings,
     61                 Bundle bundle) throws NetworkErrorException {
     62             AccountManager accountManager = getSystemService(AccountManager.class);
     63 
     64             String accountName = "foo" + sNumAccountsAdded;
     65             sNumAccountsAdded++;
     66 
     67             accountManager.addAccountExplicitly(new Account(accountName, accountType), "bar", null);
     68 
     69             Bundle result = new Bundle();
     70             result.putString(AccountManager.KEY_ACCOUNT_NAME, accountName);
     71             result.putString(AccountManager.KEY_ACCOUNT_TYPE, accountType);
     72             response.onResult(result);
     73 
     74             return null;
     75         }
     76 
     77         @Override
     78         public Bundle confirmCredentials(AccountAuthenticatorResponse response,
     79                 Account account, Bundle bundle) throws NetworkErrorException {
     80             return null;
     81         }
     82 
     83         @Override
     84         public Bundle getAuthToken(AccountAuthenticatorResponse response,
     85                 Account account, String type, Bundle bundle) throws NetworkErrorException {
     86             if (TOKEN_TYPE_REMOVE_ACCOUNTS.equals(type)) {
     87                 removeAccounts();
     88             }
     89             return null;
     90         }
     91 
     92         @Override
     93         public String getAuthTokenLabel(String tokenName) {
     94             return null;
     95         }
     96 
     97         @Override
     98         public Bundle updateCredentials(AccountAuthenticatorResponse response,
     99                 Account account, String tokenType, Bundle bundle)
    100                 throws NetworkErrorException {
    101             return null;
    102         }
    103 
    104         @Override
    105         public Bundle hasFeatures(AccountAuthenticatorResponse response,
    106                 Account account, String[] options) throws NetworkErrorException {
    107             return null;
    108         }
    109 
    110         @Override
    111         public Bundle getAccountRemovalAllowed(AccountAuthenticatorResponse response,
    112                 Account account) throws NetworkErrorException {
    113             Bundle result = new Bundle();
    114             result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true);
    115             return result;
    116         }
    117 
    118         private void removeAccounts() {
    119             AccountManager accountManager = getSystemService(AccountManager.class);
    120             for (Account account : accountManager.getAccounts()) {
    121                 accountManager.removeAccountExplicitly(account);
    122             }
    123         }
    124     }
    125 }
    126