From b9b278d603c6a96523ce1a289a8e1af76cb57c03 Mon Sep 17 00:00:00 2001 From: Binnette Date: Fri, 6 Dec 2024 08:40:32 +0100 Subject: [PATCH] Replace getDefaultSharedPreferences by getSharedPreferences --- .../layouts/DownloadLayoutTest.java | 7 ++-- .../java/net/osmtracker/util/TestUtils.java | 11 ++++--- .../java/net/osmtracker/activity/About.java | 24 ++++++-------- .../osmtracker/activity/AvailableLayouts.java | 32 +++++++++---------- .../osmtracker/activity/ButtonsPresets.java | 12 +++---- .../net/osmtracker/activity/DisplayTrack.java | 24 +++++++------- .../osmtracker/activity/DisplayTrackMap.java | 2 +- .../java/net/osmtracker/activity/Intro.kt | 3 +- .../activity/OpenStreetMapUpload.java | 4 +-- .../net/osmtracker/activity/Preferences.java | 20 ++++-------- .../net/osmtracker/activity/TrackDetail.java | 2 +- .../net/osmtracker/activity/TrackLogger.java | 4 +-- .../net/osmtracker/activity/TrackManager.java | 28 ++++++++-------- .../java/net/osmtracker/db/DataHelper.java | 14 ++++---- .../osmtracker/gpx/ExportToStorageTask.java | 4 +-- .../net/osmtracker/gpx/ExportTrackTask.java | 24 +++++++------- .../layout/DownloadCustomLayoutTask.java | 16 ++++------ .../osmtracker/layout/GpsStatusRecord.java | 24 +++++++------- .../osm/UploadToOpenStreetMapTask.java | 9 ++++-- .../net/osmtracker/service/gps/GPSLogger.java | 9 +++--- .../osmtracker/util/CustomLayoutsUtils.java | 6 ++-- .../net/osmtracker/util/ThemeValidator.java | 10 +++--- .../java/net/osmtracker/util/URLCreator.java | 7 ++-- .../net/osmtracker/view/VoiceRecDialog.java | 5 +-- .../main/res/values/strings-preferences.xml | 2 ++ .../activity/ButtonsPresetsTest.java | 32 +++++++++---------- .../gpx/ExportToStorageTaskTest.java | 3 +- .../layout/DownloadCustomLayoutTaskTest.java | 17 ++++++---- .../util/CustomLayoutsUtilsTest.java | 15 +++++---- .../osmtracker/util/ThemeValidatorTest.java | 22 +++++++------ .../net/osmtracker/util/URLCreatorTest.java | 16 ++++++---- 31 files changed, 209 insertions(+), 199 deletions(-) diff --git a/app/src/androidTest/java/net/osmtracker/layouts/DownloadLayoutTest.java b/app/src/androidTest/java/net/osmtracker/layouts/DownloadLayoutTest.java index 1c20d9c7e..72887b841 100644 --- a/app/src/androidTest/java/net/osmtracker/layouts/DownloadLayoutTest.java +++ b/app/src/androidTest/java/net/osmtracker/layouts/DownloadLayoutTest.java @@ -1,5 +1,6 @@ package net.osmtracker.layouts; +import static android.content.Context.MODE_PRIVATE; import static androidx.test.espresso.Espresso.onData; import static androidx.test.espresso.Espresso.onView; import static androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu; @@ -16,8 +17,8 @@ import static net.osmtracker.util.WaitForView.waitForView; import android.Manifest; +import android.content.Context; import android.content.SharedPreferences; -import android.preference.PreferenceManager; import androidx.test.espresso.Espresso; import androidx.test.rule.ActivityTestRule; @@ -50,8 +51,8 @@ public class DownloadLayoutTest { @Override protected void beforeActivityLaunched() { // Skip cool intro - SharedPreferences dtPrefs = PreferenceManager - .getDefaultSharedPreferences(getInstrumentation().getTargetContext()); + Context context = getInstrumentation().getTargetContext(); + SharedPreferences dtPrefs = context.getSharedPreferences(context.getString(R.string.shared_pref), MODE_PRIVATE); dtPrefs.edit().putBoolean(OSMTracker.Preferences.KEY_DISPLAY_APP_INTRO, false).apply(); } }; diff --git a/app/src/androidTest/java/net/osmtracker/util/TestUtils.java b/app/src/androidTest/java/net/osmtracker/util/TestUtils.java index 2c733c75f..78fcdef55 100644 --- a/app/src/androidTest/java/net/osmtracker/util/TestUtils.java +++ b/app/src/androidTest/java/net/osmtracker/util/TestUtils.java @@ -1,14 +1,16 @@ package net.osmtracker.util; +import static android.content.Context.MODE_PRIVATE; +import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation; import static net.osmtracker.util.LogcatHelper.checkLogForMessage; import android.content.Context; import android.content.SharedPreferences; -import android.preference.PreferenceManager; import androidx.test.platform.app.InstrumentationRegistry; import net.osmtracker.OSMTracker; +import net.osmtracker.R; import net.osmtracker.activity.Preferences; import net.osmtracker.data.Mocks; @@ -126,12 +128,13 @@ public static String getStringResource(int resourceId){ } public static void setGithubRepositorySettings(String user, String repo, String branch){ - Context context = InstrumentationRegistry.getInstrumentation().getTargetContext(); - SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit(); + Context context = getInstrumentation().getTargetContext(); + SharedPreferences sharedPrefs = context.getSharedPreferences(context.getString(R.string.shared_pref), MODE_PRIVATE); + SharedPreferences.Editor editor = sharedPrefs.edit(); editor.putString(OSMTracker.Preferences.KEY_GITHUB_USERNAME, user); editor.putString(OSMTracker.Preferences.KEY_REPOSITORY_NAME, repo); editor.putString(OSMTracker.Preferences.KEY_BRANCH_NAME, branch); - editor.commit(); + editor.apply(); } public static void setLayoutsTestingRepository(){ diff --git a/app/src/main/java/net/osmtracker/activity/About.java b/app/src/main/java/net/osmtracker/activity/About.java index 84aa7804c..608b777a6 100644 --- a/app/src/main/java/net/osmtracker/activity/About.java +++ b/app/src/main/java/net/osmtracker/activity/About.java @@ -1,10 +1,5 @@ package net.osmtracker.activity; -import net.osmtracker.OSMTracker; -import net.osmtracker.R; -import net.osmtracker.db.DatabaseHelper; -import net.osmtracker.db.ExportDatabaseTask; - import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; @@ -15,12 +10,15 @@ import android.content.pm.PackageManager.NameNotFoundException; import android.os.Bundle; import android.os.Environment; -import android.preference.PreferenceManager; import android.view.View; import android.view.View.OnClickListener; -import android.widget.Button; import android.widget.TextView; +import net.osmtracker.OSMTracker; +import net.osmtracker.R; +import net.osmtracker.db.DatabaseHelper; +import net.osmtracker.db.ExportDatabaseTask; + import java.io.File; /** @@ -75,13 +73,11 @@ public void onClick(DialogInterface dialog, int which) { public void onClick(View view) { showDialog(DIALOG_EXPORT_DB); + SharedPreferences sharedPrefs = getSharedPreferences(getString(R.string.shared_pref), MODE_PRIVATE); File dbFile = getDatabasePath(DatabaseHelper.DB_NAME); - File targetFolder = new File( - Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), - //Environment.getExternalStorageDirectory(), - PreferenceManager.getDefaultSharedPreferences(About.this).getString( - OSMTracker.Preferences.KEY_STORAGE_DIR, - OSMTracker.Preferences.VAL_STORAGE_DIR)); + String storageDir = sharedPrefs.getString(OSMTracker.Preferences.KEY_STORAGE_DIR, OSMTracker.Preferences.VAL_STORAGE_DIR); + File publicDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS); + File targetFolder = new File(publicDirectory, storageDir); new ExportDatabaseTask(About.this, targetFolder) .execute(dbFile); @@ -129,7 +125,7 @@ public ProgressDialog getExportDbProgressDialog() { private String getDebugInfo() { File externalStorageDir = this.getExternalFilesDir(null); - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); + SharedPreferences preferences = getSharedPreferences(getString(R.string.shared_pref), MODE_PRIVATE); String exportDirectoryNameInPreferences = preferences.getString( OSMTracker.Preferences.KEY_STORAGE_DIR, OSMTracker.Preferences.VAL_STORAGE_DIR); File baseExportDirectory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), diff --git a/app/src/main/java/net/osmtracker/activity/AvailableLayouts.java b/app/src/main/java/net/osmtracker/activity/AvailableLayouts.java index 64a321e4b..6c7438432 100644 --- a/app/src/main/java/net/osmtracker/activity/AvailableLayouts.java +++ b/app/src/main/java/net/osmtracker/activity/AvailableLayouts.java @@ -13,7 +13,6 @@ import android.net.NetworkInfo; import android.os.Build; import android.os.Bundle; -import android.preference.PreferenceManager; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; @@ -26,9 +25,6 @@ import android.widget.TextView; import android.widget.Toast; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; import net.osmtracker.OSMTracker; import net.osmtracker.R; import net.osmtracker.layout.DownloadCustomLayoutTask; @@ -36,6 +32,10 @@ import net.osmtracker.layout.URLValidatorTask; import net.osmtracker.util.CustomLayoutsUtils; import net.osmtracker.util.URLCreator; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserFactory; @@ -73,7 +73,7 @@ public class AvailableLayouts extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); + sharedPrefs = getSharedPreferences(getString(R.string.shared_pref), MODE_PRIVATE); editor = sharedPrefs.edit(); setTitle(getResources().getString(R.string.prefs_ui_available_layout)); // call task to download and parse the response to get the list of available layouts @@ -219,7 +219,7 @@ public void onClick(View v) { isDefChecked = true; //we save the status into the sharedPreferences file editor.putBoolean("defCheck", isDefChecked); - editor.commit(); + editor.apply(); } }); customServerCheckBox.setOnClickListener(new View.OnClickListener() { @@ -230,7 +230,7 @@ public void onClick(View v) { isDefChecked = false; //we save the status into the sharedPreferences file editor.putBoolean("defCheck", isDefChecked); - editor.commit(); + editor.apply(); } }); //creating the alert dialog with the github_repository_setting view @@ -254,19 +254,19 @@ protected void onPostExecute(Boolean result){ editor.putString(OSMTracker.Preferences.KEY_GITHUB_USERNAME, repositoryCustomOptions[0]); editor.putString(OSMTracker.Preferences.KEY_REPOSITORY_NAME, repositoryCustomOptions[1]); editor.putString(OSMTracker.Preferences.KEY_BRANCH_NAME, repositoryCustomOptions[2]); - editor.commit(); + editor.apply(); //to avoid the request of invalid server at the beginning - tmpSharedPref.edit().putBoolean("isCallBack", false).commit(); + tmpSharedPref.edit().putBoolean("isCallBack", false).apply(); retrieveAvailableLayouts(); }else{ String message = getResources().getString(R.string.github_repository_settings_invalid_server); Log.e("TOAST", message); Toast.makeText(AvailableLayouts.this, message, Toast.LENGTH_SHORT).show(); - tmpSharedPref.edit().putString(OSMTracker.Preferences.KEY_GITHUB_USERNAME, repositoryCustomOptions[0]).commit(); - tmpSharedPref.edit().putString(OSMTracker.Preferences.KEY_REPOSITORY_NAME, repositoryCustomOptions[1]).commit(); - tmpSharedPref.edit().putString(OSMTracker.Preferences.KEY_BRANCH_NAME, repositoryCustomOptions[2]).commit(); + tmpSharedPref.edit().putString(OSMTracker.Preferences.KEY_GITHUB_USERNAME, repositoryCustomOptions[0]).apply(); + tmpSharedPref.edit().putString(OSMTracker.Preferences.KEY_REPOSITORY_NAME, repositoryCustomOptions[1]).apply(); + tmpSharedPref.edit().putString(OSMTracker.Preferences.KEY_BRANCH_NAME, repositoryCustomOptions[2]).apply(); //to make a request at the beginning of pop-up - tmpSharedPref.edit().putBoolean("isCallBack", true).commit(); + tmpSharedPref.edit().putBoolean("isCallBack", true).apply(); onOptionsItemSelected(item); } } @@ -276,21 +276,21 @@ protected void onPostExecute(Boolean result){ .setNegativeButton(getResources().getString(R.string.menu_cancel), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - tmpSharedPref.edit().putBoolean("isCallBack", false).commit(); + tmpSharedPref.edit().putBoolean("isCallBack", false).apply(); if (checkBoxPressed){ if(!isDefChecked){ toggleRepositoryOptions(true); isDefChecked = true; //save the status into the sharedPreferences file editor.putBoolean("defCheck", isDefChecked); - editor.commit(); + editor.apply(); } else{ toggleRepositoryOptions(false); isDefChecked = false; //save the status into the sharedPreferences file editor.putBoolean("defCheck", isDefChecked); - editor.commit(); + editor.apply(); } } dialog.cancel(); diff --git a/app/src/main/java/net/osmtracker/activity/ButtonsPresets.java b/app/src/main/java/net/osmtracker/activity/ButtonsPresets.java index 2d5f864dd..9339bb6e2 100644 --- a/app/src/main/java/net/osmtracker/activity/ButtonsPresets.java +++ b/app/src/main/java/net/osmtracker/activity/ButtonsPresets.java @@ -11,9 +11,6 @@ import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; -import android.preference.PreferenceManager; -import androidx.core.app.ActivityCompat; -import androidx.core.content.ContextCompat; import android.util.Log; import android.view.ContextMenu; import android.view.Menu; @@ -24,6 +21,9 @@ import android.widget.TextView; import android.widget.Toast; +import androidx.core.app.ActivityCompat; +import androidx.core.content.ContextCompat; + import net.osmtracker.OSMTracker; import net.osmtracker.R; import net.osmtracker.layout.DownloadCustomLayoutTask; @@ -101,7 +101,7 @@ private void initializeAttributes(){ setTitle(getResources().getString(R.string.prefs_ui_buttons_layout)); setContentView(R.layout.buttons_presets); listener = new CheckBoxChangedListener(); - prefs = PreferenceManager.getDefaultSharedPreferences(this); + prefs = getSharedPreferences(getString(R.string.shared_pref), MODE_PRIVATE); layoutsFileNames = new Hashtable(); storageDir = File.separator + OSMTracker.Preferences.VAL_STORAGE_DIR; } @@ -195,7 +195,7 @@ private void checkCurrentLayout(LinearLayout downloadedLayouts, LinearLayout def selected = (CheckBox) defCheck; String targetLayout = layoutsFileNames.get(selected.getText()); prefs.edit().putString(OSMTracker.Preferences.KEY_UI_BUTTONS_LAYOUT, - targetLayout).commit(); + targetLayout).apply(); //reload the activity refreshActivity(); } @@ -210,7 +210,7 @@ private void selectLayout(CheckBox pressed){ selected=pressed; String targetLayout = layoutsFileNames.get(pressed.getText()); prefs.edit().putString(OSMTracker.Preferences.KEY_UI_BUTTONS_LAYOUT, - targetLayout).commit(); + targetLayout).apply(); } //Class that manages the changes on the selected layout diff --git a/app/src/main/java/net/osmtracker/activity/DisplayTrack.java b/app/src/main/java/net/osmtracker/activity/DisplayTrack.java index 5b8cc6175..9cb807059 100644 --- a/app/src/main/java/net/osmtracker/activity/DisplayTrack.java +++ b/app/src/main/java/net/osmtracker/activity/DisplayTrack.java @@ -1,21 +1,19 @@ package net.osmtracker.activity; -import net.osmtracker.OSMTracker; -import net.osmtracker.util.ThemeValidator; -import net.osmtracker.view.DisplayTrackView; -import net.osmtracker.db.TrackContentProvider; - import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; -import android.preference.PreferenceManager; -import androidx.core.app.ActivityCompat; -import androidx.core.content.ContextCompat; import android.view.ViewGroup.LayoutParams; +import net.osmtracker.OSMTracker; +import net.osmtracker.R; +import net.osmtracker.db.TrackContentProvider; +import net.osmtracker.util.ThemeValidator; +import net.osmtracker.view.DisplayTrackView; + /** * Displays current track in 2D view. *

@@ -34,7 +32,7 @@ public class DisplayTrack extends Activity { protected void onCreate(Bundle savedInstanceState) { // Set application theme according to user settings setTheme(getResources().getIdentifier(ThemeValidator.getValidTheme( - PreferenceManager.getDefaultSharedPreferences(this), getResources()), null, null)); + getSharedPreferences(getString(R.string.shared_pref), MODE_PRIVATE), getResources()), null, null)); super.onCreate(savedInstanceState); @@ -48,9 +46,9 @@ protected void onCreate(Bundle savedInstanceState) { // If this is the first time showing this activity, // wait for everything to initialize and then ask // the user if they'd rather see the OSM background. - SharedPreferences dtPrefs = PreferenceManager.getDefaultSharedPreferences(this); + SharedPreferences dtPrefs = getSharedPreferences(getString(R.string.shared_pref), MODE_PRIVATE); if (! dtPrefs.getBoolean(OSMTracker.Preferences.KEY_UI_ASKED_DISPLAYTRACK_OSM, false)) { - dtPrefs.edit().putBoolean(OSMTracker.Preferences.KEY_UI_ASKED_DISPLAYTRACK_OSM, true).commit(); + dtPrefs.edit().putBoolean(OSMTracker.Preferences.KEY_UI_ASKED_DISPLAYTRACK_OSM, true).apply(); dtv.post(new Runnable() { @Override public void run() { @@ -61,8 +59,8 @@ public void run() { .setPositiveButton(net.osmtracker.R.string.displaytrack_map, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - PreferenceManager.getDefaultSharedPreferences(DisplayTrack.this).edit() - .putBoolean(OSMTracker.Preferences.KEY_UI_DISPLAYTRACK_OSM, true).commit(); + SharedPreferences sharedPrefs = getSharedPreferences(getString(R.string.shared_pref), MODE_PRIVATE); + sharedPrefs.edit().putBoolean(OSMTracker.Preferences.KEY_UI_DISPLAYTRACK_OSM, true).apply(); Intent i = new Intent(DisplayTrack.this, DisplayTrackMap.class); i.putExtra(TrackContentProvider.Schema.COL_TRACK_ID, trackId); startActivity(i); diff --git a/app/src/main/java/net/osmtracker/activity/DisplayTrackMap.java b/app/src/main/java/net/osmtracker/activity/DisplayTrackMap.java index 678db42a5..8b393c1d2 100644 --- a/app/src/main/java/net/osmtracker/activity/DisplayTrackMap.java +++ b/app/src/main/java/net/osmtracker/activity/DisplayTrackMap.java @@ -158,7 +158,7 @@ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // loading the preferences - prefs = PreferenceManager.getDefaultSharedPreferences(this); + prefs = getSharedPreferences(getString(R.string.shared_pref), MODE_PRIVATE); setContentView(R.layout.displaytrackmap); diff --git a/app/src/main/java/net/osmtracker/activity/Intro.kt b/app/src/main/java/net/osmtracker/activity/Intro.kt index 9646b0de0..dcf0d406e 100644 --- a/app/src/main/java/net/osmtracker/activity/Intro.kt +++ b/app/src/main/java/net/osmtracker/activity/Intro.kt @@ -1,7 +1,6 @@ package net.osmtracker.activity import android.os.Bundle -import android.preference.PreferenceManager import androidx.fragment.app.Fragment import com.github.appintro.AppIntro import com.github.appintro.AppIntroFragment @@ -38,7 +37,7 @@ class Intro : AppIntro() { override fun onDonePressed(currentFragment: Fragment?) { super.onDonePressed(currentFragment) // Decide what to do when the user clicks on "Done" - PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit().putBoolean(OSMTracker.Preferences.KEY_DISPLAY_APP_INTRO, false).apply() + getSharedPreferences(getString(R.string.shared_pref), MODE_PRIVATE).edit().putBoolean(OSMTracker.Preferences.KEY_DISPLAY_APP_INTRO, false).apply() finish() } } diff --git a/app/src/main/java/net/osmtracker/activity/OpenStreetMapUpload.java b/app/src/main/java/net/osmtracker/activity/OpenStreetMapUpload.java index 3b7df4b85..57156ab8e 100644 --- a/app/src/main/java/net/osmtracker/activity/OpenStreetMapUpload.java +++ b/app/src/main/java/net/osmtracker/activity/OpenStreetMapUpload.java @@ -121,7 +121,7 @@ protected void onResume() { * or ask the user to authenticate via the browser. */ private void startUpload() { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); + SharedPreferences prefs = getSharedPreferences(getString(R.string.shared_pref), MODE_PRIVATE); if ( prefs.contains(OSMTracker.Preferences.KEY_OSM_OAUTH2_ACCESSTOKEN) ) { // Re-use saved token uploadToOsm(prefs.getString(OSMTracker.Preferences.KEY_OSM_OAUTH2_ACCESSTOKEN, "")); @@ -171,7 +171,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { } else if (resp == null) { Log.e(TAG, "Authorization Error. Null response from server."); } else { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); + SharedPreferences prefs = getSharedPreferences(getString(R.string.shared_pref), MODE_PRIVATE); //Exchanging the authorization code authService.performTokenRequest( diff --git a/app/src/main/java/net/osmtracker/activity/Preferences.java b/app/src/main/java/net/osmtracker/activity/Preferences.java index 415170f7b..cb4ffb2bc 100644 --- a/app/src/main/java/net/osmtracker/activity/Preferences.java +++ b/app/src/main/java/net/osmtracker/activity/Preferences.java @@ -1,33 +1,27 @@ package net.osmtracker.activity; -import java.io.File; -import java.io.FilenameFilter; - -import net.osmtracker.OSMTracker; -import net.osmtracker.R; - -import android.Manifest; import android.app.AlertDialog; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; -import android.content.pm.PackageManager; import android.os.Bundle; -import android.os.Environment; - import android.preference.EditTextPreference; import android.preference.ListPreference; import android.preference.Preference; import android.preference.Preference.OnPreferenceChangeListener; import android.preference.Preference.OnPreferenceClickListener; import android.preference.PreferenceActivity; -import android.preference.PreferenceManager; import android.provider.Settings; import android.text.Editable; import android.text.TextWatcher; import android.widget.Button; import android.widget.EditText; +import net.osmtracker.OSMTracker; +import net.osmtracker.R; + +import java.io.File; + /** * Manages preferences screen. @@ -66,7 +60,7 @@ protected void onCreate(Bundle savedInstanceState) { // Set summary of some preferences to their actual values // and register a change listener to set again the summary in case of change - final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); + final SharedPreferences prefs = getSharedPreferences(getString(R.string.shared_pref), MODE_PRIVATE); // Explicit execution of buttons presets window Preference buttonLayoutPref = findPreference("prefs_ui_buttons_layout"); @@ -224,7 +218,7 @@ public boolean onPreferenceChange(Preference preference, Object newValue) { // Clear data Editor editor = prefs.edit(); editor.remove(OSMTracker.Preferences.KEY_OSM_OAUTH2_ACCESSTOKEN); - editor.commit(); + editor.apply(); preference.setEnabled(false); return false; diff --git a/app/src/main/java/net/osmtracker/activity/TrackDetail.java b/app/src/main/java/net/osmtracker/activity/TrackDetail.java index 5f4eb6d95..5e93ca6a3 100644 --- a/app/src/main/java/net/osmtracker/activity/TrackDetail.java +++ b/app/src/main/java/net/osmtracker/activity/TrackDetail.java @@ -226,7 +226,7 @@ public boolean onOptionsItemSelected(MenuItem item) { break; case R.id.trackdetail_menu_display: - boolean useOpenStreetMapBackground = PreferenceManager.getDefaultSharedPreferences(this).getBoolean( + boolean useOpenStreetMapBackground = getSharedPreferences(getString(R.string.shared_pref), MODE_PRIVATE).getBoolean( OSMTracker.Preferences.KEY_UI_DISPLAYTRACK_OSM, OSMTracker.Preferences.VAL_UI_DISPLAYTRACK_OSM); if (useOpenStreetMapBackground) { i = new Intent(this, DisplayTrackMap.class); diff --git a/app/src/main/java/net/osmtracker/activity/TrackLogger.java b/app/src/main/java/net/osmtracker/activity/TrackLogger.java index 6ab9153e7..d37d29669 100644 --- a/app/src/main/java/net/osmtracker/activity/TrackLogger.java +++ b/app/src/main/java/net/osmtracker/activity/TrackLogger.java @@ -197,7 +197,7 @@ protected void onCreate(Bundle savedInstanceState) { PreferenceManager.setDefaultValues(this, R.xml.preferences, false); // get shared preferences - prefs = PreferenceManager.getDefaultSharedPreferences(this); + prefs = getSharedPreferences(getString(R.string.shared_pref), MODE_PRIVATE); // Set application theme according to user settings setTheme(getResources().getIdentifier(ThemeValidator.getValidTheme(prefs, getResources()), null, null)); @@ -497,7 +497,7 @@ public boolean onOptionsItemSelected(MenuItem item) { break; case R.id.tracklogger_menu_displaytrack: // Start display track activity, with or without OSM background - boolean useOpenStreetMapBackground = PreferenceManager.getDefaultSharedPreferences(this).getBoolean( + boolean useOpenStreetMapBackground = getSharedPreferences(getString(R.string.shared_pref), MODE_PRIVATE).getBoolean( OSMTracker.Preferences.KEY_UI_DISPLAYTRACK_OSM, OSMTracker.Preferences.VAL_UI_DISPLAYTRACK_OSM); if (useOpenStreetMapBackground) { i = new Intent(this, DisplayTrackMap.class); diff --git a/app/src/main/java/net/osmtracker/activity/TrackManager.java b/app/src/main/java/net/osmtracker/activity/TrackManager.java index 0c6c9b577..590da3564 100644 --- a/app/src/main/java/net/osmtracker/activity/TrackManager.java +++ b/app/src/main/java/net/osmtracker/activity/TrackManager.java @@ -1,14 +1,5 @@ package net.osmtracker.activity; -import androidx.appcompat.app.AppCompatActivity; -import androidx.appcompat.widget.Toolbar; -import androidx.core.app.ActivityCompat; -import androidx.core.content.ContextCompat; -import androidx.core.content.FileProvider; -import androidx.recyclerview.widget.DividerItemDecoration; -import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.recyclerview.widget.RecyclerView; - import android.Manifest; import android.app.AlertDialog; import android.content.ContentUris; @@ -16,12 +7,12 @@ import android.content.Context; import android.content.DialogInterface; import android.content.Intent; +import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.database.Cursor; import android.net.Uri; import android.os.Build; import android.os.Bundle; -import android.preference.PreferenceManager; import android.util.Log; import android.view.ContextMenu; import android.view.Menu; @@ -30,6 +21,15 @@ import android.widget.TextView; import android.widget.Toast; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; +import androidx.core.app.ActivityCompat; +import androidx.core.content.ContextCompat; +import androidx.core.content.FileProvider; +import androidx.recyclerview.widget.DividerItemDecoration; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.snackbar.Snackbar; @@ -105,7 +105,7 @@ public void onClick(View view) { }); // should check if is the first time using the app - boolean showAppIntro = PreferenceManager.getDefaultSharedPreferences(this) + boolean showAppIntro = getSharedPreferences(getString(R.string.shared_pref), MODE_PRIVATE) .getBoolean(OSMTracker.Preferences.KEY_DISPLAY_APP_INTRO, OSMTracker.Preferences.VAL_DISPLAY_APP_INTRO); if (showAppIntro) { @@ -490,10 +490,8 @@ private void displayTrack(long trackId){ Log.e(TAG, "On Display Track"); // Start display track activity, with or without OSM background Intent i; - boolean useOpenStreetMapBackground = PreferenceManager - .getDefaultSharedPreferences(this).getBoolean( - OSMTracker.Preferences.KEY_UI_DISPLAYTRACK_OSM, - OSMTracker.Preferences.VAL_UI_DISPLAYTRACK_OSM); + SharedPreferences sharedPrefs = getSharedPreferences(getString(R.string.shared_pref), MODE_PRIVATE); + boolean useOpenStreetMapBackground = sharedPrefs.getBoolean(OSMTracker.Preferences.KEY_UI_DISPLAYTRACK_OSM, OSMTracker.Preferences.VAL_UI_DISPLAYTRACK_OSM); if (useOpenStreetMapBackground) { i = new Intent(this, DisplayTrackMap.class); } else { diff --git a/app/src/main/java/net/osmtracker/db/DataHelper.java b/app/src/main/java/net/osmtracker/db/DataHelper.java index ea8daa96b..b509f446b 100644 --- a/app/src/main/java/net/osmtracker/db/DataHelper.java +++ b/app/src/main/java/net/osmtracker/db/DataHelper.java @@ -1,5 +1,7 @@ package net.osmtracker.db; +import static android.content.Context.MODE_PRIVATE; + import android.content.ContentResolver; import android.content.ContentUris; import android.content.ContentValues; @@ -8,11 +10,10 @@ import android.database.Cursor; import android.location.Location; import android.net.Uri; -import android.os.Environment; -import android.preference.PreferenceManager; import android.util.Log; import net.osmtracker.OSMTracker; +import net.osmtracker.R; import net.osmtracker.db.model.Track; import net.osmtracker.db.model.TrackPoint; import net.osmtracker.db.model.WayPoint; @@ -127,8 +128,8 @@ public void track(long trackId, Location location, float azimuth, int accuracy, if (location.hasSpeed()) { values.put(TrackContentProvider.Schema.COL_SPEED, location.getSpeed()); } - - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + + SharedPreferences prefs = context.getSharedPreferences(context.getString(R.string.shared_pref), MODE_PRIVATE); if (prefs.getBoolean(OSMTracker.Preferences.KEY_GPS_IGNORE_CLOCK, OSMTracker.Preferences.VAL_GPS_IGNORE_CLOCK)) { // Use OS clock values.put(TrackContentProvider.Schema.COL_TIMESTAMP, System.currentTimeMillis()); @@ -199,7 +200,7 @@ public void wayPoint(long trackId, Location location, String name, String link, values.put(TrackContentProvider.Schema.COL_LINK, renameFile(trackId, link, FILENAME_FORMATTER.format(location.getTime()))); } - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + SharedPreferences prefs = context.getSharedPreferences(context.getString(R.string.shared_pref), MODE_PRIVATE); if (prefs.getBoolean(OSMTracker.Preferences.KEY_GPS_IGNORE_CLOCK, OSMTracker.Preferences.VAL_GPS_IGNORE_CLOCK)) { // Use OS clock values.put(TrackContentProvider.Schema.COL_TIMESTAMP, System.currentTimeMillis()); @@ -410,7 +411,8 @@ public static File getGPXTrackFile(long trackId, ContentResolver contentResolver File sdRoot = Environment.getExternalStorageDirectory(); // The location where the user has specified gpx files and associated content to be written - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + + SharedPreferences prefs = context.getSharedPreferences(context.getString(R.string.shared_pref), MODE_PRIVATE); String userGPXExportDirectoryName = prefs.getString( OSMTracker.Preferences.KEY_STORAGE_DIR, OSMTracker.Preferences.VAL_STORAGE_DIR); diff --git a/app/src/main/java/net/osmtracker/gpx/ExportToStorageTask.java b/app/src/main/java/net/osmtracker/gpx/ExportToStorageTask.java index 855929f85..21345ef9e 100644 --- a/app/src/main/java/net/osmtracker/gpx/ExportToStorageTask.java +++ b/app/src/main/java/net/osmtracker/gpx/ExportToStorageTask.java @@ -1,11 +1,11 @@ package net.osmtracker.gpx; +import static android.content.Context.MODE_PRIVATE; import static net.osmtracker.util.FileSystemUtils.getUniqueChildNameFor; import android.content.Context; import android.content.SharedPreferences; import android.os.Environment; -import android.preference.PreferenceManager; import android.util.Log; import net.osmtracker.OSMTracker; @@ -49,7 +49,7 @@ public ExportToStorageTask(Context context, long... trackId) { public ExportToStorageTask(Context context, DataHelper dataHelper, long... trackId) { super(context, trackId); this.dataHelper = dataHelper; - this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); + this.sharedPreferences = context.getSharedPreferences(context.getString(R.string.shared_pref), MODE_PRIVATE); ERROR_MESSAGE = context.getString(R.string.error_create_track_dir); } diff --git a/app/src/main/java/net/osmtracker/gpx/ExportTrackTask.java b/app/src/main/java/net/osmtracker/gpx/ExportTrackTask.java index 5e419a42f..7aba937fe 100644 --- a/app/src/main/java/net/osmtracker/gpx/ExportTrackTask.java +++ b/app/src/main/java/net/osmtracker/gpx/ExportTrackTask.java @@ -1,6 +1,9 @@ package net.osmtracker.gpx; -import android.Manifest; +import static android.content.Context.MODE_PRIVATE; +import static net.osmtracker.db.DataHelper.EXTENSION_GPX; +import static net.osmtracker.util.FileSystemUtils.getUniqueChildNameFor; + import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.ContentResolver; @@ -8,13 +11,10 @@ import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; -import android.content.pm.PackageManager; +import android.content.SharedPreferences; import android.database.Cursor; import android.media.MediaScannerConnection; import android.os.AsyncTask; -import android.os.Environment; -import android.preference.PreferenceManager; -import androidx.core.content.ContextCompat; import android.util.Log; import android.widget.Toast; @@ -37,9 +37,6 @@ import java.util.TimeZone; import java.util.regex.Pattern; -import static net.osmtracker.db.DataHelper.EXTENSION_GPX; -import static net.osmtracker.util.FileSystemUtils.getUniqueChildNameFor; - /** * Base class to writes a GPX file and export * track media (Photos, Sounds) @@ -277,14 +274,14 @@ protected void exportTrackAsGpx(long trackId) throws ExportTrackException { * @throws IOException */ private void writeGpxFile(String trackName, String tags, String track_description, Cursor cTrackPoints, Cursor cWayPoints, File target) throws IOException { - - String accuracyOutput = PreferenceManager.getDefaultSharedPreferences(context).getString( + SharedPreferences sharedPrefs = context.getSharedPreferences(context.getString(R.string.shared_pref), MODE_PRIVATE); + String accuracyOutput = sharedPrefs.getString( OSMTracker.Preferences.KEY_OUTPUT_ACCURACY, OSMTracker.Preferences.VAL_OUTPUT_ACCURACY); - boolean fillHDOP = PreferenceManager.getDefaultSharedPreferences(context).getBoolean( + boolean fillHDOP = sharedPrefs.getBoolean( OSMTracker.Preferences.KEY_OUTPUT_GPX_HDOP_APPROXIMATION, OSMTracker.Preferences.VAL_OUTPUT_GPX_HDOP_APPROXIMATION); - String compassOutput = PreferenceManager.getDefaultSharedPreferences(context).getString( + String compassOutput = sharedPrefs.getString( OSMTracker.Preferences.KEY_OUTPUT_COMPASS, OSMTracker.Preferences.VAL_OUTPUT_COMPASS); @@ -549,7 +546,8 @@ private void copyWaypointFiles(long trackId, File gpxOutputDirectory) { * @return GPX filename, not including the path */ public String buildGPXFilename(Cursor cursor, File parentDirectory) { - String desiredOutputFormat = PreferenceManager.getDefaultSharedPreferences(context).getString( + SharedPreferences sharedPrefs = context.getSharedPreferences(context.getString(R.string.shared_pref), MODE_PRIVATE); + String desiredOutputFormat = sharedPrefs.getString( OSMTracker.Preferences.KEY_OUTPUT_FILENAME, OSMTracker.Preferences.VAL_OUTPUT_FILENAME); diff --git a/app/src/main/java/net/osmtracker/layout/DownloadCustomLayoutTask.java b/app/src/main/java/net/osmtracker/layout/DownloadCustomLayoutTask.java index 2c4048aa5..e4287a59f 100644 --- a/app/src/main/java/net/osmtracker/layout/DownloadCustomLayoutTask.java +++ b/app/src/main/java/net/osmtracker/layout/DownloadCustomLayoutTask.java @@ -1,15 +1,18 @@ package net.osmtracker.layout; import android.content.Context; -import android.content.SharedPreferences; import android.os.AsyncTask; import android.os.Environment; -import android.preference.PreferenceManager; import android.util.Log; +import net.osmtracker.OSMTracker; +import net.osmtracker.R; +import net.osmtracker.activity.Preferences; +import net.osmtracker.util.CustomLayoutsUtils; +import net.osmtracker.util.URLCreator; + import org.json.JSONArray; import org.json.JSONObject; -import net.osmtracker.OSMTracker; import java.io.File; import java.io.FileOutputStream; @@ -20,11 +23,6 @@ import java.util.HashMap; import java.util.Set; -import net.osmtracker.R; -import net.osmtracker.activity.Preferences; -import net.osmtracker.util.CustomLayoutsUtils; -import net.osmtracker.util.URLCreator; - /** * Created by aton1698 on 13/12/17. */ @@ -49,7 +47,7 @@ protected Boolean doInBackground(String[] layoutData) { public boolean downloadLayout(String layoutName, String iso){ String layoutFolderName = layoutName.replace(" ", "_"); - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + // No usage of SharedPreferences why? String storageDir = File.separator + OSMTracker.Preferences.VAL_STORAGE_DIR; Log.d(TAG,"storage directory: " + storageDir); diff --git a/app/src/main/java/net/osmtracker/layout/GpsStatusRecord.java b/app/src/main/java/net/osmtracker/layout/GpsStatusRecord.java index f2b264d9d..1355be1ec 100644 --- a/app/src/main/java/net/osmtracker/layout/GpsStatusRecord.java +++ b/app/src/main/java/net/osmtracker/layout/GpsStatusRecord.java @@ -1,14 +1,10 @@ package net.osmtracker.layout; -import java.text.DecimalFormat; - -import net.osmtracker.OSMTracker; -import net.osmtracker.R; -import net.osmtracker.activity.TrackLogger; +import static android.content.Context.MODE_PRIVATE; import android.Manifest; import android.content.Context; -import android.content.Intent; +import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.location.GnssStatus; import android.location.Location; @@ -16,10 +12,6 @@ import android.location.LocationManager; import android.location.LocationProvider; import android.os.Bundle; -import android.preference.PreferenceManager; - -import androidx.core.app.ActivityCompat; -import androidx.core.content.ContextCompat; import android.util.AttributeSet; import android.util.Log; import android.view.LayoutInflater; @@ -27,6 +19,15 @@ import android.widget.LinearLayout; import android.widget.TextView; +import androidx.core.app.ActivityCompat; +import androidx.core.content.ContextCompat; + +import net.osmtracker.OSMTracker; +import net.osmtracker.R; +import net.osmtracker.activity.TrackLogger; + +import java.text.DecimalFormat; + /** * Layout for the GPS Status image and misc action buttons. * @@ -96,7 +97,8 @@ public GpsStatusRecord(Context context, AttributeSet attrs) { LayoutInflater.from(context).inflate(R.layout.gpsstatus_record, this, true); //read the logging interval from preferences - gpsLoggingInterval = Long.parseLong(PreferenceManager.getDefaultSharedPreferences(context).getString( + SharedPreferences sharedPrefs = context.getSharedPreferences(context.getString(R.string.shared_pref), MODE_PRIVATE); + gpsLoggingInterval = Long.parseLong(sharedPrefs.getString( OSMTracker.Preferences.KEY_GPS_LOGGING_INTERVAL, OSMTracker.Preferences.VAL_GPS_LOGGING_INTERVAL)) * 1000; if (context instanceof TrackLogger) { diff --git a/app/src/main/java/net/osmtracker/osm/UploadToOpenStreetMapTask.java b/app/src/main/java/net/osmtracker/osm/UploadToOpenStreetMapTask.java index f78c80b9f..06fcbaea2 100644 --- a/app/src/main/java/net/osmtracker/osm/UploadToOpenStreetMapTask.java +++ b/app/src/main/java/net/osmtracker/osm/UploadToOpenStreetMapTask.java @@ -1,12 +1,14 @@ package net.osmtracker.osm; +import static android.content.Context.MODE_PRIVATE; + import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.DialogInterface; +import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.AsyncTask; -import android.preference.PreferenceManager; import android.util.Log; import net.osmtracker.OSMTracker; @@ -153,9 +155,10 @@ public void onClick(DialogInterface dialog, int which) { .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - Editor editor = PreferenceManager.getDefaultSharedPreferences(activity).edit(); + SharedPreferences sharedPrefs = activity.getSharedPreferences(activity.getString(R.string.shared_pref), MODE_PRIVATE); + Editor editor = sharedPrefs.edit(); editor.remove(OSMTracker.Preferences.KEY_OSM_OAUTH2_ACCESSTOKEN); - editor.commit(); + editor.apply(); dialog.dismiss(); } diff --git a/app/src/main/java/net/osmtracker/service/gps/GPSLogger.java b/app/src/main/java/net/osmtracker/service/gps/GPSLogger.java index 77ae1987c..147330eff 100644 --- a/app/src/main/java/net/osmtracker/service/gps/GPSLogger.java +++ b/app/src/main/java/net/osmtracker/service/gps/GPSLogger.java @@ -10,6 +10,7 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.location.Location; import android.location.LocationListener; @@ -18,7 +19,6 @@ import android.os.Build; import android.os.Bundle; import android.os.IBinder; -import android.preference.PreferenceManager; import android.util.Log; import androidx.core.app.NotificationCompat; @@ -209,11 +209,12 @@ public void onCreate() { dataHelper = new DataHelper(this); //read the logging interval from preferences - gpsLoggingInterval = Long.parseLong(PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext()).getString( + SharedPreferences sharedPrefs = getSharedPreferences(getString(R.string.shared_pref), MODE_PRIVATE); + gpsLoggingInterval = Long.parseLong(sharedPrefs.getString( OSMTracker.Preferences.KEY_GPS_LOGGING_INTERVAL, OSMTracker.Preferences.VAL_GPS_LOGGING_INTERVAL)) * 1000; - gpsLoggingMinDistance = Long.parseLong(PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext()).getString( + gpsLoggingMinDistance = Long.parseLong(sharedPrefs.getString( OSMTracker.Preferences.KEY_GPS_LOGGING_MIN_DISTANCE, OSMTracker.Preferences.VAL_GPS_LOGGING_MIN_DISTANCE)); - use_barometer = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext()).getBoolean( + use_barometer = sharedPrefs.getBoolean( OSMTracker.Preferences.KEY_USE_BAROMETER, OSMTracker.Preferences.VAL_USE_BAROMETER); // Register our broadcast receiver diff --git a/app/src/main/java/net/osmtracker/util/CustomLayoutsUtils.java b/app/src/main/java/net/osmtracker/util/CustomLayoutsUtils.java index b07f7884f..930a14d2f 100644 --- a/app/src/main/java/net/osmtracker/util/CustomLayoutsUtils.java +++ b/app/src/main/java/net/osmtracker/util/CustomLayoutsUtils.java @@ -1,10 +1,12 @@ package net.osmtracker.util; +import static android.content.Context.MODE_PRIVATE; + import android.content.Context; import android.content.SharedPreferences; -import android.preference.PreferenceManager; import net.osmtracker.OSMTracker; +import net.osmtracker.R; import net.osmtracker.activity.AvailableLayouts; import net.osmtracker.activity.Preferences; import net.osmtracker.layout.GetStringResponseTask; @@ -101,7 +103,7 @@ public static String getStringFromStream(InputStream stream) throws IOException public static String getCurrentLayoutName(Context context){ String layoutName = ""; try { - SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); + SharedPreferences sharedPreferences = context.getSharedPreferences(context.getString(R.string.shared_pref), MODE_PRIVATE); layoutName = sharedPreferences.getString(OSMTracker.Preferences.KEY_UI_BUTTONS_LAYOUT, OSMTracker.Preferences.VAL_UI_BUTTONS_LAYOUT); } catch (Exception e){ diff --git a/app/src/main/java/net/osmtracker/util/ThemeValidator.java b/app/src/main/java/net/osmtracker/util/ThemeValidator.java index 8ead833c5..f310358fb 100644 --- a/app/src/main/java/net/osmtracker/util/ThemeValidator.java +++ b/app/src/main/java/net/osmtracker/util/ThemeValidator.java @@ -1,13 +1,13 @@ package net.osmtracker.util; -import java.util.Arrays; +import android.content.SharedPreferences; +import android.content.SharedPreferences.Editor; +import android.content.res.Resources; import net.osmtracker.OSMTracker; import net.osmtracker.R; -import android.content.SharedPreferences; -import android.content.SharedPreferences.Editor; -import android.content.res.Resources; +import java.util.Arrays; /** *

Validates the theme from the preferences @@ -35,7 +35,7 @@ public static String getValidTheme(SharedPreferences prefs, Resources res) { theme = OSMTracker.Preferences.VAL_UI_THEME; Editor e = prefs.edit(); e.putString(OSMTracker.Preferences.KEY_UI_THEME, OSMTracker.Preferences.VAL_UI_THEME); - e.commit(); + e.apply(); } return theme; diff --git a/app/src/main/java/net/osmtracker/util/URLCreator.java b/app/src/main/java/net/osmtracker/util/URLCreator.java index e87f5d81e..06ab6533c 100644 --- a/app/src/main/java/net/osmtracker/util/URLCreator.java +++ b/app/src/main/java/net/osmtracker/util/URLCreator.java @@ -1,9 +1,12 @@ package net.osmtracker.util; +import static android.content.Context.MODE_PRIVATE; + import android.content.Context; import android.content.SharedPreferences; -import android.preference.PreferenceManager; + import net.osmtracker.OSMTracker; +import net.osmtracker.R; import net.osmtracker.activity.Preferences; /** @@ -114,7 +117,7 @@ public static String createTestURL(String ghUsername, String repositoryName, Str */ private static String[] getGithubParams(Context context) { //the shared preferences file where the values are saved - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + SharedPreferences preferences = context.getSharedPreferences(context.getString(R.string.shared_pref), MODE_PRIVATE); String username = preferences.getString(OSMTracker.Preferences.KEY_GITHUB_USERNAME, OSMTracker.Preferences.VAL_GITHUB_USERNAME); String repo = preferences.getString(OSMTracker.Preferences.KEY_REPOSITORY_NAME, OSMTracker.Preferences.VAL_REPOSITORY_NAME); diff --git a/app/src/main/java/net/osmtracker/view/VoiceRecDialog.java b/app/src/main/java/net/osmtracker/view/VoiceRecDialog.java index 97018ffc0..43923a94e 100644 --- a/app/src/main/java/net/osmtracker/view/VoiceRecDialog.java +++ b/app/src/main/java/net/osmtracker/view/VoiceRecDialog.java @@ -1,5 +1,7 @@ package net.osmtracker.view; +import static android.content.Context.MODE_PRIVATE; + import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; @@ -10,7 +12,6 @@ import android.media.MediaRecorder; import android.media.MediaRecorder.OnInfoListener; import android.os.SystemClock; -import android.preference.PreferenceManager; import android.util.Log; import android.view.KeyEvent; import android.widget.Toast; @@ -117,7 +118,7 @@ public void onStart() { // we'll need the start time of this dialog to check if a key has been pressed before the dialog was opened dialogStartTime = SystemClock.uptimeMillis(); - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + SharedPreferences preferences = context.getSharedPreferences(context.getString(R.string.shared_pref), MODE_PRIVATE); if (!isRecording) recordingDuration = Integer.parseInt( diff --git a/app/src/main/res/values/strings-preferences.xml b/app/src/main/res/values/strings-preferences.xml index 7f908c769..15f6f1222 100644 --- a/app/src/main/res/values/strings-preferences.xml +++ b/app/src/main/res/values/strings-preferences.xml @@ -1,5 +1,7 @@ + + net.osmtracker.preferences Settings diff --git a/app/src/test/java/net/osmtracker/activity/ButtonsPresetsTest.java b/app/src/test/java/net/osmtracker/activity/ButtonsPresetsTest.java index 07c7b5f6b..17427a1bb 100644 --- a/app/src/test/java/net/osmtracker/activity/ButtonsPresetsTest.java +++ b/app/src/test/java/net/osmtracker/activity/ButtonsPresetsTest.java @@ -1,12 +1,23 @@ package net.osmtracker.activity; -import android.content.SharedPreferences; -import android.widget.CheckBox; +import static android.content.Context.MODE_PRIVATE; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.verify; +import static org.powermock.api.mockito.PowerMockito.mock; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.mockito.PowerMockito.verifyPrivate; +import static org.powermock.api.mockito.PowerMockito.when; + import android.content.Context; +import android.content.SharedPreferences; import android.content.res.Resources; import android.os.Environment; import android.preference.PreferenceManager; import android.view.View; +import android.widget.CheckBox; import android.widget.LinearLayout; import android.widget.TextView; @@ -20,19 +31,11 @@ import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.reflect.Whitebox; -import java.lang.reflect.Field; import java.io.File; +import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.Hashtable; -import static org.junit.Assert.*; -import static org.mockito.Mockito.verify; -import static org.powermock.api.mockito.PowerMockito.mock; - -import static org.powermock.api.mockito.PowerMockito.mockStatic; -import static org.powermock.api.mockito.PowerMockito.verifyPrivate; -import static org.powermock.api.mockito.PowerMockito.when; - @RunWith(PowerMockRunner.class) @PowerMockIgnore("jdk.internal.reflect.*") public class ButtonsPresetsTest { @@ -215,8 +218,7 @@ private void callListLayouts(ButtonsPresets mockActivity, LinearLayout mockRootL private void mockDefaultSharedPreferences(Context context) { SharedPreferences mockPrefs = mock(SharedPreferences.class); - mockStatic(PreferenceManager.class); - when(PreferenceManager.getDefaultSharedPreferences(context)).thenReturn(mockPrefs); + when(context.getSharedPreferences(context.getString(R.string.shared_pref), MODE_PRIVATE)).thenReturn(mockPrefs); } private void checkAttributesAfterInitialization(ButtonsPresets mockActivity) { @@ -266,10 +268,8 @@ private void setupMocksForSelectLayoutTest() throws Exception{ // Mock and set the PreferencesEditor mockEditor = mock(SharedPreferences.Editor.class); - when(mockEditor.commit()).thenReturn(true); when(mockEditor.putString("ui.buttons.layout", label)).thenReturn(mockEditor); - // Mock prefs to use the mock editor mockPrefs = mock(SharedPreferences.class); when(mockPrefs.edit()).thenReturn(mockEditor); @@ -305,7 +305,7 @@ private void makeAssertionsForSelectLayout() throws Exception{ // Make sure the value in SharedPreferences is updated to match the just selected verify(mockEditor).putString(OSMTracker.Preferences.KEY_UI_BUTTONS_LAYOUT, label); - verify(mockEditor).commit(); + verify(mockEditor).apply(); } } diff --git a/app/src/test/java/net/osmtracker/gpx/ExportToStorageTaskTest.java b/app/src/test/java/net/osmtracker/gpx/ExportToStorageTaskTest.java index ce5d5b5a0..85d052d52 100644 --- a/app/src/test/java/net/osmtracker/gpx/ExportToStorageTaskTest.java +++ b/app/src/test/java/net/osmtracker/gpx/ExportToStorageTaskTest.java @@ -1,5 +1,6 @@ package net.osmtracker.gpx; +import static android.content.Context.MODE_PRIVATE; import static junit.framework.TestCase.assertEquals; import static junit.framework.TestCase.assertNull; import static net.osmtracker.OSMTracker.Preferences; @@ -64,7 +65,7 @@ public void setUp() { mockStatic(Environment.class); mockStatic(PreferenceManager.class); - when(PreferenceManager.getDefaultSharedPreferences(mockContext)).thenReturn(mockPrefs); + when(mockContext.getSharedPreferences(mockContext.getString(R.string.shared_pref), MODE_PRIVATE)).thenReturn(mockPrefs); when(mockContext.getResources()).thenReturn(mockResources); when(mockResources.getString(R.string.error_create_track_dir)).thenReturn(ERROR_CREATE_TRACK_DIR); when(mockResources.getString(R.string.error_externalstorage_not_writable)).thenReturn(UNABLE_TO_WRITE_STORAGE); diff --git a/app/src/test/java/net/osmtracker/layout/DownloadCustomLayoutTaskTest.java b/app/src/test/java/net/osmtracker/layout/DownloadCustomLayoutTaskTest.java index f0f3334c1..f50045954 100644 --- a/app/src/test/java/net/osmtracker/layout/DownloadCustomLayoutTaskTest.java +++ b/app/src/test/java/net/osmtracker/layout/DownloadCustomLayoutTaskTest.java @@ -1,5 +1,12 @@ package net.osmtracker.layout; +import static android.content.Context.MODE_PRIVATE; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.powermock.api.mockito.PowerMockito.mock; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.mockito.PowerMockito.when; + import android.content.Context; import android.content.SharedPreferences; import android.os.Environment; @@ -7,6 +14,7 @@ import android.util.Log; import net.osmtracker.OSMTracker; +import net.osmtracker.R; import net.osmtracker.activity.Preferences; import net.osmtracker.util.UnitTestUtils; @@ -18,12 +26,6 @@ import java.io.File; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.powermock.api.mockito.PowerMockito.mock; -import static org.powermock.api.mockito.PowerMockito.mockStatic; -import static org.powermock.api.mockito.PowerMockito.when; - @RunWith(PowerMockRunner.class) @PrepareForTest({PreferenceManager.class, Environment.class, Log.class}) @PowerMockIgnore("jdk.internal.reflect.*") @@ -48,7 +50,8 @@ public void setupMocks() { // Create PreferenceManager mock mockContext = mock(Context.class); mockStatic(PreferenceManager.class); - when(PreferenceManager.getDefaultSharedPreferences(mockContext)).thenReturn(mockPrefs); + + when(mockContext.getSharedPreferences(mockContext.getString(R.string.shared_pref), MODE_PRIVATE)).thenReturn(mockPrefs); // external storage is writeable mockStatic(Environment.class); when(Environment.getExternalStorageState()).thenReturn(Environment.MEDIA_MOUNTED); diff --git a/app/src/test/java/net/osmtracker/util/CustomLayoutsUtilsTest.java b/app/src/test/java/net/osmtracker/util/CustomLayoutsUtilsTest.java index 0d0a2efaa..dfffadeb2 100644 --- a/app/src/test/java/net/osmtracker/util/CustomLayoutsUtilsTest.java +++ b/app/src/test/java/net/osmtracker/util/CustomLayoutsUtilsTest.java @@ -1,11 +1,18 @@ package net.osmtracker.util; +import static android.content.Context.MODE_PRIVATE; +import static org.junit.Assert.assertEquals; +import static org.powermock.api.mockito.PowerMockito.mock; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.mockito.PowerMockito.when; + import android.content.Context; import android.content.SharedPreferences; import android.content.res.AssetManager; import android.preference.PreferenceManager; import net.osmtracker.OSMTracker; +import net.osmtracker.R; import org.junit.Test; import org.junit.runner.RunWith; @@ -19,12 +26,6 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; -import java.util.Scanner; - -import static org.junit.Assert.assertEquals; -import static org.powermock.api.mockito.PowerMockito.mock; -import static org.powermock.api.mockito.PowerMockito.mockStatic; -import static org.powermock.api.mockito.PowerMockito.when; @RunWith(PowerMockRunner.class) @PrepareForTest(PreferenceManager.class) @@ -49,7 +50,7 @@ public void setupMocks() { mockStatic(PreferenceManager.class); - when(PreferenceManager.getDefaultSharedPreferences(mockContext)).thenReturn(mockPrefs); + when(mockContext.getSharedPreferences(mockContext.getString(R.string.shared_pref), MODE_PRIVATE)).thenReturn(mockPrefs); mockAssetManager = mock(AssetManager.class); diff --git a/app/src/test/java/net/osmtracker/util/ThemeValidatorTest.java b/app/src/test/java/net/osmtracker/util/ThemeValidatorTest.java index 7072e3966..449be643b 100644 --- a/app/src/test/java/net/osmtracker/util/ThemeValidatorTest.java +++ b/app/src/test/java/net/osmtracker/util/ThemeValidatorTest.java @@ -1,9 +1,18 @@ package net.osmtracker.util; + +import static android.content.Context.MODE_PRIVATE; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.verify; +import static org.powermock.api.mockito.PowerMockito.mock; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.mockito.PowerMockito.when; + import android.content.Context; import android.content.SharedPreferences; +import android.content.SharedPreferences.Editor; import android.content.res.Resources; import android.preference.PreferenceManager; -import android.content.SharedPreferences.Editor; import net.osmtracker.OSMTracker; import net.osmtracker.R; @@ -14,13 +23,6 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import static org.junit.Assert.*; -import static org.mockito.Mockito.atLeastOnce; -import static org.mockito.Mockito.verify; -import static org.powermock.api.mockito.PowerMockito.mock; -import static org.powermock.api.mockito.PowerMockito.mockStatic; -import static org.powermock.api.mockito.PowerMockito.when; - @RunWith(PowerMockRunner.class) @PrepareForTest(PreferenceManager.class) @@ -57,7 +59,7 @@ public void setupMocks(String theme) { mockStatic(PreferenceManager.class); - when(PreferenceManager.getDefaultSharedPreferences(mockContext)).thenReturn(mockPrefs); + when(mockContext.getSharedPreferences(mockContext.getString(R.string.shared_pref), MODE_PRIVATE)).thenReturn(mockPrefs); mockEditor=mock(SharedPreferences.Editor.class); @@ -85,6 +87,6 @@ public void validateWrongTheme(){ assertEquals(result, expected); verify(mockPrefs,atLeastOnce()).edit(); verify(mockEditor,atLeastOnce()).putString(OSMTracker.Preferences.KEY_UI_THEME, OSMTracker.Preferences.VAL_UI_THEME); - verify(mockEditor).commit(); + verify(mockEditor).apply(); } } diff --git a/app/src/test/java/net/osmtracker/util/URLCreatorTest.java b/app/src/test/java/net/osmtracker/util/URLCreatorTest.java index 001366bea..f13c51d4d 100644 --- a/app/src/test/java/net/osmtracker/util/URLCreatorTest.java +++ b/app/src/test/java/net/osmtracker/util/URLCreatorTest.java @@ -1,20 +1,23 @@ package net.osmtracker.util; +import static android.content.Context.MODE_PRIVATE; +import static org.junit.Assert.assertEquals; +import static org.powermock.api.mockito.PowerMockito.mock; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.mockito.PowerMockito.when; + import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; +import net.osmtracker.R; + import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import static org.junit.Assert.*; -import static org.powermock.api.mockito.PowerMockito.mock; -import static org.powermock.api.mockito.PowerMockito.mockStatic; -import static org.powermock.api.mockito.PowerMockito.when; - @RunWith(PowerMockRunner.class) @PrepareForTest(PreferenceManager.class) @@ -34,8 +37,7 @@ public void setupMocks(){ // Create PreferenceManager mock mockContext = mock(Context.class); mockStatic(PreferenceManager.class); - when(PreferenceManager.getDefaultSharedPreferences(mockContext)).thenReturn(mockPrefs); - + when(mockContext.getSharedPreferences(mockContext.getString(R.string.shared_pref), MODE_PRIVATE)).thenReturn(mockPrefs); } @Test