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

Update to design which means services are used instead of standard classes....

Update to design which means services are used instead of standard classes. This also means context can be obtained easily in these classes, however further issues with this solution are still to be resolved, mainly in the stimulus and stimulus strategy areas of the code.
parent 9c5ca662
No related branches found
No related tags found
No related merge requests found
package com.example.notification;
import android.annotation.TargetApi;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import com.example.pharmacy.frontEnd.MainActivity;
/**
* Created by pharmacy on 09/01/2018.
*/
public interface Notification {
public abstract class Notification extends Service{
protected NotificationManager nm;
protected int snId = 0;
@Override
public void onCreate(){
nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
buildNotification();
}
protected void buildNotification(){
NotificationCompat.Builder mBuilder;
if(Build.VERSION.SDK_INT >= 26){
NotificationChannel nc = createNotificationChannel();
mBuilder = new NotificationCompat.Builder(this, "sednc");
} else {
mBuilder = new NotificationCompat.Builder(this, "sednc");
setParameters();
}
//Used to send the notification - will call Android OS for this purpose
public boolean send(Context context);
//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(this, 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(this);
// 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.
nm.notify(snId, mBuilder.build());
}
@TargetApi(26)
private NotificationChannel createNotificationChannel(){
NotificationChannel nc = new NotificationChannel("sednc", "sedentaryNotification", NotificationManager.IMPORTANCE_DEFAULT);
setChannelParameters();
return nc;
}
//TBC used to alter alert type - audio, visual, haptic etc. May need separate methods for these
public boolean setParameters();
protected abstract void setParameters();
protected abstract void setChannelParameters();
}
......@@ -4,10 +4,13 @@ import android.annotation.TargetApi;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
......@@ -19,61 +22,21 @@ import com.example.pharmacy.frontEnd.R;
* Created by pharmacy on 09/01/2018.
*/
public class SedentaryNotification implements Notification {
public class SedentaryNotification extends Notification {
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
}
protected void setParameters() {
//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;
protected void setChannelParameters() {
}
public boolean setParameters() {
return false;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
......@@ -41,6 +41,7 @@ class SedentaryStimulus extends BroadcastReceiver implements Stimulus {
// strategy = defaultStrategy;
//can no longer have default strategy in interface as strategies require context
}
//Should be context.startService('selectedstrategy') but require context here first?
strategy.monitor();
}
......@@ -51,8 +52,7 @@ class SedentaryStimulus extends BroadcastReceiver implements Stimulus {
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(context);
context.startService(intent);
}
......
package com.example.stimulusStrategy;
import android.app.Activity;
import android.app.Notification;
import android.app.Service;
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 android.os.IBinder;
import android.support.annotation.Nullable;
import com.example.stimulus.Stimulus;
......@@ -17,22 +21,28 @@ import java.util.Observer;
* Created by pharmacy on 09/01/2018.
*/
public class Accelerometer implements StimulusStrategy, SensorEventListener {
public class Accelerometer extends Service implements StimulusStrategy, SensorEventListener {
private SensorManager mSensorManager;
private Sensor accl;
private Context context;
//private Context context;
public Accelerometer(Context context){
this.context = context;
mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
@Override
public void onCreate(){
mSensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
accl = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
//Parameters to be set here such as delay etc.
//Does data streaming/listening need to be set up? If so here
}
/* public Accelerometer(Context context){
this.context = context;
}*/
public void monitor() {
Intent i = new Intent(context, Stimulus.class);
context.sendBroadcast(i);
Intent i = new Intent(this, Notification.class);
this.sendBroadcast(i);
}
......@@ -47,4 +57,9 @@ public class Accelerometer implements StimulusStrategy, SensorEventListener {
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
......@@ -2,9 +2,13 @@ package com.example.stimulusStrategy;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import com.example.notification.Notification;
import com.example.stimulus.Stimulus;
import java.util.Observable;
......@@ -14,20 +18,27 @@ import java.util.Observer;
* Created by pharmacy on 09/01/2018.
*/
public class Clock implements StimulusStrategy {
public class Clock extends Service implements StimulusStrategy {
private Context context;
public Clock(Context context){
this.context = context;
@Override
public void onCreate(){
monitor();
}
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);
//Intent should also be configured here to set up notification properly
AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(this, Notification.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
am.set(AlarmManager.ELAPSED_REALTIME, AlarmManager.INTERVAL_HOUR, pi);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
package com.example.stimulusStrategy;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import com.example.stimulus.Stimulus;
import java.util.Observable;
......@@ -9,17 +14,18 @@ import java.util.Observer;
* Created by pharmacy on 09/01/2018.
*/
public class LockScreenActivity extends Observable implements StimulusStrategy {
public class LockScreenActivity extends Service implements StimulusStrategy {
public void monitor() {
//Monitor a thing
setChanged();
notifyObservers();
clearChanged();
}
public void addObserver(Stimulus s) {
addObserver((Observer) s);
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
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