mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge.git
synced 2025-01-13 18:41:14 +01:00
a1cb246e27
Previously, the DeviceCommunicationService was invoked directly, via Intent intent = new Intent(foo, bar); intent.setExtra(EXTRA_BAZ, baz); startService(...); and this was scattered throughout GadgetBridge. Now there is a "frontend" available, so that you can call the service more easily, like GBApplication.deviceService().connect(); For a start, this client interface (DeviceService) actually implements the same interface (EventHandler) as the receiving side (DeviceSupport). This may change in the future. This will also make testing much easier, because we can use this client interface to invoke the test service as well.
58 lines
2.0 KiB
Java
58 lines
2.0 KiB
Java
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 org.json.JSONArray;
|
|
import org.json.JSONException;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
|
|
|
public class PebbleReceiver extends BroadcastReceiver {
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(PebbleReceiver.class);
|
|
|
|
@Override
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
if ("never".equals(sharedPrefs.getString("notification_mode_pebblemsg", "when_screen_off"))) {
|
|
return;
|
|
}
|
|
if ("when_screen_off".equals(sharedPrefs.getString("notification_mode_pebblemsg", "when_screen_off"))) {
|
|
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.info("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) {
|
|
GBApplication.deviceService().onSMS(title, body);
|
|
}
|
|
}
|
|
}
|