From 323eb215d59a8506434b8e474e1fbf4a33d6d768 Mon Sep 17 00:00:00 2001 From: Jannik Date: Fri, 26 Aug 2016 18:51:01 +0200 Subject: [PATCH] New Break Activity and bug fixes Now only one body part comes at a time in the breaks If the language is changed, the app will not crush anymore Added Fixme to DB handler Saved the old Break Activity --- .../AppWidget.java | 22 +- .../BreakActivity.java | 50 ++- .../BreakActivity_old.java | 403 ++++++++++++++++++ .../BreakReminder.java | 23 + .../DBHandler.java | 9 +- 5 files changed, 470 insertions(+), 37 deletions(-) create mode 100644 app/src/main/java/orgprivacy_friendly_apps/secuso/privacyfriendlybreakreminder/BreakActivity_old.java diff --git a/app/src/main/java/orgprivacy_friendly_apps/secuso/privacyfriendlybreakreminder/AppWidget.java b/app/src/main/java/orgprivacy_friendly_apps/secuso/privacyfriendlybreakreminder/AppWidget.java index aad0909..f8d4d0d 100644 --- a/app/src/main/java/orgprivacy_friendly_apps/secuso/privacyfriendlybreakreminder/AppWidget.java +++ b/app/src/main/java/orgprivacy_friendly_apps/secuso/privacyfriendlybreakreminder/AppWidget.java @@ -34,8 +34,12 @@ public class AppWidget extends AppWidgetProvider { clockTime = workTime + ":00"; // Construct the RemoteViews object - if (views == null) - onAppWidgetOptionsReset(context,appWidgetManager,appWidgetId,appWidgetManager.getAppWidgetOptions(appWidgetId)); + if (views == null){ + int minWidth = appWidgetManager.getAppWidgetOptions(appWidgetId).getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH); + int minHeight = appWidgetManager.getAppWidgetOptions(appWidgetId).getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT); + + getRemoteViews(context, minWidth, minHeight); + } views.setTextViewText(R.id.appwidget_text, widgetText); if (time.equals("")) @@ -86,20 +90,6 @@ public class AppWidget extends AppWidgetProvider { super.onReceive(context, intent); } - public static void onAppWidgetOptionsReset(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) { - - - System.out.println("Minimal width: " + newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH) + " minimal height: " + newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT)); - - // Get min width and height. - int minWidth = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH); - int minHeight = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT); - - - getRemoteViews(context, minWidth, minHeight); - - } - @Override public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) { diff --git a/app/src/main/java/orgprivacy_friendly_apps/secuso/privacyfriendlybreakreminder/BreakActivity.java b/app/src/main/java/orgprivacy_friendly_apps/secuso/privacyfriendlybreakreminder/BreakActivity.java index 316313a..c8bf4c4 100644 --- a/app/src/main/java/orgprivacy_friendly_apps/secuso/privacyfriendlybreakreminder/BreakActivity.java +++ b/app/src/main/java/orgprivacy_friendly_apps/secuso/privacyfriendlybreakreminder/BreakActivity.java @@ -9,6 +9,7 @@ import android.net.Uri; import android.os.CountDownTimer; import android.os.Vibrator; import android.preference.PreferenceManager; +import android.support.annotation.NonNull; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; @@ -17,6 +18,9 @@ import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; +import java.util.Collection; +import java.util.Iterator; +import java.util.ListIterator; import java.util.Random; import java.util.ArrayList; @@ -155,7 +159,6 @@ public class BreakActivity extends AppCompatActivity implements View.OnClickList if (!sections.contains(currentExerciseSection)) { sections.add(currentExerciseSection); exerciseList = allAvailableExercises.get(currentExerciseSection); - break_exercise_type.setText(exercises[currentExerciseSection]); System.out.println("Random id for section election: " + currentExerciseSection); break; } @@ -205,17 +208,39 @@ public class BreakActivity extends AppCompatActivity implements View.OnClickList } } + private void setRandomExercises() { allAvailableExercises = new ArrayList<>(); System.out.println("Number of sections: " + exercises.length); - for (int i = 0; i < exercises.length; i++) { - List list = dbHandler.getExercisesFromSection(exercises[i]); - allAvailableExercises.add(list); - System.out.println("Section: " + exercises[i] + " and number of ex for it: " + list.size()); + + String usedSectionsString = sharedPrefs.getString("currently_done_exercises", ""); + System.out.println("Number of used sections " + usedSectionsString.split("\\.").length + " " + usedSectionsString); + SharedPreferences.Editor editor = sharedPrefs.edit(); + + + if(exercises.length == usedSectionsString.split("\\.").length) { + usedSectionsString = ""; } + //Selection of the Section + boolean notFoundYet = true; + + while(notFoundYet){ + currentExerciseSection = random.nextInt(exercises.length); + if(!usedSectionsString.contains(exercises[currentExerciseSection])){ + List list = dbHandler.getExercisesFromSection(exercises[currentExerciseSection]); + allAvailableExercises.add(list); + usedSectionsString += exercises[currentExerciseSection] + "."; + editor.putString("currently_done_exercises", usedSectionsString); + notFoundYet = false; + System.out.println("Section: " + exercises[currentExerciseSection] + " and number of ex for it: " + list.size()); + } + } + + editor.apply(); + currentExerciseSection = random.nextInt(allAvailableExercises.size()); System.out.println("Random id for section election: " + currentExerciseSection); @@ -308,7 +333,6 @@ public class BreakActivity extends AppCompatActivity implements View.OnClickList if (!sections.contains(currentExerciseSection)) { sections.add(currentExerciseSection); exerciseList = allAvailableExercises.get(currentExerciseSection); - break_exercise_type.setText(exercises[currentExerciseSection]); System.out.println("Random id for section election: " + currentExerciseSection); break; } @@ -343,20 +367,6 @@ public class BreakActivity extends AppCompatActivity implements View.OnClickList // Update image and description of the exercise update(); - -// //Show how much time is left -// timeLeft = sharedPrefs.getBoolean("notifications_new_message_timeLeft", false); -// if (timeLeft) { -// Notification notification = new NotificationCompat.Builder(getApplicationContext()).setCategory(Notification.CATEGORY_MESSAGE) -// .setSmallIcon(R.drawable.ic_notifications_black_24dp) -// .setContentTitle("Break Activity Reminder: ") -// .setContentText(((millisUntilFinished / 1000) / 60) + "Minutes and " + (millisUntilFinished / 1000 % 60) + " seconds") -// .setAutoCancel(true) -// .setVisibility(NotificationCompat.VISIBILITY_PUBLIC).build(); -// NotificationManager notificationManager = -// (NotificationManager) getSystemService(NOTIFICATION_SERVICE); -// notificationManager.notify(999, notification); -// } } public void onFinish() { diff --git a/app/src/main/java/orgprivacy_friendly_apps/secuso/privacyfriendlybreakreminder/BreakActivity_old.java b/app/src/main/java/orgprivacy_friendly_apps/secuso/privacyfriendlybreakreminder/BreakActivity_old.java new file mode 100644 index 0000000..7aad6d2 --- /dev/null +++ b/app/src/main/java/orgprivacy_friendly_apps/secuso/privacyfriendlybreakreminder/BreakActivity_old.java @@ -0,0 +1,403 @@ +package orgprivacy_friendly_apps.secuso.privacyfriendlybreakreminder; + +import android.app.NotificationManager; +import android.content.Context; +import android.content.SharedPreferences; +import android.media.Ringtone; +import android.media.RingtoneManager; +import android.net.Uri; +import android.os.Bundle; +import android.os.CountDownTimer; +import android.os.Vibrator; +import android.preference.PreferenceManager; +import android.support.v7.app.AppCompatActivity; +import android.view.View; +import android.view.WindowManager; +import android.widget.Button; +import android.widget.ImageView; +import android.widget.TextView; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +/** + * Created by badri_000 on 26.08.2016. + */ +public class BreakActivity_old extends AppCompatActivity implements View.OnClickListener { + + private TextView ct_text; + private CountDownTimer ct; + private String stopTime = "", sideRepetition = ""; + String image1, image2; + private boolean isRunning = false; + private List exerciseList; + private SharedPreferences sharedPrefs; + private TextView description, side_repetition, break_exercise_type, execution; + private int currentExercise, breakTime = 0, currentExerciseSection; + private ImageView image; + private String[] exercises; + private DBHandler dbHandler; + private List> allAvailableExercises; + private List sections; + private Random random; + private boolean exerciseSide = false; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + currentExercise = 0; + currentExerciseSection = 0; + sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); + int mins = sharedPrefs.getInt("break_value", 5); + String bufferZeroMinute = ""; + + if (mins < 10) + bufferZeroMinute = "0"; + + String[] allProfiles = sharedPrefs.getString("profiles", "").split(";"); + String currentProfile = sharedPrefs.getString("name_text", ""); + + for (int i = 0; i < allProfiles.length; i++) { + if (allProfiles[i].split(",")[0].equals(currentProfile) && !allProfiles[i].split(",")[4].equals("-1")) { + exercises = allProfiles[i].split(",")[4].split("\\."); + } + } + + if (exercises == null) { + setContentView(R.layout.activity_break_no_exercises); + Button cancelButton = (Button) findViewById(R.id.button_cancel); + cancelButton.setOnClickListener(this); + ct_text = (TextView) findViewById(R.id.textViewBreak1); + + ct_text.setText(bufferZeroMinute + mins + ":00"); + ct_text.setOnClickListener(this); + } else { + setContentView(R.layout.activity_break); + Button nextButton = (Button) findViewById(R.id.button_next); + nextButton.setOnClickListener(this); + ct_text = (TextView) findViewById(R.id.textViewBreak); + ct_text.setText(bufferZeroMinute + mins + ":00"); + ct_text.setOnClickListener(this); + + dbHandler = new DBHandler(this); + random = new Random(); + sections = new ArrayList<>(); + setRandomExercises(); + } + + //Keep screen on while on break + getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + } + + public void onClick(View v) { + int mins = sharedPrefs.getInt("break_value", 10); + String bufferZeroMinute = ""; + String bufferZeroSecond = ""; + int time = mins * 60 * 1000; + + if (stopTime == "" && !isRunning) { + if (time / 1000 / 60 < 10) + bufferZeroMinute = "0"; + + ct_text.setText(bufferZeroMinute + time / 1000 / 60 + ":00"); + } else if (!isRunning) { + ct_text.setText(stopTime); + String stringTime = (String) ct_text.getText(); + String[] timef = stringTime.split(":"); + int minute = Integer.parseInt(timef[0]); + int second = Integer.parseInt(timef[1]); + System.out.println("Minute: " + minute + " Second: " + second); + time = (1000 * (minute * 60)) + (1000 * second); + + if (minute < 10) + bufferZeroMinute = "0"; + if (second < 10) + bufferZeroSecond = "0"; + + ct_text.setText(bufferZeroMinute + minute + ":" + bufferZeroSecond + second); + + } + + switch (v.getId()) { + + case R.id.textViewBreak1: + case R.id.textViewBreak: + if (isRunning) { + ct.cancel(); + stopTime = (String) ct_text.getText(); + isRunning = false; + } else { + startTimer(time); + } + break; + + + case R.id.button_cancel: + if (ct != null) + ct.cancel(); + finish(); + break; + + case R.id.button_next: + + // Next Exercise + currentExercise++; + side_repetition.setText(R.string.exercise_break); + if (currentExercise > exerciseList.size() - 1) { + currentExercise = 0; + if (sections.size() == allAvailableExercises.size()) { + System.out.println("Did all exercises, restart!"); + sections = new ArrayList<>(); + } + while (true) { + + currentExerciseSection = random.nextInt(allAvailableExercises.size()); + if (!sections.contains(currentExerciseSection)) { + sections.add(currentExerciseSection); + exerciseList = allAvailableExercises.get(currentExerciseSection); + break_exercise_type.setText(exercises[currentExerciseSection]); + System.out.println("Random id for section election: " + currentExerciseSection); + break; + } + + } + } + + //Set description and execution text of current exercise + description.setText(exerciseList.get(currentExercise).getDescription()); + execution.setText(exerciseList.get(currentExercise).getExecution()); + + //FIXME + setExerciseImage(); + + //Update Timer + String[] currentTime = ((String) ct_text.getText()).split(":"); + int minute = Integer.parseInt(currentTime[0]); + int second = Integer.parseInt(currentTime[1]); + + if (second != 0) { + ct.cancel(); + breakTime = 0; + if (minute == 0 && second > 0) { + minute = 1; + second = 0; + } else if (minute > 0 && second > 0) { + minute++; + second = 0; + } + if (minute < 10) + bufferZeroMinute = "0"; + if (second < 10) + bufferZeroSecond = "0"; + + + System.out.println("New Time: " + bufferZeroMinute + minute + ":" + bufferZeroSecond + second); + if (isRunning) { + time = minute * 60 * 1000; + startTimer(time); + } else { + stopTime = bufferZeroMinute + minute + ":" + bufferZeroSecond + second; + ct_text.setText(stopTime); + } + } + + break; + } + } + + private void setRandomExercises() { + + allAvailableExercises = new ArrayList<>(); + System.out.println("Number of sections: " + exercises.length); + + for (int i = 0; i < exercises.length; i++) { + List list = dbHandler.getExercisesFromSection(exercises[i]); + allAvailableExercises.add(list); + System.out.println("Section: " + exercises[i] + " and number of ex for it: " + list.size()); + } + + currentExerciseSection = random.nextInt(allAvailableExercises.size()); + System.out.println("Random id for section election: " + currentExerciseSection); + + // Set exercise list to current section + exerciseList = allAvailableExercises.get(currentExerciseSection); + sections.add(currentExerciseSection); + + description = (TextView) findViewById(R.id.textViewDescription); + description.setText(exerciseList.get(currentExercise).getDescription()); + + execution = (TextView) findViewById(R.id.textViewExecution); + execution.setText(exerciseList.get(currentExercise).getExecution()); + + side_repetition = (TextView) findViewById(R.id.textSideRepetition); + side_repetition.setText(R.string.exercise_break); + + break_exercise_type = (TextView) findViewById(R.id.break_exercise_type); + break_exercise_type.setText(exerciseList.get(currentExercise).getSection()); + + //FIXME + setExerciseImage(); + } + + private void setExerciseImage() { + String imageID = exerciseList.get(currentExercise).getImageID(); + image = (ImageView) findViewById(R.id.imageMid); + if (imageID.split(",").length == 1) { + sideRepetition = getResources().getText(R.string.exercise_repetition).toString(); + //FIXME Set correct image depending on imageID + image1 = imageID; + //Hardcoded + image.setImageResource(R.drawable.train_left); + exerciseSide = false; +// int imageResID = getResources().getIdentifier(image1, "drawable", getPackageName()); +// image.setImageResource(imageResID); + } else { + // There are 2 sides for an exercise + exerciseSide = true; + sideRepetition = getResources().getText(R.string.exercise_side).toString(); + image1 = imageID.split(",")[0]; + image2 = imageID.split(",")[1]; + System.out.println("Id of first image: " + image1 + " , id of second: " + image2); + + image.setImageResource(R.drawable.train_middle); + + //image ID from Resource +// int imageResID = getResources().getIdentifier(image1, "drawable", getPackageName()); +// image.setImageResource(imageResID); + + } + } + + //FIXME Change to the correct picture and whether its side or repetition + private void update() { + //After 10 seconds first side/repetition, then after 20 seconds break for 10 seconds, afterwards second side/repetition and after 20 seconds break and new exercise + breakTime++; + switch (breakTime) { + case 10: + System.out.println("Time for Exercise: Left!"); + side_repetition.setText(sideRepetition + " 1"); + break; + case 30: + System.out.println("Time for Break between sides!"); + side_repetition.setText(R.string.exercise_break); + //If exercise contains 2 images, set ImageView to the second image + if (exerciseSide) { + image.setImageResource(R.drawable.train_right); + //image ID from Resource +// int imageResID = getResources().getIdentifier(image2, "drawable", getPackageName()); +// image.setImageResource(imageResID); + } + break; + case 40: + System.out.println("Time for Exercise: Right!"); + side_repetition.setText(sideRepetition + " 2"); + break; + case 60: + System.out.println("Next Exercise!"); + breakTime = 0; + currentExercise++; + if (currentExercise > exerciseList.size() - 1) { + currentExercise = 0; + if (sections.size() == allAvailableExercises.size()) { + System.out.println("Did all exercises, restart!"); + sections = new ArrayList<>(); + } + while (true) { + + currentExerciseSection = random.nextInt(allAvailableExercises.size()); + if (!sections.contains(currentExerciseSection)) { + sections.add(currentExerciseSection); + exerciseList = allAvailableExercises.get(currentExerciseSection); + break_exercise_type.setText(exercises[currentExerciseSection]); + System.out.println("Random id for section election: " + currentExerciseSection); + break; + } + + } + } + description.setText(exerciseList.get(currentExercise).getDescription()); + execution.setText(exerciseList.get(currentExercise).getExecution()); + side_repetition.setText(R.string.exercise_break); + //FIXME + setExerciseImage(); + break; + } + } + + private void startTimer(int time) { + + ct = new CountDownTimer(time, 1000) { + boolean timeLeft = false; + + public void onTick(long millisUntilFinished) { + String bufferZeroMinute = ""; + String bufferZeroSecond = ""; + + if ((millisUntilFinished / 1000) / 60 < 10) + bufferZeroMinute = "0"; + + if (millisUntilFinished / 1000 % 60 < 10) + bufferZeroSecond = "0"; + + ct_text.setText(bufferZeroMinute + (millisUntilFinished / 1000) / 60 + ":" + bufferZeroSecond + millisUntilFinished / 1000 % 60); + + // Update image and description of the exercise + update(); + +// //Show how much time is left +// timeLeft = sharedPrefs.getBoolean("notifications_new_message_timeLeft", false); +// if (timeLeft) { +// Notification notification = new NotificationCompat.Builder(getApplicationContext()).setCategory(Notification.CATEGORY_MESSAGE) +// .setSmallIcon(R.drawable.ic_notifications_black_24dp) +// .setContentTitle("Break Activity Reminder: ") +// .setContentText(((millisUntilFinished / 1000) / 60) + "Minutes and " + (millisUntilFinished / 1000 % 60) + " seconds") +// .setAutoCancel(true) +// .setVisibility(NotificationCompat.VISIBILITY_PUBLIC).build(); +// NotificationManager notificationManager = +// (NotificationManager) getSystemService(NOTIFICATION_SERVICE); +// notificationManager.notify(999, notification); +// } + } + + public void onFinish() { + isRunning = false; + ct_text.setText("00:00"); + //Trigger the alarm + String ringPref = sharedPrefs.getString("notifications_new_message_ringtone", ""); + + if (!ringPref.equals("")) { + Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), Uri.parse(ringPref)); + r.play(); + } + + //Vibration + boolean vibrateChecked = sharedPrefs.getBoolean("notifications_new_message_vibrate", false); + if (vibrateChecked) { + // Get instance of Vibrator from current Context + Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); + + if (v != null) { + // Vibrate for 1500 milliseconds + v.vibrate(1500); + } + } + + //Cancel the notification + if (timeLeft) { + NotificationManager notificationManager = + (NotificationManager) getSystemService(NOTIFICATION_SERVICE); + notificationManager.cancel(999); + } + //Remove lag to keep screen on when the break ends + getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + + //Close database connection + dbHandler.close(); + finish(); + } + }.start(); + isRunning = true; + } +} \ No newline at end of file diff --git a/app/src/main/java/orgprivacy_friendly_apps/secuso/privacyfriendlybreakreminder/BreakReminder.java b/app/src/main/java/orgprivacy_friendly_apps/secuso/privacyfriendlybreakreminder/BreakReminder.java index 008d01f..acb7621 100644 --- a/app/src/main/java/orgprivacy_friendly_apps/secuso/privacyfriendlybreakreminder/BreakReminder.java +++ b/app/src/main/java/orgprivacy_friendly_apps/secuso/privacyfriendlybreakreminder/BreakReminder.java @@ -35,6 +35,9 @@ import android.widget.Button; import android.widget.Spinner; import android.widget.TextView; +import java.util.Arrays; +import java.util.Locale; + public class BreakReminder extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener { @@ -75,12 +78,32 @@ public class BreakReminder extends AppCompatActivity String exercises = this.getResources().getText(R.string.all_exercises).toString(); editor.putString("exercise_value", exercises); editor.putString("profiles", allProfiles); + editor.putString("current_language", Locale.getDefault().getLanguage()); editor.apply(); WelcomeDialog welcomeDialog = new WelcomeDialog(); welcomeDialog.show(getFragmentManager(), "WelcomeDialog"); + } else if(!sharedPrefs.getString("current_language", "en").equals(Locale.getDefault().getLanguage())){ + //FIXME could be nice to translate the body parts hear... but it would be a complex function + editor.putString("current_language", Locale.getDefault().getLanguage()); + String[] profiles = allProfiles.split(";"); + allProfiles = ""; + + for(int j = 0; j < profiles.length; j++) { + String[] profile = profiles[j].split(","); + profile[4] = "-1"; + profiles[j] = ""; + for(int i = 0; i exerciseList = new ArrayList<>(); dataBase = this.getReadableDatabase(); - Cursor res = dataBase.rawQuery("SELECT * FROM EXERCISES_" + DEVICE_LANGUAGE + " WHERE " + EXERCISES_SECTION + " LIKE " + "\"%" + section + "%\"", null); + //FIXME DES GRAUENS + String table = ""; + if(DEVICE_LANGUAGE.equals("fr")) + table = "en"; + else + table = DEVICE_LANGUAGE; + + Cursor res = dataBase.rawQuery("SELECT * FROM EXERCISES_" + table + " WHERE " + EXERCISES_SECTION + " LIKE " + "\"%" + section + "%\"", null); res.moveToFirst(); while (!res.isAfterLast()) { exercise = new Exercise(res.getInt(0), res.getString(1), section, res.getString(3), res.getString(4));