commit dafed99d20b0ca398a04c2c8db5f3e24c995893d Author: Hashik Donthineni HashikDonthineni@gmail.com Date: Sat Jul 18 00:01:14 2020 +0530
Moved and renamed String.xml SharedPreference keys --- .../java/org/torproject/snowflake/MainActivity.java | 20 ++++++++++---------- .../torproject/snowflake/MyPersistentService.java | 10 +++++----- app/src/main/res/values/strings.xml | 5 ----- app/src/main/res/values/strings_preference_keys.xml | 8 ++++++++ 4 files changed, 23 insertions(+), 20 deletions(-)
diff --git a/app/src/main/java/org/torproject/snowflake/MainActivity.java b/app/src/main/java/org/torproject/snowflake/MainActivity.java index 293f539..69948a5 100644 --- a/app/src/main/java/org/torproject/snowflake/MainActivity.java +++ b/app/src/main/java/org/torproject/snowflake/MainActivity.java @@ -58,17 +58,17 @@ public class MainActivity extends AppCompatActivity implements MainFragmentCallb .observeOn(AndroidSchedulers.mainThread()) .subscribe((status) -> { //Runs on main thread //By this point the servedCount must be reset or left as is after checking the dates. - servedCount = sharedPreferences.getInt(getString(R.string.users_served), 0); + servedCount = sharedPreferences.getInt(getString(R.string.users_served_key), 0);
setListenerForCount(); updateCountInFragment(); });
//Creating notification channel if app is being run for the first time - if (sharedPreferences.getBoolean(getString(R.string.initial_run_boolean), true)) { + if (sharedPreferences.getBoolean(getString(R.string.initial_run_boolean_key), true)) { createNotificationChannel(); //Setting initial run to false. - sharedPreferences.edit().putBoolean(getString(R.string.initial_run_boolean), false).apply(); + sharedPreferences.edit().putBoolean(getString(R.string.initial_run_boolean_key), false).apply(); }
settingsButton.setOnClickListener(new View.OnClickListener() { @@ -108,7 +108,7 @@ public class MainActivity extends AppCompatActivity implements MainFragmentCallb listener = (prefs, key) -> { Log.d(TAG, "setListenerForCount: Listener: Key = " + key);
- if (key.equals(getString(R.string.users_served))) { + if (key.equals(getString(R.string.users_served_key))) { servedCount = sharedPreferences.getInt(key, 0); updateCountInFragment(); } @@ -158,7 +158,7 @@ public class MainActivity extends AppCompatActivity implements MainFragmentCallb * @return boolean whether the service is running or not. */ public boolean isServiceRunning() { - return sharedPreferences.getBoolean(getString(R.string.is_service_running_bool), false); + return sharedPreferences.getBoolean(getString(R.string.is_service_running_bool_key), false); }
/** @@ -202,12 +202,12 @@ public class MainActivity extends AppCompatActivity implements MainFragmentCallb
try { String stringCurrentDate = simpleDateFormat.format(Calendar.getInstance().getTime()); - String stringRecordedDate = sharedPreferences.getString(getString(R.string.served_date), ""); + String stringRecordedDate = sharedPreferences.getString(getString(R.string.served_date_key), "");
//No value for key. Set the date value to current date and users served to 0. if (stringRecordedDate.equals("")) { - editor.putString(getString(R.string.served_date), stringCurrentDate); - editor.putInt(getString(R.string.users_served), 0); + editor.putString(getString(R.string.served_date_key), stringCurrentDate); + editor.putInt(getString(R.string.users_served_key), 0); } else { //Check if the current system date is greater than recorded date, if so reset the "served" flag. Date recordedDate = simpleDateFormat.parse(stringRecordedDate); @@ -221,8 +221,8 @@ public class MainActivity extends AppCompatActivity implements MainFragmentCallb return true; } else { //Current date is bigger than recorded date. Reset the values. i.e comparision > 0 - editor.putString(getString(R.string.served_date), simpleDateFormat.format(currentDate)); - editor.putInt(getString(R.string.users_served), 0); + editor.putString(getString(R.string.served_date_key), simpleDateFormat.format(currentDate)); + editor.putInt(getString(R.string.users_served_key), 0); } }
diff --git a/app/src/main/java/org/torproject/snowflake/MyPersistentService.java b/app/src/main/java/org/torproject/snowflake/MyPersistentService.java index 20f0a47..c8c2304 100644 --- a/app/src/main/java/org/torproject/snowflake/MyPersistentService.java +++ b/app/src/main/java/org/torproject/snowflake/MyPersistentService.java @@ -80,7 +80,7 @@ public class MyPersistentService extends Service { public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG, "onStartCommand: executed with startId: " + startId); sharedPreferences = getSharedPreferences(getString(R.string.sharedpreference_file), MODE_PRIVATE); - isServiceStarted = sharedPreferences.getBoolean(getString(R.string.is_service_running_bool), false); + isServiceStarted = sharedPreferences.getBoolean(getString(R.string.is_service_running_bool_key), false);
if (intent != null) { if (intent.getAction().equals(ForegroundServiceConstants.ACTION_START)) @@ -132,10 +132,10 @@ public class MyPersistentService extends Service {
if (setState == ForegroundServiceConstants.SERVICE_RUNNING) { isServiceStarted = true; - editor.putBoolean(getString(R.string.is_service_running_bool), true); + editor.putBoolean(getString(R.string.is_service_running_bool_key), true); } else { isServiceStarted = false; - editor.putBoolean(getString(R.string.is_service_running_bool), false); + editor.putBoolean(getString(R.string.is_service_running_bool_key), false); } editor.apply(); } @@ -146,8 +146,8 @@ public class MyPersistentService extends Service { private void updateServedCount() { SharedPreferences sp = GlobalApplication.getAppPreferences(); sp.edit() - .putInt(getString(R.string.users_served), - sp.getInt(getString(R.string.users_served), 0) + 1) + .putInt(getString(R.string.users_served_key), + sp.getInt(getString(R.string.users_served_key), 0) + 1) .apply(); }
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 68b36a1..856c648 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,11 +1,6 @@ <resources> <string name="app_name">Snowflake</string> - <string name="sharedpreference_file">org.torproject.snowflake.snowflake_preferences</string> - <string name="is_service_running_bool">is_service_running</string> <string name="not_channel_desc">This Channel should not be muted. The Android system will consider Snowflake not import and will kill the service if it's muted.</string> - <string name="initial_run_boolean">initial_run</string> <string name="not_channel_name">Snowflake Service</string> - <string name="users_served">users_served</string> - <string name="served_date">date</string> <string name="users_served_text">Users you have helped circumvent censorship in the past day \n</string> </resources> diff --git a/app/src/main/res/values/strings_preference_keys.xml b/app/src/main/res/values/strings_preference_keys.xml new file mode 100644 index 0000000..2727405 --- /dev/null +++ b/app/src/main/res/values/strings_preference_keys.xml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="utf-8"?> +<resources> + <string name="sharedpreference_file">org.torproject.snowflake.snowflake_preferences</string> + <string name="users_served_key">users_served</string> + <string name="served_date_key">date</string> + <string name="initial_run_boolean_key">initial_run</string> + <string name="is_service_running_bool_key">is_service_running</string> +</resources> \ No newline at end of file