Ensure type safety in Pebble notifications

Also use forward-compatible screen-on detection
This commit is contained in:
Sam Whited 2015-01-13 22:46:06 -05:00
parent 6976c4ffe1
commit caf861d96f
1 changed files with 30 additions and 15 deletions

View File

@ -1,5 +1,6 @@
package eu.siacs.conversations.services; package eu.siacs.conversations.services;
import android.annotation.SuppressLint;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
@ -8,6 +9,7 @@ import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.net.Uri; import android.net.Uri;
import android.os.Build;
import android.os.PowerManager; import android.os.PowerManager;
import android.os.SystemClock; import android.os.SystemClock;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
@ -18,6 +20,9 @@ import android.text.Html;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.util.Log; import android.util.Log;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
@ -27,15 +32,10 @@ import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.json.JSONArray;
import org.json.JSONObject;
import eu.siacs.conversations.Config; import eu.siacs.conversations.Config;
import eu.siacs.conversations.R; import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Downloadable;
import eu.siacs.conversations.entities.DownloadableFile;
import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.ui.ConversationActivity; import eu.siacs.conversations.ui.ConversationActivity;
import eu.siacs.conversations.ui.ManageAccountActivity; import eu.siacs.conversations.ui.ManageAccountActivity;
@ -70,14 +70,14 @@ public class NotificationService {
); );
} }
public void notifyPebble(Message message) { public void notifyPebble(final Message message) {
final Intent i = new Intent("com.getpebble.action.SEND_NOTIFICATION"); final Intent i = new Intent("com.getpebble.action.SEND_NOTIFICATION");
final HashMap data = new HashMap();
final Conversation conversation = message.getConversation(); final Conversation conversation = message.getConversation();
data.put("title", conversation.getName()); final JSONObject jsonData = new JSONObject(new HashMap<String, String>(2) {{
data.put("body", message.getBody()); put("title", conversation.getName());
final JSONObject jsonData = new JSONObject(data); put("body", message.getBody());
}});
final String notificationData = new JSONArray().put(jsonData).toString(); final String notificationData = new JSONArray().put(jsonData).toString();
i.putExtra("messageType", "PEBBLE_ALERT"); i.putExtra("messageType", "PEBBLE_ALERT");
@ -111,13 +111,28 @@ public class NotificationService {
return mXmppConnectionService.getPreferences().getBoolean("always_notify_in_conference", false); return mXmppConnectionService.getPreferences().getBoolean("always_notify_in_conference", false);
} }
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
private boolean isInteractive() {
final PowerManager pm = (PowerManager) mXmppConnectionService
.getSystemService(Context.POWER_SERVICE);
final boolean isScreenOn;
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
isScreenOn = pm.isScreenOn();
} else {
isScreenOn = pm.isInteractive();
}
return isScreenOn;
}
public void push(final Message message) { public void push(final Message message) {
if (!notify(message)) { if (!notify(message)) {
return; return;
} }
final PowerManager pm = (PowerManager) mXmppConnectionService
.getSystemService(Context.POWER_SERVICE); final boolean isScreenOn = isInteractive();
final boolean isScreenOn = pm.isScreenOn();
if (this.mIsInForeground && isScreenOn if (this.mIsInForeground && isScreenOn
&& this.mOpenConversation == message.getConversation()) { && this.mOpenConversation == message.getConversation()) {