From 9fe51f632965f5d085ae45a1089c7c97dcec8881 Mon Sep 17 00:00:00 2001 From: Makoto Onuki Date: Tue, 2 Feb 2010 11:17:48 -0800 Subject: [PATCH] Gather common exchange related methods into one place So that it'll be easier to remove exchange dependency. --- .../android/email/AccountBackupRestore.java | 4 +- src/com/android/email/Controller.java | 7 +-- src/com/android/email/ExchangeUtils.java | 50 +++++++++++++++++++ src/com/android/email/activity/Welcome.java | 4 +- .../activity/setup/AccountSetupExchange.java | 5 +- .../activity/setup/AccountSetupOptions.java | 3 +- .../email/mail/store/ExchangeStore.java | 8 ++- .../email/service/EmailServiceProxy.java | 19 +++---- src/com/android/exchange/BootReceiver.java | 4 +- 9 files changed, 72 insertions(+), 32 deletions(-) create mode 100644 src/com/android/email/ExchangeUtils.java diff --git a/src/com/android/email/AccountBackupRestore.java b/src/com/android/email/AccountBackupRestore.java index 0d2810cf9..64324d72e 100644 --- a/src/com/android/email/AccountBackupRestore.java +++ b/src/com/android/email/AccountBackupRestore.java @@ -23,7 +23,6 @@ import com.android.exchange.Eas; import android.accounts.AccountManagerFuture; import android.content.ContentResolver; import android.content.Context; -import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.provider.Calendar; @@ -66,8 +65,7 @@ public class AccountBackupRestore { // after restoring accounts, register services appropriately Log.w(Email.LOG_TAG, "Register services after restoring accounts"); Email.setServicesEnabled(context); - context.startService(new Intent(context.getApplicationContext(), - com.android.exchange.SyncManager.class)); + ExchangeUtils.startExchangeService(context); } } diff --git a/src/com/android/email/Controller.java b/src/com/android/email/Controller.java index 9fcdd80a9..740ed0802 100644 --- a/src/com/android/email/Controller.java +++ b/src/com/android/email/Controller.java @@ -27,11 +27,9 @@ import com.android.email.provider.EmailContent.Mailbox; import com.android.email.provider.EmailContent.MailboxColumns; import com.android.email.provider.EmailContent.Message; import com.android.email.provider.EmailContent.MessageColumns; -import com.android.email.service.EmailServiceProxy; import com.android.email.service.EmailServiceStatus; import com.android.email.service.IEmailService; import com.android.email.service.IEmailServiceCallback; -import com.android.exchange.SyncManager; import android.content.ContentResolver; import android.content.ContentUris; @@ -136,8 +134,7 @@ public class Controller { * Generally this should be called by anybody who changes Email.DEBUG */ public void serviceLogging(int debugEnabled) { - IEmailService service = - new EmailServiceProxy(mContext, SyncManager.class, mServiceCallback); + IEmailService service = ExchangeUtils.getExchangeEmailService(mContext, mServiceCallback); try { service.setLogging(debugEnabled); } catch (RemoteException e) { @@ -787,7 +784,7 @@ public class Controller { if (account == null || isMessagingController(account)) { return null; } else { - return new EmailServiceProxy(mContext, SyncManager.class, mServiceCallback); + return ExchangeUtils.getExchangeEmailService(mContext, mServiceCallback); } } diff --git a/src/com/android/email/ExchangeUtils.java b/src/com/android/email/ExchangeUtils.java new file mode 100644 index 000000000..953c66fc4 --- /dev/null +++ b/src/com/android/email/ExchangeUtils.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2010 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 com.android.email.service.EmailServiceProxy; +import com.android.email.service.IEmailService; +import com.android.email.service.IEmailServiceCallback; +import com.android.exchange.SyncManager; + +import android.content.Context; +import android.content.Intent; + +/** + * Utility functions for Exchange support. + */ +public class ExchangeUtils { + /** + * Starts the service for Exchange, if supported. + */ + public static void startExchangeService(Context context) { + context.startService(new Intent(context, SyncManager.class)); + } + + /** + * Returns an {@link IEmailService} for the Exchange service, if supported. Otherwise it'll + * return an empty {@link IEmailService} implementation. + * + * @param context + * @param callback Object to get callback, or can be null + */ + public static IEmailService getExchangeEmailService(Context context, + IEmailServiceCallback callback) { + // TODO Return an empty IEmailService impl if exchange support is removed + return new EmailServiceProxy(context, SyncManager.class, callback); + } +} diff --git a/src/com/android/email/activity/Welcome.java b/src/com/android/email/activity/Welcome.java index 977b060f6..137b2a569 100644 --- a/src/com/android/email/activity/Welcome.java +++ b/src/com/android/email/activity/Welcome.java @@ -17,10 +17,10 @@ package com.android.email.activity; import com.android.email.AccountBackupRestore; +import com.android.email.ExchangeUtils; import com.android.email.activity.setup.AccountSetupBasics; import com.android.email.provider.EmailContent.Account; import com.android.email.provider.EmailContent.Mailbox; -import com.android.exchange.SyncManager; import android.app.Activity; import android.content.Intent; @@ -59,7 +59,7 @@ public class Welcome extends Activity { // SyncManager gets a chance to start. There is no harm to starting it if it has already // been started // TODO More completely separate SyncManager from Email app - startService(new Intent(this, SyncManager.class)); + ExchangeUtils.startExchangeService(this); // Find out how many accounts we have, and if there's just one, go directly to it Cursor c = null; diff --git a/src/com/android/email/activity/setup/AccountSetupExchange.java b/src/com/android/email/activity/setup/AccountSetupExchange.java index 60d00d864..2dd390415 100644 --- a/src/com/android/email/activity/setup/AccountSetupExchange.java +++ b/src/com/android/email/activity/setup/AccountSetupExchange.java @@ -17,13 +17,12 @@ package com.android.email.activity.setup; import com.android.email.AccountBackupRestore; +import com.android.email.ExchangeUtils; import com.android.email.R; import com.android.email.Utility; import com.android.email.provider.EmailContent; import com.android.email.provider.EmailContent.Account; import com.android.email.provider.EmailContent.HostAuth; -import com.android.email.service.EmailServiceProxy; -import com.android.exchange.SyncManager; import android.app.Activity; import android.app.AlertDialog; @@ -308,7 +307,7 @@ public class AccountSetupExchange extends Activity implements OnClickListener, if (mAccount.mHostAuthRecv.mProtocol.equals("eas")) { // For EAS, notify SyncManager that the password has changed try { - new EmailServiceProxy(this, SyncManager.class) + ExchangeUtils.getExchangeEmailService(this, null) .hostChanged(mAccount.mId); } catch (RemoteException e) { // Nothing to be done if this fails diff --git a/src/com/android/email/activity/setup/AccountSetupOptions.java b/src/com/android/email/activity/setup/AccountSetupOptions.java index 7dfd8b858..d395dc850 100644 --- a/src/com/android/email/activity/setup/AccountSetupOptions.java +++ b/src/com/android/email/activity/setup/AccountSetupOptions.java @@ -17,6 +17,7 @@ package com.android.email.activity.setup; import com.android.email.Email; +import com.android.email.ExchangeUtils; import com.android.email.R; import com.android.email.mail.Store; import com.android.email.mail.store.ExchangeStore; @@ -186,7 +187,7 @@ public class AccountSetupOptions extends Activity implements OnClickListener { Email.setServicesEnabled(this); AccountSetupNames.actionSetNames(this, mAccount.mId, mEasFlowMode); // Start up SyncManager (if it isn't already running) - startService(new Intent(getApplicationContext(), com.android.exchange.SyncManager.class)); + ExchangeUtils.startExchangeService(this); finish(); } diff --git a/src/com/android/email/mail/store/ExchangeStore.java b/src/com/android/email/mail/store/ExchangeStore.java index 514e9a653..0f01e3192 100644 --- a/src/com/android/email/mail/store/ExchangeStore.java +++ b/src/com/android/email/mail/store/ExchangeStore.java @@ -17,6 +17,7 @@ package com.android.email.mail.store; import com.android.email.Email; +import com.android.email.ExchangeUtils; import com.android.email.mail.AuthenticationFailedException; import com.android.email.mail.Folder; import com.android.email.mail.MessagingException; @@ -24,9 +25,7 @@ import com.android.email.mail.Store; import com.android.email.mail.StoreSynchronizer; import com.android.email.provider.EmailContent.Account; import com.android.email.service.EasAuthenticatorService; -import com.android.email.service.EmailServiceProxy; import com.android.exchange.Eas; -import com.android.exchange.SyncManager; import android.accounts.AccountManager; import android.accounts.AccountManagerCallback; @@ -240,7 +239,7 @@ public class ExchangeStore extends Store { boolean tssl = uri.getScheme().contains("+trustallcerts"); try { int port = ssl ? 443 : 80; - int result = new EmailServiceProxy(mContext, SyncManager.class) + int result = ExchangeUtils.getExchangeEmailService(mContext, null) .validate("eas", mHost, mUsername, mPassword, port, ssl, tssl); if (result != MessagingException.NO_ERROR) { if (result == MessagingException.AUTHENTICATION_FAILED) { @@ -263,11 +262,10 @@ public class ExchangeStore extends Store { public Bundle autoDiscover(Context context, String username, String password) throws MessagingException { try { - return new EmailServiceProxy(context, SyncManager.class) + return ExchangeUtils.getExchangeEmailService(context, null) .autoDiscover(username, password); } catch (RemoteException e) { return null; } } } - diff --git a/src/com/android/email/service/EmailServiceProxy.java b/src/com/android/email/service/EmailServiceProxy.java index 98186cfd3..d8793c735 100644 --- a/src/com/android/email/service/EmailServiceProxy.java +++ b/src/com/android/email/service/EmailServiceProxy.java @@ -50,9 +50,9 @@ public class EmailServiceProxy implements IEmailService { public static final String AUTO_DISCOVER_BUNDLE_ERROR_CODE = "autodiscover_error_code"; public static final String AUTO_DISCOVER_BUNDLE_HOST_AUTH = "autodiscover_host_auth"; - private Context mContext; - private Class mClass; - private IEmailServiceCallback mCallback; + private final Context mContext; + private final Class mClass; + private final IEmailServiceCallback mCallback; private Runnable mRunnable; private ServiceConnection mSyncManagerConnection = new EmailServiceConnection (); private IEmailService mService = null; @@ -61,21 +61,16 @@ public class EmailServiceProxy implements IEmailService { private boolean mDead = false; public EmailServiceProxy(Context _context, Class _class) { - mContext = _context; - mClass = _class; - // Proxy calls have a timeout, and this can cause failures while debugging due to the - // far slower execution speed. In particular, validate calls fail regularly with ssl - // connections at the default timeout (30 seconds) - if (Debug.isDebuggerConnected()) { - mTimeout <<= 2; - } + this(_context, _class, null); } public EmailServiceProxy(Context _context, Class _class, IEmailServiceCallback _callback) { mContext = _context; mClass = _class; mCallback = _callback; - // See comment above + // Proxy calls have a timeout, and this can cause failures while debugging due to the + // far slower execution speed. In particular, validate calls fail regularly with ssl + // connections at the default timeout (30 seconds) if (Debug.isDebuggerConnected()) { mTimeout <<= 2; } diff --git a/src/com/android/exchange/BootReceiver.java b/src/com/android/exchange/BootReceiver.java index 5312bfb26..1ebfa7bf1 100644 --- a/src/com/android/exchange/BootReceiver.java +++ b/src/com/android/exchange/BootReceiver.java @@ -16,6 +16,8 @@ package com.android.exchange; +import com.android.email.ExchangeUtils; + import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -25,6 +27,6 @@ public class BootReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.d("Exchange", "BootReceiver onReceive"); - context.startService(new Intent(context, SyncManager.class)); + ExchangeUtils.startExchangeService(context); } }