diff --git a/OsmAnd/src/net/osmand/access/AccessibilitySettingsFragment.java b/OsmAnd/src/net/osmand/access/AccessibilitySettingsFragment.java index 6e1868124f..42384a2b97 100644 --- a/OsmAnd/src/net/osmand/access/AccessibilitySettingsFragment.java +++ b/OsmAnd/src/net/osmand/access/AccessibilitySettingsFragment.java @@ -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(); diff --git a/OsmAnd/src/net/osmand/plus/OsmandPlugin.java b/OsmAnd/src/net/osmand/plus/OsmandPlugin.java index 9aa4551a84..0ffa9daafa 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandPlugin.java +++ b/OsmAnd/src/net/osmand/plus/OsmandPlugin.java @@ -57,6 +57,8 @@ public abstract class OsmandPlugin { private static List allPlugins = new ArrayList(); private static final Log LOG = PlatformUtil.getLog(OsmandPlugin.class); + protected List pluginPreferences = new ArrayList<>(); + private boolean active; private String installURL = null; @@ -81,6 +83,10 @@ public abstract class OsmandPlugin { return null; } + public List getPreferences() { + return pluginPreferences; + } + public String getPrefsDescription() { return null; } @@ -628,4 +634,40 @@ public abstract class OsmandPlugin { p.addMyPlacesTab(favoritesActivity, mTabs, intent); } } -} + + protected OsmandSettings.CommonPreference registerBooleanPreference(OsmandApplication app, String prefId, boolean defValue) { + OsmandSettings.CommonPreference preference = app.getSettings().registerBooleanPreference(prefId, defValue); + pluginPreferences.add(preference); + return preference; + } + + protected OsmandSettings.CommonPreference registerStringPreference(OsmandApplication app, String prefId, String defValue) { + OsmandSettings.CommonPreference preference = app.getSettings().registerStringPreference(prefId, defValue); + pluginPreferences.add(preference); + return preference; + } + + protected OsmandSettings.CommonPreference registerIntPreference(OsmandApplication app, String prefId, int defValue) { + OsmandSettings.CommonPreference preference = app.getSettings().registerIntPreference(prefId, defValue); + pluginPreferences.add(preference); + return preference; + } + + protected OsmandSettings.CommonPreference registerLongPreference(OsmandApplication app, String prefId, long defValue) { + OsmandSettings.CommonPreference preference = app.getSettings().registerLongPreference(prefId, defValue); + pluginPreferences.add(preference); + return preference; + } + + protected OsmandSettings.CommonPreference registerFloatPreference(OsmandApplication app, String prefId, float defValue) { + OsmandSettings.CommonPreference preference = app.getSettings().registerFloatPreference(prefId, defValue); + pluginPreferences.add(preference); + return preference; + } + + protected OsmandSettings.CommonPreference registerEnumIntPreference(OsmandApplication app, String prefId, Enum defaultValue, Enum[] values, Class clz) { + OsmandSettings.CommonPreference preference = app.getSettings().registerEnumIntPreference(prefId, defaultValue, values, clz); + pluginPreferences.add(preference); + return preference; + } +} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index 32bd59b209..4962943b89 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -410,11 +410,17 @@ public class OsmandSettings { } public boolean copyPreferencesFromProfile(ApplicationMode modeFrom, ApplicationMode modeTo) { + return copyProfilePreferences(modeFrom, modeTo, new ArrayList(registeredPreferences.values())); + } + + public boolean copyProfilePreferences(ApplicationMode modeFrom, ApplicationMode modeTo, List 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 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 registerBooleanAccessibilityPreference(String id, boolean defValue) { + if (registeredPreferences.containsKey(id)) { + return (CommonPreference) registeredPreferences.get(id); + } + BooleanPreference p = new BooleanAccessibilityPreference(id, defValue); + registeredPreferences.put(id, p); + return p; + } + @SuppressWarnings("unchecked") public CommonPreference registerStringPreference(String id, String defValue) { if (registeredPreferences.containsKey(id)) { @@ -1147,6 +1173,16 @@ public class OsmandSettings { return p; } + @SuppressWarnings("unchecked") + public CommonPreference registerEnumIntPreference(String id, Enum defaultValue, Enum[] values, Class clz) { + if (registeredPreferences.containsKey(id)) { + return (CommonPreference) registeredPreferences.get(id); + } + EnumIntPreference p = new EnumIntPreference(id, defaultValue, values); + registeredPreferences.put(id, p); + return p; + } + public final CommonPreference RULER_MODE = new EnumIntPreference<>("ruler_mode", RulerMode.FIRST, RulerMode.values()).makeGlobal(); public final OsmandPreference SHOW_COMPASS_CONTROL_RULER = new BooleanPreference("show_compass_ruler", true).makeGlobal(); diff --git a/OsmAnd/src/net/osmand/plus/audionotes/MultimediaNotesFragment.java b/OsmAnd/src/net/osmand/plus/audionotes/MultimediaNotesFragment.java index 3abdb37e56..58fddf092c 100644 --- a/OsmAnd/src/net/osmand/plus/audionotes/MultimediaNotesFragment.java +++ b/OsmAnd/src/net/osmand/plus/audionotes/MultimediaNotesFragment.java @@ -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()); + } + } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/monitoring/MonitoringSettingsFragment.java b/OsmAnd/src/net/osmand/plus/monitoring/MonitoringSettingsFragment.java index 3d29e9bb13..63f636b106 100644 --- a/OsmAnd/src/net/osmand/plus/monitoring/MonitoringSettingsFragment.java +++ b/OsmAnd/src/net/osmand/plus/monitoring/MonitoringSettingsFragment.java @@ -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()); + } + } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/profiles/SelectCopyAppModeBottomSheet.java b/OsmAnd/src/net/osmand/plus/profiles/SelectCopyAppModeBottomSheet.java index fa497fcb92..2e8024e0ea 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/SelectCopyAppModeBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/profiles/SelectCopyAppModeBottomSheet.java @@ -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); + } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/settings/ConfigureProfileFragment.java b/OsmAnd/src/net/osmand/plus/settings/ConfigureProfileFragment.java index ea40709d09..f3c11e3819 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ConfigureProfileFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/ConfigureProfileFragment.java @@ -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)); diff --git a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/ResetProfilePrefsBottomSheet.java b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/ResetProfilePrefsBottomSheet.java index 14fdb63b30..5bbfa9f31a 100644 --- a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/ResetProfilePrefsBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/ResetProfilePrefsBottomSheet.java @@ -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); + } } \ No newline at end of file