Fixes Issue 1482

Utilize cketti's AutoSyncHelper in order to provide a way to make K-9
Mail obey the global Auto-sync setting.  K-9 Mail doesn't obey the
Auto-sync setting by default because K-9 Mail isn't listed as one of
the listed applications.
This commit is contained in:
Daniel Applebaum 2010-05-15 21:26:15 +00:00
parent a59ade4fdd
commit 71c43b9634
10 changed files with 65 additions and 16 deletions

View File

@ -13,6 +13,7 @@
/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS"/>
<uses-permission android:name="android.permission.READ_OWNER_DATA"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
@ -237,6 +238,9 @@
</intent-filter>
<intent-filter>
<action android:name="android.net.conn.BACKGROUND_DATA_SETTING_CHANGED" />
</intent-filter>
<intent-filter>
<action android:name="com.android.sync.SYNC_CONN_STATUS_CHANGED" />
</intent-filter>
<intent-filter>
<action android:name="com.fsck.k9.service.BroadcastReceiver.scheduleIntent"/>

View File

@ -246,12 +246,14 @@
<string-array name="background_ops_entries">
<item>@string/background_ops_enabled</item>
<item>@string/background_ops_auto_sync</item>
<item>@string/background_ops_always</item>
<item>@string/background_ops_never</item>
</string-array>
<string-array name="background_ops_values">
<item>WHEN_CHECKED</item>
<item>WHEN_CHECKED_AUTO_SYNC</item>
<item>ALWAYS</item>
<item>NEVER</item>
</string-array>

View File

@ -673,6 +673,7 @@ Welcome to K-9 Mail setup. K-9 is an open source mail client for Android origin
<string name="background_ops_never">Never</string>
<string name="background_ops_always">Always</string>
<string name="background_ops_enabled">When \'Background data\' is checked</string>
<string name="background_ops_auto_sync">When \'Background data\' &amp; \'Auto-sync\' are checked</string>
<string name="no_message_seletected_toast">No message selected</string>

View File

@ -32,7 +32,7 @@ public class K9 extends Application
public enum BACKGROUND_OPS
{
WHEN_CHECKED, ALWAYS, NEVER
WHEN_CHECKED, ALWAYS, NEVER, WHEN_CHECKED_AUTO_SYNC
}
private static int theme = android.R.style.Theme_Light;

View File

@ -24,7 +24,10 @@ public class AutoSyncHelper
*/
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.
*

View File

@ -1,8 +1,12 @@
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
{
@ -39,7 +43,8 @@ public class AutoSyncSdk4 implements IAutoSync
}
catch (Exception e)
{
return false;
Log.e(K9.LOG_TAG, "Could not query for network tickle", e);
return true;
}
}
}

View File

@ -107,6 +107,7 @@ public class K9RemoteControl
public final static String K9_BACKGROUND_OPERATIONS_WHEN_CHECKED = "WHEN_CHECKED";
public final static String K9_BACKGROUND_OPERATIONS_ALWAYS = "ALWAYS";
public final static String K9_BACKGROUND_OPERATIONS_NEVER = "NEVER";
public final static String K9_BACKGROUND_OPERATIONS_WHEN_CHECKED_AUTO_SYNC = "WHEN_CHECKED_AUTO_SYNC";
/**
* Key for the {@link Intent} Extra to set for controlling which display theme K-9 will use. Acceptable values are

View File

@ -12,6 +12,7 @@ import android.net.Uri;
import android.util.Log;
import com.fsck.k9.K9;
import com.fsck.k9.helper.AutoSyncHelper;
public class BootReceiver extends CoreReceiver
{
@ -22,7 +23,7 @@ public class BootReceiver extends CoreReceiver
public static String ALARMED_INTENT = "com.fsck.k9.service.BroadcastReceiver.pendingIntent";
public static String AT_TIME = "com.fsck.k9.service.BroadcastReceiver.atTime";
@Override
public Integer receive(Context context, Intent intent, Integer tmpWakeLockId)
{
@ -46,14 +47,26 @@ public class BootReceiver extends CoreReceiver
}
else if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction()))
{
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
MailService.connectivityChange(context, !noConnectivity, tmpWakeLockId);
MailService.connectivityChange(context, tmpWakeLockId);
tmpWakeLockId = null;
}
else if (AutoSyncHelper.SYNC_CONN_STATUS_CHANGE.equals(intent.getAction()))
{
K9.BACKGROUND_OPS bOps = K9.getBackgroundOps();
if (bOps == K9.BACKGROUND_OPS.WHEN_CHECKED_AUTO_SYNC)
{
MailService.actionReset(context, tmpWakeLockId);
tmpWakeLockId = null;
}
}
else if (ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED.equals(intent.getAction()))
{
MailService.actionReset(context, tmpWakeLockId);
tmpWakeLockId = null;
K9.BACKGROUND_OPS bOps = K9.getBackgroundOps();
if (bOps == K9.BACKGROUND_OPS.WHEN_CHECKED || bOps == K9.BACKGROUND_OPS.WHEN_CHECKED_AUTO_SYNC)
{
MailService.actionReset(context, tmpWakeLockId);
tmpWakeLockId = null;
}
}
else if (FIRE_INTENT.equals(intent.getAction()))
{

View File

@ -22,6 +22,7 @@ import com.fsck.k9.MessagingController;
import com.fsck.k9.Preferences;
import com.fsck.k9.R;
import com.fsck.k9.Account.FolderMode;
import com.fsck.k9.helper.AutoSyncHelper;
import com.fsck.k9.mail.Pusher;
/**
@ -37,8 +38,6 @@ public class MailService extends CoreService
private static final String CONNECTIVITY_CHANGE = "com.fsck.k9.intent.action.MAIL_SERVICE_CONNECTIVITY_CHANGE";
private static final String CANCEL_CONNECTIVITY_NOTICE = "com.fsck.k9.intent.action.MAIL_SERVICE_CANCEL_CONNECTIVITY_NOTICE";
private static final String HAS_CONNECTIVITY = "com.fsck.k9.intent.action.MAIL_SERVICE_HAS_CONNECTIVITY";
private static long nextCheck = -1;
public static void actionReset(Context context, Integer wakeLockId)
@ -89,12 +88,11 @@ public class MailService extends CoreService
context.startService(i);
}
public static void connectivityChange(Context context, boolean hasConnectivity, Integer wakeLockId)
public static void connectivityChange(Context context, Integer wakeLockId)
{
Intent i = new Intent();
i.setClass(context, MailService.class);
i.setAction(MailService.CONNECTIVITY_CHANGE);
i.putExtra(HAS_CONNECTIVITY, hasConnectivity);
addWakeLockId(i, wakeLockId);
context.startService(i);
}
@ -127,11 +125,32 @@ 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);
}
K9.BACKGROUND_OPS bOps = K9.getBackgroundOps();
doBackground = (backgroundData == true && bOps != K9.BACKGROUND_OPS.NEVER)
| (backgroundData == false && bOps == K9.BACKGROUND_OPS.ALWAYS);
switch (bOps)
{
case NEVER:
doBackground = false;
break;
case ALWAYS:
doBackground = true;
break;
case WHEN_CHECKED:
doBackground = backgroundData;
break;
case WHEN_CHECKED_AUTO_SYNC:
doBackground = backgroundData & autoSync;
break;
}
}
if (K9.DEBUG)

View File

@ -140,7 +140,8 @@ public class RemoteControlService extends CoreService
String backgroundOps = intent.getStringExtra(K9_BACKGROUND_OPERATIONS);
if (K9RemoteControl.K9_BACKGROUND_OPERATIONS_ALWAYS.equals(backgroundOps)
|| K9RemoteControl.K9_BACKGROUND_OPERATIONS_NEVER.equals(backgroundOps)
|| K9RemoteControl.K9_BACKGROUND_OPERATIONS_WHEN_CHECKED.equals(backgroundOps))
|| K9RemoteControl.K9_BACKGROUND_OPERATIONS_WHEN_CHECKED.equals(backgroundOps)
|| K9RemoteControl.K9_BACKGROUND_OPERATIONS_WHEN_CHECKED_AUTO_SYNC.equals(backgroundOps))
{
BACKGROUND_OPS newBackgroundOps = BACKGROUND_OPS.valueOf(backgroundOps);
boolean needsReset = K9.setBackgroundOps(newBackgroundOps);