Skip to content

Commit

Permalink
Prefer injecting preferences instead of using PreferenceManager directly
Browse files Browse the repository at this point in the history
  • Loading branch information
adamszewe committed Apr 3, 2024
1 parent 3127951 commit 79449d5
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.nutomic.syncthingandroid.activities.FolderPickerActivity;
import com.nutomic.syncthingandroid.activities.MainActivity;
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.service.RunConditionMonitor;
import com.nutomic.syncthingandroid.service.EventProcessor;
Expand Down Expand Up @@ -34,4 +36,7 @@ public interface DaggerComponent {
void inject(AppConfigReceiver appConfigReceiver);
void inject(RestApi restApi);
void inject(SettingsActivity.SettingsFragment fragment);
void inject(ShareActivity activity);
void inject(ThemedAppCompatActivity activity);

}
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ private boolean shouldSkipSlide(Slide slide) {
case API_LEVEL_30:
// Skip if running as root, as that circumvents any Android FS restrictions.
return upgradedToApiLevel30()
|| PreferenceManager.getDefaultSharedPreferences(this).getBoolean(Constants.PREF_USE_ROOT, false);
|| mPreferences.getBoolean(Constants.PREF_USE_ROOT, false);
case NOTIFICATION:
return isNotificationPermissionGranted();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import java.util.Collections;
import java.util.Iterator;

import javax.inject.Inject;

/**
* Activity that allows selecting a directory in the local file system.
*/
Expand All @@ -68,6 +70,9 @@ public class FolderPickerActivity extends SyncthingActivity
*/
private File mLocation;

@Inject
SharedPreferences mPreferences;

public static Intent createIntent(Context context, String initialDirectory, @Nullable String rootDirectory) {
Intent intent = new Intent(context, FolderPickerActivity.class);

Expand Down Expand Up @@ -103,7 +108,7 @@ protected void onCreate(Bundle savedInstanceState) {
displayRoot();
}

Boolean prefUseRoot = PreferenceManager.getDefaultSharedPreferences(this).getBoolean(Constants.PREF_USE_ROOT, false);
Boolean prefUseRoot = mPreferences.getBoolean(Constants.PREF_USE_ROOT, false);
if (!prefUseRoot) {
Toast.makeText(this, R.string.kitkat_external_storage_warning, Toast.LENGTH_LONG)
.show();
Expand Down Expand Up @@ -133,8 +138,7 @@ private void populateRoots() {
roots.add(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS));

// Add paths that might not be accessible to Syncthing.
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
if (sp.getBoolean("advanced_folder_picker", false)) {
if (mPreferences.getBoolean("advanced_folder_picker", false)) {
Collections.addAll(roots, new File("/storage/").listFiles());
roots.add(new File("/"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class SettingsActivity extends SyncthingActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((SyncthingApp) getApplication()).component().inject(this);
setContentView(R.layout.activity_preferences);
setTitle(R.string.settings_title);

Expand Down Expand Up @@ -268,7 +269,6 @@ public void onActivityCreated(Bundle savedInstanceState) {
mHttpProxyAddress.setOnPreferenceChangeListener(this);

/* Initialize summaries */
mPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
screen.findPreference(Constants.PREF_POWER_SOURCE).setSummary(mPowerSource.getEntry());
String wifiSsidSummary = TextUtils.join(", ", mPreferences.getStringSet(Constants.PREF_WIFI_SSID_WHITELIST, new HashSet<>()));
screen.findPreference(Constants.PREF_WIFI_SSID_WHITELIST).setSummary(TextUtils.isEmpty(wifiSsidSummary) ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
Expand All @@ -23,6 +24,7 @@

import com.google.common.io.Files;
import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.SyncthingApp;
import com.nutomic.syncthingandroid.databinding.ActivityShareBinding;
import com.nutomic.syncthingandroid.model.Folder;
import com.nutomic.syncthingandroid.service.SyncthingService;
Expand All @@ -40,6 +42,8 @@
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

/**
* Shares incoming files to syncthing folders.
* <p>
Expand All @@ -60,6 +64,9 @@ public class ShareActivity extends StateDialogActivity

private ActivityShareBinding binding;

@Inject
SharedPreferences mPreferences;

@Override
public void onServiceStateChange(SyncthingService.State currentState) {
if (currentState != SyncthingService.State.ACTIVE || getApi() == null)
Expand All @@ -69,8 +76,7 @@ public void onServiceStateChange(SyncthingService.State currentState) {

// Get the index of the previously selected folder.
int folderIndex = 0;
String savedFolderId = PreferenceManager.getDefaultSharedPreferences(this)
.getString(PREF_PREVIOUSLY_SELECTED_SYNCTHING_FOLDER, "");
String savedFolderId = mPreferences.getString(PREF_PREVIOUSLY_SELECTED_SYNCTHING_FOLDER, "");
for (Folder folder : folders) {
if (folder.id.equals(savedFolderId)) {
folderIndex = folders.indexOf(folder);
Expand Down Expand Up @@ -102,6 +108,7 @@ protected void onPostCreate(Bundle savedInstanceState) {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((SyncthingApp) getApplication()).component().inject(this);
binding = ActivityShareBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());

Expand Down Expand Up @@ -266,8 +273,7 @@ private String getSavedSubDirectory() {
String savedSubDirectory = "";

if (selectedFolder != null) {
savedSubDirectory = PreferenceManager.getDefaultSharedPreferences(this)
.getString(PREF_FOLDER_SAVED_SUBDIRECTORY + selectedFolder.id, "");
savedSubDirectory = mPreferences.getString(PREF_FOLDER_SAVED_SUBDIRECTORY + selectedFolder.id, "");
}

return savedSubDirectory;
Expand Down Expand Up @@ -362,7 +368,7 @@ protected void onPause() {
super.onPause();
if (mFoldersSpinner.getSelectedItem() != null) {
Folder selectedFolder = (Folder) mFoldersSpinner.getSelectedItem();
PreferenceManager.getDefaultSharedPreferences(this).edit()
mPreferences.edit()
.putString(PREF_PREVIOUSLY_SELECTED_SYNCTHING_FOLDER, selectedFolder.id)
.apply();
}
Expand All @@ -379,7 +385,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
subDirectory = subDirectory.replace(folderDirectory, "");
mSubDirectoryTextView.setText(subDirectory);

PreferenceManager.getDefaultSharedPreferences(this)
mPreferences
.edit().putString(PREF_FOLDER_SAVED_SUBDIRECTORY + selectedFolder.id, subDirectory)
.apply();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,27 @@
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;

import com.nutomic.syncthingandroid.SyncthingApp;
import com.nutomic.syncthingandroid.service.Constants;

import javax.inject.Inject;

/**
* Provides a themed instance of AppCompatActivity.
*/
public class ThemedAppCompatActivity extends AppCompatActivity {

private static final String FOLLOW_SYSTEM = "-1";

@Inject
SharedPreferences mPreferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
((SyncthingApp) getApplication()).component().inject(this);
// Load theme.
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
//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);
super.onCreate(savedInstanceState);
}
Expand Down
2 changes: 1 addition & 1 deletion syncthing/src/github.com/syncthing/syncthing
Submodule syncthing updated 154 files

0 comments on commit 79449d5

Please sign in to comment.