Reconcile all types of local accounts in MailService

Bug: 7078247

Change-Id: I43eb5b8a85e40217893bdc08cd839450358d07ea
This commit is contained in:
Marc Blank 2012-09-03 11:46:10 -07:00
parent b82ae909d7
commit f53490dc86
4 changed files with 48 additions and 104 deletions

View File

@ -1,61 +0,0 @@
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.email;
import android.util.Log;
import com.android.emailcommon.Logging;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* Base class for a task that runs at most one instance at any given moment.
*
* Call {@link #run} to start the task. If the task is already running on another thread, it'll do
* nothing.
*/
public abstract class SingleRunningTask<Param> {
private final AtomicBoolean mIsRunning = new AtomicBoolean(false);
private final String mLogTaskName;
public SingleRunningTask(String logTaskName) {
mLogTaskName = logTaskName;
}
/**
* Calls {@link #runInternal} if it's not running already.
*/
public final void run(Param param) {
if (mIsRunning.compareAndSet(false, true)) {
Log.i(Logging.LOG_TAG, mLogTaskName + ": start");
try {
runInternal(param);
} finally {
mIsRunning.set(false);
}
}
}
/**
* The actual task must be implemented by subclasses.
*/
protected abstract void runInternal(Param param);
/* package */ boolean isRunningForTest() {
return mIsRunning.get();
}
}

View File

@ -33,12 +33,15 @@ import com.android.email.NotificationController;
import com.android.email.Preferences;
import com.android.email.SecurityPolicy;
import com.android.email.activity.setup.AccountSettings;
import com.android.email.service.EmailServiceUtils.EmailServiceInfo;
import com.android.emailcommon.Logging;
import com.android.emailcommon.VendorPolicyLoader;
import com.android.emailcommon.provider.Account;
import com.android.emailcommon.provider.EmailContent.AccountColumns;
import com.android.emailcommon.provider.HostAuth;
import java.util.List;
/**
* The service that really handles broadcast intents on a worker thread.
*
@ -129,7 +132,12 @@ public class EmailBroadcastProcessorService extends IntentService {
*/
private void onBootCompleted() {
performOneTimeInitialization();
reconcileAndStartServices();
}
private void reconcileAndStartServices() {
// Reconcile accounts
MailService.reconcileLocalAccountsSync(this);
// Starts remote services, if any
EmailServiceUtils.startRemoteServices(this);
}
@ -203,9 +211,6 @@ public class EmailBroadcastProcessorService extends IntentService {
private void onSystemAccountChanged() {
Log.i(Logging.LOG_TAG, "System accounts updated.");
MailService.reconcilePopAccountsSync(this);
// Start any remote services
EmailServiceUtils.startRemoteServices(this);
reconcileAndStartServices();
}
}

View File

@ -450,7 +450,7 @@ public abstract class EmailServiceStub extends IEmailService.Stub implements IEm
@Override
public void deleteAccountPIMData(long accountId) throws RemoteException {
MailService.reconcilePopAccountsSync(mContext);
MailService.reconcileLocalAccountsSync(mContext);
}
@Override

View File

@ -25,9 +25,8 @@ import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import com.android.email.R;
import com.android.email.SingleRunningTask;
import com.android.email.provider.AccountReconciler;
import com.android.email.service.EmailServiceUtils.EmailServiceInfo;
import com.android.email2.ui.MailActivityEmail;
@ -47,22 +46,10 @@ public class MailService extends Service {
@Override
public int onStartCommand(final Intent intent, int flags, final int startId) {
super.onStartCommand(intent, flags, startId);
EmailAsyncTask.runAsyncParallel(new Runnable() {
@Override
public void run() {
reconcilePopAccountsSync(MailService.this);
}
});
reconcileLocalAccountsSync(this);
// Make sure our services are running, if necessary
MailActivityEmail.setServicesEnabledAsync(this);
// Returning START_NOT_STICKY means that if a mail check is killed (e.g. due to memory
// pressure, there will be no explicit restart. This is OK; Note that we set a watchdog
// alarm before each mailbox check. If the mailbox check never completes, the watchdog
// will fire and get things running again.
return START_NOT_STICKY;
return START_STICKY;
}
@Override
@ -70,17 +57,14 @@ public class MailService extends Service {
return null;
}
public static ArrayList<Account> getPopAccountList(Context context) {
public static ArrayList<Account> getAccountList(Context context, String protocol) {
ArrayList<Account> providerAccounts = new ArrayList<Account>();
Cursor c = context.getContentResolver().query(Account.CONTENT_URI, Account.ID_PROJECTION,
null, null, null);
try {
while (c.moveToNext()) {
long accountId = c.getLong(Account.CONTENT_ID_COLUMN);
String protocol = Account.getProtocol(context, accountId);
EmailServiceInfo info = EmailServiceUtils.getServiceInfo(context, protocol);
if ((info != null) && info.accountType.equals(
context.getString(R.string.account_manager_type_pop3))) {
if (protocol.equals(Account.getProtocol(context, accountId))) {
Account account = Account.restoreAccountWithId(context, accountId);
if (account != null) {
providerAccounts.add(account);
@ -93,25 +77,41 @@ public class MailService extends Service {
return providerAccounts;
}
private static final SingleRunningTask<Context> sReconcilePopAccountsSyncExecutor =
new SingleRunningTask<Context>("ReconcilePopImapAccountsSync") {
@Override
protected void runInternal(Context context) {
android.accounts.Account[] accountManagerAccounts = AccountManager.get(context)
.getAccountsByType(
context.getString(R.string.account_manager_type_pop3));
ArrayList<Account> providerAccounts = getPopAccountList(context);
MailService.reconcileAccountsWithAccountManager(context, providerAccounts,
accountManagerAccounts, context);
}
};
/**
* Reconcile POP/IMAP accounts.
* Reconcile local (i.e. non-remote) accounts.
*/
public static void reconcilePopAccountsSync(Context context) {
sReconcilePopAccountsSyncExecutor.run(context);
public static void reconcileLocalAccountsSync(Context context) {
List<EmailServiceInfo> serviceList = EmailServiceUtils.getServiceInfoList(context);
for (EmailServiceInfo info: serviceList) {
if (info.klass != null) {
new AccountReconcilerTask(context, info).runAsync();
}
}
}
static class AccountReconcilerTask implements Runnable {
private final Context mContext;
private final EmailServiceInfo mInfo;
AccountReconcilerTask(Context context, EmailServiceInfo info) {
mContext = context;
mInfo = info;
}
public void runAsync() {
EmailAsyncTask.runAsyncSerial(this);
}
@Override
public void run() {
Log.d("MailService", "Reconciling accounts of type " + mInfo.accountType +
", protocol " + mInfo.protocol);
android.accounts.Account[] accountManagerAccounts = AccountManager.get(mContext)
.getAccountsByType(mInfo.accountType);
ArrayList<Account> providerAccounts = getAccountList(mContext, mInfo.protocol);
reconcileAccountsWithAccountManager(mContext, providerAccounts,
accountManagerAccounts, mContext);
}
}
/**