2015-05-04 01:03:56 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.externalevents;
|
|
|
|
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.os.PowerManager;
|
|
|
|
import android.preference.PreferenceManager;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.BluetoothCommunicationService;
|
|
|
|
|
|
|
|
public class PebbleReceiver extends BroadcastReceiver {
|
|
|
|
|
|
|
|
private final String TAG = this.getClass().getSimpleName();
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
|
|
|
|
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
|
2015-05-10 15:07:28 +02:00
|
|
|
if ("never".equals(sharedPrefs.getString("notification_mode_pebblemsg", "when_screen_off"))) {
|
2015-05-04 01:03:56 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-05-10 15:07:28 +02:00
|
|
|
if ("when_screen_off".equals(sharedPrefs.getString("notification_mode_pebblemsg", "when_screen_off"))) {
|
2015-05-04 01:03:56 +02:00
|
|
|
PowerManager powermanager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
|
|
|
if (powermanager.isScreenOn()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String title;
|
|
|
|
String body;
|
|
|
|
|
|
|
|
String messageType = intent.getStringExtra("messageType");
|
|
|
|
if (!messageType.equals("PEBBLE_ALERT")) {
|
|
|
|
Log.i(TAG, "non PEBBLE_ALERT message type not supported");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
String notificationData = intent.getStringExtra("notificationData");
|
|
|
|
try {
|
|
|
|
JSONArray notificationJSON = new JSONArray(notificationData);
|
|
|
|
title = notificationJSON.getJSONObject(0).getString("title");
|
|
|
|
body = notificationJSON.getJSONObject(0).getString("body");
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (title != null && body != null) {
|
|
|
|
Intent startIntent = new Intent(context, BluetoothCommunicationService.class);
|
|
|
|
startIntent.setAction(BluetoothCommunicationService.ACTION_NOTIFICATION_SMS);
|
|
|
|
startIntent.putExtra("notification_sender", title);
|
|
|
|
startIntent.putExtra("notification_body", body);
|
|
|
|
context.startService(startIntent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|