1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-15 14:05:05 -05:00
k-9/src/com/fsck/k9/service/BootReceiver.java

136 lines
5.1 KiB
Java
Raw Normal View History

package com.fsck.k9.service;
import java.util.Date;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.Uri;
Complete merge of DAmail functionality into K9mail. Following features are added to K9mail: 1) Show unread message count on each folder 2) Sum unread count of all shown folders in an account to the account display 3) Periodically check selected folders for new mail, not just Inbox 4) Don't refresh folder when opened (unless folder is empty) 5) Show date and time of last sync for each folder 6) Fix timer for automatic periodic sync (use wakelock to assure completion) 7) Optimize local folder queries (speeds up account and folder lists) 8) Show Loading... message in status bar indicating which folder is being synced 9) Eliminate redundant sync of new messages (performance enhancement) 10) Improve notification text for multiple accounts 11) Do not automatically sync folders more often than the account-specific period 12) Use user-configured date and time formats 13) Select which folders are shown, using configurable Classes 14) Select which folders are synced, using configurable Classes 15) Added context (long press) menu to folders, to provide for Refresh and Folder Settings 16) Status light flashes purple when there are unread messages 17) Folder list more quickly eliminates display of deleted and out-of-Class folders. 18) Delete works 19) Mark all messages as read (in the folder context menu) 20) Notifications only for new unread messages 21) One minute synchronization frequency 22) Deleting an unread message decrements unread counter 23) Notifications work for POP3 accounts 24) Message deletes work for POP3 accounts 25) Explicit errors show in folder list 26) Stack traces saved to folder K9mail-errors 27) Clear pending actions (danger, for emergencies only!) 28) Delete policy in Account settings 29) DNS cache in InetAddress disabled 30) Trapped some crash-causing error conditions 31) Eliminate duplicate copies to Sent folder 32) Prevent crashes due to message listener concurrency 33) Empty Trash 34) Nuclear "Mark all messages as read" (marks all messages as read in server-side folder, irrespective of which messages have been downloaded) 35) Forward (alternate) to allow forwarding email through other programs 36) Accept text/plain Intents to allow other programs to send email through K9mail 37) Displays Outbox sending status 38) Manual retry of outbox sending when "Refresh"ing Outbox 39) Folder error status is persisted 40) Ability to log to arbitrary file Fixes K9 issues 11, 23, 24, 65, 69, 71, 79, 81, 82, 83, 87, 101, 104, 107, 120, 148, 154
2008-12-30 22:49:09 -05:00
import android.util.Log;
import com.fsck.k9.K9;
public class BootReceiver extends CoreReceiver
{
public static String FIRE_INTENT = "com.fsck.k9.service.BroadcastReceiver.fireIntent";
public static String SCHEDULE_INTENT = "com.fsck.k9.service.BroadcastReceiver.scheduleIntent";
public static String CANCEL_INTENT = "com.fsck.k9.service.BroadcastReceiver.cancelIntent";
public static String ALARMED_INTENT = "com.fsck.k9.service.BroadcastReceiver.pendingIntent";
public static String AT_TIME = "com.fsck.k9.service.BroadcastReceiver.atTime";
public Integer receive(Context context, Intent intent, Integer tmpWakeLockId)
{
if (K9.DEBUG)
Log.i(K9.LOG_TAG, "BootReceiver.onReceive" + intent);
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction()))
{
//K9.setServicesEnabled(context, tmpWakeLockId);
//tmpWakeLockId = null;
}
else if (Intent.ACTION_DEVICE_STORAGE_LOW.equals(intent.getAction()))
{
MailService.actionCancel(context, tmpWakeLockId);
tmpWakeLockId = null;
}
else if (Intent.ACTION_DEVICE_STORAGE_OK.equals(intent.getAction()))
{
MailService.actionReschedule(context, tmpWakeLockId);
tmpWakeLockId = null;
}
else if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction()))
{
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
MailService.connectivityChange(context, !noConnectivity, tmpWakeLockId);
tmpWakeLockId = null;
}
else if (ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED.equals(intent.getAction()))
{
MailService.backgroundDataChanged(context, tmpWakeLockId);
tmpWakeLockId = null;
}
else if (FIRE_INTENT.equals(intent.getAction()))
{
Intent alarmedIntent = intent.getParcelableExtra(ALARMED_INTENT);
String alarmedAction = alarmedIntent.getAction();
2010-01-02 20:50:32 -05:00
if (K9.DEBUG)
Log.i(K9.LOG_TAG, "BootReceiver Got alarm to fire alarmedIntent " + alarmedAction);
alarmedIntent.putExtra(WAKE_LOCK_ID, tmpWakeLockId);
tmpWakeLockId = null;
if (alarmedIntent != null)
{
context.startService(alarmedIntent);
}
}
else if (SCHEDULE_INTENT.equals(intent.getAction()))
{
long atTime = intent.getLongExtra(AT_TIME, -1);
Intent alarmedIntent = intent.getParcelableExtra(ALARMED_INTENT);
if (K9.DEBUG)
Log.i(K9.LOG_TAG,"BootReceiver Scheduling intent " + alarmedIntent + " for " + new Date(atTime));
PendingIntent pi = buildPendingIntent(context, intent);
AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmMgr.set(AlarmManager.RTC_WAKEUP, atTime, pi);
}
else if (CANCEL_INTENT.equals(intent.getAction()))
{
Intent alarmedIntent = intent.getParcelableExtra(ALARMED_INTENT);
if (K9.DEBUG)
Log.i(K9.LOG_TAG, "BootReceiver Canceling alarmedIntent " + alarmedIntent);
PendingIntent pi = buildPendingIntent(context, intent);
AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmMgr.cancel(pi);
}
return tmpWakeLockId;
}
private PendingIntent buildPendingIntent(Context context, Intent intent)
{
Intent alarmedIntent = intent.getParcelableExtra(ALARMED_INTENT);
String alarmedAction = alarmedIntent.getAction();
Intent i = new Intent(context, BootReceiver.class);
i.setAction(FIRE_INTENT);
i.putExtra(ALARMED_INTENT, alarmedIntent);
Uri uri = Uri.parse("action://" + alarmedAction);
i.setData(uri);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
return pi;
}
public static void scheduleIntent(Context context, long atTime, Intent alarmedIntent)
{
2010-01-02 20:50:32 -05:00
if (K9.DEBUG)
Log.i(K9.LOG_TAG, "BootReceiver Got request to schedule alarmedIntent " + alarmedIntent.getAction());
Intent i = new Intent();
i.setClass(context, BootReceiver.class);
i.setAction(SCHEDULE_INTENT);
i.putExtra(ALARMED_INTENT, alarmedIntent);
i.putExtra(AT_TIME, atTime);
context.sendBroadcast(i);
}
public static void cancelIntent(Context context, Intent alarmedIntent)
{
2010-01-02 20:50:32 -05:00
if (K9.DEBUG)
Log.i(K9.LOG_TAG, "BootReceiver Got request to cancel alarmedIntent " + alarmedIntent.getAction());
Intent i = new Intent();
i.setClass(context, BootReceiver.class);
i.setAction(CANCEL_INTENT);
i.putExtra(ALARMED_INTENT, alarmedIntent);
context.sendBroadcast(i);
}
}