Fixes Issue 668

Fixes Issue 577

K-9 has earned its Canine Good Citizen.

K-9 now respects the Android-global Background data preference, by
default.  However, there is an override, allowing K-9 to ignore the
global preference, by either always running around and synchronizing
in the background, or staying put and not synchronizing.

A user can now elect to turn off mail synchronization across all
accounts in either the K-9 Preferences page by setting Background
synchronization to Never, or leaving that value as "When 'Background
data' is checked" and deselecting the Android-global Background data
preference.
This commit is contained in:
Daniel Applebaum 2009-10-31 18:51:59 +00:00
parent 581486fbd5
commit eabc9af9b3
8 changed files with 132 additions and 29 deletions

View file

@ -207,6 +207,9 @@
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
<intent-filter>
<action android:name="android.net.conn.BACKGROUND_DATA_SETTING_CHANGED" />
</intent-filter>
</receiver>
<service
android:name="com.android.email.service.MailService"

View file

@ -184,5 +184,16 @@
<item>dark</item>
</string-array>
<string-array name="background_ops_entries">
<item>@string/background_ops_enabled</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>ALWAYS</item>
<item>NEVER</item>
</string-array>
</resources>

View file

@ -533,5 +533,12 @@ Welcome to K-9 Mail setup. K-9 is an open source email client for Android based
<string name="setting_theme_light">Light</string>
<string name="display_preferences">Display Preferences</string>
<string name="debug_preferences">Diagnostic Preferences</string>
<string name="operational_preferences">Operational Preferences</string>
<string name="settings_theme_label">Theme</string>
<string name="background_ops_label">Background synchronization</string>
<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>
</resources>

View file

@ -25,6 +25,16 @@
android:entryValues="@array/settings_theme_values"
android:dialogTitle="@string/settings_theme_label" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/operational_preferences" android:key="operational_preferences">
<ListPreference
android:key="background_ops"
android:title="@string/background_ops_label"
android:entries="@array/background_ops_entries"
android:entryValues="@array/background_ops_values"
android:dialogTitle="@string/background_ops_label" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/debug_preferences" android:key="debug_preferences">
<CheckBoxPreference

View file

@ -25,8 +25,14 @@ public class Email extends Application {
public static File tempDirectory;
public static final String LOG_TAG = "k9";
public enum BACKGROUND_OPS
{
WHEN_CHECKED, ALWAYS, NEVER
}
private static int theme = android.R.style.Theme_Light;
private static BACKGROUND_OPS backgroundOps = BACKGROUND_OPS.WHEN_CHECKED;
/**
* Some log messages can be sent to a file, so that the logs
* can be read using unprivileged access (eg. Terminal Emulator)
@ -242,6 +248,7 @@ public class Email extends Application {
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("enableDebugLogging", Email.DEBUG);
editor.putBoolean("enableSensitiveLogging", Email.DEBUG_SENSITIVE);
editor.putString("backgroundOperations", Email.backgroundOps.toString());
editor.putInt("theme", theme);
editor.commit();
}
@ -254,6 +261,16 @@ public class Email extends Application {
SharedPreferences sprefs = prefs.getPreferences();
DEBUG = sprefs.getBoolean("enableDebugLogging", false);
DEBUG_SENSITIVE = sprefs.getBoolean("enableSensitiveLogging", false);
try
{
setBackgroundOps(BACKGROUND_OPS.valueOf(sprefs.getString("backgroundOperations", "WHEN_CHECKED")));
}
catch (Exception e)
{
setBackgroundOps(BACKGROUND_OPS.WHEN_CHECKED);
}
Email.setK9Theme(sprefs.getInt("theme", android.R.style.Theme_Light));
MessagingController.getInstance(this).resetVisibleLimits(prefs.getAccounts());
@ -304,6 +321,21 @@ public class Email extends Application {
{
theme = ntheme;
}
public static BACKGROUND_OPS getBackgroundOps()
{
return backgroundOps;
}
public static void setBackgroundOps(BACKGROUND_OPS backgroundOps)
{
Email.backgroundOps = backgroundOps;
}
public static void setBackgroundOps(String nbackgroundOps)
{
Email.backgroundOps = BACKGROUND_OPS.valueOf(nbackgroundOps);
}
}

View file

@ -22,18 +22,23 @@ import com.android.email.Account;
import com.android.email.Email;
import com.android.email.Preferences;
import com.android.email.R;
import com.android.email.service.MailService;
public class Prefs extends K9PreferenceActivity {
private static final String PREFERENCE_TOP_CATERGORY = "preferences";
private static final String PREFERENCE_THEME = "theme";
private static final String PREFERENCE_BACKGROUND_OPS = "background_ops";
private static final String PREFERENCE_DEBUG_LOGGING = "debug_logging";
private static final String PREFERENCE_SENSITIVE_LOGGING = "sensitive_logging";
private ListPreference mTheme;
private ListPreference mBackgroundOps;
private CheckBoxPreference mDebugLogging;
private CheckBoxPreference mSensitiveLogging;
private String initBackgroundOps;
public static void actionPrefs(Context context) {
Intent i = new Intent(context, Prefs.class);
@ -60,6 +65,20 @@ public class Prefs extends K9PreferenceActivity {
}
});
mBackgroundOps = (ListPreference) findPreference(PREFERENCE_BACKGROUND_OPS);
initBackgroundOps = Email.getBackgroundOps().toString();
mBackgroundOps.setValue(initBackgroundOps);
mBackgroundOps.setSummary(mBackgroundOps.getEntry());
mBackgroundOps.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
final String summary = newValue.toString();
int index = mBackgroundOps.findIndexOfValue(summary);
mBackgroundOps.setSummary(mBackgroundOps.getEntries()[index]);
mBackgroundOps.setValue(summary);
return false;
}
});
mDebugLogging = (CheckBoxPreference)findPreference(PREFERENCE_DEBUG_LOGGING);
mSensitiveLogging = (CheckBoxPreference)findPreference(PREFERENCE_SENSITIVE_LOGGING);
@ -78,7 +97,13 @@ public class Prefs extends K9PreferenceActivity {
Email.setK9Theme(mTheme.getValue().equals("dark") ? android.R.style.Theme : android.R.style.Theme_Light);
Email.DEBUG = mDebugLogging.isChecked();
Email.DEBUG_SENSITIVE = mSensitiveLogging.isChecked();
String newBackgroundOps = mBackgroundOps.getValue();
Email.setBackgroundOps(newBackgroundOps);
Email.save(preferences);
if (newBackgroundOps.equals(initBackgroundOps) == false)
{
MailService.backgroundDataChanged(this);
}
}
@Override

View file

@ -27,5 +27,9 @@ public class BootReceiver extends BroadcastReceiver {
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
MailService.connectivityChange(context, !noConnectivity);
}
else if (ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED.equals(intent.getAction())) {
MailService.backgroundDataChanged(context);
}
}
}

View file

@ -43,6 +43,7 @@ public class MailService extends Service {
private static final String ACTION_CANCEL = "com.android.email.intent.action.MAIL_SERVICE_CANCEL";
private static final String ACTION_REFRESH_PUSHERS = "com.android.email.intent.action.MAIL_SERVICE_REFRESH_PUSHERS";
private static final String CONNECTIVITY_CHANGE = "com.android.email.intent.action.MAIL_SERVICE_CONNECTIVITY_CHANGE";
private static final String BACKGROUND_DATA_CHANGED = "com.android.email.intent.action.MAIL_SERVICE_BACKGROUND_DATA_CHANGED";
private static final String CANCEL_CONNECTIVITY_NOTICE = "com.android.email.intent.action.MAIL_SERVICE_CANCEL_CONNECTIVITY_NOTICE";
private static final String HAS_CONNECTIVITY = "com.android.email.intent.action.MAIL_SERVICE_HAS_CONNECTIVITY";
@ -50,7 +51,7 @@ public class MailService extends Service {
private Listener mListener = new Listener();
private State state = null;
private int mStartId;
public static void actionReschedule(Context context) {
@ -88,6 +89,13 @@ public class MailService extends Service {
i.putExtra(HAS_CONNECTIVITY, hasConnectivity);
context.startService(i);
}
public static void backgroundDataChanged(Context context) {
Intent i = new Intent();
i.setClass(context, MailService.class);
i.setAction(MailService.BACKGROUND_DATA_CHANGED);
context.startService(i);
}
@Override
public void onCreate() {
@ -102,10 +110,13 @@ public class MailService extends Service {
WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Email");
wakeLock.setReferenceCounted(false);
wakeLock.acquire(Email.MAIL_SERVICE_WAKE_LOCK_TIMEOUT);
try
{
ConnectivityManager connectivityManager = (ConnectivityManager)getApplication().getSystemService(Context.CONNECTIVITY_SERVICE);
boolean doBackground = true;
state = State.DISCONNECTED;
if (connectivityManager != null)
{
@ -113,21 +124,18 @@ public class MailService extends Service {
if (netInfo != null)
{
state = netInfo.getState();
if (state == State.CONNECTED)
{
Log.i(Email.LOG_TAG, "Currently connected to a network");
}
else
{
Log.i(Email.LOG_TAG, "Current network state = " + state);
}
}
boolean backgroundData = connectivityManager.getBackgroundDataSetting();
Email.BACKGROUND_OPS bOps = Email.getBackgroundOps();
doBackground = (backgroundData == true && bOps != Email.BACKGROUND_OPS.NEVER)
| (backgroundData == false && bOps == Email.BACKGROUND_OPS.ALWAYS);
}
setForeground(true); // if it gets killed once, it'll never restart
Log.v(Email.LOG_TAG, "***** MailService *****: onStart(" + intent + ", " + startId + ")");
Log.i(Email.LOG_TAG, "MailService.onStart(" + intent + ", " + startId
+ "), state = " + state + ", doBackground = " + doBackground);
super.onStart(intent, startId);
this.mStartId = startId;
@ -137,7 +145,7 @@ public class MailService extends Service {
MessagingController.getInstance(getApplication()).log("***** MailService *****: checking mail");
Log.v(Email.LOG_TAG, "***** MailService *****: checking mail");
//}
if (state == State.CONNECTED)
if (state == State.CONNECTED && doBackground)
{
MessagingController controller = MessagingController.getInstance(getApplication());
Listener listener = (Listener)controller.getCheckMailListener();
@ -174,8 +182,17 @@ public class MailService extends Service {
Log.v(Email.LOG_TAG, "***** MailService *****: reschedule");
}
MessagingController.getInstance(getApplication()).log("***** MailService *****: reschedule");
boolean polling = reschedule();
boolean pushing = reschedulePushers();
boolean polling = false;
boolean pushing = false;
if (state == State.CONNECTED && doBackground)
{
polling = reschedule();
pushing = reschedulePushers();
}
else
{
stopPushers();
}
if (polling == false && pushing == false)
{
Log.i(Email.LOG_TAG, "Neither pushing nor polling, so stopping");
@ -188,7 +205,7 @@ public class MailService extends Service {
schedulePushers();
try
{
if (state == State.CONNECTED)
if (state == State.CONNECTED && doBackground)
{
Log.i(Email.LOG_TAG, "Refreshing pushers");
Collection<Pusher> pushers = MessagingController.getInstance(getApplication()).getPushers();
@ -203,21 +220,15 @@ public class MailService extends Service {
Log.e(Email.LOG_TAG, "Exception while refreshing pushers", e);
}
}
else if (CONNECTIVITY_CHANGE.equals(intent.getAction()))
else if (CONNECTIVITY_CHANGE.equals(intent.getAction()) ||
BACKGROUND_DATA_CHANGED.equals(intent.getAction()))
{
actionReschedule(this);
boolean hasConnectivity = intent.getBooleanExtra(HAS_CONNECTIVITY, true);
Log.i(Email.LOG_TAG, "Got connectivity action with hasConnectivity = " + hasConnectivity);
Log.i(Email.LOG_TAG, "Got connectivity action with hasConnectivity = " + hasConnectivity + ", doBackground = " + doBackground);
notifyConnectionStatus(hasConnectivity);
if (hasConnectivity)
{
reschedulePushers();
// TODO: Make it send pending outgoing messages here
//checkMail(getApplication());
}
else
{
stopPushers();
}
}
else if (CANCEL_CONNECTIVITY_NOTICE.equals(intent.getAction()))
{
@ -313,7 +324,7 @@ public class MailService extends Service {
try
{
String checkString = "Next check for package " + getApplication().getPackageName() + " scheduled for " + new Date(nextTime);
Log.v(Email.LOG_TAG, checkString);
Log.i(Email.LOG_TAG, checkString);
MessagingController.getInstance(getApplication()).log(checkString);
}
catch (Exception e)