MailService.java revision f53490dc86f5a61ab09024dc4938ce8419c61fc5
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; 28import android.util.Log; 29 30import com.android.email.provider.AccountReconciler; 31import com.android.email.service.EmailServiceUtils.EmailServiceInfo; 32import com.android.email2.ui.MailActivityEmail; 33import com.android.emailcommon.provider.Account; 34import com.android.emailcommon.provider.HostAuth; 35import com.android.emailcommon.utility.EmailAsyncTask; 36import com.google.common.annotations.VisibleForTesting; 37 38import java.util.ArrayList; 39import java.util.List; 40 41/** 42 * Legacy service, now used mainly for account reconciliation 43 */ 44public class MailService extends Service { 45 46 @Override 47 public int onStartCommand(final Intent intent, int flags, final int startId) { 48 super.onStartCommand(intent, flags, startId); 49 reconcileLocalAccountsSync(this); 50 // Make sure our services are running, if necessary 51 MailActivityEmail.setServicesEnabledAsync(this); 52 return START_STICKY; 53 } 54 55 @Override 56 public IBinder onBind(Intent intent) { 57 return null; 58 } 59 60 public static ArrayList<Account> getAccountList(Context context, String protocol) { 61 ArrayList<Account> providerAccounts = new ArrayList<Account>(); 62 Cursor c = context.getContentResolver().query(Account.CONTENT_URI, Account.ID_PROJECTION, 63 null, null, null); 64 try { 65 while (c.moveToNext()) { 66 long accountId = c.getLong(Account.CONTENT_ID_COLUMN); 67 if (protocol.equals(Account.getProtocol(context, accountId))) { 68 Account account = Account.restoreAccountWithId(context, accountId); 69 if (account != null) { 70 providerAccounts.add(account); 71 } 72 } 73 } 74 } finally { 75 c.close(); 76 } 77 return providerAccounts; 78 } 79 80 /** 81 * Reconcile local (i.e. non-remote) accounts. 82 */ 83 public static void reconcileLocalAccountsSync(Context context) { 84 List<EmailServiceInfo> serviceList = EmailServiceUtils.getServiceInfoList(context); 85 for (EmailServiceInfo info: serviceList) { 86 if (info.klass != null) { 87 new AccountReconcilerTask(context, info).runAsync(); 88 } 89 } 90 } 91 92 static class AccountReconcilerTask implements Runnable { 93 private final Context mContext; 94 private final EmailServiceInfo mInfo; 95 96 AccountReconcilerTask(Context context, EmailServiceInfo info) { 97 mContext = context; 98 mInfo = info; 99 } 100 101 public void runAsync() { 102 EmailAsyncTask.runAsyncSerial(this); 103 } 104 105 @Override 106 public void run() { 107 Log.d("MailService", "Reconciling accounts of type " + mInfo.accountType + 108 ", protocol " + mInfo.protocol); 109 android.accounts.Account[] accountManagerAccounts = AccountManager.get(mContext) 110 .getAccountsByType(mInfo.accountType); 111 ArrayList<Account> providerAccounts = getAccountList(mContext, mInfo.protocol); 112 reconcileAccountsWithAccountManager(mContext, providerAccounts, 113 accountManagerAccounts, mContext); 114 } 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