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.activity.setup.AccountSetupBasics;
     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                     AccountSetupBasics.actionSetupExchangeIntent(EasTestAuthenticatorService.this);
     71                 intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
     72                 b.putParcelable(AccountManager.KEY_INTENT, intent);
     73                 return b;
     74             }
     75         }
     76 
     77         @Override
     78         public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account,
     79                 Bundle options) {
     80             return null;
     81         }
     82 
     83         @Override
     84         public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
     85             return null;
     86         }
     87 
     88         @Override
     89         public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account,
     90                 String authTokenType, Bundle loginOptions) throws NetworkErrorException {
     91             return null;
     92         }
     93 
     94         @Override
     95         public String getAuthTokenLabel(String authTokenType) {
     96             // null means we don't have compartmentalized authtoken types
     97             return null;
     98         }
     99 
    100         @Override
    101         public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account,
    102                 String[] features) throws NetworkErrorException {
    103             return null;
    104         }
    105 
    106         @Override
    107         public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account,
    108                 String authTokenType, Bundle loginOptions) {
    109             return null;
    110         }
    111 
    112     }
    113 
    114     @Override
    115     public IBinder onBind(Intent intent) {
    116         if (AccountManager.ACTION_AUTHENTICATOR_INTENT.equals(intent.getAction())) {
    117             return new EasAuthenticator(this).getIBinder();
    118         } else {
    119             return null;
    120         }
    121     }
    122 }
    123