Use lambdas in SettingsFragment, remove Consumer class.

This commit is contained in:
tzugen 2021-10-30 15:28:53 +02:00
parent e19d43d6b3
commit 4d42c0d9d2
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
2 changed files with 73 additions and 141 deletions

View File

@ -1,7 +1,6 @@
package org.moire.ultrasonic.fragment; package org.moire.ultrasonic.fragment;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
@ -11,14 +10,12 @@ import android.view.View;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.StringRes; import androidx.annotation.StringRes;
import androidx.fragment.app.DialogFragment; import androidx.fragment.app.DialogFragment;
import androidx.navigation.Navigation;
import androidx.preference.CheckBoxPreference; import androidx.preference.CheckBoxPreference;
import androidx.preference.EditTextPreference; import androidx.preference.EditTextPreference;
import androidx.preference.ListPreference; import androidx.preference.ListPreference;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceCategory; import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceFragmentCompat; import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.koin.java.KoinJavaComponent; import org.koin.java.KoinJavaComponent;
@ -28,7 +25,6 @@ import org.moire.ultrasonic.featureflags.FeatureStorage;
import org.moire.ultrasonic.filepicker.FilePickerDialog; import org.moire.ultrasonic.filepicker.FilePickerDialog;
import org.moire.ultrasonic.log.FileLoggerTree; import org.moire.ultrasonic.log.FileLoggerTree;
import org.moire.ultrasonic.provider.SearchSuggestionProvider; import org.moire.ultrasonic.provider.SearchSuggestionProvider;
import org.moire.ultrasonic.service.Consumer;
import org.moire.ultrasonic.service.MediaPlayerController; import org.moire.ultrasonic.service.MediaPlayerController;
import org.moire.ultrasonic.util.Constants; import org.moire.ultrasonic.util.Constants;
import org.moire.ultrasonic.util.FileUtil; import org.moire.ultrasonic.util.FileUtil;
@ -43,10 +39,10 @@ import org.moire.ultrasonic.util.Util;
import java.io.File; import java.io.File;
import kotlin.Lazy; import kotlin.Lazy;
import kotlin.jvm.functions.Function1;
import timber.log.Timber; import timber.log.Timber;
import static org.koin.java.KoinJavaComponent.inject; import static org.koin.java.KoinJavaComponent.inject;
import static org.moire.ultrasonic.fragment.ServerSelectorFragment.SERVER_SELECTOR_MANAGE_MODE;
/** /**
* Shows main app settings. * Shows main app settings.
@ -84,8 +80,6 @@ public class SettingsFragment extends PreferenceFragmentCompat
private Preference pauseOnBluetoothDevice; private Preference pauseOnBluetoothDevice;
private CheckBoxPreference debugLogToFile; private CheckBoxPreference debugLogToFile;
private SharedPreferences settings;
private final Lazy<MediaPlayerController> mediaPlayerControllerLazy = inject(MediaPlayerController.class); private final Lazy<MediaPlayerController> mediaPlayerControllerLazy = inject(MediaPlayerController.class);
private final Lazy<PermissionUtil> permissionUtil = inject(PermissionUtil.class); private final Lazy<PermissionUtil> permissionUtil = inject(PermissionUtil.class);
private final Lazy<ThemeChangedEventDistributor> themeChangedEventDistributor = inject(ThemeChangedEventDistributor.class); private final Lazy<ThemeChangedEventDistributor> themeChangedEventDistributor = inject(ThemeChangedEventDistributor.class);
@ -94,8 +88,6 @@ public class SettingsFragment extends PreferenceFragmentCompat
@Override @Override
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
settings = PreferenceManager.getDefaultSharedPreferences(getActivity());
} }
@Override @Override
@ -148,9 +140,11 @@ public class SettingsFragment extends PreferenceFragmentCompat
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
PreferenceCategory notificationsCategory = findPreference(Constants.PREFERENCES_KEY_CATEGORY_NOTIFICATIONS); PreferenceCategory notificationsCategory = findPreference(Constants.PREFERENCES_KEY_CATEGORY_NOTIFICATIONS);
Preference preferenceToRemove = findPreference(Constants.PREFERENCES_KEY_SHOW_NOTIFICATION); Preference preferenceToRemove = findPreference(Constants.PREFERENCES_KEY_SHOW_NOTIFICATION);
if (preferenceToRemove != null) notificationsCategory.removePreference(preferenceToRemove); if (preferenceToRemove != null)
notificationsCategory.removePreference(preferenceToRemove);
preferenceToRemove = findPreference(Constants.PREFERENCES_KEY_ALWAYS_SHOW_NOTIFICATION); preferenceToRemove = findPreference(Constants.PREFERENCES_KEY_ALWAYS_SHOW_NOTIFICATION);
if (preferenceToRemove != null) notificationsCategory.removePreference(preferenceToRemove); if (preferenceToRemove != null)
notificationsCategory.removePreference(preferenceToRemove);
} }
} }
@ -196,24 +190,19 @@ public class SettingsFragment extends PreferenceFragmentCompat
} }
@Override @Override
public void onDisplayPreferenceDialog(Preference preference) public void onDisplayPreferenceDialog(Preference preference) {
{
DialogFragment dialogFragment = null; DialogFragment dialogFragment = null;
if (preference instanceof TimeSpanPreference) if (preference instanceof TimeSpanPreference) {
{
dialogFragment = new TimeSpanPreferenceDialogFragmentCompat(); dialogFragment = new TimeSpanPreferenceDialogFragmentCompat();
Bundle bundle = new Bundle(1); Bundle bundle = new Bundle(1);
bundle.putString("key", preference.getKey()); bundle.putString("key", preference.getKey());
dialogFragment.setArguments(bundle); dialogFragment.setArguments(bundle);
} }
if (dialogFragment != null) if (dialogFragment != null) {
{
dialogFragment.setTargetFragment(this, 0); dialogFragment.setTargetFragment(this, 0);
dialogFragment.show(this.getParentFragmentManager(), "android.support.v7.preference.PreferenceFragment.DIALOG"); dialogFragment.show(this.getParentFragmentManager(), "android.support.v7.preference.PreferenceFragment.DIALOG");
} } else {
else
{
super.onDisplayPreferenceDialog(preference); super.onDisplayPreferenceDialog(preference);
} }
} }
@ -221,27 +210,21 @@ public class SettingsFragment extends PreferenceFragmentCompat
private void setupCacheLocationPreference() { private void setupCacheLocationPreference() {
cacheLocation.setSummary(Settings.getCacheLocation()); cacheLocation.setSummary(Settings.getCacheLocation());
cacheLocation.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { cacheLocation.setOnPreferenceClickListener(preference -> {
@Override
public boolean onPreferenceClick(Preference preference) {
// If the user tries to change the cache location, we must first check to see if we have write access. // If the user tries to change the cache location, we must first check to see if we have write access.
PermissionUtil.requestInitialPermission(getActivity(), new PermissionUtil.PermissionRequestFinishedCallback() { PermissionUtil.requestInitialPermission(getActivity(), hasPermission -> {
@Override if (hasPermission) {
public void onPermissionRequestFinished(boolean hasPermission) { FilePickerDialog filePickerDialog = FilePickerDialog.Companion.createFilePickerDialog(getContext());
if (hasPermission) { filePickerDialog.setDefaultDirectory(FileUtil.getDefaultMusicDirectory().getPath());
FilePickerDialog filePickerDialog = FilePickerDialog.Companion.createFilePickerDialog(getContext()); filePickerDialog.setInitialDirectory(cacheLocation.getSummary().toString());
filePickerDialog.setDefaultDirectory(FileUtil.getDefaultMusicDirectory().getPath()); filePickerDialog.setOnFileSelectedListener((file, path) -> {
filePickerDialog.setInitialDirectory(cacheLocation.getSummary().toString()); Settings.setCacheLocation(path);
filePickerDialog.setOnFileSelectedListener((file, path) -> { setCacheLocation(path);
Settings.setCacheLocation(path); });
setCacheLocation(path); filePickerDialog.show();
}); }
filePickerDialog.show(); });
}
}
});
return true; return true;
}
}); });
} }
@ -252,75 +235,56 @@ public class SettingsFragment extends PreferenceFragmentCompat
resumeOnBluetoothDevice.setSummary(bluetoothDevicePreferenceToString(resumeSetting)); resumeOnBluetoothDevice.setSummary(bluetoothDevicePreferenceToString(resumeSetting));
pauseOnBluetoothDevice.setSummary(bluetoothDevicePreferenceToString(pauseSetting)); pauseOnBluetoothDevice.setSummary(bluetoothDevicePreferenceToString(pauseSetting));
resumeOnBluetoothDevice.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { resumeOnBluetoothDevice.setOnPreferenceClickListener(preference -> {
@Override
public boolean onPreferenceClick(Preference preference) {
showBluetoothDevicePreferenceDialog( showBluetoothDevicePreferenceDialog(
R.string.settings_playback_resume_on_bluetooth_device, R.string.settings_playback_resume_on_bluetooth_device,
Settings.getResumeOnBluetoothDevice(), Settings.getResumeOnBluetoothDevice(),
new Consumer<Integer>() { choice -> {
@Override
public void accept(Integer choice) {
SharedPreferences.Editor editor = resumeOnBluetoothDevice.getSharedPreferences().edit(); SharedPreferences.Editor editor = resumeOnBluetoothDevice.getSharedPreferences().edit();
editor.putInt(Constants.PREFERENCES_KEY_RESUME_ON_BLUETOOTH_DEVICE, choice); editor.putInt(Constants.PREFERENCES_KEY_RESUME_ON_BLUETOOTH_DEVICE, choice);
editor.apply(); editor.apply();
resumeOnBluetoothDevice.setSummary(bluetoothDevicePreferenceToString(choice)); resumeOnBluetoothDevice.setSummary(bluetoothDevicePreferenceToString(choice));
} return null;
}); });
return true; return true;
}
}); });
pauseOnBluetoothDevice.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { pauseOnBluetoothDevice.setOnPreferenceClickListener(preference -> {
@Override
public boolean onPreferenceClick(Preference preference) {
showBluetoothDevicePreferenceDialog( showBluetoothDevicePreferenceDialog(
R.string.settings_playback_pause_on_bluetooth_device, R.string.settings_playback_pause_on_bluetooth_device,
Settings.getPauseOnBluetoothDevice(), Settings.getPauseOnBluetoothDevice(), choice ->
new Consumer<Integer>() { {
@Override
public void accept(Integer choice) {
Settings.setPauseOnBluetoothDevice(choice); Settings.setPauseOnBluetoothDevice(choice);
pauseOnBluetoothDevice.setSummary(bluetoothDevicePreferenceToString(choice)); pauseOnBluetoothDevice.setSummary(bluetoothDevicePreferenceToString(choice));
} return null;
}); });
return true; return true;
}
}); });
} }
private void showBluetoothDevicePreferenceDialog(@StringRes int title, int defaultChoice, final Consumer<Integer> onChosen) { private void showBluetoothDevicePreferenceDialog(@StringRes int title, int defaultChoice, final Function1<Integer, Void> onChosen) {
final int[] choice = {defaultChoice}; final int[] choice = {defaultChoice};
new AlertDialog.Builder(getActivity()).setTitle(title) new AlertDialog.Builder(getActivity()).setTitle(title)
.setSingleChoiceItems(R.array.bluetoothDeviceSettingNames, defaultChoice, .setSingleChoiceItems(R.array.bluetoothDeviceSettingNames, defaultChoice,
new DialogInterface.OnClickListener() { (dialogInterface, i) -> choice[0] = i)
@Override .setNegativeButton(R.string.common_cancel, (dialogInterface, i) -> dialogInterface.cancel())
public void onClick(DialogInterface dialogInterface, int i) { .setPositiveButton(R.string.common_ok, (dialogInterface, i) -> {
choice[0] = i; onChosen.invoke(choice[0]);
}
})
.setNegativeButton(R.string.common_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
})
.setPositiveButton(R.string.common_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
onChosen.accept(choice[0]);
dialogInterface.dismiss(); dialogInterface.dismiss();
} })
}) .create().show();
.create().show();
} }
private String bluetoothDevicePreferenceToString(int preferenceValue) { private String bluetoothDevicePreferenceToString(int preferenceValue) {
switch (preferenceValue) { switch (preferenceValue) {
case Constants.PREFERENCE_VALUE_ALL: return getString(R.string.settings_playback_bluetooth_all); case Constants.PREFERENCE_VALUE_ALL:
case Constants.PREFERENCE_VALUE_A2DP: return getString(R.string.settings_playback_bluetooth_a2dp); return getString(R.string.settings_playback_bluetooth_all);
case Constants.PREFERENCE_VALUE_DISABLED: return getString(R.string.settings_playback_bluetooth_disabled); case Constants.PREFERENCE_VALUE_A2DP:
default: return ""; return getString(R.string.settings_playback_bluetooth_a2dp);
case Constants.PREFERENCE_VALUE_DISABLED:
return getString(R.string.settings_playback_bluetooth_disabled);
default:
return "";
} }
} }
@ -328,17 +292,14 @@ public class SettingsFragment extends PreferenceFragmentCompat
Preference clearSearchPreference = findPreference(Constants.PREFERENCES_KEY_CLEAR_SEARCH_HISTORY); Preference clearSearchPreference = findPreference(Constants.PREFERENCES_KEY_CLEAR_SEARCH_HISTORY);
if (clearSearchPreference != null) { if (clearSearchPreference != null) {
clearSearchPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { clearSearchPreference.setOnPreferenceClickListener(preference -> {
@Override SearchRecentSuggestions suggestions =
public boolean onPreferenceClick(Preference preference) { new SearchRecentSuggestions(getActivity(),
SearchRecentSuggestions suggestions = SearchSuggestionProvider.AUTHORITY,
new SearchRecentSuggestions(getActivity(), SearchSuggestionProvider.MODE);
SearchSuggestionProvider.AUTHORITY, suggestions.clearHistory();
SearchSuggestionProvider.MODE); Util.toast(getActivity(), R.string.settings_search_history_cleared);
suggestions.clearHistory(); return false;
Util.toast(getActivity(), R.string.settings_search_history_cleared);
return false;
}
}); });
} }
} }
@ -351,12 +312,9 @@ public class SettingsFragment extends PreferenceFragmentCompat
if (useFiveStarRating != null) { if (useFiveStarRating != null) {
useFiveStarRating.setChecked(featureStorage.isFeatureEnabled(Feature.FIVE_STAR_RATING)); useFiveStarRating.setChecked(featureStorage.isFeatureEnabled(Feature.FIVE_STAR_RATING));
useFiveStarRating.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { useFiveStarRating.setOnPreferenceChangeListener((preference, o) -> {
@Override featureStorage.changeFeatureFlag(Feature.FIVE_STAR_RATING, (Boolean) o);
public boolean onPreferenceChange(Preference preference, Object o) { return true;
featureStorage.changeFeatureFlag(Feature.FIVE_STAR_RATING, (Boolean) o);
return true;
}
}); });
} }
@ -397,7 +355,7 @@ public class SettingsFragment extends PreferenceFragmentCompat
if (debugLogToFile.isChecked()) { if (debugLogToFile.isChecked()) {
debugLogToFile.setSummary(getString(R.string.settings_debug_log_path, debugLogToFile.setSummary(getString(R.string.settings_debug_log_path,
FileUtil.getUltrasonicDirectory(), FileLoggerTree.FILENAME)); FileUtil.getUltrasonicDirectory(), FileLoggerTree.FILENAME));
} else { } else {
debugLogToFile.setSummary(""); debugLogToFile.setSummary("");
} }
@ -437,8 +395,7 @@ public class SettingsFragment extends PreferenceFragmentCompat
String currentPath = Settings.getCacheLocation(); String currentPath = Settings.getCacheLocation();
cacheLocation.setSummary(currentPath); cacheLocation.setSummary(currentPath);
}); });
} } else {
else {
cacheLocation.setSummary(path); cacheLocation.setSummary(path);
} }
@ -457,35 +414,22 @@ public class SettingsFragment extends PreferenceFragmentCompat
int fileNum = FileLoggerTree.Companion.getLogFileNumber(); int fileNum = FileLoggerTree.Companion.getLogFileNumber();
long fileSize = FileLoggerTree.Companion.getLogFileSizes(); long fileSize = FileLoggerTree.Companion.getLogFileSizes();
String message = getString(R.string.settings_debug_log_summary, String message = getString(R.string.settings_debug_log_summary,
String.valueOf(fileNum), String.valueOf(fileNum),
String.valueOf(Math.ceil(fileSize / 1000000d)), String.valueOf(Math.ceil(fileSize / 1000000d)),
FileUtil.getUltrasonicDirectory()); FileUtil.getUltrasonicDirectory());
new AlertDialog.Builder(getActivity()) new AlertDialog.Builder(getActivity())
.setMessage(message) .setMessage(message)
.setIcon(android.R.drawable.ic_dialog_info) .setIcon(android.R.drawable.ic_dialog_info)
.setNegativeButton(R.string.settings_debug_log_keep, new DialogInterface.OnClickListener() { .setNegativeButton(R.string.settings_debug_log_keep, (dialogInterface, i) -> dialogInterface.cancel())
@Override .setPositiveButton(R.string.settings_debug_log_delete, (dialogInterface, i) -> {
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
})
.setPositiveButton(R.string.settings_debug_log_delete, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
FileLoggerTree.Companion.deleteLogFiles(); FileLoggerTree.Companion.deleteLogFiles();
Timber.i("Deleted debug log files"); Timber.i("Deleted debug log files");
dialogInterface.dismiss(); dialogInterface.dismiss();
new AlertDialog.Builder(getActivity()).setMessage(R.string.settings_debug_log_deleted) new AlertDialog.Builder(getActivity()).setMessage(R.string.settings_debug_log_deleted)
.setPositiveButton(R.string.common_ok, new DialogInterface.OnClickListener() { .setPositiveButton(R.string.common_ok, (dialogInterface1, i1) -> dialogInterface1.dismiss()).create().show();
@Override })
public void onClick(DialogInterface dialogInterface, int i) { .create().show();
dialogInterface.dismiss();
}
}).create().show();
}
})
.create().show();
} }
} }
} }

View File

@ -1,12 +0,0 @@
package org.moire.ultrasonic.service;
/**
* Deprecated: Should be replaced with lambdas
* Abstract class for consumers with one parameter
* @param <T> The type of the object to consume
*/
@Deprecated
public abstract class Consumer<T>
{
public abstract void accept(T t);
}