From c08a827f5a2ce5736c255a66ae014e2771237030 Mon Sep 17 00:00:00 2001 From: Peter De Jonckheere <peter.de-jonckheere.2014@uni.strath.ac.uk> Date: Wed, 18 Jul 2018 16:41:59 +0100 Subject: [PATCH] Very basic rules --- app.iml | 3 +- build.gradle | 3 +- .../sedentary/frontEnd/DataAnalysis.java | 25 ++-- .../sedentary/frontEnd/SaveFile.java | 109 +++++++++++++++++- .../stimulus/SedentaryStimulus.java | 16 ++- 5 files changed, 138 insertions(+), 18 deletions(-) diff --git a/app.iml b/app.iml index ecda003..7ecb5a7 100644 --- a/app.iml +++ b/app.iml @@ -99,7 +99,6 @@ <excludeFolder url="file://$MODULE_DIR$/build/intermediates/instant-run-support" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/javaPrecompile" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" /> - <excludeFolder url="file://$MODULE_DIR$/build/intermediates/lint" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/prebuild" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/reload-dex" /> @@ -112,7 +111,6 @@ <excludeFolder url="file://$MODULE_DIR$/build/intermediates/tmp" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/transforms" /> <excludeFolder url="file://$MODULE_DIR$/build/outputs" /> - <excludeFolder url="file://$MODULE_DIR$/build/reports" /> <excludeFolder url="file://$MODULE_DIR$/build/tmp" /> </content> <orderEntry type="jdk" jdkName="Android API 27 Platform" jdkType="Android SDK" /> @@ -132,6 +130,7 @@ <orderEntry type="library" name="com.jjoe64:graphview-4.2.2" level="project" /> <orderEntry type="library" name="com.google.android.gms:play-services-basement-15.0.1" level="project" /> <orderEntry type="library" scope="TEST" name="com.android.support.test:runner-1.0.2" level="project" /> + <orderEntry type="library" name="com.google.code.gson:gson:2.8.2@jar" level="project" /> <orderEntry type="library" name="com.android.support:support-vector-drawable-27.1.1" level="project" /> <orderEntry type="library" scope="TEST" name="com.google.code.findbugs:jsr305:2.0.1@jar" level="project" /> <orderEntry type="library" name="android.arch.lifecycle:runtime-1.1.0" level="project" /> diff --git a/build.gradle b/build.gradle index 2710eab..0ca755a 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ apply plugin: 'com.android.application' android { compileSdkVersion 27 defaultConfig { - applicationId "com.example.pharmacy.myapplication" + applicationId "com.notificationFramework" minSdkVersion 19 targetSdkVersion 27 versionCode 1 @@ -25,6 +25,7 @@ dependencies { implementation 'com.android.support.constraint:constraint-layout:1.1.2' implementation 'com.jjoe64:graphview:4.2.2' implementation 'com.google.android.gms:play-services-awareness:15.0.1' + implementation 'com.google.code.gson:gson:2.8.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' diff --git a/src/main/java/com/notificationFramework/sedentary/frontEnd/DataAnalysis.java b/src/main/java/com/notificationFramework/sedentary/frontEnd/DataAnalysis.java index 800503b..cc796fd 100644 --- a/src/main/java/com/notificationFramework/sedentary/frontEnd/DataAnalysis.java +++ b/src/main/java/com/notificationFramework/sedentary/frontEnd/DataAnalysis.java @@ -7,6 +7,7 @@ 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; @@ -72,20 +73,22 @@ public class DataAnalysis { editor.putInt(context.getString(R.string.ack_days), ackDays); editor.putFloat(context.getString(R.string.avg_ack), avgAck); editor.commit(); - } - private void placeAnalysis(Context context){ + static void placeAnalysis(Context context){ List<Place> places = SaveFile.getPlaces(context); - Place[] sortedPlaces = (Place[])(quicksort(places.toArray(), 0, places.size()-1)); + Object[] sortedPlaces = (quicksort(places.toArray(), 0, places.size()-1)); int popularType = getPopularElement(sortedPlaces); + SaveFile.writePopularType(context, popularType); + int mostPopular = SaveFile.findMostPopularType(context); + Toast.makeText(context, String.valueOf(mostPopular), Toast.LENGTH_LONG).show(); SharedPreferences preferences = context.getSharedPreferences(context.getString(R.string.preference_file_key), Context.MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); - editor.putInt(context.getString(R.string.store_mcp), 0); + editor.putInt(context.getString(R.string.store_mcp), mostPopular); editor.commit(); } - private Object[] quicksort(Object[] array, int low, int high) { + private static Object[] quicksort(Object[] array, int low, int high) { if (low < high) { int p = partition(array, low, high); quicksort(array, low, p - 1); @@ -94,7 +97,7 @@ public class DataAnalysis { return array; } - private int partition(Object[] array, int low, int high){ + 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++){ @@ -111,19 +114,19 @@ public class DataAnalysis { return i+1; } - private int getPopularElement(Place[] a) + private static int getPopularElement(Object[] a) { int count = 1; int tempCount; - int popular = a[0].getType(); - int temp = 0; + int popular = ((Place)a[0]).getType(); + int temp; for (int i = 0; i < (a.length - 1); i++) { - temp = a[i].getType(); + temp = ((Place)a[i]).getType(); tempCount = 0; for (int j = 1; j < a.length; j++) { - if (temp == a[j].getType()) + if (temp == ((Place)a[j]).getType()) tempCount++; } if (tempCount > count) diff --git a/src/main/java/com/notificationFramework/sedentary/frontEnd/SaveFile.java b/src/main/java/com/notificationFramework/sedentary/frontEnd/SaveFile.java index c57343d..be98283 100644 --- a/src/main/java/com/notificationFramework/sedentary/frontEnd/SaveFile.java +++ b/src/main/java/com/notificationFramework/sedentary/frontEnd/SaveFile.java @@ -36,7 +36,13 @@ import java.io.Serializable; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Calendar; +import java.util.Collection; +import java.util.Comparator; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.SortedMap; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -352,6 +358,8 @@ public class SaveFile { } } + + DataAnalysis.placeAnalysis(context); } @@ -446,7 +454,7 @@ public class SaveFile { com.notificationFramework.sedentary.frontEnd.Place place = new com.notificationFramework.sedentary.frontEnd.Place( items[i + 2], Float.parseFloat(items[i + 4]), Integer.parseInt(items[i + 3])); places.add(place); - }catch(NumberFormatException e){ + }catch(NumberFormatException | IndexOutOfBoundsException e){ } } @@ -459,6 +467,105 @@ public class SaveFile { return places; } + static void writePopularType(Context context, int popularType) { + String line = ""; + ArrayList<String> contents = new ArrayList<String>(); + boolean matched = false; + if (isExternalStorageMounted()) { + File dir = getDirectory(context); + File file = new File(dir, "POPLOG.txt"); + //Creates a new file if one doesn't already exist + if (!file.exists()) { + try { + file.createNewFile(); + } catch (IOException e) { + Log.e("FILE_ERROR", "COULDN'T CREATE NEW FILE"); + } + } + //Reads the current contents of the log file + try { + if (file.isFile() && file.canRead()) { + BufferedReader br = new BufferedReader(new FileReader(file)); + while ((line = br.readLine()) != null) { + contents.add(line + '\n'); + } + } + if (file.isFile() & file.canWrite()) { + BufferedWriter bw = new BufferedWriter(new FileWriter(file)); + for (String s : contents) { + String[] items = s.split(" "); + if (items[0].equals(String.valueOf(popularType))) { + int no = Integer.parseInt(items[1].trim()); + no++; + bw.write(items[0] + " " + String.valueOf(no)); + matched = true; + }else{ + bw.write(s); + } + } + if(!matched){ + bw.write(String.valueOf(popularType) + " " + String.valueOf(1)); + } + bw.close(); + } + }catch(IOException e){ + } + } + } + + static int findMostPopularType(Context context) { + String line = ""; + ArrayList<String> contents = new ArrayList<String>(); + boolean matched = false; + if (isExternalStorageMounted()) { + File dir = getDirectory(context); + File file = new File(dir, "POPLOG.txt"); + //Creates a new file if one doesn't already exist + if (!file.exists()) { + try { + file.createNewFile(); + } catch (IOException e) { + Log.e("FILE_ERROR", "COULDN'T CREATE NEW FILE"); + } + } + //Reads the current contents of the log file + try { + if (file.isFile() && file.canRead()) { + BufferedReader br = new BufferedReader(new FileReader(file)); + while ((line = br.readLine()) != null) { + contents.add(line + '\n'); + } + if(contents.size() >= 100) { + return mostPopular(contents); + } + } + }catch(IOException e){ + + } + + } + return -1; + } + + private static int mostPopular(List<String> contents){ + int count = 1; + int tempCount; + String[] items = contents.get(0).split(" "); + int popular = Integer.parseInt(items[0]); + int temp; + for (int i = 0; i < (contents.size() - 1); i++) + { + items = contents.get(i).split(" "); + temp = Integer.parseInt(items[0]); + tempCount = Integer.parseInt(items[1]); + if (tempCount > count){ + popular = temp; + count = tempCount; + } + } + return popular; + } + /** * Checks if external storage is available on the device. * diff --git a/src/main/java/com/notificationFramework/stimulus/SedentaryStimulus.java b/src/main/java/com/notificationFramework/stimulus/SedentaryStimulus.java index d3cad58..2f24b99 100644 --- a/src/main/java/com/notificationFramework/stimulus/SedentaryStimulus.java +++ b/src/main/java/com/notificationFramework/stimulus/SedentaryStimulus.java @@ -90,7 +90,8 @@ public class SedentaryStimulus extends BroadcastReceiver implements Stimulus { 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)) - && checkAcknowledged(shared, context)) { + && checkAcknowledged(shared, context) + && checkContext(shared, context)) { Log.d("SEDSTIM", "SENDING NOTIFICATION"); Intent i = new Intent(context.getApplicationContext(), com.notificationFramework.notification.SedentaryNotification.class); @@ -177,7 +178,16 @@ public class SedentaryStimulus extends BroadcastReceiver implements Stimulus { } private boolean checkContext(SharedPreferences shared, Context context){ - //Use weather information etc here to determine if notification should be sent. - return true; + int mostPop = shared.getInt(context.getString(R.string.store_mcp), -1); + float temp = shared.getFloat(context.getString(R.string.store_temp), 18); + if (mostPop >= 0 && mostPop < 999) { + //Assume work + return false; + } else if(temp > 18){ + //Assume hot so outside??? + return false; + }else{ + return true; + } } } -- GitLab