• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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 com.android.email.activity.setup.AccountSetupBasics;
20 import com.android.emailcommon.AccountManagerTypes;
21 import com.android.emailcommon.CalendarProviderStub;
22 import com.android.emailcommon.provider.EmailContent;
23 
24 import android.accounts.AbstractAccountAuthenticator;
25 import android.accounts.Account;
26 import android.accounts.AccountAuthenticatorResponse;
27 import android.accounts.AccountManager;
28 import android.accounts.NetworkErrorException;
29 import android.app.Service;
30 import android.content.ContentResolver;
31 import android.content.Context;
32 import android.content.Intent;
33 import android.os.Bundle;
34 import android.os.IBinder;
35 import android.provider.ContactsContract;
36 
37 /**
38  * A very basic authenticator service for POP/IMAP.  At the moment, it has no UI hooks.  When called
39  * with addAccount, it simply adds the account to AccountManager directly with a username and
40  * password.
41  */
42 public class PopImapAuthenticatorService extends Service {
43     public static final String OPTIONS_USERNAME = "username";
44     public static final String OPTIONS_PASSWORD = "password";
45     public static final String OPTIONS_CONTACTS_SYNC_ENABLED = "contacts";
46     public static final String OPTIONS_CALENDAR_SYNC_ENABLED = "calendar";
47     public static final String OPTIONS_EMAIL_SYNC_ENABLED = "email";
48 
49     class PopImapAuthenticator extends AbstractAccountAuthenticator {
50 
PopImapAuthenticator(Context context)51         public PopImapAuthenticator(Context context) {
52             super(context);
53         }
54 
55         @Override
addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, String[] requiredFeatures, Bundle options)56         public Bundle addAccount(AccountAuthenticatorResponse response, String accountType,
57                 String authTokenType, String[] requiredFeatures, Bundle options)
58                 throws NetworkErrorException {
59             // There are two cases here:
60             // 1) We are called with a username/password; this comes from the traditional email
61             //    app UI; we simply create the account and return the proper bundle
62             if (options != null && options.containsKey(OPTIONS_PASSWORD)
63                     && options.containsKey(OPTIONS_USERNAME)) {
64                 final Account account = new Account(options.getString(OPTIONS_USERNAME),
65                         AccountManagerTypes.TYPE_POP_IMAP);
66                 AccountManager.get(PopImapAuthenticatorService.this).addAccountExplicitly(
67                             account, options.getString(OPTIONS_PASSWORD), null);
68 
69                 // Set up email syncing
70                 boolean syncEmail = false;
71                 if (options.containsKey(OPTIONS_EMAIL_SYNC_ENABLED) &&
72                         options.getBoolean(OPTIONS_EMAIL_SYNC_ENABLED)) {
73                     syncEmail = true;
74                 }
75                 ContentResolver.setIsSyncable(account, EmailContent.AUTHORITY, 1);
76                 ContentResolver.setSyncAutomatically(account, EmailContent.AUTHORITY, syncEmail);
77                 ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 0);
78                 ContentResolver.setIsSyncable(account, CalendarProviderStub.AUTHORITY, 0);
79 
80                 Bundle b = new Bundle();
81                 b.putString(AccountManager.KEY_ACCOUNT_NAME, options.getString(OPTIONS_USERNAME));
82                 b.putString(AccountManager.KEY_ACCOUNT_TYPE, AccountManagerTypes.TYPE_POP_IMAP);
83                 return b;
84             // 2) The other case is that we're creating a new account from an Account manager
85             //    activity.  In this case, we add an intent that will be used to gather the
86             //    account information...
87             } else {
88                 Bundle b = new Bundle();
89                 Intent intent =
90                     AccountSetupBasics.actionSetupPopImapIntent(PopImapAuthenticatorService.this);
91                 intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
92                 b.putParcelable(AccountManager.KEY_INTENT, intent);
93                 return b;
94             }
95         }
96 
97         @Override
confirmCredentials(AccountAuthenticatorResponse response, Account account, Bundle options)98         public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account,
99                 Bundle options) {
100             return null;
101         }
102 
103         @Override
editProperties(AccountAuthenticatorResponse response, String accountType)104         public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
105             return null;
106         }
107 
108         @Override
getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle loginOptions)109         public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account,
110                 String authTokenType, Bundle loginOptions) throws NetworkErrorException {
111             return null;
112         }
113 
114         @Override
getAuthTokenLabel(String authTokenType)115         public String getAuthTokenLabel(String authTokenType) {
116             // null means we don't have compartmentalized authtoken types
117             return null;
118         }
119 
120         @Override
hasFeatures(AccountAuthenticatorResponse response, Account account, String[] features)121         public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account,
122                 String[] features) throws NetworkErrorException {
123             return null;
124         }
125 
126         @Override
updateCredentials(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle loginOptions)127         public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account,
128                 String authTokenType, Bundle loginOptions) {
129             return null;
130         }
131 
132     }
133 
134     @Override
onBind(Intent intent)135     public IBinder onBind(Intent intent) {
136         if (AccountManager.ACTION_AUTHENTICATOR_INTENT.equals(intent.getAction())) {
137             return new PopImapAuthenticator(this).getIBinder();
138         } else {
139             return null;
140         }
141     }
142 }
143