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

import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
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(context.getApplicationContext(),
                    com.notificationFramework.stimulus.FeedbackStimulus.class);
            LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context.getApplicationContext());
            lbm.sendBroadcast(i);
            logAcknowledgement(context);

    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);
        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();
Peter Joseph De Jonckheere CESM2014's avatar
a  
Peter Joseph De Jonckheere CESM2014 committed
        SharedPreferences.Editor editor = preferences.edit();
        editor.putInt(context.getString(R.string.store_mcp), mostPopular);
Peter Joseph De Jonckheere CESM2014's avatar
a  
Peter Joseph De Jonckheere CESM2014 committed
        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;
    }