Something went wrong on our end
-
Peter Joseph De Jonckheere CESM2014 authoredPeter Joseph De Jonckheere CESM2014 authored
DataAnalysis.java 9.23 KiB
package com.notificationFramework.sedentary.frontEnd;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.icu.util.RangeValueIterator;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.widget.Toast;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import static com.notificationFramework.sedentary.frontEnd.SaveFile.getPlaces;
/**
* Created by Peter De Jonckheere on 19/06/2018.
*/
public class DataAnalysis {
static boolean analyse(String line, Calendar cal, Context context) {
Calendar compCal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEEMMMddHH:mm:sszyyyy", Locale.ENGLISH);
String items[] = line.split(" ");
String dateString = items[0] + items[1] + items[2] + items[3] + items[4] + items[5];
try {
compCal.setTime(sdf.parse(dateString));
} catch (ParseException e) {
Log.e("ANALYSING", "PARSINGERROR");
}
compCal.add(Calendar.MINUTE, -6);
if (compCal.before(cal.getTime())) {
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
try {
nm.cancel(context.getResources().getInteger(R.integer.notf_id));
} catch(NullPointerException e){
Log.i("NOTIFICATION", "NOTIFICATION WAS CLICKED ON");
}
Intent i = new Intent("FEEDBACKFILTER");
final LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context.getApplicationContext());
BroadcastReceiver feedback = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("FEEDBACK STIM", "EHH HELP");
SharedPreferences shared =
context.getSharedPreferences(context.getString(R.string.preference_file_key), Context.MODE_PRIVATE);
if ((checkTime(shared, context)) && (shared.getBoolean(context.getString(R.string.notf_switch), true))) {
Intent i = new Intent(context.getApplicationContext(),
com.notificationFramework.notification.FeedbackNotification.class);
context.getApplicationContext().startService(i);
}
lbm.unregisterReceiver(this);
}
/**
* Checks the current time against the do not disturb time by first checking if the time spans 2
* days, then checking between the first time and midnight, then midnight and the second time,
* followed finally by the simple check between the two times if the time does not span 2 days.
* It is done in this way to avoid compications with date changes as each time is obtained using
* the current calendar as a template so if done correctly dates should not need to be changed.
*
* @param shared the SharedPreferences instance set up in the onReceive() method
* @param context the application context which this method has been called from
* @return false if the time is within the do not disturb period and so a notification should
* not be sent, true if a notification should be sent
*/
private boolean checkTime(SharedPreferences shared, Context context) {
Calendar start = Calendar.getInstance();
start.set(Calendar.HOUR_OF_DAY, shared.getInt(context.getString(R.string.dnd_shour), 23));
start.set(Calendar.MINUTE, shared.getInt(context.getString(R.string.dnd_smin), 0));
Calendar midnightCal = Calendar.getInstance();
midnightCal.set(Calendar.HOUR_OF_DAY, 23);
midnightCal.set(Calendar.MINUTE, 59);
midnightCal.set(Calendar.SECOND, 59);
Calendar end = Calendar.getInstance();
end.set(Calendar.HOUR_OF_DAY, shared.getInt(context.getString(R.string.dnd_ehour), 9));
end.set(Calendar.MINUTE, shared.getInt(context.getString(R.string.dnd_emin), 0));
Calendar curCal = Calendar.getInstance();
if (shared.getInt(context.getString(R.string.dnd_shour), 23) > shared.getInt(context.getString(R.string.dnd_ehour), 9)) {
if (curCal.getTime().after(start.getTime()) && curCal.getTime().before(midnightCal.getTime())) {
return false;
} else {
midnightCal.set(Calendar.HOUR_OF_DAY, 0);
midnightCal.set(Calendar.MINUTE, 0);
midnightCal.set(Calendar.SECOND, 1);
if (curCal.getTime().after(midnightCal.getTime()) && curCal.getTime().before(end.getTime())) {
return false;
} else {
return true;
}
}
} else if (curCal.getTime().after(start.getTime()) && curCal.getTime().before(end.getTime())) {
return false;
} else {
return true;
}
}
};
lbm.registerReceiver(feedback, new IntentFilter("FEEDBACKFILTER"));
lbm.sendBroadcast(i);
logAcknowledgement(context);
return true;
} else {
return false;
}
}
private static void logAcknowledgement(Context context) {
SharedPreferences sharedPref = context.getSharedPreferences(context.getString(R.string.preference_file_key), Context.MODE_PRIVATE);
int totalAck = sharedPref.getInt(context.getString(R.string.ack_total), 0);
int ackDays = sharedPref.getInt(context.getString(R.string.ack_days), 1);
float avgAck = 0;
int currentDay = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
SharedPreferences.Editor editor = sharedPref.edit();;
if (currentDay != sharedPref.getInt(context.getString(R.string.progress_day), 0)) {
avgAck = totalAck / ackDays;
ackDays++;
editor.putInt(context.getString(R.string.progress_day), currentDay);
}
totalAck++;
editor.putInt(context.getString(R.string.ack_total), totalAck);
editor.putInt(context.getString(R.string.ack_days), ackDays);
editor.putFloat(context.getString(R.string.avg_ack), avgAck);
editor.commit();
}
static void placeAnalysis(Context context){
SharedPreferences preferences = context.getSharedPreferences(context.getString(R.string.preference_file_key), Context.MODE_PRIVATE);
int acknowledged = preferences.getInt(context.getString(R.string.store_last_ack), 0);
List<Place> places = SaveFile.getPlaces(context);
Object[] sortedPlaces = (quicksort(places.toArray(), 0, places.size()-1));
int popularType = getPopularElement(sortedPlaces);
SaveFile.writePopularType(context, popularType, acknowledged);
int mostPopular = SaveFile.findMostPopularType(context);
Toast.makeText(context, String.valueOf(mostPopular), Toast.LENGTH_LONG).show();
SharedPreferences.Editor editor = preferences.edit();
editor.putInt(context.getString(R.string.store_mcp), mostPopular);
editor.commit();
}
private static Object[] quicksort(Object[] array, int low, int high) {
if (low < high) {
int p = partition(array, low, high);
quicksort(array, low, p - 1);
quicksort(array, p + 1, high);
}
return array;
}
private static int partition(Object[] array, int low, int high){
int pivot = ((Place) array[high]).getType();
int i = low - 1;
for(int j = low; j < high - 1; j++){
if(((Place)array[j]).getType() < pivot){
i++;
Place swap = ((Place)array[j]);
array[j] = array[i];
array[i] = swap;
}
}
Place swap = ((Place) array[i+1]);
array[i+1] = array[high];
array[high] = swap;
return i+1;
}
private static int getPopularElement(Object[] a)
{
int count = 1;
int tempCount;
int popular = ((Place)a[0]).getType();
int temp;
for (int i = 0; i < (a.length - 1); i++)
{
temp = ((Place)a[i]).getType();
tempCount = 0;
for (int j = 1; j < a.length; j++)
{
if (temp == ((Place)a[j]).getType())
tempCount++;
}
if (tempCount > count)
{
popular = temp;
count = tempCount;
}
}
return popular;
}
}