Home | History | Annotate | Download | only in service
      1 /*
      2  * Copyright (C) 2011 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.email.service;
     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 import com.android.email.setup.AuthenticatorSetupIntentHelper;
     31 
     32 /**
     33  * Anauthenticator service for reconciliation tests; it simply adds the account to AccountManager
     34  * directly with a username and password.
     35  */
     36 public class EasTestAuthenticatorService extends Service {
     37     public static final String OPTIONS_USERNAME = "username";
     38     public static final String OPTIONS_PASSWORD = "password";
     39     private static final String TEST_ACCOUNT_TYPE = "com.android.test_exchange";
     40 
     41     class EasAuthenticator extends AbstractAccountAuthenticator {
     42 
     43         public EasAuthenticator(Context context) {
     44             super(context);
     45         }
     46 
     47         @Override
     48         public Bundle addAccount(AccountAuthenticatorResponse response, String accountType,
     49                 String authTokenType, String[] requiredFeatures, Bundle options)
     50                 throws NetworkErrorException {
     51             // There are two cases here:
     52             // 1) We are called with a username/password; this comes from the traditional email
     53             //    app UI; we simply create the account and return the proper bundle
     54             if (options != null && options.containsKey(OPTIONS_PASSWORD)
     55                     && options.containsKey(OPTIONS_USERNAME)) {
     56                 final Account account = new Account(options.getString(OPTIONS_USERNAME),
     57                         TEST_ACCOUNT_TYPE);
     58                 AccountManager.get(EasTestAuthenticatorService.this).addAccountExplicitly(
     59                             account, options.getString(OPTIONS_PASSWORD), null);
     60 
     61                 Bundle b = new Bundle();
     62                 b.putString(AccountManager.KEY_ACCOUNT_NAME, TEST_ACCOUNT_TYPE);
     63                 return b;
     64             // 2) The other case is that we're creating a new account from an Account manager
     65             //    activity.  In this case, we add an intent that will be used to gather the
     66             //    account information...
     67             } else {
     68                 Bundle b = new Bundle();
     69                 Intent intent =
     70                         AuthenticatorSetupIntentHelper.actionGetCreateAccountIntent(
     71                                 EasTestAuthenticatorService.this, accountType);
     72                 intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
     73                 b.putParcelable(AccountManager.KEY_INTENT, intent);
     74                 return b;
     75             }
     76         }
     77 
     78         @Override
     79         public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account,
     80                 Bundle options) {
     81             return null;
     82         }
     83 
     84         @Override
     85         public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
     86             return null;
     87         }
     88 
     89         @Override
     90         public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account,
     91                 String authTokenType, Bundle loginOptions) throws NetworkErrorException {
     92             return null;
     93         }
     94 
     95         @Override
     96         public String getAuthTokenLabel(String authTokenType) {
     97             // null means we don't have compartmentalized authtoken types
     98             return null;
     99         }
    100 
    101         @Override
    102         public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account,
    103                 String[] features) throws NetworkErrorException {
    104             return null;
    105         }
    106 
    107         @Override
    108         public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account,
    109                 String authTokenType, Bundle loginOptions) {
    110             return null;
    111         }
    112 
    113     }
    114 
    115     @Override
    116     public IBinder onBind(Intent intent) {
    117         if (AccountManager.ACTION_AUTHENTICATOR_INTENT.equals(intent.getAction())) {
    118             return new EasAuthenticator(this).getIBinder();
    119         } else {
    120             return null;
    121         }
    122     }
    123 }
    124