Skip to content

Commit

Permalink
Refactor preference handling
Browse files Browse the repository at this point in the history
- Move all access to preferences into PrefManager.java
- Move preference keys to separate file preference_keys.xml
- Use getSharedPreferences instead of deprecated getDefaultSharedPreferences
- Fix usage of incorrect string values in pref_notification.xml and pref_workout.xml
- Migrate old translated preference values to new non-translatable values in PrefManager.migratePreferences
- Migrate preference from different file to default preference file in PrefManager.migratePreferences
  • Loading branch information
Tobias Länge committed Nov 23, 2022
1 parent c80a27e commit 7f67d93
Show file tree
Hide file tree
Showing 17 changed files with 475 additions and 360 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@
package org.secuso.privacyfriendlycircuittraining.activities;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.navigation.NavigationView.OnNavigationItemSelectedListener;
import androidx.core.app.TaskStackBuilder;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import android.view.MenuItem;
import android.view.View;

import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import androidx.core.app.TaskStackBuilder;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;

import com.google.android.material.navigation.NavigationView;
import com.google.android.material.navigation.NavigationView.OnNavigationItemSelectedListener;

import org.secuso.privacyfriendlycircuittraining.R;
import org.secuso.privacyfriendlycircuittraining.tutorial.PrefManager;
Expand Down Expand Up @@ -58,13 +58,11 @@ public abstract class BaseActivity extends AppCompatActivity implements OnNaviga

// Helper
private Handler mHandler;
protected SharedPreferences mSharedPreferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
mHandler = new Handler();

overridePendingTransition(0, 0);
Expand Down Expand Up @@ -174,7 +172,7 @@ private void callDrawerItem(final int itemId) {
createBackStack(intent);
break;
case R.id.nav_tutorial:
PrefManager prefManager = new PrefManager(getBaseContext());
PrefManager.performMigrations(getBaseContext());
//prefManager.setFirstTimeLaunch(true);
intent = new Intent(this, TutorialActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,24 @@
package org.secuso.privacyfriendlycircuittraining.activities;

import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.ItemTouchHelper;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import org.secuso.privacyfriendlycircuittraining.R;
import org.secuso.privacyfriendlycircuittraining.adapters.MotivationAlertTextsAdapter;
import org.secuso.privacyfriendlycircuittraining.tutorial.PrefManager;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -122,9 +123,7 @@ public void onResume() {
* If motivation texts set is empty the view will be set to 'empty view'
*/
protected void showMotivationAlertTexts() {
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
Set<String> defaultStringSet = new HashSet<>(Arrays.asList(getResources().getStringArray(R.array.pref_default_notification_motivation_alert_messages)));
Set<String> stringSet = sharedPref.getStringSet(this.getString(R.string.pref_notification_motivation_alert_texts), defaultStringSet);
Set<String> stringSet = PrefManager.getNotificationMotivationAlertTexts(getBaseContext());
motivationTexts = new ArrayList<>(Arrays.asList(stringSet.toArray(new String[stringSet.size()])));

this.mAdapter.setItems(motivationTexts);
Expand All @@ -139,10 +138,7 @@ protected void showMotivationAlertTexts() {
* Stores the motivation texts to shared preferences
*/
protected void save() {
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putStringSet(this.getString(R.string.pref_notification_motivation_alert_texts), new HashSet<>(motivationTexts));
editor.apply();
PrefManager.setNotificationMotivationAlertTexts(getBaseContext(), new HashSet<>(motivationTexts));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.secuso.privacyfriendlycircuittraining.R;
import org.secuso.privacyfriendlycircuittraining.fragments.GrantExactAlarmPermissionDialogFragment;
import org.secuso.privacyfriendlycircuittraining.helpers.NotificationHelper;
import org.secuso.privacyfriendlycircuittraining.tutorial.PrefManager;

import java.util.List;

Expand Down Expand Up @@ -103,9 +104,9 @@ private static void bindPreferenceSummaryToValue(Preference preference) {
// Trigger the listener immediately with the preference's
// current value.
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
PrefManager.getPreferences(preference.getContext())
.getString(preference.getKey(), ""));

}

@Override
Expand Down Expand Up @@ -248,7 +249,7 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
}

//Hotfix since I could not get the intent to launch from XML file
final Preference motivationText = (Preference) findPreference(getString(R.string.pref_notification_motivation_texts));
final Preference motivationText = (Preference) findPreference(getString(R.string.pref_notification_motivation_alert_texts));

if (motivationText != null) {
motivationText.setOnPreferenceClickListener(new OnPreferenceClickListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,14 @@

public class SplashActivity extends AppCompatActivity {

private PrefManager prefManager;
private PFASQLiteHelper db = new PFASQLiteHelper(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

prefManager = new PrefManager(this);
if (prefManager.isFirstTimeLaunch()) {
PrefManager.performMigrations(getBaseContext());
if (PrefManager.isFirstTimeLaunch(getBaseContext())) {
//add two example exercises

Uri ic_squat = Uri.parse("android.resource://" + this.getPackageName() + "/" + R.drawable.ic_exercise_squat);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,15 @@
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import androidx.core.content.ContextCompat;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
Expand All @@ -41,13 +36,18 @@
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.content.DialogInterface.OnCancelListener;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import org.secuso.privacyfriendlycircuittraining.R;
import org.secuso.privacyfriendlycircuittraining.database.PFASQLiteHelper;
import org.secuso.privacyfriendlycircuittraining.services.TimerService;
import org.secuso.privacyfriendlycircuittraining.tutorial.PrefManager;

import java.util.ArrayList;

Expand All @@ -61,9 +61,6 @@
*/
public class WorkoutActivity extends AppCompatActivity {

//General
private SharedPreferences settings;

// GUI Text
private TextView currentSetsInfo = null;
private TextView workoutTimer = null;
Expand Down Expand Up @@ -110,7 +107,6 @@ protected void onCreate(Bundle savedInstanceState) {
this.progressBar = (ProgressBar) this.findViewById(R.id.progressBar);
this.workoutTimer = (TextView) this.findViewById(R.id.workout_timer);
this.workoutTitle = (TextView) this.findViewById(R.id.workout_title);
this.settings = PreferenceManager.getDefaultSharedPreferences(this);
this.nextTimer = (ImageView) this.findViewById(R.id.workout_next);
this.finishedView = findViewById(R.id.finishedView);
this.finishedView.setVisibility(View.GONE);
Expand Down Expand Up @@ -471,10 +467,10 @@ private void showCancelAlert(final boolean showFinish){
public void onClick(DialogInterface dialog, int indexSelected, boolean isChecked) {
if (isChecked) {
selectedItem.add(indexSelected);
settings.edit().putBoolean(getString(R.string.pref_cancel_workout_check), false).commit();
PrefManager.setCancelWorkoutCheck(getBaseContext(), false);
} else if (selectedItem.contains(indexSelected)) {
selectedItem.remove(Integer.valueOf(indexSelected));
settings.edit().putBoolean(getString(R.string.pref_cancel_workout_check), true).commit();
PrefManager.setCancelWorkoutCheck(getBaseContext(), true);
}
}
});
Expand Down Expand Up @@ -641,55 +637,33 @@ private void cleanTimerServiceFinish(){
* @param mute Flag to mute or unmute all sounds
*/
private void muteAllSounds(boolean mute){
if(this.settings != null) {
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(getResources().getString(R.string.pref_sounds_muted), mute);
editor.apply();
}
PrefManager.setSoundsMuted(getBaseContext(), mute);
}

/*
* Multiple checks for what was enabled inside the settings
*/
public boolean isKeepScreenOnEnabled(Context context){
if(this.settings != null){
return settings.getBoolean(context.getString(R.string.pref_keep_screen_on_switch_enabled), true);
}
return false;
return PrefManager.getKeepScreenOnSwitchEnabled(context);
}

public boolean isStartTimerEnabled(Context context) {
if (this.settings != null) {
return settings.getBoolean(context.getString(R.string.pref_start_timer_switch_enabled), true);
}
return false;
return PrefManager.getStartTimerSwitchEnabled(context);
}

public boolean isBlinkingProgressBarEnabled(Context context) {
if (this.settings != null) {
return settings.getBoolean(context.getString(R.string.pref_blinking_progress_bar), false);
}
return false;
return PrefManager.getBlinkingProgressBar(context);
}

public boolean isCaloriesEnabled(Context context) {
if (this.settings != null) {
return settings.getBoolean(context.getString(R.string.pref_calories_counter), true);
}
return false;
return PrefManager.getCaloriesCounter(context);
}

public boolean isSoundsMuted(Context context) {
if (this.settings != null) {
return settings.getBoolean(context.getString(R.string.pref_sounds_muted), true);
}
return true;
return PrefManager.getSoundsMuted(context);
}

public boolean isCancelDialogEnabled(Context context) {
if (this.settings != null) {
return settings.getBoolean(context.getString(R.string.pref_cancel_workout_check), true);
}
return true;
return PrefManager.getCancelWorkoutCheck(context);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.secuso.privacyfriendlycircuittraining.backup

import android.content.Context
import android.preference.PreferenceManager
import android.util.JsonWriter
import android.util.Log
import org.secuso.privacyfriendlybackup.api.backup.DatabaseUtil.getSupportSQLiteOpenHelper
import org.secuso.privacyfriendlybackup.api.backup.DatabaseUtil.writeDatabase
import org.secuso.privacyfriendlybackup.api.backup.PreferenceUtil.writePreferences
import org.secuso.privacyfriendlybackup.api.pfa.IBackupCreator
import org.secuso.privacyfriendlycircuittraining.database.PFASQLiteHelper
import org.secuso.privacyfriendlycircuittraining.tutorial.PrefManager
import java.io.OutputStream
import java.io.OutputStreamWriter

Expand All @@ -33,7 +33,8 @@ class BackupCreator : IBackupCreator {
Log.d(TAG, "Writing preferences")
writer.name("preferences")

val pref = PreferenceManager.getDefaultSharedPreferences(context.applicationContext)
PrefManager.performMigrations(context)
val pref = PrefManager.getPreferences(context)
writePreferences(writer, pref)

writer.endObject()
Expand Down
Loading

0 comments on commit 7f67d93

Please sign in to comment.