Skip to content
Snippets Groups Projects
DataAnalysis.java 2.88 KiB
package com.notificationFramework.sedentary.frontEnd;

import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

/**
 * 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(context.getApplicationContext(),
                    com.notificationFramework.stimulus.FeedbackStimulus.class);
            LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context.getApplicationContext());
            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();

    }

    private void placeAnalysis(){
        
    }
}