2015-04-20 22:39:35 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.externalevents;
|
2015-01-26 18:52:19 +01:00
|
|
|
|
2016-05-19 16:34:59 +02:00
|
|
|
import android.app.NotificationManager;
|
2015-01-26 18:52:19 +01:00
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.net.Uri;
|
2015-03-04 23:47:47 +01:00
|
|
|
import android.os.PowerManager;
|
2015-01-26 18:52:19 +01:00
|
|
|
|
2015-05-12 06:28:11 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2015-08-21 00:58:18 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
2015-09-24 14:45:21 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.NotificationType;
|
2016-04-25 23:18:55 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
2015-04-20 22:39:35 +02:00
|
|
|
|
2015-01-26 18:52:19 +01:00
|
|
|
public class K9Receiver extends BroadcastReceiver {
|
|
|
|
|
2015-05-12 06:28:11 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(K9Receiver.class);
|
2015-01-26 18:52:19 +01:00
|
|
|
private final Uri k9Uri = Uri.parse("content://com.fsck.k9.messageprovider/inbox_messages");
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
2015-03-04 23:47:47 +01:00
|
|
|
|
2016-04-25 23:18:55 +02:00
|
|
|
Prefs prefs = GBApplication.getPrefs();
|
|
|
|
if ("never".equals(prefs.getString("notification_mode_k9mail", "when_screen_off"))) {
|
2015-03-04 23:47:47 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-04-25 23:18:55 +02:00
|
|
|
if ("when_screen_off".equals(prefs.getString("notification_mode_k9mail", "when_screen_off"))) {
|
2015-05-04 01:03:56 +02:00
|
|
|
PowerManager powermanager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
2015-03-06 14:00:56 +01:00
|
|
|
if (powermanager.isScreenOn()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-05-19 23:58:13 +02:00
|
|
|
switch (GBApplication.getGrantedInterruptionFilter()) {
|
|
|
|
case NotificationManager.INTERRUPTION_FILTER_ALL:
|
|
|
|
break;
|
|
|
|
case NotificationManager.INTERRUPTION_FILTER_ALARMS:
|
|
|
|
case NotificationManager.INTERRUPTION_FILTER_NONE:
|
|
|
|
case NotificationManager.INTERRUPTION_FILTER_PRIORITY:
|
|
|
|
return;
|
2016-05-19 16:34:59 +02:00
|
|
|
}
|
2015-03-04 23:47:47 +01:00
|
|
|
|
2015-03-16 16:18:11 +01:00
|
|
|
String uriWanted = intent.getData().toString();
|
2015-01-26 18:52:19 +01:00
|
|
|
|
|
|
|
String[] messagesProjection = {
|
2015-03-16 16:18:11 +01:00
|
|
|
"senderAddress",
|
|
|
|
"subject",
|
|
|
|
"preview",
|
|
|
|
"uri"
|
2015-01-26 18:52:19 +01:00
|
|
|
};
|
|
|
|
|
2015-09-24 14:45:21 +02:00
|
|
|
NotificationSpec notificationSpec = new NotificationSpec();
|
|
|
|
notificationSpec.id = -1;
|
|
|
|
notificationSpec.type = NotificationType.EMAIL;
|
2015-03-16 16:18:11 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* there seems to be no way to specify the the uri in the where clause.
|
|
|
|
* If we do so, we just get the newest message, not the one requested.
|
|
|
|
* So, we will just search our message and match the uri manually.
|
|
|
|
* It should be the first one returned by the query in most cases,
|
|
|
|
*/
|
2015-11-27 22:42:47 +01:00
|
|
|
|
2016-03-08 23:48:31 +01:00
|
|
|
try (Cursor c = context.getContentResolver().query(k9Uri, messagesProjection, null, null, null)) {
|
2015-11-23 22:46:12 +01:00
|
|
|
if (c != null) {
|
2016-03-08 23:29:42 +01:00
|
|
|
while (c.moveToNext()) {
|
2015-11-23 22:46:12 +01:00
|
|
|
String uri = c.getString(c.getColumnIndex("uri"));
|
|
|
|
if (uri.equals(uriWanted)) {
|
|
|
|
notificationSpec.sender = c.getString(c.getColumnIndex("senderAddress"));
|
|
|
|
notificationSpec.subject = c.getString(c.getColumnIndex("subject"));
|
|
|
|
notificationSpec.body = c.getString(c.getColumnIndex("preview"));
|
|
|
|
break;
|
|
|
|
}
|
2016-03-08 21:26:55 +01:00
|
|
|
}
|
2015-03-16 16:18:11 +01:00
|
|
|
}
|
2016-03-08 23:48:31 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
notificationSpec.sender = "Gadgetbridge";
|
|
|
|
notificationSpec.subject = "Permission Error?";
|
|
|
|
notificationSpec.body = "Please reinstall Gadgetbridge to enable K-9 Mail notifications";
|
2015-11-23 22:46:12 +01:00
|
|
|
}
|
2015-01-26 18:52:19 +01:00
|
|
|
|
2015-09-24 14:45:21 +02:00
|
|
|
GBApplication.deviceService().onNotification(notificationSpec);
|
2015-01-26 18:52:19 +01:00
|
|
|
}
|
|
|
|
}
|