Add ability to copy and reset plugin preferences

This commit is contained in:
Vitaliy 2020-01-20 15:48:19 +02:00
parent 6dd741f483
commit 3b428ec681
8 changed files with 225 additions and 17 deletions

View file

@ -1,17 +1,25 @@
package net.osmand.access;
import android.support.v4.app.FragmentManager;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import net.osmand.plus.ApplicationMode;
import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.R;
import net.osmand.plus.access.AccessibilityMode;
import net.osmand.plus.access.RelativeDirectionStyle;
import net.osmand.plus.monitoring.OsmandMonitoringPlugin;
import net.osmand.plus.profiles.SelectCopyAppModeBottomSheet;
import net.osmand.plus.profiles.SelectCopyAppModeBottomSheet.CopyAppModePrefsListener;
import net.osmand.plus.settings.BaseSettingsFragment;
import net.osmand.plus.settings.OnPreferenceChanged;
import net.osmand.plus.settings.bottomsheets.ResetProfilePrefsBottomSheet;
import net.osmand.plus.settings.bottomsheets.ResetProfilePrefsBottomSheet.ResetAppModePrefsListener;
import net.osmand.plus.settings.preferences.ListPreferenceEx;
import net.osmand.plus.settings.preferences.SwitchPreferenceEx;
public class AccessibilitySettingsFragment extends BaseSettingsFragment implements OnPreferenceChanged {
public class AccessibilitySettingsFragment extends BaseSettingsFragment implements OnPreferenceChanged, CopyAppModePrefsListener, ResetAppModePrefsListener {
private static final String COPY_PLUGIN_SETTINGS = "copy_plugin_settings";
private static final String RESET_TO_DEFAULT = "reset_to_default";
@ -150,6 +158,39 @@ public class AccessibilitySettingsFragment extends BaseSettingsFragment implemen
}
}
@Override
public boolean onPreferenceClick(Preference preference) {
String prefId = preference.getKey();
if (COPY_PLUGIN_SETTINGS.equals(prefId)) {
FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager != null) {
SelectCopyAppModeBottomSheet.showInstance(fragmentManager, this, false, getSelectedAppMode());
}
} else if (RESET_TO_DEFAULT.equals(prefId)) {
FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager != null) {
ResetProfilePrefsBottomSheet.showInstance(fragmentManager, prefId, this, false, getSelectedAppMode());
}
}
return super.onPreferenceClick(preference);
}
@Override
public void copyAppModePrefs(ApplicationMode appMode) {
OsmandMonitoringPlugin plugin = OsmandPlugin.getPlugin(OsmandMonitoringPlugin.class);
if (plugin != null) {
app.getSettings().copyProfilePreferences(appMode, getSelectedAppMode(), plugin.getPreferences());
}
}
@Override
public void resetAppModePrefs(ApplicationMode appMode) {
OsmandMonitoringPlugin plugin = OsmandPlugin.getPlugin(OsmandMonitoringPlugin.class);
if (plugin != null) {
app.getSettings().resetProfilePreferences(appMode, plugin.getPreferences());
}
}
private void updateAccessibilityOptions() {
boolean accessibilityEnabled = app.accessibilityEnabledForMode(getSelectedAppMode());
PreferenceScreen screen = getPreferenceScreen();

View file

@ -57,6 +57,8 @@ public abstract class OsmandPlugin {
private static List<OsmandPlugin> allPlugins = new ArrayList<OsmandPlugin>();
private static final Log LOG = PlatformUtil.getLog(OsmandPlugin.class);
protected List<OsmandSettings.OsmandPreference> pluginPreferences = new ArrayList<>();
private boolean active;
private String installURL = null;
@ -81,6 +83,10 @@ public abstract class OsmandPlugin {
return null;
}
public List<OsmandSettings.OsmandPreference> getPreferences() {
return pluginPreferences;
}
public String getPrefsDescription() {
return null;
}
@ -628,4 +634,40 @@ public abstract class OsmandPlugin {
p.addMyPlacesTab(favoritesActivity, mTabs, intent);
}
}
}
protected OsmandSettings.CommonPreference<Boolean> registerBooleanPreference(OsmandApplication app, String prefId, boolean defValue) {
OsmandSettings.CommonPreference<Boolean> preference = app.getSettings().registerBooleanPreference(prefId, defValue);
pluginPreferences.add(preference);
return preference;
}
protected OsmandSettings.CommonPreference<String> registerStringPreference(OsmandApplication app, String prefId, String defValue) {
OsmandSettings.CommonPreference<String> preference = app.getSettings().registerStringPreference(prefId, defValue);
pluginPreferences.add(preference);
return preference;
}
protected OsmandSettings.CommonPreference<Integer> registerIntPreference(OsmandApplication app, String prefId, int defValue) {
OsmandSettings.CommonPreference<Integer> preference = app.getSettings().registerIntPreference(prefId, defValue);
pluginPreferences.add(preference);
return preference;
}
protected OsmandSettings.CommonPreference<Long> registerLongPreference(OsmandApplication app, String prefId, long defValue) {
OsmandSettings.CommonPreference<Long> preference = app.getSettings().registerLongPreference(prefId, defValue);
pluginPreferences.add(preference);
return preference;
}
protected OsmandSettings.CommonPreference<Float> registerFloatPreference(OsmandApplication app, String prefId, float defValue) {
OsmandSettings.CommonPreference<Float> preference = app.getSettings().registerFloatPreference(prefId, defValue);
pluginPreferences.add(preference);
return preference;
}
protected <T extends Enum> OsmandSettings.CommonPreference<T> registerEnumIntPreference(OsmandApplication app, String prefId, Enum defaultValue, Enum[] values, Class<T> clz) {
OsmandSettings.CommonPreference<T> preference = app.getSettings().registerEnumIntPreference(prefId, defaultValue, values, clz);
pluginPreferences.add(preference);
return preference;
}
}

View file

@ -410,11 +410,17 @@ public class OsmandSettings {
}
public boolean copyPreferencesFromProfile(ApplicationMode modeFrom, ApplicationMode modeTo) {
return copyProfilePreferences(modeFrom, modeTo, new ArrayList<OsmandPreference>(registeredPreferences.values()));
}
public boolean copyProfilePreferences(ApplicationMode modeFrom, ApplicationMode modeTo, List<OsmandPreference> profilePreferences) {
SettingsEditor settingsEditor = settingsAPI.edit(getProfilePreferences(modeTo));
for (OsmandPreference pref : registeredPreferences.values()) {
for (OsmandPreference pref : profilePreferences) {
if (pref instanceof CommonPreference && !((CommonPreference) pref).global) {
CommonPreference profilePref = (CommonPreference) pref;
if (profilePref.isSetForMode(modeFrom) || profilePref.hasDefaultValueForMode(modeFrom)) {
Object defaultFrom = profilePref.getProfileDefaultValue(modeFrom);
Object defaultTo = profilePref.getProfileDefaultValue(modeFrom);
if (profilePref.isSetForMode(modeFrom) || !defaultFrom.equals(defaultTo)) {
Object copiedValue = profilePref.getModeValue(modeFrom);
if (copiedValue instanceof String) {
settingsEditor.putString(pref.getId(), (String) copiedValue);
@ -439,6 +445,16 @@ public class OsmandSettings {
return settingsAPI.edit(getProfilePreferences(mode)).clear().commit();
}
public boolean resetProfilePreferences(ApplicationMode mode, List<OsmandPreference> profilePreferences) {
SettingsEditor settingsEditor = settingsAPI.edit(getProfilePreferences(mode));
for (OsmandPreference pref : profilePreferences) {
if (pref instanceof CommonPreference && !((CommonPreference) pref).global) {
settingsEditor.remove(pref.getId());
}
}
return settingsEditor.commit();
}
public ApplicationMode LAST_ROUTING_APPLICATION_MODE = null;
// this value string is synchronized with settings_pref.xml preference name
@ -1107,6 +1123,16 @@ public class OsmandSettings {
return p;
}
@SuppressWarnings("unchecked")
public CommonPreference<Boolean> registerBooleanAccessibilityPreference(String id, boolean defValue) {
if (registeredPreferences.containsKey(id)) {
return (CommonPreference<Boolean>) registeredPreferences.get(id);
}
BooleanPreference p = new BooleanAccessibilityPreference(id, defValue);
registeredPreferences.put(id, p);
return p;
}
@SuppressWarnings("unchecked")
public CommonPreference<String> registerStringPreference(String id, String defValue) {
if (registeredPreferences.containsKey(id)) {
@ -1147,6 +1173,16 @@ public class OsmandSettings {
return p;
}
@SuppressWarnings("unchecked")
public <T extends Enum> CommonPreference<T> registerEnumIntPreference(String id, Enum defaultValue, Enum[] values, Class<T> clz) {
if (registeredPreferences.containsKey(id)) {
return (CommonPreference<T>) registeredPreferences.get(id);
}
EnumIntPreference p = new EnumIntPreference(id, defaultValue, values);
registeredPreferences.put(id, p);
return p;
}
public final CommonPreference<RulerMode> RULER_MODE = new EnumIntPreference<>("ruler_mode", RulerMode.FIRST, RulerMode.values()).makeGlobal();
public final OsmandPreference<Boolean> SHOW_COMPASS_CONTROL_RULER = new BooleanPreference("show_compass_ruler", true).makeGlobal();

View file

@ -9,10 +9,15 @@ import android.os.StatFs;
import android.support.v7.preference.Preference;
import net.osmand.PlatformUtil;
import net.osmand.plus.ApplicationMode;
import net.osmand.plus.OsmAndAppCustomization;
import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.R;
import net.osmand.plus.profiles.SelectCopyAppModeBottomSheet;
import net.osmand.plus.profiles.SelectCopyAppModeBottomSheet.CopyAppModePrefsListener;
import net.osmand.plus.settings.BaseSettingsFragment;
import net.osmand.plus.settings.bottomsheets.ResetProfilePrefsBottomSheet;
import net.osmand.plus.settings.bottomsheets.ResetProfilePrefsBottomSheet.ResetAppModePrefsListener;
import net.osmand.plus.settings.preferences.ListPreferenceEx;
import net.osmand.plus.settings.preferences.SwitchPreferenceEx;
@ -34,7 +39,7 @@ import static net.osmand.plus.audionotes.AudioVideoNotesPlugin.AV_CAMERA_FOCUS_M
import static net.osmand.plus.audionotes.AudioVideoNotesPlugin.NOTES_TAB;
import static net.osmand.plus.audionotes.AudioVideoNotesPlugin.cameraPictureSizeDefault;
public class MultimediaNotesFragment extends BaseSettingsFragment {
public class MultimediaNotesFragment extends BaseSettingsFragment implements CopyAppModePrefsListener, ResetAppModePrefsListener {
private static final Log log = PlatformUtil.getLog(MultimediaNotesFragment.class);
@ -45,7 +50,6 @@ public class MultimediaNotesFragment extends BaseSettingsFragment {
@Override
protected void setupPreferences() {
AudioVideoNotesPlugin plugin = OsmandPlugin.getPlugin(AudioVideoNotesPlugin.class);
if (plugin != null) {
Camera cam = openCamera();
@ -346,13 +350,24 @@ public class MultimediaNotesFragment extends BaseSettingsFragment {
@Override
public boolean onPreferenceClick(Preference preference) {
if (OPEN_NOTES.equals(preference.getKey())) {
String prefId = preference.getKey();
if (OPEN_NOTES.equals(prefId)) {
OsmAndAppCustomization appCustomization = app.getAppCustomization();
Intent favorites = new Intent(preference.getContext(), appCustomization.getFavoritesActivity());
favorites.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
app.getSettings().FAVORITES_TAB.set(NOTES_TAB);
startActivity(favorites);
return true;
} else if (COPY_PLUGIN_SETTINGS.equals(prefId)) {
FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager != null) {
SelectCopyAppModeBottomSheet.showInstance(fragmentManager, this, false, getSelectedAppMode());
}
} else if (RESET_TO_DEFAULT.equals(prefId)) {
FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager != null) {
ResetProfilePrefsBottomSheet.showInstance(fragmentManager, prefId, this, false, getSelectedAppMode());
}
}
return super.onPreferenceClick(preference);
}
@ -365,4 +380,20 @@ public class MultimediaNotesFragment extends BaseSettingsFragment {
return null;
}
}
@Override
public void copyAppModePrefs(ApplicationMode appMode) {
AudioVideoNotesPlugin plugin = OsmandPlugin.getPlugin(AudioVideoNotesPlugin.class);
if (plugin != null) {
app.getSettings().copyProfilePreferences(appMode, getSelectedAppMode(), plugin.getPreferences());
}
}
@Override
public void resetAppModePrefs(ApplicationMode appMode) {
AudioVideoNotesPlugin plugin = OsmandPlugin.getPlugin(AudioVideoNotesPlugin.class);
if (plugin != null) {
app.getSettings().resetProfilePreferences(appMode, plugin.getPreferences());
}
}
}

View file

@ -4,12 +4,18 @@ import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.support.v7.preference.Preference;
import net.osmand.plus.ApplicationMode;
import net.osmand.plus.OsmAndAppCustomization;
import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.myplaces.FavoritesActivity;
import net.osmand.plus.profiles.SelectCopyAppModeBottomSheet;
import net.osmand.plus.profiles.SelectCopyAppModeBottomSheet.CopyAppModePrefsListener;
import net.osmand.plus.settings.BaseSettingsFragment;
import net.osmand.plus.settings.bottomsheets.ChangeGeneralProfilesPrefBottomSheet;
import net.osmand.plus.settings.bottomsheets.ResetProfilePrefsBottomSheet;
import net.osmand.plus.settings.bottomsheets.ResetProfilePrefsBottomSheet.ResetAppModePrefsListener;
import net.osmand.plus.settings.preferences.ListPreferenceEx;
import net.osmand.plus.settings.preferences.SwitchPreferenceEx;
@ -21,7 +27,7 @@ import static net.osmand.plus.OsmandSettings.REC_DIRECTORY;
import static net.osmand.plus.monitoring.OsmandMonitoringPlugin.MINUTES;
import static net.osmand.plus.monitoring.OsmandMonitoringPlugin.SECONDS;
public class MonitoringSettingsFragment extends BaseSettingsFragment {
public class MonitoringSettingsFragment extends BaseSettingsFragment implements CopyAppModePrefsListener, ResetAppModePrefsListener {
private static final String COPY_PLUGIN_SETTINGS = "copy_plugin_settings";
private static final String RESET_TO_DEFAULT = "reset_to_default";
@ -172,13 +178,24 @@ public class MonitoringSettingsFragment extends BaseSettingsFragment {
@Override
public boolean onPreferenceClick(Preference preference) {
if (OPEN_TRACKS.equals(preference.getKey())) {
String prefId = preference.getKey();
if (OPEN_TRACKS.equals(prefId)) {
OsmAndAppCustomization appCustomization = app.getAppCustomization();
Intent favorites = new Intent(preference.getContext(), appCustomization.getFavoritesActivity());
favorites.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
app.getSettings().FAVORITES_TAB.set(FavoritesActivity.GPX_TAB);
startActivity(favorites);
return true;
} else if (COPY_PLUGIN_SETTINGS.equals(prefId)) {
FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager != null) {
SelectCopyAppModeBottomSheet.showInstance(fragmentManager, this, false, getSelectedAppMode());
}
} else if (RESET_TO_DEFAULT.equals(prefId)) {
FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager != null) {
ResetProfilePrefsBottomSheet.showInstance(fragmentManager, prefId, this, false, getSelectedAppMode());
}
}
return super.onPreferenceClick(preference);
}
@ -199,4 +216,20 @@ public class MonitoringSettingsFragment extends BaseSettingsFragment {
return true;
}
@Override
public void copyAppModePrefs(ApplicationMode appMode) {
OsmandMonitoringPlugin plugin = OsmandPlugin.getPlugin(OsmandMonitoringPlugin.class);
if (plugin != null) {
app.getSettings().copyProfilePreferences(appMode, getSelectedAppMode(), plugin.getPreferences());
}
}
@Override
public void resetAppModePrefs(ApplicationMode appMode) {
OsmandMonitoringPlugin plugin = OsmandPlugin.getPlugin(OsmandMonitoringPlugin.class);
if (plugin != null) {
app.getSettings().resetProfilePreferences(appMode, plugin.getPreferences());
}
}
}

View file

@ -103,9 +103,10 @@ public class SelectCopyAppModeBottomSheet extends AppModesBottomSheetDialogFragm
@Override
protected void onRightBottomButtonClick() {
OsmandApplication app = getMyApplication();
if (app != null && selectedAppMode != null) {
app.getSettings().copyPreferencesFromProfile(selectedAppMode, currentAppMode);
Fragment targetFragment = getTargetFragment();
if (selectedAppMode != null && targetFragment instanceof CopyAppModePrefsListener) {
CopyAppModePrefsListener listener = (CopyAppModePrefsListener) targetFragment;
listener.copyAppModePrefs(selectedAppMode);
}
dismiss();
}
@ -127,4 +128,8 @@ public class SelectCopyAppModeBottomSheet extends AppModesBottomSheetDialogFragm
LOG.error("showInstance", e);
}
}
public interface CopyAppModePrefsListener {
void copyAppModePrefs(ApplicationMode appMode);
}
}

View file

@ -41,7 +41,9 @@ import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.helpers.FontCache;
import net.osmand.plus.openseamapsplugin.NauticalMapsPlugin;
import net.osmand.plus.profiles.SelectCopyAppModeBottomSheet;
import net.osmand.plus.profiles.SelectCopyAppModeBottomSheet.CopyAppModePrefsListener;
import net.osmand.plus.settings.bottomsheets.ResetProfilePrefsBottomSheet;
import net.osmand.plus.settings.bottomsheets.ResetProfilePrefsBottomSheet.ResetAppModePrefsListener;
import net.osmand.plus.skimapsplugin.SkiMapsPlugin;
import org.apache.commons.logging.Log;
@ -55,7 +57,7 @@ import static net.osmand.plus.profiles.EditProfileFragment.OPEN_CONFIG_ON_MAP;
import static net.osmand.plus.profiles.EditProfileFragment.SCREEN_CONFIG;
import static net.osmand.plus.profiles.EditProfileFragment.SELECTED_ITEM;
public class ConfigureProfileFragment extends BaseSettingsFragment {
public class ConfigureProfileFragment extends BaseSettingsFragment implements CopyAppModePrefsListener, ResetAppModePrefsListener {
public static final String TAG = ConfigureProfileFragment.class.getSimpleName();
@ -147,6 +149,20 @@ public class ConfigureProfileFragment extends BaseSettingsFragment {
updateToolbarSwitch();
}
@Override
public void copyAppModePrefs(ApplicationMode appMode) {
if (appMode != null) {
app.getSettings().copyPreferencesFromProfile(appMode, getSelectedAppMode());
}
}
@Override
public void resetAppModePrefs(ApplicationMode appMode) {
if (appMode != null) {
app.getSettings().resetPreferencesForProfile(appMode);
}
}
private RecyclerView.ItemDecoration createDividerItemDecoration() {
final Drawable dividerLight = new ColorDrawable(ContextCompat.getColor(app, R.color.list_background_color_light));
final Drawable dividerDark = new ColorDrawable(ContextCompat.getColor(app, R.color.list_background_color_dark));

View file

@ -12,7 +12,6 @@ import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import net.osmand.plus.ApplicationMode;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.UiUtilities;
import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem;
@ -71,9 +70,10 @@ public class ResetProfilePrefsBottomSheet extends BasePreferenceBottomSheet {
@Override
protected void onRightBottomButtonClick() {
OsmandApplication app = getMyApplication();
if (app != null) {
app.getSettings().resetPreferencesForProfile(getAppMode());
Fragment targetFragment = getTargetFragment();
if (targetFragment instanceof ResetAppModePrefsListener) {
ResetAppModePrefsListener listener = (ResetAppModePrefsListener) targetFragment;
listener.resetAppModePrefs(getAppMode());
}
dismiss();
}
@ -100,4 +100,8 @@ public class ResetProfilePrefsBottomSheet extends BasePreferenceBottomSheet {
return false;
}
}
public interface ResetAppModePrefsListener {
void resetAppModePrefs(ApplicationMode appMode);
}
}