2015-01-26 18:52:19 +01:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge;
|
|
|
|
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2015-03-06 14:00:56 +01:00
|
|
|
import android.content.SharedPreferences;
|
2015-01-26 18:52:19 +01:00
|
|
|
import android.database.Cursor;
|
|
|
|
import android.net.Uri;
|
2015-03-04 23:47:47 +01:00
|
|
|
import android.os.PowerManager;
|
2015-03-06 14:00:56 +01:00
|
|
|
import android.preference.PreferenceManager;
|
2015-01-26 18:52:19 +01:00
|
|
|
|
|
|
|
public class K9Receiver extends BroadcastReceiver {
|
|
|
|
|
|
|
|
private final String TAG = this.getClass().getSimpleName();
|
|
|
|
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
|
|
|
|
2015-03-06 14:00:56 +01:00
|
|
|
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
if (!sharedPrefs.getBoolean("notifications_k9mail", true)) {
|
2015-03-04 23:47:47 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-03-06 14:00:56 +01:00
|
|
|
if (!sharedPrefs.getBoolean("notifications_k9mail_whenscreenon", false)) {
|
|
|
|
PowerManager powermanager = (PowerManager) context.getSystemService(context.POWER_SERVICE);
|
|
|
|
if (powermanager.isScreenOn()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-03-04 23:47:47 +01:00
|
|
|
|
2015-01-26 18:52:19 +01:00
|
|
|
// get sender and subject from the Intent
|
|
|
|
String sender = intent.getStringExtra("com.fsck.k9.intent.extra.FROM");
|
|
|
|
String subject = intent.getStringExtra("com.fsck.k9.intent.extra.SUBJECT");
|
|
|
|
|
|
|
|
// get preview from K9 Content Provider, unfortunately this does not come with the Intent
|
|
|
|
String[] whereParameters = {intent.getData().toString()};
|
|
|
|
String[] messagesProjection = {
|
|
|
|
"preview"
|
|
|
|
};
|
|
|
|
|
|
|
|
Cursor c = context.getContentResolver().query(k9Uri, null, "uri=?", whereParameters, " LIMIT 1");
|
|
|
|
c.moveToFirst();
|
|
|
|
String preview = c.getString(c.getColumnIndex("preview"));
|
|
|
|
c.close();
|
|
|
|
|
|
|
|
Intent startIntent = new Intent(context, BluetoothCommunicationService.class);
|
|
|
|
startIntent.setAction(BluetoothCommunicationService.ACTION_NOTIFICATION_EMAIL);
|
|
|
|
startIntent.putExtra("notification_sender", sender);
|
|
|
|
startIntent.putExtra("notification_subject", subject);
|
|
|
|
startIntent.putExtra("notification_body", preview);
|
|
|
|
|
|
|
|
context.startService(startIntent);
|
|
|
|
}
|
|
|
|
}
|