Skip to content

Commit

Permalink
Replace getDefaultSharedPreferences by getSharedPreferences
Browse files Browse the repository at this point in the history
  • Loading branch information
Binnette committed Dec 6, 2024
1 parent bce871a commit b9b278d
Show file tree
Hide file tree
Showing 31 changed files with 209 additions and 199 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}
};
Expand Down
11 changes: 7 additions & 4 deletions app/src/androidTest/java/net/osmtracker/util/TestUtils.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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(){
Expand Down
24 changes: 10 additions & 14 deletions app/src/main/java/net/osmtracker/activity/About.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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),
Expand Down
32 changes: 16 additions & 16 deletions app/src/main/java/net/osmtracker/activity/AvailableLayouts.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,16 +25,17 @@
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;
import net.osmtracker.layout.GetStringResponseTask;
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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand All @@ -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
Expand All @@ -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);
}
}
Expand All @@ -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();
Expand Down
12 changes: 6 additions & 6 deletions app/src/main/java/net/osmtracker/activity/ButtonsPresets.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<String, String>();
storageDir = File.separator + OSMTracker.Preferences.VAL_STORAGE_DIR;
}
Expand Down Expand Up @@ -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();
}
Expand All @@ -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
Expand Down
24 changes: 11 additions & 13 deletions app/src/main/java/net/osmtracker/activity/DisplayTrack.java
Original file line number Diff line number Diff line change
@@ -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.
*<P>
Expand All @@ -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);

Expand All @@ -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() {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/net/osmtracker/activity/Intro.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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, ""));
Expand Down Expand Up @@ -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(
Expand Down
Loading

0 comments on commit b9b278d

Please sign in to comment.