MailService.java revision e714bb9d153cfe13a7f0932e7d67ea08fa5a1d98
1/* 2 * Copyright (C) 2008 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 17package com.android.email.service; 18 19import android.accounts.AccountManager; 20import android.accounts.AccountManagerCallback; 21import android.accounts.AccountManagerFuture; 22import android.app.Service; 23import android.content.Context; 24import android.content.Intent; 25import android.database.Cursor; 26import android.os.Bundle; 27import android.os.IBinder; 28 29import com.android.email.R; 30import com.android.email.SingleRunningTask; 31import com.android.email.provider.AccountReconciler; 32import com.android.email.service.EmailServiceUtils.EmailServiceInfo; 33import com.android.email2.ui.MailActivityEmail; 34import com.android.emailcommon.provider.Account; 35import com.android.emailcommon.provider.HostAuth; 36import com.android.emailcommon.utility.EmailAsyncTask; 37import com.google.common.annotations.VisibleForTesting; 38 39import java.util.ArrayList; 40import java.util.List; 41 42/** 43 * Legacy service, now used mainly for account reconciliation 44 */ 45public class MailService extends Service { 46 47 @Override 48 public int onStartCommand(final Intent intent, int flags, final int startId) { 49 super.onStartCommand(intent, flags, startId); 50 51 EmailAsyncTask.runAsyncParallel(new Runnable() { 52 @Override 53 public void run() { 54 reconcilePopAccountsSync(MailService.this); 55 } 56 }); 57 58 // Make sure our services are running, if necessary 59 MailActivityEmail.setServicesEnabledAsync(this); 60 61 // Returning START_NOT_STICKY means that if a mail check is killed (e.g. due to memory 62 // pressure, there will be no explicit restart. This is OK; Note that we set a watchdog 63 // alarm before each mailbox check. If the mailbox check never completes, the watchdog 64 // will fire and get things running again. 65 return START_NOT_STICKY; 66 } 67 68 @Override 69 public IBinder onBind(Intent intent) { 70 return null; 71 } 72 73 public static ArrayList<Account> getPopAccountList(Context context) { 74 ArrayList<Account> providerAccounts = new ArrayList<Account>(); 75 Cursor c = context.getContentResolver().query(Account.CONTENT_URI, Account.ID_PROJECTION, 76 null, null, null); 77 try { 78 while (c.moveToNext()) { 79 long accountId = c.getLong(Account.CONTENT_ID_COLUMN); 80 String protocol = Account.getProtocol(context, accountId); 81 EmailServiceInfo info = EmailServiceUtils.getServiceInfo(context, protocol); 82 if ((info != null) && info.accountType.equals( 83 context.getString(R.string.account_manager_type_pop3))) { 84 Account account = Account.restoreAccountWithId(context, accountId); 85 if (account != null) { 86 providerAccounts.add(account); 87 } 88 } 89 } 90 } finally { 91 c.close(); 92 } 93 return providerAccounts; 94 } 95 96 private static final SingleRunningTask<Context> sReconcilePopAccountsSyncExecutor = 97 new SingleRunningTask<Context>("ReconcilePopImapAccountsSync") { 98 @Override 99 protected void runInternal(Context context) { 100 android.accounts.Account[] accountManagerAccounts = AccountManager.get(context) 101 .getAccountsByType( 102 context.getString(R.string.account_manager_type_pop3)); 103 ArrayList<Account> providerAccounts = getPopAccountList(context); 104 MailService.reconcileAccountsWithAccountManager(context, providerAccounts, 105 accountManagerAccounts, context); 106 107 } 108 }; 109 110 /** 111 * Reconcile POP/IMAP accounts. 112 */ 113 public static void reconcilePopAccountsSync(Context context) { 114 sReconcilePopAccountsSyncExecutor.run(context); 115 } 116 117 /** 118 * See Utility.reconcileAccounts for details 119 * @param context The context in which to operate 120 * @param emailProviderAccounts the exchange provider accounts to work from 121 * @param accountManagerAccounts The account manager accounts to work from 122 * @param providerContext the provider's context (in unit tests, this may differ from context) 123 */ 124 @VisibleForTesting 125 public static void reconcileAccountsWithAccountManager(Context context, 126 List<Account> emailProviderAccounts, android.accounts.Account[] accountManagerAccounts, 127 Context providerContext) { 128 AccountReconciler.reconcileAccounts(context, emailProviderAccounts, accountManagerAccounts, 129 providerContext); 130 } 131 132 public static AccountManagerFuture<Bundle> setupAccountManagerAccount(Context context, 133 Account account, boolean email, boolean calendar, boolean contacts, 134 AccountManagerCallback<Bundle> callback) { 135 Bundle options = new Bundle(); 136 HostAuth hostAuthRecv = HostAuth.restoreHostAuthWithId(context, account.mHostAuthKeyRecv); 137 if (hostAuthRecv == null) { 138 return null; 139 } 140 // Set up username/password 141 options.putString(EasAuthenticatorService.OPTIONS_USERNAME, account.mEmailAddress); 142 options.putString(EasAuthenticatorService.OPTIONS_PASSWORD, hostAuthRecv.mPassword); 143 options.putBoolean(EasAuthenticatorService.OPTIONS_CONTACTS_SYNC_ENABLED, contacts); 144 options.putBoolean(EasAuthenticatorService.OPTIONS_CALENDAR_SYNC_ENABLED, calendar); 145 options.putBoolean(EasAuthenticatorService.OPTIONS_EMAIL_SYNC_ENABLED, email); 146 EmailServiceInfo info = EmailServiceUtils.getServiceInfo(context, hostAuthRecv.mProtocol); 147 return AccountManager.get(context).addAccount(info.accountType, null, null, options, null, 148 callback, null); 149 } 150} 151