Settings Activity

Added contiuously and exercises to the settings activity.
This commit is contained in:
Jannik 2016-08-23 18:11:50 +02:00
commit 254d9d9fbc
7 changed files with 166 additions and 33 deletions

View file

@ -136,7 +136,7 @@ public class BreakReminder extends AppCompatActivity
String allProfiles = sharedPrefs.getString("profiles", "");
String currentProfile = sharedPrefs.getString("name_text", "") + "," + sharedPrefs.getInt("work_value", -1) + "," + sharedPrefs.getInt("break_value", -1)+ "," + sharedPrefs.getString("exercise_value", "-1");
String currentProfile = sharedPrefs.getString("name_text", "") + "," + sharedPrefs.getInt("work_value", -1) + "," + sharedPrefs.getInt("break_value", -1) + "," + sharedPrefs.getBoolean("cont_value", false)+ "," + sharedPrefs.getString("exercise_value", "-1");
if (allProfiles.contains(currentProfile) && profileSelected.equals(sharedPrefs.getString("name_text", ""))) {
System.out.println("Profile didn´t change");
@ -151,13 +151,15 @@ public class BreakReminder extends AppCompatActivity
String profileName = profileNames[i].split(",")[0];
int interval = Integer.parseInt(profileNames[i].split(",")[1]);
int break_time = Integer.parseInt(profileNames[i].split(",")[2]);
String exercises = profileNames[i].split(",")[3];
boolean cont = Boolean.parseBoolean(profileNames[i].split(",")[3]);
String exercises = profileNames[i].split(",")[4];
if (profileName.equals(profileSelected)) {
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putString("current_profile", "" + i);
editor.putString("name_text", profileName);
editor.putInt("work_value", interval);
editor.putInt("break_value", break_time);
editor.putBoolean("cont_value", cont);
editor.putString("exercise_value", exercises);
editor.apply();

View file

@ -17,7 +17,7 @@ import android.widget.ListView;
import android.widget.Toast;
public class DynamicListPreference extends ListPreference implements Preference.OnPreferenceChangeListener,DialogInterface.OnClickListener {
public class DynamicListPreference extends ListPreference implements DialogInterface.OnClickListener {
Context mContext;
SharedPreferences sharedPreferences;
@ -76,33 +76,6 @@ public class DynamicListPreference extends ListPreference implements Preference.
return entries;
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
System.out.println("Geschafft!!!");
ListPreference listPref = (ListPreference) preference;
int index = listPref.findIndexOfValue((String) newValue);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("current_profile", ""+index);
editor.apply();
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
String[] allProfile = sharedPreferences.getString("profiles", "").split(";");
//FIXME Deactivate the onPrefListener in SettingsActivity
for (int i = 0; i < allProfile.length; i++) {
if (allProfile[i].split(",")[0].equals(getValue())){
editor.putString("name_text",allProfile[i].split(",")[0]);
editor.putString("work_value",allProfile[i].split(",")[1]);
editor.putString("break_value",allProfile[i].split(",")[2]);
editor.apply();
}
}
return true;
}
}

View file

@ -0,0 +1,115 @@
package orgprivacy_friendly_apps.secuso.privacyfriendlybreakreminder;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.preference.ListPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
/**
* Created by badri_000 on 23.08.2016.
*/
public class ExerciseListPreference extends ListPreference implements DialogInterface.OnClickListener {
Context mContext;
SharedPreferences sharedPreferences;
private boolean[] mClickedDialogEntryIndices;
String[] exercises;
public ExerciseListPreference(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
mClickedDialogEntryIndices = new boolean[getEntries().length];
}
public ExerciseListPreference(Context context) {
this(context, null);
mClickedDialogEntryIndices = new boolean[getEntries().length];
}
@Override
protected View onCreateDialogView() {
ListView view = new ListView(getContext());
view.setAdapter(adapter());
sharedPreferences = getSharedPreferences();
String exercise = sharedPreferences.getString("exercise_value", "-1");
exercises = exercise.split("\\.");
return view;
}
@Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
CharSequence[] entries = getEntries();
CharSequence[] entryValues = getEntryValues();
if (entries == null || entryValues == null || entries.length != entryValues.length ) {
throw new IllegalStateException(
"ListPreference requires an entries array and an entryValues array which are both the same length");
}
restoreCheckedEntries();
builder.setMultiChoiceItems(entries, mClickedDialogEntryIndices,
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int which, boolean val) {
mClickedDialogEntryIndices[which] = val;
}
});
}
private void restoreCheckedEntries() {
CharSequence[] entryValues = getEntries();
System.out.println("VALUE-CHECK" + getValue());
for ( int j=0; j<exercises.length; j++ ) {
String val = exercises[j].trim();
for ( int i=0; i<entryValues.length; i++ ) {
CharSequence entry = entryValues[i];
if ( entry.equals(val) ) {
mClickedDialogEntryIndices[i] = true;
break;
}
}
}
}
private ListAdapter adapter() {
return new ArrayAdapter(getContext(), android.R.layout.select_dialog_multichoice);
}
@Override
protected void onDialogClosed(boolean positiveResult) {
// super.onDialogClosed(positiveResult);
CharSequence[] entryValues = getEntries();
String exs = "";
if (positiveResult && entryValues != null) {
for ( int i=0; i<entryValues.length; i++ ) {
if ( mClickedDialogEntryIndices[i] ) {
exs += entryValues[i] + ".";
}
}
//if (callChangeListener(exs)) {
// if ( exs.length() > 0 )
// exs = exs.substring(0, exs.length());
// setValue(exs);
//}
}
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("exercise_value", exs);
editor.apply();
}
}

View file

@ -63,7 +63,6 @@ public class ExerciseTypeActivity extends AppCompatActivity implements View.OnCl
for (int i = 0;i<adapter.size();i++){
exerciseTypes += adapter.get(i) + ".";
}
editor.putString("exercise_value",exerciseTypes);
editor.apply();

View file

@ -187,6 +187,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
private SeekBarPreference _seekBarBreak;
private DynamicListPreference dlp;
private ExerciseListPreference elp;
private String currentProfile = "";
private Bundle bundle;
@ -203,6 +204,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
_seekBarBreak = (SeekBarPreference) this.findPreference("break_value");
dlp = (DynamicListPreference) this.findPreference("current_profile");
elp = (ExerciseListPreference) this.findPreference("exercise");
// Set listener :
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
@ -238,6 +240,15 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if(key.equals("exercise")) {
System.out.println("GOGOGOGOGOG");
System.out.println(sharedPreferences.getString("exercise", "Damn"));
}
if (key.equals("current_profile")) {
ListPreference listPref = (ListPreference) findPreference("current_profile");
int i = Integer.parseInt(listPref.getValue());
@ -255,11 +266,16 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
editor.putInt("break_value", Integer.parseInt(allProfile[i].split(",")[2]));
editor.apply();
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
// FIXME Has to be done because the summary of the name
onDestroy();
onCreate(bundle);
return;
}
if(key.equals("cont_value"))
System.out.println("Cont: " + sharedPreferences.getBoolean("cont_value", false));
// Set seekbar summary :
int radius = PreferenceManager.getDefaultSharedPreferences(this.getActivity()).getInt("work_value", 50);
_seekBarWork.setSummary(this.getString(R.string.settings_summary).replace("$1", "" + radius));
@ -280,6 +296,9 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
@Override
public void onPause() {
getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
System.out.println("All Profiles" + PreferenceManager.getDefaultSharedPreferences(this.getActivity()).getString("profiles", ""));
super.onPause();
}
@ -288,8 +307,10 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
int break_radius = PreferenceManager.getDefaultSharedPreferences(this.getActivity()).getInt("break_value", 10);
String newProfileName = PreferenceManager.getDefaultSharedPreferences(this.getActivity()).getString("name_text", "");
String allProfiles = PreferenceManager.getDefaultSharedPreferences(this.getActivity()).getString("profiles", "");
boolean cont = PreferenceManager.getDefaultSharedPreferences(this.getActivity()).getBoolean("cont_value", false);
String exercises = PreferenceManager.getDefaultSharedPreferences(this.getActivity()).getString("exercise_value", "-1");
if (allProfiles.contains(newProfileName + "," + work_radius + "," + break_radius) && newProfileName.equals(currentProfile)) {
if (allProfiles.contains(newProfileName + "," + work_radius + "," + break_radius + "," + cont + "," + exercises) && newProfileName.equals(currentProfile)) {
//Nothing changes
System.out.println("No changes for a profile in general settings");
} else {
@ -316,7 +337,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
for (int i = 0; i < profiles.length; i++) {
if (profiles[i].split(",")[0].equals(currentProfile)) {
profiles[i] = newProfileName + "," + work_radius + "," + break_radius + "," + profiles[i].split(",")[3] + "," + profiles[i].split(",")[4];
profiles[i] = newProfileName + "," + work_radius + "," + break_radius + "," + cont + "," + exercises;
break;
}
}

View file

@ -25,6 +25,8 @@
<string name="pref_title_display_name">Profile name</string>
<string name="pref_default_display_name">Pomodoro</string>
<string name="pref_current_profile">Select profile</string>
<string name="pref_current_exercises">Select exercises</string>
<string name="pref_title_cont">Continuously</string>
<string name="pref_default_profile">Sport</string>
<!-- settings for Notifications -->
@ -107,5 +109,14 @@
<item>Pelvis</item>
<item>Legs</item>
</string-array>
<string-array name="value_spinner">
<item>0</item>
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
</string-array>
</resources>

View file

@ -40,5 +40,17 @@
android:text="@string/settings_unit"
android:title="@string/settings_break_title" />
<SwitchPreference
android:defaultValue="false"
android:key="cont_value"
android:title="@string/pref_title_cont" />
<orgprivacy_friendly_apps.secuso.privacyfriendlybreakreminder.ExerciseListPreference
android:defaultValue="1"
android:entryValues="@array/value_spinner"
android:entries="@array/type_spinner"
android:title="@string/pref_current_exercises" />
</PreferenceScreen>
<!--android:key="exercise"-->