From c5cc62b57002a578487b395bb8a4d506775505c1 Mon Sep 17 00:00:00 2001 From: cketti Date: Thu, 10 Nov 2011 05:05:05 +0100 Subject: [PATCH] Removed AutoSyncHelper and use API 5 methods directly --- src/com/fsck/k9/helper/AutoSyncHelper.java | 115 --------------------- src/com/fsck/k9/helper/AutoSyncSdk3.java | 39 ------- src/com/fsck/k9/helper/AutoSyncSdk4.java | 41 -------- src/com/fsck/k9/helper/AutoSyncSdk5.java | 18 ---- src/com/fsck/k9/helper/IAutoSync.java | 26 ----- src/com/fsck/k9/service/BootReceiver.java | 3 +- src/com/fsck/k9/service/MailService.java | 9 +- 7 files changed, 3 insertions(+), 248 deletions(-) delete mode 100644 src/com/fsck/k9/helper/AutoSyncHelper.java delete mode 100644 src/com/fsck/k9/helper/AutoSyncSdk3.java delete mode 100644 src/com/fsck/k9/helper/AutoSyncSdk4.java delete mode 100644 src/com/fsck/k9/helper/AutoSyncSdk5.java delete mode 100644 src/com/fsck/k9/helper/IAutoSync.java diff --git a/src/com/fsck/k9/helper/AutoSyncHelper.java b/src/com/fsck/k9/helper/AutoSyncHelper.java deleted file mode 100644 index 23e6b5907..000000000 --- a/src/com/fsck/k9/helper/AutoSyncHelper.java +++ /dev/null @@ -1,115 +0,0 @@ -package com.fsck.k9.helper; - -import com.fsck.k9.K9; -import android.os.Build; -import android.util.Log; - -/** - * Helper class to get the current state of the auto-sync setting. - */ -public class AutoSyncHelper { - /** - * False, if we never tried to load the class for this SDK version. - * True, otherwise. - * - * Note: if sAutoSync is null and sChecked is true, then an error occurred - * while loading the class for the SDK version we're running on. - */ - private static boolean sChecked = false; - - /** - * Instance of the SDK specific class that implements the IAutoSync - * interface. - */ - private static IAutoSync sAutoSync = null; - - /** - * String for the auto-sync changed Intent. This isn't currently exposed by the API - */ - public static String SYNC_CONN_STATUS_CHANGE = "com.android.sync.SYNC_CONN_STATUS_CHANGED"; - /** - * Try loading the class that implements IAutoSync for this SDK version. - * - * @return the IAutoSync object for this SDK version, or null if something - * went wrong. - */ - private static IAutoSync loadAutoSync() { - /* - * We're trying to load the class for this SDK version. If anything - * goes wrong after this point, we don't want to try again. - */ - sChecked = true; - - /* - * Check the version of the SDK we are running on. Choose an - * implementation class designed for that version of the SDK. - */ - int sdkVersion = Integer.parseInt(Build.VERSION.SDK); - - String className = null; - if (sdkVersion == Build.VERSION_CODES.CUPCAKE) { - className = "com.fsck.k9.helper.AutoSyncSdk3"; - } else if (sdkVersion == Build.VERSION_CODES.DONUT) { - className = "com.fsck.k9.helper.AutoSyncSdk4"; - } else if (sdkVersion >= Build.VERSION_CODES.ECLAIR) { - className = "com.fsck.k9.helper.AutoSyncSdk5"; - } - - /* - * Find the required class by name and instantiate it. - */ - try { - Class clazz = - Class.forName(className).asSubclass(IAutoSync.class); - - IAutoSync autoSync = clazz.newInstance(); - autoSync.initialize(K9.app); - - return autoSync; - } catch (ClassNotFoundException e) { - Log.e(K9.LOG_TAG, "Couldn't find class: " + className, e); - } catch (InstantiationException e) { - Log.e(K9.LOG_TAG, "Couldn't instantiate class: " + className, e); - } catch (IllegalAccessException e) { - Log.e(K9.LOG_TAG, "Couldn't access class: " + className, e); - } catch (NoSuchMethodException e) { - if (K9.DEBUG) { - Log.d(K9.LOG_TAG, "Couldn't load method to get auto-sync state", e); - } - } - return null; - } - - /** - * Checks whether we can query the auto-sync state using - * getMasterSyncAutomatically() or not. - * - * @return true, if calls to getMasterSyncAutomatically() will return the - * state of the auto-sync setting. false, otherwise. - */ - public static boolean isAvailable() { - if (!sChecked) { - sAutoSync = loadAutoSync(); - } - return (sAutoSync != null); - } - - /** - * Query the state of the auto-sync setting. - * - * @return the state of the auto-sync setting. - * @see IAutoSync - */ - public static boolean getMasterSyncAutomatically() { - if (!sChecked) { - sAutoSync = loadAutoSync(); - } - - if (sAutoSync == null) { - throw new RuntimeException( - "Called getMasterSyncAutomatically() before checking if it's available."); - } - - return sAutoSync.getMasterSyncAutomatically(); - } -} diff --git a/src/com/fsck/k9/helper/AutoSyncSdk3.java b/src/com/fsck/k9/helper/AutoSyncSdk3.java deleted file mode 100644 index 7fc636199..000000000 --- a/src/com/fsck/k9/helper/AutoSyncSdk3.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.fsck.k9.helper; - -import java.lang.reflect.Constructor; -import java.lang.reflect.Method; -import android.content.ContentResolver; -import android.content.Context; -import android.os.Handler; - -public class AutoSyncSdk3 implements IAutoSync { - private Method mGetListenForNetworkTickles; - private Object mQueryMap; - - public void initialize(Context context) throws NoSuchMethodException { - /* - * There's no documented/official way to query the state of the - * auto-sync setting for a normal application in SDK 1.5/API 3. - * - * We use reflection to get an Sync.Settings.QueryMap" object, so we - * can call its getListenForNetworkTickles() method. This will return - * the current auto-sync state. - */ - try { - Class clazz = Class.forName("android.provider.Sync$Settings$QueryMap"); - Constructor c = clazz.getConstructor(ContentResolver.class, boolean.class, Handler.class); - mQueryMap = c.newInstance(context.getContentResolver(), true, null); - mGetListenForNetworkTickles = mQueryMap.getClass().getMethod("getListenForNetworkTickles"); - } catch (Exception e) { - throw new NoSuchMethodException(); - } - } - - public boolean getMasterSyncAutomatically() { - try { - return (Boolean) mGetListenForNetworkTickles.invoke(mQueryMap); - } catch (Exception e) { - return false; - } - } -} diff --git a/src/com/fsck/k9/helper/AutoSyncSdk4.java b/src/com/fsck/k9/helper/AutoSyncSdk4.java deleted file mode 100644 index d5c9ac89b..000000000 --- a/src/com/fsck/k9/helper/AutoSyncSdk4.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.fsck.k9.helper; - -import java.lang.reflect.Method; - -import com.fsck.k9.K9; - -import android.content.ContentResolver; -import android.content.Context; -import android.util.Log; - -public class AutoSyncSdk4 implements IAutoSync { - private Method mGetListenForNetworkTickles; - private Object mContentService; - - public void initialize(Context context) throws NoSuchMethodException { - /* - * There's no documented/official way to query the state of the - * auto-sync setting for a normal application in SDK 1.6/API 4. - * - * We use reflection to get an ContentService object, so we can call its - * getListenForNetworkTickles() method. This will return the current - * auto-sync state. - */ - try { - Method getContentService = ContentResolver.class.getMethod("getContentService"); - mContentService = getContentService.invoke(null); - mGetListenForNetworkTickles = mContentService.getClass().getMethod("getListenForNetworkTickles"); - } catch (Exception e) { - throw new NoSuchMethodException(); - } - } - - public boolean getMasterSyncAutomatically() { - try { - return (Boolean) mGetListenForNetworkTickles.invoke(mContentService); - } catch (Exception e) { - Log.e(K9.LOG_TAG, "Could not query for network tickle", e); - return true; - } - } -} diff --git a/src/com/fsck/k9/helper/AutoSyncSdk5.java b/src/com/fsck/k9/helper/AutoSyncSdk5.java deleted file mode 100644 index d6e6d684d..000000000 --- a/src/com/fsck/k9/helper/AutoSyncSdk5.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.fsck.k9.helper; - -import android.content.ContentResolver; -import android.content.Context; - -public class AutoSyncSdk5 implements IAutoSync { - public void initialize(Context context) throws NoSuchMethodException { - // Nothing to do here - } - - public boolean getMasterSyncAutomatically() { - /* - * SDK 2.0/API 5 introduced an official method to query the auto-sync - * state. - */ - return ContentResolver.getMasterSyncAutomatically(); - } -} diff --git a/src/com/fsck/k9/helper/IAutoSync.java b/src/com/fsck/k9/helper/IAutoSync.java deleted file mode 100644 index 660da060d..000000000 --- a/src/com/fsck/k9/helper/IAutoSync.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.fsck.k9.helper; - -import android.content.Context; - -/** - * Classes that implement this interface know how to query the system for the - * current state of the auto-sync setting. This method differs from SDK 3 to - * SDK 5, so there are specialized implementations for each SDK version. - */ -public interface IAutoSync { - /** - * Do the necessary reflection magic to get the necessary objects and/or - * methods to later query the state of the auto-sync setting. - * - * @param context The application context object. - * @throws NoSuchMethodException if something went wrong. - */ - public void initialize(Context context) throws NoSuchMethodException; - - /** - * Query the state of the auto-sync setting. - * - * @return the state of the auto-sync setting. - */ - public boolean getMasterSyncAutomatically(); -} diff --git a/src/com/fsck/k9/service/BootReceiver.java b/src/com/fsck/k9/service/BootReceiver.java index bbe8ea734..8f4120655 100644 --- a/src/com/fsck/k9/service/BootReceiver.java +++ b/src/com/fsck/k9/service/BootReceiver.java @@ -12,7 +12,6 @@ import android.net.Uri; import android.util.Log; import com.fsck.k9.K9; -import com.fsck.k9.helper.AutoSyncHelper; public class BootReceiver extends CoreReceiver { @@ -41,7 +40,7 @@ public class BootReceiver extends CoreReceiver { } else if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) { MailService.connectivityChange(context, tmpWakeLockId); tmpWakeLockId = null; - } else if (AutoSyncHelper.SYNC_CONN_STATUS_CHANGE.equals(action)) { + } else if ("com.android.sync.SYNC_CONN_STATUS_CHANGED".equals(action)) { K9.BACKGROUND_OPS bOps = K9.getBackgroundOps(); if (bOps == K9.BACKGROUND_OPS.WHEN_CHECKED_AUTO_SYNC) { MailService.actionReset(context, tmpWakeLockId); diff --git a/src/com/fsck/k9/service/MailService.java b/src/com/fsck/k9/service/MailService.java index 8f3330073..3d8dac3b7 100644 --- a/src/com/fsck/k9/service/MailService.java +++ b/src/com/fsck/k9/service/MailService.java @@ -4,6 +4,7 @@ package com.fsck.k9.service; import java.util.Collection; import java.util.Date; +import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; @@ -18,7 +19,6 @@ import com.fsck.k9.K9; import com.fsck.k9.Preferences; import com.fsck.k9.Account.FolderMode; import com.fsck.k9.controller.MessagingController; -import com.fsck.k9.helper.AutoSyncHelper; import com.fsck.k9.mail.Pusher; public class MailService extends CoreService { @@ -98,12 +98,7 @@ public class MailService extends CoreService { hasConnectivity = state == State.CONNECTED; } boolean backgroundData = connectivityManager.getBackgroundDataSetting(); - boolean autoSync = true; - if (AutoSyncHelper.isAvailable()) { - autoSync = AutoSyncHelper.getMasterSyncAutomatically(); - - Log.i(K9.LOG_TAG, "AutoSync help is available, autoSync = " + autoSync); - } + boolean autoSync = ContentResolver.getMasterSyncAutomatically(); K9.BACKGROUND_OPS bOps = K9.getBackgroundOps();