diff --git a/app/src/main/java/is/hellos/demos/MainApplication.java b/app/src/main/java/is/hellos/demos/MainApplication.java index ceec094..ae10910 100644 --- a/app/src/main/java/is/hellos/demos/MainApplication.java +++ b/app/src/main/java/is/hellos/demos/MainApplication.java @@ -16,6 +16,7 @@ public class MainApplication extends Application { private static final String PREF_NAME = SettingsActivity.class.getSimpleName() + ".PREF_NAME"; private static final String KEY_DEATH_NOTIFICATIONS = SettingsActivity.class.getSimpleName() + ".KEY_DEATH_NOTIFICATIONS"; + private static final String KEY_CRYING_NOTIFICATIONS = SettingsActivity.class.getSimpleName() + ".KEY_CRYING_NOTIFICATIONS"; @Nullable private ApiService apiService = null; @@ -67,4 +68,12 @@ public boolean shouldNotifyDeath() { public void setNotifyDeath(final boolean notify) { getSharedPreferences().edit().putBoolean(KEY_DEATH_NOTIFICATIONS, notify).apply(); } + + public void setNotifyCrying(boolean notify) { + getSharedPreferences().edit().putBoolean(KEY_CRYING_NOTIFICATIONS, notify).apply(); + } + + public boolean shouldNotifyCrying() { + return getSharedPreferences().getBoolean(KEY_CRYING_NOTIFICATIONS, false); + } } diff --git a/app/src/main/java/is/hellos/demos/activities/SettingsActivity.java b/app/src/main/java/is/hellos/demos/activities/SettingsActivity.java index 5f844dd..beff688 100644 --- a/app/src/main/java/is/hellos/demos/activities/SettingsActivity.java +++ b/app/src/main/java/is/hellos/demos/activities/SettingsActivity.java @@ -9,6 +9,7 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.annotation.StringRes; +import android.support.v4.content.LocalBroadcastManager; import android.util.Log; import android.widget.CompoundButton; import android.widget.Switch; @@ -16,11 +17,9 @@ import com.google.protobuf.InvalidProtocolBufferException; -import java.text.DecimalFormat; -import java.util.Locale; - import butterknife.BindView; import is.hellos.demos.R; +import is.hellos.demos.broadcastreceivers.NotificationBroadcastReceiver; import is.hellos.demos.models.protos.RadarMessages; import is.hellos.demos.models.protos.RespirationHealth; import is.hellos.demos.models.respiration.RespirationStat; @@ -34,7 +33,8 @@ public class SettingsActivity extends BaseActivity { private static final int REQUEST_DEATH = 1000; - private static final int NOTIFICATION_ID = 50; + private static final int NOTIFICATION_RESPIRATION_ID = 50; + private static final int NOTIFICATION_CRYING_ID = 51; private static final String EXTRA_OK = SettingsActivity.class.getSimpleName() + ".EXTRA_OK"; private Handler handler = new Handler(); private NotificationManager notificationManager; @@ -42,6 +42,8 @@ public class SettingsActivity extends BaseActivity { @BindView(R.id.activity_settings_switch_death) Switch deathSwitch; + @BindView(R.id.activity_settings_switch_crying) + Switch cryingSwitch; private CompoundButton.OnCheckedChangeListener deathSwitchCheckChangedListener = new CompoundButton.OnCheckedChangeListener() { @Override @@ -51,6 +53,14 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { } }; + private CompoundButton.OnCheckedChangeListener cryingSwitchCheckChangedListener = new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + getMainApplication().setNotifyCrying(isChecked); + updateCryingState(); + } + }; + public ZeroMQSubscriber.Listener babyStateListener = new MessageReceivedListener() { @@ -93,6 +103,27 @@ public void run() { }; + public ZeroMQSubscriber.Listener cryingListener = new MessageReceivedListener() { + + @Override + public void onMessageReceived(@NonNull final byte[] message) { + handler.post(new Runnable() { + @Override + public void run() { + try { + + if (getMainApplication().shouldNotifyCrying()) { + updateCryingNotification(RadarMessages.FeatureVector.parseFrom(message)); + } + } catch (InvalidProtocolBufferException e) { + e.printStackTrace(); + } + } + }); + } + + }; + @Override protected int getLayoutRes() { return R.layout.activity_settings; @@ -103,6 +134,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); updateState(); + updateCryingState(); ZeroMQSubscriber babyStateSubscriber = ZeroMQSubscriber.getBabyStateSubscriber(); babyStateSubscriber.setListener(babyStateListener); @@ -111,6 +143,10 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { ZeroMQSubscriber respirationSubscriber = new ZeroMQSubscriber(RESPIRATION_STATS_TOPIC); respirationSubscriber.setListener(respirationListener); new Thread(respirationSubscriber).start(); + + ZeroMQSubscriber cryingSubscriber = ZeroMQSubscriber.getCryingDetectorSubscriber(); + cryingSubscriber.setListener(cryingListener); + new Thread(cryingSubscriber).start(); onNewIntent(getIntent()); } @@ -160,7 +196,38 @@ private void updateState() { } else { stopNotification(); } + } + + private void updateCryingState() { + final boolean shouldNotifyCrying = getMainApplication().shouldNotifyCrying(); + this.cryingSwitch.setOnCheckedChangeListener(null); + this.cryingSwitch.setChecked(shouldNotifyCrying); + this.cryingSwitch.setOnCheckedChangeListener(cryingSwitchCheckChangedListener); + if (!shouldNotifyCrying) { + cancelCryingNotification(); + } + } + + private void updateCryingNotification(@NonNull final RadarMessages.FeatureVector featureVector) { + Log.d(SettingsActivity.class.getSimpleName(), featureVector.toString()); + + final float isCrying = featureVector.getFloatfeats(0); + + if (isCrying < 0.5) { + return; + } + + final is.hellos.demos.models.notification.Notification notification = new is.hellos.demos.models.notification.Notification( + "Crying Detected", + "Your baby is crying right now.", + NOTIFICATION_CRYING_ID, + MainActivity.class /*openActivity on press*/, + false /*isImportant*/); + LocalBroadcastManager.getInstance(this).sendBroadcast(NotificationBroadcastReceiver.getPushIntent(notification)); + } + private void cancelCryingNotification() { + LocalBroadcastManager.getInstance(this).sendBroadcast(NotificationBroadcastReceiver.getCancelIntent(NOTIFICATION_CRYING_ID)); } @@ -200,12 +267,12 @@ private void updateNotification(@Nullable final RespirationHealth.RespirationSta .setStyle(new Notification.BigTextStyle().bigText(messageText)) .setOngoing(true) .build(); - notificationManager.notify(NOTIFICATION_ID, builder.build()); + notificationManager.notify(NOTIFICATION_RESPIRATION_ID, builder.build()); } private void stopNotification() { - this.notificationManager.cancel(NOTIFICATION_ID); + this.notificationManager.cancel(NOTIFICATION_RESPIRATION_ID); } diff --git a/app/src/main/java/is/hellos/demos/broadcastreceivers/NotificationBroadcastReceiver.java b/app/src/main/java/is/hellos/demos/broadcastreceivers/NotificationBroadcastReceiver.java index 189a3c8..a5f050b 100644 --- a/app/src/main/java/is/hellos/demos/broadcastreceivers/NotificationBroadcastReceiver.java +++ b/app/src/main/java/is/hellos/demos/broadcastreceivers/NotificationBroadcastReceiver.java @@ -63,7 +63,7 @@ public void onReceive(Context context, Intent intent) { final Notification notification = (Notification) intent.getSerializableExtra(EXTRA_NOTIFICATION); final Intent activityIntent = new Intent(context, notification.getTargetClass()); final NotificationCompat.Builder builder = new NotificationCompat.Builder(context); - builder.setSmallIcon(R.mipmap.ic_launcher); + builder.setSmallIcon(R.drawable.ic_child_care_white_24dp); builder.setContentTitle(notification.getTitle()); builder.setContentText(notification.getMsg()); builder.setCategory(android.app.Notification.CATEGORY_EVENT); diff --git a/app/src/main/java/is/hellos/demos/network/zmq/MessageReceivedListener.java b/app/src/main/java/is/hellos/demos/network/zmq/MessageReceivedListener.java index 32ec922..b9a4212 100644 --- a/app/src/main/java/is/hellos/demos/network/zmq/MessageReceivedListener.java +++ b/app/src/main/java/is/hellos/demos/network/zmq/MessageReceivedListener.java @@ -1,15 +1,19 @@ package is.hellos.demos.network.zmq; +import android.util.Log; + public abstract class MessageReceivedListener implements ZeroMQSubscriber.Listener { @Override public void onConnecting() { + Log.d(MessageReceivedListener.class.getSimpleName(), "onConnecting"); // postToast(R.string.state_connecting); } @Override public void onConnected() { + Log.d(MessageReceivedListener.class.getSimpleName(), "onConnected"); // postToast(R.string.state_connected); } diff --git a/app/src/main/java/is/hellos/demos/network/zmq/ZeroMQSubscriber.java b/app/src/main/java/is/hellos/demos/network/zmq/ZeroMQSubscriber.java index 3a0b28c..78fbf39 100644 --- a/app/src/main/java/is/hellos/demos/network/zmq/ZeroMQSubscriber.java +++ b/app/src/main/java/is/hellos/demos/network/zmq/ZeroMQSubscriber.java @@ -1,6 +1,5 @@ package is.hellos.demos.network.zmq; -import android.os.NetworkOnMainThreadException; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.util.Log; @@ -10,6 +9,7 @@ public class ZeroMQSubscriber implements Runnable { + public static final String CRYING_TOPIC = "v1/is_crying"; public static final String BASEBAND_TOPIC = "v1/baseband"; public static final String PLOT_TOPIC = "PLOT"; public static final String STATS_TOPIC = "STATS"; @@ -47,6 +47,7 @@ public class ZeroMQSubscriber implements Runnable { private static final String BABY_STATE_IP_ADDRESS = "tcp://192.168.128.40:5565"; // private static final String IP_ADDRESS = "tcp://192.168.128.119:5564"; private static final String IP_ADDRESS = "tcp://192.168.128.40:5564"; + private static final String CRYING_IP_ADDRESS = "tcp://192.168.128.40:5566"; private static final Listener EMPTY_LISTENER = new Listener() { @Override @@ -81,6 +82,10 @@ public static ZeroMQSubscriber getBabyStateSubscriber() { return new ZeroMQSubscriber(BABY_STATE_TOPIC, BABY_STATE_IP_ADDRESS); } + public static ZeroMQSubscriber getCryingDetectorSubscriber() { + return new ZeroMQSubscriber(CRYING_TOPIC, CRYING_IP_ADDRESS); + } + public ZeroMQSubscriber(@NonNull final String topic) { this(topic, IP_ADDRESS); } diff --git a/app/src/main/res/layout/activity_settings.xml b/app/src/main/res/layout/activity_settings.xml index 86ff70f..49c6300 100644 --- a/app/src/main/res/layout/activity_settings.xml +++ b/app/src/main/res/layout/activity_settings.xml @@ -6,8 +6,15 @@ android:padding="24dp"> + + + android:text="Crying Detected Notifications" + android:layout_marginTop="24dp"/> \ No newline at end of file