Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v1/is_crying push notifications #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/src/main/java/is/hellos/demos/MainApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}
79 changes: 73 additions & 6 deletions app/src/main/java/is/hellos/demos/activities/SettingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@
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;
import android.widget.Toast;

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;
Expand All @@ -34,14 +33,17 @@
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;
private int currentRpm = -1;

@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
Expand All @@ -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() {

Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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());
}

Expand Down Expand Up @@ -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));
}


Expand Down Expand Up @@ -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);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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);

}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
11 changes: 9 additions & 2 deletions app/src/main/res/layout/activity_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@
android:padding="24dp">

<Switch
android:id="@+id/activity_settings_switch_death"
android:id="@+id/activity_settings_switch_death"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Respiration Notifications"/>

<Switch
android:id="@+id/activity_settings_switch_crying"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Notify on Death"/>
android:text="Crying Detected Notifications"
android:layout_marginTop="24dp"/>
</LinearLayout>