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

Various additions across the application

parent de0863b6
No related branches found
No related tags found
No related merge requests found
Showing
with 202 additions and 61 deletions
......@@ -4,6 +4,8 @@
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
......@@ -33,6 +35,12 @@
</service>
<receiver android:name="com.example.stimulus.SedentaryStimulus">
</receiver>
<receiver android:name="com.example.stimulus.BootLauncher">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
</action>
</intent-filter>
</receiver>
</application>
</manifest>
\ No newline at end of file
package com.example.notification;
import android.annotation.TargetApi;
import android.app.IntentService;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
......@@ -15,12 +16,13 @@ import android.util.Log;
import com.example.pharmacy.frontEnd.NotificationClicked;
import com.example.pharmacy.frontEnd.R;
import com.example.pharmacy.frontEnd.SaveFile;
/**
* Created by pharmacy on 09/01/2018.
*/
public abstract class Notification extends Service{
public abstract class Notification extends Service {
protected NotificationManager nm;
protected int snId = 0;
......@@ -56,6 +58,9 @@ public abstract class Notification extends Service{
);
mBuilder.setContentIntent(resultPendingIntent);
nm.notify(snId, mBuilder.build());
SaveFile.recordNotification(1,0,0,this);
SaveFile.recordNotificationType(this);
stopSelf();
}
@TargetApi(26)
......
......@@ -34,9 +34,14 @@ public class SedentaryNotification extends Notification {
protected NotificationCompat.Builder setParameters(NotificationCompat.Builder builder) {
//Get parameters from settings? or set strings?
builder.setSmallIcon(android.R.drawable.btn_star);
builder.setContentTitle(this.getClass().getName());
builder.setContentTitle(this.getClass().getSimpleName());
builder.setContentText(preferences.getString(getString(R.string.daily_goal_text), getString(R.string.notf_text)));
builder.setCategory(NotificationCompat.CATEGORY_ALARM);
builder.mNotification.flags |= android.app.Notification.FLAG_AUTO_CANCEL;
//SET VIBRATION PATTERN AND SOUND HERE
builder.setVibrate(new long[] {0, 500});
//builder.setSound();
builder.setColor(256);
return builder;
}
......
package com.example.pharmacy.frontEnd;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
......@@ -24,9 +25,20 @@ public class MainActivity extends AppCompatActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpButtons();
}
@Override
protected void onStart(){
super.onStart();
setUpStimulus();
}
@Override
protected void onStop(){
super.onStop();
}
private void setUpButtons(){
Button settings = (Button)findViewById(R.id.settings);
settings.setOnClickListener(new View.OnClickListener() {
......@@ -43,9 +55,19 @@ public class MainActivity extends AppCompatActivity {
}
private void setUpStimulus(){
Stimulus stimulus = new SedentaryStimulus(this, new Accelerometer());
if(!(isServiceRunning(com.example.stimulusStrategy.Accelerometer.class))) {
Stimulus stimulus = new SedentaryStimulus(this, new Accelerometer());
}
}
private boolean isServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
}
......@@ -28,61 +28,16 @@ public class NotificationClicked extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recordClick();
setUpButtons();
}
private void recordClick() {
int ns;
int nc;
int na;
if (isExternalStorageMounted()) {
File dir = getDirectory();
if (dir.isDirectory()) {
File file = new File(dir.getAbsolutePath() + "NLOG");
if (file.isFile() & file.canRead()) {
try {
BufferedReader br = new BufferedReader(new FileReader(file));
ns = Integer.parseInt(br.readLine());
nc = Integer.parseInt(br.readLine());
na = Integer.parseInt(br.readLine());
if (file.isFile() & file.canWrite()) {
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
nc++;
bw.write(ns);
bw.newLine();
bw.write(nc);
bw.newLine();
bw.write(na);
}
}catch (IOException e) {
}
}
}
}
}
/* Checks if external storage is available */
public boolean isExternalStorageMounted() {
return (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()));
@Override
protected void onStart(){
super.onStart();
SaveFile.recordNotification(0,1,0,this);
}
/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
return (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(Environment.getExternalStorageState()));
}
public File getDirectory(){
File file = new File(this.getExternalFilesDir(
Environment.DIRECTORY_DOCUMENTS), "NOTLOGS");
if (!file.mkdirs()) {
Log.e("ERR", "Directory not created");
}
return file;
}
private void setUpButtons(){
Button settings = (Button)findViewById(R.id.settings);
settings.setOnClickListener(new View.OnClickListener() {
......
package com.example.pharmacy.frontEnd;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.util.Log;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* Created by pharmacy on 12/02/2018.
*/
public class SaveFile {
public static void recordNotification(int sent, int clicked, int acknowledged, Context context) {
int ns;
int nc;
int na;
if (isExternalStorageMounted()) {
File dir = getDirectory(context);
if (dir.isDirectory()) {
File file = new File(dir.getAbsolutePath() + "NLOG");
if (file.isFile() & file.canRead()) {
try {
BufferedReader br = new BufferedReader(new FileReader(file));
ns = Integer.parseInt(br.readLine());
nc = Integer.parseInt(br.readLine());
na = Integer.parseInt(br.readLine());
if (file.isFile() & file.canWrite()) {
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
ns += sent;
nc += clicked;
na += acknowledged;
bw.write(ns);
bw.newLine();
bw.write(nc);
bw.newLine();
bw.write(na);
}
} catch (IOException e) {
}
}
}
}
}
public static void recordNotificationType(Context context) {
SharedPreferences shared = context.getSharedPreferences(context.getString(R.string.preference_file_key), Context.MODE_PRIVATE);
int sound = 0;
int led = 0;
int vibration = 0;
if (isExternalStorageMounted()) {
File dir = getDirectory(context);
if (dir.isDirectory()) {
File file = new File(dir.getAbsolutePath() + "NTYPELOG");
if (file.isFile() & file.canRead()) {
try {
BufferedReader br = new BufferedReader(new FileReader(file));
sound = Integer.parseInt(br.readLine());
led = Integer.parseInt(br.readLine());
vibration = Integer.parseInt(br.readLine());
if (file.isFile() & file.canWrite()) {
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
if(shared.getBoolean(context.getString(R.string.audio), false)) {
sound++;
}
if(shared.getBoolean(context.getString(R.string.led), false)){
led++;
}
if(shared.getBoolean(context.getString(R.string.vibration), false)) {
vibration++;
}
bw.write(sound);
bw.newLine();
bw.write(led);
bw.newLine();
bw.write(vibration);
}
} catch (IOException e) {
}
}
}
}
}
/* Checks if external storage is available */
private static boolean isExternalStorageMounted() {
return (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()));
}
private static File getDirectory(Context context){
File file = new File(context.getExternalFilesDir(
Environment.DIRECTORY_DOCUMENTS), "NOTLOGS");
if (!file.mkdirs()) {
Log.e("ERR", "Directory not created");
}
return file;
}
}
\ No newline at end of file
......@@ -35,6 +35,12 @@ public class Settings extends AppCompatActivity{
retrieveSettings();
}
@Override
protected void onStop(){
super.onStop();
saveSharedPreferenceData();
}
private void retrieveSettings(){
SharedPreferences shared = this.getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
((CheckBox)findViewById(R.id.Audio)).setChecked(shared.getBoolean(getString(R.string.audio), false));
......@@ -53,7 +59,7 @@ public class Settings extends AppCompatActivity{
@Override
public void onClick(View view) {
//Saved in shared preferences, add interger resource for default values to be used when obtaining from shared preferences.
saveSharedPreferenceData(view);
saveSharedPreferenceData();
Intent intent = new Intent(view.getContext(), MainActivity.class);
displaySavedToast(view);
......@@ -64,7 +70,6 @@ public class Settings extends AppCompatActivity{
}
private void populateSpinner(){
Spinner spinner = findViewById(R.id.PeriodSpinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.notification_periods, android.R.layout.simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
......@@ -76,7 +81,7 @@ public class Settings extends AppCompatActivity{
saved.show();
}
private void saveSharedPreferenceData(View view){
private void saveSharedPreferenceData(){
boolean audio = ((CheckBox)findViewById(R.id.Audio)).isChecked();
boolean vibration = ((CheckBox)findViewById(R.id.Vibration)).isChecked();
boolean led = ((CheckBox)findViewById(R.id.LEDFlash)).isChecked();
......@@ -84,7 +89,7 @@ public class Settings extends AppCompatActivity{
String message = ((EditText)findViewById(R.id.notificationMessage)).getText().toString();
int progress = ((SeekBar) findViewById(R.id.seekBar2)).getProgress();
boolean nswitch = ((Switch)findViewById(R.id.NotSwitch)).isChecked();
SharedPreferences sharedPref = view.getContext().getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
SharedPreferences sharedPref = this.getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean(getString(R.string.audio), audio);
editor.putBoolean(getString(R.string.vibration), vibration);
......
package com.example.stimulus;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import com.example.stimulusStrategy.Accelerometer;
/**
* Created by pharmacy on 12/02/2018.
*/
public class BootLauncher extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Stimulus stimulus = new SedentaryStimulus(context.getApplicationContext(), new Accelerometer());
}
}
}
......@@ -71,6 +71,9 @@ public class SedentaryStimulus extends BroadcastReceiver implements Stimulus {
Intent i = new Intent(context.getApplicationContext(), com.example.notification.SedentaryNotification.class);
context.getApplicationContext().startService(i);
}
//Stops previous service
Intent intent1 = new Intent(context, strategy.getClass());
context.stopService(intent1);
//Restarts the chosen strategy process.
chooseStrategy(strategy, context.getApplicationContext());
......
......@@ -6,6 +6,7 @@ import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
......@@ -34,11 +35,13 @@ public class Accelerometer extends Service implements StimulusStrategy, SensorEv
private Sensor accl;
private AlarmManager am;
//private Context context;
private int startId;
@Override
public int onStartCommand(Intent intent, int flags, int startId){
Log.d("ACCEL", "CREATED ACCEL");
this.startId = startId;
mSensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 60000000);
......@@ -51,13 +54,14 @@ public class Accelerometer extends Service implements StimulusStrategy, SensorEv
}
private void setUpClock() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 30);
SharedPreferences shared = this.getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
Intent i = new Intent(getBaseContext(), com.example.stimulus.SedentaryStimulus.class);
// i.putExtra(getString(R.string.serviceID), startId);
PendingIntent pi = PendingIntent.getBroadcast(getBaseContext(), R.integer.alarm_rc, i, PendingIntent.FLAG_UPDATE_CURRENT);
//test
am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+3000, pi);
//actual am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+AlarmManager.INTERVAL_HOUR, pi);
// int interval = shared.getInt(getString(R.string.notify_me), R.integer.notify_period_minutes);
//am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+(1000*60*interval), pi);
Log.d("myTag", "ALARM SET" + am.toString());
}
......
......@@ -19,5 +19,5 @@
<string name="daily_progress">Daily Progress</string>
<string name="motion">Motion Detected</string>
<string name="minute_progress">One Minute of Progress Made</string>
<string name="serviceID">com.example.sedbeh.serv</string>
</resources>
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