1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-12 04:25:08 -05:00
k-9/src/com/fsck/k9/service/RemoteControlService.java

163 lines
8.8 KiB
Java
Raw Normal View History

package com.fsck.k9.service;
import com.fsck.k9.Account;
import com.fsck.k9.K9;
import com.fsck.k9.remotecontrol.K9RemoteControl;
import com.fsck.k9.Preferences;
import com.fsck.k9.R;
import com.fsck.k9.Account.FolderMode;
import com.fsck.k9.K9.BACKGROUND_OPS;
import static com.fsck.k9.remotecontrol.K9RemoteControl.*;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.widget.Toast;
public class RemoteControlService extends CoreService {
private final static String RESCHEDULE_ACTION = "com.fsck.k9.service.RemoteControlService.RESCHEDULE_ACTION";
private final static String PUSH_RESTART_ACTION = "com.fsck.k9.service.RemoteControlService.PUSH_RESTART_ACTION";
private final static String SET_ACTION = "com.fsck.k9.service.RemoteControlService.SET_ACTION";
public static void set(Context context, Intent i, Integer wakeLockId) {
2010-01-17 19:11:02 -05:00
// Intent i = new Intent();
i.setClass(context, RemoteControlService.class);
i.setAction(RemoteControlService.SET_ACTION);
addWakeLockId(context, i, wakeLockId, true);
context.startService(i);
}
public static final int REMOTE_CONTROL_SERVICE_WAKE_LOCK_TIMEOUT = 20000;
2010-01-17 19:11:02 -05:00
@Override
public int startService(final Intent intent, final int startId) {
if (K9.DEBUG)
Log.i(K9.LOG_TAG, "RemoteControlService started with startId = " + startId);
2010-01-17 19:11:02 -05:00
final Preferences preferences = Preferences.getPreferences(this);
if (RESCHEDULE_ACTION.equals(intent.getAction())) {
2010-01-17 19:11:02 -05:00
if (K9.DEBUG)
Log.i(K9.LOG_TAG, "RemoteControlService requesting MailService poll reschedule");
MailService.actionReschedulePoll(this, null);
}
if (PUSH_RESTART_ACTION.equals(intent.getAction())) {
if (K9.DEBUG)
Log.i(K9.LOG_TAG, "RemoteControlService requesting MailService push restart");
MailService.actionRestartPushers(this, null);
} else if (RemoteControlService.SET_ACTION.equals(intent.getAction())) {
2010-01-17 19:11:02 -05:00
if (K9.DEBUG)
Log.i(K9.LOG_TAG, "RemoteControlService got request to change settings");
execute(getApplication(), new Runnable() {
public void run() {
try {
boolean needsReschedule = false;
boolean needsPushRestart = false;
2010-01-17 19:11:02 -05:00
String uuid = intent.getStringExtra(K9_ACCOUNT_UUID);
boolean allAccounts = intent.getBooleanExtra(K9_ALL_ACCOUNTS, false);
if (K9.DEBUG) {
if (allAccounts) {
2010-01-17 19:11:02 -05:00
Log.i(K9.LOG_TAG, "RemoteControlService changing settings for all accounts");
} else {
2010-01-17 19:11:02 -05:00
Log.i(K9.LOG_TAG, "RemoteControlService changing settings for account with UUID " + uuid);
}
}
Account[] accounts = preferences.getAccounts();
for (Account account : accounts) {
Merge branch 'mail-on-sd' * mail-on-sd: (40 commits) Added more comments to explain how the locking mecanism works for LocalStore Fixed wrong method being called during experimental provider initialization (since provider isn't enabled, that didn't harm) Add more comments about how the various StorageProviders work and how they're enabled find src/com/fsck/ -name \*.java|xargs astyle --style=ansi --mode=java --indent-switches --indent=spaces=4 --convert-tabs French localization for storage related settings Remove unused SD card strings (replaced with storage indirection) Merge mail-on-sd branch from trunk Reset mail service on storage mount (even if no account uses the storage, to be improved) find src/com/fsck/ -name \*.java|xargs astyle --style=ansi --mode=java --indent-switches --indent=spaces=4 --convert-tabs Migraion -> Migration move the Storage location preference into preferences rather than the wizard. Made LocalStore log less verbose Added @Override compile checks Added ACTION_SHUTDOWN broadcast receiver to properly initiate shutdown sequence (not yet implemented) and cancel any scheduled Intent Be more consistent about which SQLiteDatabase variable is used (from instance variable to argument variable) to make code more refactoring-friendly (class is already big, code extraction should be easier if not referencing the instance variable). Added transaction timing logging Factorised storage lock/transaction handling code for regular operations. Use DB transactions to batch modifications (makes code more robust / could improve performances) Merge mail-on-sd branch from trunk Update issue 888 Added DB close on unmount / DB open on mount Update issue 888 Back to account list when underlying storage not available/unmounting in MessageView / MessageList ...
2010-11-13 16:40:56 -05:00
//warning: account may not be isAvailable()
if (allAccounts || account.getUuid().equals(uuid)) {
2010-01-17 19:11:02 -05:00
if (K9.DEBUG)
Log.i(K9.LOG_TAG, "RemoteControlService changing settings for account " + account.getDescription());
String notificationEnabled = intent.getStringExtra(K9_NOTIFICATION_ENABLED);
String ringEnabled = intent.getStringExtra(K9_RING_ENABLED);
String vibrateEnabled = intent.getStringExtra(K9_VIBRATE_ENABLED);
String pushClasses = intent.getStringExtra(K9_PUSH_CLASSES);
String pollClasses = intent.getStringExtra(K9_POLL_CLASSES);
String pollFrequency = intent.getStringExtra(K9_POLL_FREQUENCY);
if (notificationEnabled != null) {
2010-01-17 19:11:02 -05:00
account.setNotifyNewMail(Boolean.parseBoolean(notificationEnabled));
}
if (ringEnabled != null) {
account.getNotificationSetting().setRing(Boolean.parseBoolean(ringEnabled));
}
if (vibrateEnabled != null) {
account.getNotificationSetting().setVibrate(Boolean.parseBoolean(vibrateEnabled));
2010-01-17 19:11:02 -05:00
}
if (pushClasses != null) {
needsPushRestart |= account.setFolderPushMode(FolderMode.valueOf(pushClasses));
2010-01-17 19:11:02 -05:00
}
if (pollClasses != null) {
needsReschedule |= account.setFolderSyncMode(FolderMode.valueOf(pollClasses));
2010-01-17 19:11:02 -05:00
}
if (pollFrequency != null) {
2010-01-17 19:11:02 -05:00
String[] allowedFrequencies = getResources().getStringArray(R.array.account_settings_check_frequency_values);
for (String allowedFrequency : allowedFrequencies) {
if (allowedFrequency.equals(pollFrequency)) {
Integer newInterval = Integer.parseInt(allowedFrequency);
needsReschedule |= account.setAutomaticCheckIntervalMinutes(newInterval);
}
}
}
2010-01-17 19:11:02 -05:00
account.save(Preferences.getPreferences(RemoteControlService.this));
}
2010-01-17 19:11:02 -05:00
}
if (K9.DEBUG)
Log.i(K9.LOG_TAG, "RemoteControlService changing global settings");
2010-01-17 19:11:02 -05:00
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_AUTO_SYNC.equals(backgroundOps)) {
BACKGROUND_OPS newBackgroundOps = BACKGROUND_OPS.valueOf(backgroundOps);
boolean needsReset = K9.setBackgroundOps(newBackgroundOps);
needsPushRestart |= needsReset;
needsReschedule |= needsReset;
}
2010-01-17 19:11:02 -05:00
String theme = intent.getStringExtra(K9_THEME);
if (theme != null) {
2010-01-17 19:11:02 -05:00
K9.setK9Theme(K9RemoteControl.K9_THEME_DARK.equals(theme) ? android.R.style.Theme : android.R.style.Theme_Light);
}
2010-01-17 19:11:02 -05:00
SharedPreferences sPrefs = preferences.getPreferences();
Editor editor = sPrefs.edit();
K9.save(editor);
editor.commit();
if (needsReschedule) {
Intent i = new Intent();
i.setClassName(getApplication().getPackageName(), "com.fsck.k9.service.RemoteControlService");
i.setAction(RESCHEDULE_ACTION);
long nextTime = System.currentTimeMillis() + 10000;
BootReceiver.scheduleIntent(RemoteControlService.this, nextTime, i);
}
if (needsPushRestart) {
Intent i = new Intent();
i.setClassName(getApplication().getPackageName(), "com.fsck.k9.service.RemoteControlService");
i.setAction(PUSH_RESTART_ACTION);
long nextTime = System.currentTimeMillis() + 10000;
BootReceiver.scheduleIntent(RemoteControlService.this, nextTime, i);
}
} catch (Exception e) {
2010-01-17 19:11:02 -05:00
Log.e(K9.LOG_TAG, "Could not handle K9_SET", e);
Toast toast = Toast.makeText(RemoteControlService.this, e.getMessage(), Toast.LENGTH_LONG);
toast.show();
}
}
}
2010-01-17 19:11:02 -05:00
, RemoteControlService.REMOTE_CONTROL_SERVICE_WAKE_LOCK_TIMEOUT, startId);
}
return START_NOT_STICKY;
}
}