mirror of
https://github.com/moparisthebest/k-9
synced 2024-11-15 22:15:15 -05:00
Removed AutoSyncHelper and use API 5 methods directly
This commit is contained in:
parent
b3bfcbde15
commit
c5cc62b570
@ -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 <? extends IAutoSync > 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();
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
@ -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);
|
||||
|
@ -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();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user