2010-01-13 23:33:50 -05:00
|
|
|
package com.fsck.k9.service;
|
|
|
|
|
|
|
|
import com.fsck.k9.Account;
|
|
|
|
import com.fsck.k9.K9;
|
2010-01-15 00:05:41 -05:00
|
|
|
import com.fsck.k9.remotecontrol.K9RemoteControl;
|
2010-01-13 23:33:50 -05:00
|
|
|
import com.fsck.k9.Preferences;
|
|
|
|
import com.fsck.k9.R;
|
|
|
|
import com.fsck.k9.Account.FolderMode;
|
2010-01-22 00:54:52 -05:00
|
|
|
import com.fsck.k9.K9.BACKGROUND_OPS;
|
2010-01-13 23:33:50 -05:00
|
|
|
|
2010-01-15 00:05:41 -05:00
|
|
|
import static com.fsck.k9.remotecontrol.K9RemoteControl.*;
|
2010-01-13 23:33:50 -05:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public class RemoteControlService extends CoreService {
|
2010-01-13 23:33:50 -05:00
|
|
|
private final static String RESCHEDULE_ACTION = "com.fsck.k9.service.RemoteControlService.RESCHEDULE_ACTION";
|
2010-02-07 16:23:33 -05:00
|
|
|
private final static String PUSH_RESTART_ACTION = "com.fsck.k9.service.RemoteControlService.PUSH_RESTART_ACTION";
|
2010-01-13 23:33:50 -05:00
|
|
|
|
|
|
|
private final static String SET_ACTION = "com.fsck.k9.service.RemoteControlService.SET_ACTION";
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public static void set(Context context, Intent i, Integer wakeLockId) {
|
2010-01-17 19:11:02 -05:00
|
|
|
// Intent i = new Intent();
|
2010-01-13 23:33:50 -05:00
|
|
|
i.setClass(context, RemoteControlService.class);
|
|
|
|
i.setAction(RemoteControlService.SET_ACTION);
|
2011-09-04 16:00:02 -04:00
|
|
|
addWakeLockId(context, i, wakeLockId, true);
|
2010-01-13 23:33:50 -05:00
|
|
|
context.startService(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static final int REMOTE_CONTROL_SERVICE_WAKE_LOCK_TIMEOUT = 20000;
|
2010-01-17 19:11:02 -05:00
|
|
|
|
2010-01-13 23:33:50 -05:00
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public void startService(final Intent intent, final int startId) {
|
2010-01-13 23:33:50 -05:00
|
|
|
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);
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
if (RESCHEDULE_ACTION.equals(intent.getAction())) {
|
2010-01-17 19:11:02 -05:00
|
|
|
if (K9.DEBUG)
|
2010-02-07 16:23:33 -05:00
|
|
|
Log.i(K9.LOG_TAG, "RemoteControlService requesting MailService poll reschedule");
|
|
|
|
MailService.actionReschedulePoll(this, null);
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
if (PUSH_RESTART_ACTION.equals(intent.getAction())) {
|
2010-02-07 16:23:33 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.i(K9.LOG_TAG, "RemoteControlService requesting MailService push restart");
|
|
|
|
MailService.actionRestartPushers(this, null);
|
2011-02-06 17:09:48 -05:00
|
|
|
} 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");
|
2011-02-06 17:09:48 -05:00
|
|
|
execute(getApplication(), new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
try {
|
2010-01-22 00:54:52 -05:00
|
|
|
boolean needsReschedule = false;
|
2010-02-07 16:23:33 -05:00
|
|
|
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);
|
2011-02-06 17:09:48 -05:00
|
|
|
if (K9.DEBUG) {
|
|
|
|
if (allAccounts) {
|
2010-01-17 19:11:02 -05:00
|
|
|
Log.i(K9.LOG_TAG, "RemoteControlService changing settings for all accounts");
|
2011-02-06 17:09:48 -05:00
|
|
|
} 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();
|
2011-02-06 17:09:48 -05:00
|
|
|
for (Account account : accounts) {
|
2010-11-13 16:40:56 -05:00
|
|
|
//warning: account may not be isAvailable()
|
2011-02-06 17:09:48 -05:00
|
|
|
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);
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
if (notificationEnabled != null) {
|
2010-01-17 19:11:02 -05:00
|
|
|
account.setNotifyNewMail(Boolean.parseBoolean(notificationEnabled));
|
2010-01-13 23:33:50 -05:00
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
if (ringEnabled != null) {
|
2010-09-19 16:54:43 -04:00
|
|
|
account.getNotificationSetting().setRing(Boolean.parseBoolean(ringEnabled));
|
2010-01-13 23:33:50 -05:00
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
if (vibrateEnabled != null) {
|
2010-09-19 16:54:43 -04:00
|
|
|
account.getNotificationSetting().setVibrate(Boolean.parseBoolean(vibrateEnabled));
|
2010-01-17 19:11:02 -05:00
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
if (pushClasses != null) {
|
2010-02-07 16:23:33 -05:00
|
|
|
needsPushRestart |= account.setFolderPushMode(FolderMode.valueOf(pushClasses));
|
2010-01-17 19:11:02 -05:00
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
if (pollClasses != null) {
|
2010-02-07 16:23:33 -05:00
|
|
|
needsReschedule |= account.setFolderSyncMode(FolderMode.valueOf(pollClasses));
|
2010-01-17 19:11:02 -05:00
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
if (pollFrequency != null) {
|
2010-01-17 19:11:02 -05:00
|
|
|
String[] allowedFrequencies = getResources().getStringArray(R.array.account_settings_check_frequency_values);
|
2011-02-06 17:09:48 -05:00
|
|
|
for (String allowedFrequency : allowedFrequencies) {
|
|
|
|
if (allowedFrequency.equals(pollFrequency)) {
|
2010-01-22 00:54:52 -05:00
|
|
|
Integer newInterval = Integer.parseInt(allowedFrequency);
|
2010-02-07 16:23:33 -05:00
|
|
|
needsReschedule |= account.setAutomaticCheckIntervalMinutes(newInterval);
|
2010-01-13 23:33:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-01-17 19:11:02 -05:00
|
|
|
account.save(Preferences.getPreferences(RemoteControlService.this));
|
2010-01-13 23:33:50 -05:00
|
|
|
}
|
2010-01-17 19:11:02 -05:00
|
|
|
}
|
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.i(K9.LOG_TAG, "RemoteControlService changing global settings");
|
2010-01-13 23:33:50 -05:00
|
|
|
|
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)
|
2010-05-15 17:26:15 -04:00
|
|
|
|| K9RemoteControl.K9_BACKGROUND_OPERATIONS_WHEN_CHECKED.equals(backgroundOps)
|
2011-02-06 17:09:48 -05:00
|
|
|
|| K9RemoteControl.K9_BACKGROUND_OPERATIONS_WHEN_CHECKED_AUTO_SYNC.equals(backgroundOps)) {
|
2010-01-22 00:54:52 -05:00
|
|
|
BACKGROUND_OPS newBackgroundOps = BACKGROUND_OPS.valueOf(backgroundOps);
|
2010-02-08 20:14:55 -05:00
|
|
|
boolean needsReset = K9.setBackgroundOps(newBackgroundOps);
|
|
|
|
needsPushRestart |= needsReset;
|
|
|
|
needsReschedule |= needsReset;
|
2010-01-13 23:33:50 -05:00
|
|
|
}
|
2010-01-17 19:11:02 -05:00
|
|
|
|
|
|
|
String theme = intent.getStringExtra(K9_THEME);
|
2011-02-06 17:09:48 -05:00
|
|
|
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-13 23:33:50 -05:00
|
|
|
}
|
2010-01-17 19:11:02 -05:00
|
|
|
|
|
|
|
SharedPreferences sPrefs = preferences.getPreferences();
|
|
|
|
|
|
|
|
Editor editor = sPrefs.edit();
|
|
|
|
K9.save(editor);
|
|
|
|
editor.commit();
|
2010-04-29 00:59:14 -04:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
if (needsReschedule) {
|
2010-01-22 00:54:52 -05:00
|
|
|
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);
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
if (needsPushRestart) {
|
2010-02-07 16:23:33 -05:00
|
|
|
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);
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
} 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-13 23:33:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-01-17 19:11:02 -05:00
|
|
|
, RemoteControlService.REMOTE_CONTROL_SERVICE_WAKE_LOCK_TIMEOUT, startId);
|
|
|
|
}
|
2010-01-13 23:33:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|