1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-29 07:41:17 +00:00

Prefer injecting preferences instead of using PreferenceManager directly (#2073)

# Description
Prefer injecting preferences instead of using
`PreferenceManager.getDefaultSharedPreferences(context)` directly.

# Changes
* replace `PreferenceManager.getDefaultSharedPreferences(context)` by
injecting SharedPreferences instead
This commit is contained in:
Adam Szewera 2024-04-05 11:47:50 +02:00 committed by GitHub
parent 3127951ad4
commit 7904afd1ff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 34 additions and 20 deletions

View file

@ -4,6 +4,8 @@ import com.nutomic.syncthingandroid.activities.FirstStartActivity;
import com.nutomic.syncthingandroid.activities.FolderPickerActivity; import com.nutomic.syncthingandroid.activities.FolderPickerActivity;
import com.nutomic.syncthingandroid.activities.MainActivity; import com.nutomic.syncthingandroid.activities.MainActivity;
import com.nutomic.syncthingandroid.activities.SettingsActivity; import com.nutomic.syncthingandroid.activities.SettingsActivity;
import com.nutomic.syncthingandroid.activities.ShareActivity;
import com.nutomic.syncthingandroid.activities.ThemedAppCompatActivity;
import com.nutomic.syncthingandroid.receiver.AppConfigReceiver; import com.nutomic.syncthingandroid.receiver.AppConfigReceiver;
import com.nutomic.syncthingandroid.service.RunConditionMonitor; import com.nutomic.syncthingandroid.service.RunConditionMonitor;
import com.nutomic.syncthingandroid.service.EventProcessor; import com.nutomic.syncthingandroid.service.EventProcessor;
@ -34,4 +36,6 @@ public interface DaggerComponent {
void inject(AppConfigReceiver appConfigReceiver); void inject(AppConfigReceiver appConfigReceiver);
void inject(RestApi restApi); void inject(RestApi restApi);
void inject(SettingsActivity.SettingsFragment fragment); void inject(SettingsActivity.SettingsFragment fragment);
void inject(ShareActivity activity);
void inject(ThemedAppCompatActivity activity);
} }

View file

@ -21,7 +21,6 @@ import androidx.core.content.ContextCompat;
import androidx.viewpager.widget.PagerAdapter; import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager; import androidx.viewpager.widget.ViewPager;
import android.preference.PreferenceManager;
import android.provider.Settings; import android.provider.Settings;
import android.text.Html; import android.text.Html;
import android.util.Log; import android.util.Log;
@ -240,7 +239,7 @@ public class FirstStartActivity extends Activity {
case API_LEVEL_30: case API_LEVEL_30:
// Skip if running as root, as that circumvents any Android FS restrictions. // Skip if running as root, as that circumvents any Android FS restrictions.
return upgradedToApiLevel30() return upgradedToApiLevel30()
|| PreferenceManager.getDefaultSharedPreferences(this).getBoolean(Constants.PREF_USE_ROOT, false); || mPreferences.getBoolean(Constants.PREF_USE_ROOT, false);
case NOTIFICATION: case NOTIFICATION:
return isNotificationPermissionGranted(); return isNotificationPermissionGranted();

View file

@ -6,11 +6,10 @@ import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.os.IBinder; import android.os.IBinder;
import android.preference.PreferenceManager;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
@ -42,6 +41,8 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import javax.inject.Inject;
/** /**
* Activity that allows selecting a directory in the local file system. * Activity that allows selecting a directory in the local file system.
*/ */
@ -68,6 +69,9 @@ public class FolderPickerActivity extends SyncthingActivity
*/ */
private File mLocation; private File mLocation;
@Inject
SharedPreferences mPreferences;
public static Intent createIntent(Context context, String initialDirectory, @Nullable String rootDirectory) { public static Intent createIntent(Context context, String initialDirectory, @Nullable String rootDirectory) {
Intent intent = new Intent(context, FolderPickerActivity.class); Intent intent = new Intent(context, FolderPickerActivity.class);
@ -103,7 +107,7 @@ public class FolderPickerActivity extends SyncthingActivity
displayRoot(); displayRoot();
} }
Boolean prefUseRoot = PreferenceManager.getDefaultSharedPreferences(this).getBoolean(Constants.PREF_USE_ROOT, false); Boolean prefUseRoot = mPreferences.getBoolean(Constants.PREF_USE_ROOT, false);
if (!prefUseRoot) { if (!prefUseRoot) {
Toast.makeText(this, R.string.kitkat_external_storage_warning, Toast.LENGTH_LONG) Toast.makeText(this, R.string.kitkat_external_storage_warning, Toast.LENGTH_LONG)
.show(); .show();
@ -133,8 +137,7 @@ public class FolderPickerActivity extends SyncthingActivity
roots.add(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)); roots.add(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS));
// Add paths that might not be accessible to Syncthing. // Add paths that might not be accessible to Syncthing.
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); if (mPreferences.getBoolean("advanced_folder_picker", false)) {
if (sp.getBoolean("advanced_folder_picker", false)) {
Collections.addAll(roots, new File("/storage/").listFiles()); Collections.addAll(roots, new File("/storage/").listFiles());
roots.add(new File("/")); roots.add(new File("/"));
} }

View file

@ -12,7 +12,6 @@ import android.preference.ListPreference;
import android.preference.Preference; import android.preference.Preference;
import android.preference.PreferenceGroup; import android.preference.PreferenceGroup;
import android.preference.PreferenceFragment; import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen; import android.preference.PreferenceScreen;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -268,7 +267,6 @@ public class SettingsActivity extends SyncthingActivity {
mHttpProxyAddress.setOnPreferenceChangeListener(this); mHttpProxyAddress.setOnPreferenceChangeListener(this);
/* Initialize summaries */ /* Initialize summaries */
mPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
screen.findPreference(Constants.PREF_POWER_SOURCE).setSummary(mPowerSource.getEntry()); screen.findPreference(Constants.PREF_POWER_SOURCE).setSummary(mPowerSource.getEntry());
String wifiSsidSummary = TextUtils.join(", ", mPreferences.getStringSet(Constants.PREF_WIFI_SSID_WHITELIST, new HashSet<>())); String wifiSsidSummary = TextUtils.join(", ", mPreferences.getStringSet(Constants.PREF_WIFI_SSID_WHITELIST, new HashSet<>()));
screen.findPreference(Constants.PREF_WIFI_SSID_WHITELIST).setSummary(TextUtils.isEmpty(wifiSsidSummary) ? screen.findPreference(Constants.PREF_WIFI_SSID_WHITELIST).setSummary(TextUtils.isEmpty(wifiSsidSummary) ?

View file

@ -5,11 +5,11 @@ import static android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HI
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
@ -23,6 +23,7 @@ import android.widget.Toast;
import com.google.common.io.Files; import com.google.common.io.Files;
import com.nutomic.syncthingandroid.R; import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.SyncthingApp;
import com.nutomic.syncthingandroid.databinding.ActivityShareBinding; import com.nutomic.syncthingandroid.databinding.ActivityShareBinding;
import com.nutomic.syncthingandroid.model.Folder; import com.nutomic.syncthingandroid.model.Folder;
import com.nutomic.syncthingandroid.service.SyncthingService; import com.nutomic.syncthingandroid.service.SyncthingService;
@ -40,6 +41,8 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.inject.Inject;
/** /**
* Shares incoming files to syncthing folders. * Shares incoming files to syncthing folders.
* <p> * <p>
@ -60,6 +63,9 @@ public class ShareActivity extends StateDialogActivity
private ActivityShareBinding binding; private ActivityShareBinding binding;
@Inject
SharedPreferences mPreferences;
@Override @Override
public void onServiceStateChange(SyncthingService.State currentState) { public void onServiceStateChange(SyncthingService.State currentState) {
if (currentState != SyncthingService.State.ACTIVE || getApi() == null) if (currentState != SyncthingService.State.ACTIVE || getApi() == null)
@ -69,8 +75,7 @@ public class ShareActivity extends StateDialogActivity
// Get the index of the previously selected folder. // Get the index of the previously selected folder.
int folderIndex = 0; int folderIndex = 0;
String savedFolderId = PreferenceManager.getDefaultSharedPreferences(this) String savedFolderId = mPreferences.getString(PREF_PREVIOUSLY_SELECTED_SYNCTHING_FOLDER, "");
.getString(PREF_PREVIOUSLY_SELECTED_SYNCTHING_FOLDER, "");
for (Folder folder : folders) { for (Folder folder : folders) {
if (folder.id.equals(savedFolderId)) { if (folder.id.equals(savedFolderId)) {
folderIndex = folders.indexOf(folder); folderIndex = folders.indexOf(folder);
@ -102,6 +107,7 @@ public class ShareActivity extends StateDialogActivity
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
((SyncthingApp) getApplication()).component().inject(this);
binding = ActivityShareBinding.inflate(getLayoutInflater()); binding = ActivityShareBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot()); setContentView(binding.getRoot());
@ -266,8 +272,7 @@ public class ShareActivity extends StateDialogActivity
String savedSubDirectory = ""; String savedSubDirectory = "";
if (selectedFolder != null) { if (selectedFolder != null) {
savedSubDirectory = PreferenceManager.getDefaultSharedPreferences(this) savedSubDirectory = mPreferences.getString(PREF_FOLDER_SAVED_SUBDIRECTORY + selectedFolder.id, "");
.getString(PREF_FOLDER_SAVED_SUBDIRECTORY + selectedFolder.id, "");
} }
return savedSubDirectory; return savedSubDirectory;
@ -362,7 +367,7 @@ public class ShareActivity extends StateDialogActivity
super.onPause(); super.onPause();
if (mFoldersSpinner.getSelectedItem() != null) { if (mFoldersSpinner.getSelectedItem() != null) {
Folder selectedFolder = (Folder) mFoldersSpinner.getSelectedItem(); Folder selectedFolder = (Folder) mFoldersSpinner.getSelectedItem();
PreferenceManager.getDefaultSharedPreferences(this).edit() mPreferences.edit()
.putString(PREF_PREVIOUSLY_SELECTED_SYNCTHING_FOLDER, selectedFolder.id) .putString(PREF_PREVIOUSLY_SELECTED_SYNCTHING_FOLDER, selectedFolder.id)
.apply(); .apply();
} }
@ -379,7 +384,7 @@ public class ShareActivity extends StateDialogActivity
subDirectory = subDirectory.replace(folderDirectory, ""); subDirectory = subDirectory.replace(folderDirectory, "");
mSubDirectoryTextView.setText(subDirectory); mSubDirectoryTextView.setText(subDirectory);
PreferenceManager.getDefaultSharedPreferences(this) mPreferences
.edit().putString(PREF_FOLDER_SAVED_SUBDIRECTORY + selectedFolder.id, subDirectory) .edit().putString(PREF_FOLDER_SAVED_SUBDIRECTORY + selectedFolder.id, subDirectory)
.apply(); .apply();
} }

View file

@ -2,12 +2,14 @@ package com.nutomic.syncthingandroid.activities;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate; import androidx.appcompat.app.AppCompatDelegate;
import com.nutomic.syncthingandroid.SyncthingApp;
import com.nutomic.syncthingandroid.service.Constants; import com.nutomic.syncthingandroid.service.Constants;
import javax.inject.Inject;
/** /**
* Provides a themed instance of AppCompatActivity. * Provides a themed instance of AppCompatActivity.
*/ */
@ -15,12 +17,15 @@ public class ThemedAppCompatActivity extends AppCompatActivity {
private static final String FOLLOW_SYSTEM = "-1"; private static final String FOLLOW_SYSTEM = "-1";
@Inject
SharedPreferences mPreferences;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
((SyncthingApp) getApplication()).component().inject(this);
// Load theme. // Load theme.
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
//For api level below 28, Follow system fall backs to light mode //For api level below 28, Follow system fall backs to light mode
Integer prefAppTheme = Integer.parseInt(prefs.getString(Constants.PREF_APP_THEME, FOLLOW_SYSTEM)); Integer prefAppTheme = Integer.parseInt(mPreferences.getString(Constants.PREF_APP_THEME, FOLLOW_SYSTEM));
AppCompatDelegate.setDefaultNightMode(prefAppTheme); AppCompatDelegate.setDefaultNightMode(prefAppTheme);
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
} }

@ -1 +1 @@
Subproject commit bdfd0f0548d2f6fc4b5500690dbd383baa3b0561 Subproject commit 683b48182c08095bb396b810d0cbe40f8bccd348