Skip to content
Snippets Groups Projects
Commit 9c5ca662 authored by Peter Joseph De Jonckheere CESM2014's avatar Peter Joseph De Jonckheere CESM2014
Browse files

Mid work commit with some sensors possibly implemented and notifications...

Mid work commit with some sensors possibly implemented and notifications completed. Both are as yet untested.
parent ff4b0bb4
No related branches found
No related tags found
No related merge requests found
package com.example.notification;
import android.content.Context;
/**
* Created by pharmacy on 09/01/2018.
*/
......@@ -7,7 +8,7 @@ package com.example.notification;
public interface Notification {
//Used to send the notification - will call Android OS for this purpose
public boolean send();
public boolean send(Context context);
//TBC used to alter alert type - audio, visual, haptic etc. May need separate methods for these
public boolean setParameters();
......
package com.example.notification;
import android.annotation.TargetApi;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import com.example.pharmacy.frontEnd.MainActivity;
import com.example.pharmacy.frontEnd.R;
/**
* Created by pharmacy on 09/01/2018.
*/
public class SedentaryNotification implements Notification {
public boolean send() {
private int snId = 0;
public boolean send(Context context) {
buildNotification(context);
return false;
}
private boolean buildNotification(Context context){
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder;
if(Build.VERSION.SDK_INT >= 26){
NotificationChannel nc = createNotificationChannel();
mBuilder = new NotificationCompat.Builder(context, "sednc");
} else {
mBuilder = new NotificationCompat.Builder(context, "sednc");
//Set parameters for non nc Builder here
}
//Up to set Content Intent taken from developers page and must be refined or if works cited.
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(context, MainActivity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
// mId allows you to update the notification later on.
mNotificationManager.notify(snId, mBuilder.build());
return false;
}
@TargetApi(26)
private NotificationChannel createNotificationChannel(){
NotificationChannel nc = new NotificationChannel("sednc", "sedentaryNotification", NotificationManager.IMPORTANCE_DEFAULT);
//set parameters of nc dependent on user requiements
return nc;
}
public boolean setParameters() {
return false;
......
package com.example.pharmacy.frontEnd;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
......@@ -10,4 +13,5 @@ public class MainActivity extends AppCompatActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
package com.example.pharmacy.frontEnd;
/**
* Created by pharmacy on 21/11/2017.
*/
......
package com.example.stimulus;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import com.example.notification.Notification;
import com.example.notification.SedentaryNotification;
import com.example.stimulusStrategy.StimulusStrategy;
......@@ -10,15 +14,17 @@ import java.util.Observable;
* Created by pharmacy on 09/01/2018.
*/
class SedentaryStimulus implements Stimulus {
class SedentaryStimulus extends BroadcastReceiver implements Stimulus {
private StimulusStrategy strategy;
public SedentaryStimulus(){
// this.context = context;
chooseStrategy(null);
}
public SedentaryStimulus(StimulusStrategy s){
// this.context = context;
chooseStrategy(s);
}
......@@ -32,17 +38,22 @@ class SedentaryStimulus implements Stimulus {
if(s != null){
strategy = s;
} else{
strategy = defaultStrategy;
// strategy = defaultStrategy;
//can no longer have default strategy in interface as strategies require context
}
strategy.addObserver(this);
strategy.monitor();
}
@Override
//Called by notify and will create Notification if required then send
public void update(Observable observable, Object o) {
public void onReceive(Context context, Intent intent) {
//If a condition - such as time elapsed since last notification or not night time
//Intent data used here to configure notification?? Intent would need to be configured properly for this.
Notification notification = createNotification();
notification.send();
notification.send(context);
}
}
package com.example.stimulus;
import android.content.BroadcastReceiver;
import com.example.notification.Notification;
import com.example.stimulusStrategy.Accelerometer;
import com.example.stimulusStrategy.Clock;
......@@ -11,9 +13,9 @@ import java.util.Observer;
* Created by pharmacy on 09/01/2018.
*/
public interface Stimulus extends Observer {
public interface Stimulus {
StimulusStrategy defaultStrategy = new Clock();
//StimulusStrategy defaultStrategy = new Clock(context);
//Creates an instance of the desired notifcation type (Factory Method)
//TBD if this is to be public
......
package com.example.stimulusStrategy;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import com.example.stimulus.Stimulus;
import java.util.Observable;
......@@ -9,17 +17,34 @@ import java.util.Observer;
* Created by pharmacy on 09/01/2018.
*/
public class Accelerometer extends Observable implements StimulusStrategy {
public class Accelerometer implements StimulusStrategy, SensorEventListener {
private SensorManager mSensorManager;
private Sensor accl;
private Context context;
public Accelerometer(Context context){
this.context = context;
mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
accl = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
//Parameters to be set here such as delay etc.
}
public void monitor() {
//Monitor a thing
Intent i = new Intent(context, Stimulus.class);
context.sendBroadcast(i);
}
setChanged();
notifyObservers();
clearChanged();
@Override
public void onSensorChanged(SensorEvent event) {
monitor();
}
public void addObserver(Stimulus s) {
addObserver((Observer) s);
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
}
package com.example.stimulusStrategy;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import com.example.stimulus.Stimulus;
import java.util.Observable;
......@@ -9,17 +14,20 @@ import java.util.Observer;
* Created by pharmacy on 09/01/2018.
*/
public class Clock extends Observable implements StimulusStrategy {
public void monitor() {
//Monitor a thing
public class Clock implements StimulusStrategy {
setChanged();
notifyObservers();
clearChanged();
private Context context;
public Clock(Context context){
this.context = context;
}
public void addObserver(Stimulus s) {
addObserver((Observer) s);
public void monitor() {
//Does intent require parameters??
//Similar for pending intent??
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, Stimulus.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.set(AlarmManager.ELAPSED_REALTIME, AlarmManager.INTERVAL_HOUR, pi);
}
}
package com.example.stimulusStrategy;
import android.app.Activity;
import com.example.stimulus.Stimulus;
import java.util.Observable;
......@@ -22,6 +24,5 @@ public interface StimulusStrategy {
//Will notify the Stimulus when monitoring is true. Monitoring only started by method monitor() then carried out internally
public void notify();
*/
//
void addObserver(Stimulus s);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment