Introduce general preferences

This commit is contained in:
Chumva 2019-10-22 17:17:12 +03:00
parent 2f40c6fb56
commit ddfe4a0361
4 changed files with 51 additions and 62 deletions

View file

@ -11,7 +11,8 @@
Thx - Hardy
-->
<string name="apply_preference_to_all_profiles">You can apply this change to all profiles or only to selected.</string>
<string name="shared_preference">Shared</string>
<string name="shared_string_memory_used_tb_desc">Used %1$s TB</string>
<string name="shared_string_memory_used_gb_desc">Used %1$s GB</string>
<string name="shared_string_memory_used_mb_desc">Used %1$s MB</string>

View file

@ -239,6 +239,20 @@ public class OsmandSettings {
return registeredPreferences.get(key);
}
public boolean setSharedGeneralPreference(String key, Object value) {
OsmandPreference<?> preference = registeredPreferences.get(key);
if (preference != null) {
for (ApplicationMode mode : ApplicationMode.values(ctx)) {
settingsAPI.edit(getProfilePreferences(mode)).remove(key).commit();
}
if (preference instanceof CommonPreference) {
((CommonPreference) preference).cachedValue = null;
}
return setPreference(key, value, ApplicationMode.DEFAULT);
}
return false;
}
public boolean setPreference(String key, Object value) {
return setPreference(key, value, APPLICATION_MODE.get());
}
@ -516,6 +530,7 @@ public class OsmandSettings {
public abstract class CommonPreference<T> extends PreferenceWithListener<T> {
private final String id;
private boolean global;
private boolean general;
private T cachedValue;
private Object cachedPreference;
private boolean cache;
@ -544,6 +559,11 @@ public class OsmandSettings {
return this;
}
public CommonPreference<T> makeGeneral() {
general = true;
return this;
}
protected Object getPreferences() {
return global ? globalPreferences : profilePreferences;
}
@ -582,7 +602,7 @@ public class OsmandSettings {
if (pt != null) {
return getProfileDefaultValue(pt);
}
if (settingsAPI.contains(defaultProfilePreferences, getId())) {
if (general && settingsAPI.contains(defaultProfilePreferences, getId())) {
return getValue(defaultProfilePreferences, defaultValue);
} else {
return defaultValue;
@ -1179,7 +1199,7 @@ public class OsmandSettings {
public final OsmandPreference<Boolean> FIRST_MAP_IS_DOWNLOADED = new BooleanPreference(
"first_map_is_downloaded", false);
public final CommonPreference<Boolean> DRIVING_REGION_AUTOMATIC = new BooleanPreference("driving_region_automatic", true).makeProfile().cache();
public final CommonPreference<Boolean> DRIVING_REGION_AUTOMATIC = new BooleanPreference("driving_region_automatic", true).makeProfile().makeGeneral().cache();
public final OsmandPreference<DrivingRegion> DRIVING_REGION = new EnumIntPreference<DrivingRegion>(
"default_driving_region", DrivingRegion.EUROPE_ASIA, DrivingRegion.values()) {
protected boolean setValue(Object prefs, DrivingRegion val) {
@ -1211,7 +1231,7 @@ public class OsmandSettings {
return DrivingRegion.EUROPE_ASIA;
}
}.makeProfile().cache();
}.makeProfile().makeGeneral().cache();
public final CommonPreference<Boolean> METRIC_SYSTEM_CHANGED_MANUALLY = new BooleanPreference("metric_system_changed_manually", false).makeGlobal();
@ -1223,7 +1243,7 @@ public class OsmandSettings {
return DRIVING_REGION.get().defMetrics;
}
}.makeProfile();
}.makeProfile().makeGeneral();
//public final OsmandPreference<Integer> COORDINATES_FORMAT = new IntPreference("coordinates_format", PointDescription.FORMAT_DEGREES).makeGlobal();
@ -1233,7 +1253,7 @@ public class OsmandSettings {
protected AngularConstants getValue(Object prefs, AngularConstants defaultValue) {
return super.getValue(prefs, defaultValue);
}
}.makeProfile();
}.makeProfile().makeGeneral();
public final OsmandPreference<SpeedConstants> SPEED_SYSTEM = new EnumIntPreference<SpeedConstants>(
@ -1263,7 +1283,7 @@ public class OsmandSettings {
;
}.makeProfile();
}.makeProfile().makeGeneral();
// this value string is synchronized with settings_pref.xml preference name
@ -1327,8 +1347,8 @@ public class OsmandSettings {
new BooleanAccessibilityPreference("direction_haptic_feedback", false).makeGlobal();
// magnetic field doesn'torkmost of the time on some phones
public final OsmandPreference<Boolean> USE_MAGNETIC_FIELD_SENSOR_COMPASS = new BooleanPreference("use_magnetic_field_sensor_compass", false).makeProfile().cache();
public final OsmandPreference<Boolean> USE_KALMAN_FILTER_FOR_COMPASS = new BooleanPreference("use_kalman_filter_compass", true).makeProfile().cache();
public final OsmandPreference<Boolean> USE_MAGNETIC_FIELD_SENSOR_COMPASS = new BooleanPreference("use_magnetic_field_sensor_compass", false).makeProfile().makeGeneral().cache();
public final OsmandPreference<Boolean> USE_KALMAN_FILTER_FOR_COMPASS = new BooleanPreference("use_kalman_filter_compass", true).makeProfile().makeGeneral().cache();
public final OsmandPreference<Boolean> DO_NOT_SHOW_STARTUP_MESSAGES = new BooleanPreference("do_not_show_startup_messages", false).makeGlobal().cache();
public final OsmandPreference<Boolean> DO_NOT_USE_ANIMATIONS = new BooleanPreference("do_not_use_animations", false).makeProfile().cache();
@ -1339,7 +1359,7 @@ public class OsmandSettings {
public final OsmandPreference<Integer> SEND_ANONYMOUS_DATA_REQUESTS_COUNT = new IntPreference("send_anonymous_data_requests_count", 0).makeGlobal().cache();
public final OsmandPreference<Integer> SEND_ANONYMOUS_DATA_LAST_REQUEST_NS = new IntPreference("send_anonymous_data_last_request_ns", -1).makeGlobal().cache();
public final OsmandPreference<Boolean> MAP_EMPTY_STATE_ALLOWED = new BooleanPreference("map_empty_state_allowed", true).makeProfile().cache();
public final OsmandPreference<Boolean> MAP_EMPTY_STATE_ALLOWED = new BooleanPreference("map_empty_state_allowed", true).makeProfile().makeGeneral().cache();
public final CommonPreference<Float> TEXT_SCALE = new FloatPreference("text_scale", 1f).makeProfile().cache();
@ -1684,7 +1704,7 @@ public class OsmandSettings {
// this value string is synchronized with settings_pref.xml preference name
public final OsmandPreference<Integer> MAP_SCREEN_ORIENTATION =
new IntPreference("map_screen_orientation", -1/*ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED*/).makeProfile();
new IntPreference("map_screen_orientation", -1/*ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED*/).makeProfile().makeGeneral();
// this value string is synchronized with settings_pref.xml preference name
// public final CommonPreference<Boolean> SHOW_VIEW_ANGLE = new BooleanPreference("show_view_angle", false).makeProfile().cache();
@ -1741,7 +1761,7 @@ public class OsmandSettings {
public static final int ROTATE_MAP_BEARING = 1;
public static final int ROTATE_MAP_COMPASS = 2;
public final CommonPreference<Integer> ROTATE_MAP =
new IntPreference("rotate_map", ROTATE_MAP_NONE).makeProfile().cache();
new IntPreference("rotate_map", ROTATE_MAP_NONE).makeProfile().makeGeneral().cache();
{
ROTATE_MAP.setModeDefaultValue(ApplicationMode.CAR, ROTATE_MAP_BEARING);
@ -1853,7 +1873,7 @@ public class OsmandSettings {
public final OsmandPreference<Boolean> ANIMATE_MY_LOCATION = new BooleanPreference("animate_my_location", true).makeProfile().cache();
public final OsmandPreference<Integer> EXTERNAL_INPUT_DEVICE = new IntPreference("external_input_device", 0).makeProfile();
public final OsmandPreference<Integer> EXTERNAL_INPUT_DEVICE = new IntPreference("external_input_device", 0).makeProfile().makeGeneral();
public final OsmandPreference<Boolean> ROUTE_MAP_MARKERS_START_MY_LOC = new BooleanPreference("route_map_markers_start_my_loc", false).makeGlobal().cache();
public final OsmandPreference<Boolean> ROUTE_MAP_MARKERS_ROUND_TRIP = new BooleanPreference("route_map_markers_round_trip", false).makeGlobal().cache();
@ -3042,7 +3062,7 @@ public class OsmandSettings {
// this value string is synchronized with settings_pref.xml preference name
public final OsmandPreference<String> CONTRIBUTION_INSTALL_APP_DATE = new StringPreference("CONTRIBUTION_INSTALL_APP_DATE", null).makeGlobal();
public final OsmandPreference<Integer> COORDINATES_FORMAT = new IntPreference("coordinates_format", PointDescription.FORMAT_DEGREES).makeProfile();
public final OsmandPreference<Integer> COORDINATES_FORMAT = new IntPreference("coordinates_format", PointDescription.FORMAT_DEGREES).makeProfile().makeGeneral();
public final OsmandPreference<Boolean> FOLLOW_THE_ROUTE = new BooleanPreference("follow_to_route", false).makeGlobal();
public final OsmandPreference<String> FOLLOW_THE_GPX_ROUTE = new StringPreference("follow_gpx", null).makeGlobal();
@ -3108,7 +3128,7 @@ public class OsmandSettings {
new IntPreference("FAVORITES_TAB", 0).makeGlobal().cache();
public final CommonPreference<Integer> OSMAND_THEME =
new IntPreference("osmand_theme", OSMAND_LIGHT_THEME).makeProfile().cache();
new IntPreference("osmand_theme", OSMAND_LIGHT_THEME).makeProfile().makeGeneral().cache();
public boolean isLightActionBar() {
return isLightContent();

View file

@ -21,6 +21,7 @@ import android.widget.TextView;
import net.osmand.AndroidUtils;
import net.osmand.data.PointDescription;
import net.osmand.plus.ApplicationMode;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.base.MapViewTrackingUtilities;
@ -82,8 +83,8 @@ public class GeneralProfileSettingsFragment extends BaseSettingsFragment impleme
if (TextUtils.isEmpty(summary)) {
summary = preference.getSummary();
}
if (!osmandPreference.isSetForMode(getSelectedAppMode())) {
String baseString = getString(R.string.shared_string_by_default) + ": %s";
if (!osmandPreference.isSetForMode(getSelectedAppMode()) || getSelectedAppMode().equals(ApplicationMode.DEFAULT)) {
String baseString = getString(R.string.shared_preference) + ": %s";
summary = AndroidUtils.getStyledString(baseString, summary, new CustomTypefaceSpan(FontCache.getRobotoMedium(app)), null);
}
summaryView.setText(summary);
@ -336,16 +337,11 @@ public class GeneralProfileSettingsFragment extends BaseSettingsFragment impleme
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
OsmandSettings.OsmandPreference pref = settings.getPreference(preference.getKey());
if (pref != null && !pref.isSetForMode(getSelectedAppMode())) {
FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager != null) {
ChangeGeneralProfilesPrefBottomSheet.showInstance(fragmentManager, preference.getKey(), newValue, this, false);
}
return false;
FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager != null) {
ChangeGeneralProfilesPrefBottomSheet.showInstance(fragmentManager, preference.getKey(), newValue, this, false);
}
return true;
return false;
}
@Override

View file

@ -1,13 +1,11 @@
package net.osmand.plus.settings.bottomsheets;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.View;
import net.osmand.AndroidUtils;
import net.osmand.PlatformUtil;
import net.osmand.plus.ApplicationMode;
import net.osmand.plus.OsmandApplication;
@ -22,10 +20,6 @@ import net.osmand.plus.settings.BaseSettingsFragment;
import org.apache.commons.logging.Log;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ChangeGeneralProfilesPrefBottomSheet extends BasePreferenceBottomSheet {
public static final String TAG = ChangeGeneralProfilesPrefBottomSheet.class.getSimpleName();
@ -44,34 +38,12 @@ public class ChangeGeneralProfilesPrefBottomSheet extends BasePreferenceBottomSh
final String prefId = args.getString(PREFERENCE_ID);
CommonPreference pref = getPreference(prefId);
OsmandPreference osmandPref = app.getSettings().getPreference(prefId);
if (pref == null || osmandPref == null) {
if (pref == null) {
return;
}
items.add(new TitleItem(getString(R.string.change_default_settings)));
StringBuilder builder = new StringBuilder();
final List<ApplicationMode> values = ApplicationMode.values(app);
List<ApplicationMode> appModesDefaultValue = new ArrayList<>();
for (int i = 0; i < values.size(); i++) {
ApplicationMode mode = values.get(i);
if (!osmandPref.isSetForMode(mode)) {
appModesDefaultValue.add(mode);
}
}
Iterator<ApplicationMode> iterator = appModesDefaultValue.iterator();
while (iterator.hasNext()) {
builder.append(iterator.next().toHumanString(app));
builder.append(iterator.hasNext() ? ", " : '.');
}
if (builder.length() > 0) {
CharSequence description = AndroidUtils.getStyledString(app.getString(R.string.pref_selected_by_default_for_profiles), builder.toString(), Typeface.BOLD);
items.add(new LongDescriptionItem(description));
}
items.add(new LongDescriptionItem(getString(R.string.apply_preference_to_all_profiles)));
BaseBottomSheetItem applyToAllProfiles = new SimpleBottomSheetItem.Builder()
.setTitle(getString(R.string.apply_to_all_profiles))
@ -80,12 +52,12 @@ public class ChangeGeneralProfilesPrefBottomSheet extends BasePreferenceBottomSh
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (ApplicationMode mode : values) {
app.getSettings().setPreference(prefId, newValue, mode);
}
BaseSettingsFragment target = (BaseSettingsFragment) getTargetFragment();
if (target != null) {
target.updateAllSettings();
boolean valueSaved = app.getSettings().setSharedGeneralPreference(prefId, newValue);
if (valueSaved) {
BaseSettingsFragment target = (BaseSettingsFragment) getTargetFragment();
if (target != null) {
target.updateAllSettings();
}
}
dismiss();
}