General profile settings in progress

This commit is contained in:
Chumva 2019-08-09 18:02:51 +03:00
parent 7088846e09
commit 8a65645cee
7 changed files with 252 additions and 5 deletions

View file

@ -6,6 +6,7 @@
<Preference
android:key="general_settings"
android:layout="@layout/preference_with_descr"
android:persistent="false"
android:summary="@string/general_settings_profile_descr"
android:title="@string/general_settings_2"
app:fragment="net.osmand.plus.settings.ProfileGeneralSettings"
@ -14,16 +15,18 @@
<Preference
android:key="navigation_settings"
android:layout="@layout/preference_with_descr"
android:persistent="false"
android:summary="@string/configure_navigation"
android:title="@string/routing_settings_2"
app:fragment="net.osmand.plus.settings.NavigationFragment"
tools:icon="@drawable/ic_action_gdirections_dark" />
<Preference
android:key="map_look"
android:key="configure_map"
android:layout="@layout/preference_with_descr"
android:persistent="false"
android:summary="@string/map_look_descr"
android:title="@string/map_look"
android:title="@string/configure_map"
tools:icon="@drawable/ic_action_layers_dark" />
<Preference

View file

@ -15,17 +15,17 @@
android:title="@string/choose_osmand_theme" />
<net.osmand.plus.views.ListIntPreference
android:defaultValue="0"
android:key="rotate_map"
android:layout="@layout/preference_with_descr"
android:summary="@string/rotate_map_to_bearing_descr"
android:defaultValue="0"
android:title="@string/rotate_map_to_bearing" />
<net.osmand.plus.views.ListIntPreference
android:defaultValue="-1"
android:key="map_screen_orientation"
android:layout="@layout/preference_with_descr"
android:summary="@string/map_screen_orientation_descr"
android:defaultValue="-1"
android:title="@string/map_screen_orientation" />
<Preference

View file

@ -203,6 +203,7 @@ public class OsmandSettings {
return settingsAPI.getPreferenceObject(getSharedPreferencesName(mode));
}
@SuppressWarnings("unchecked")
public boolean setPreference(String key, Object value) {
OsmandPreference<?> preference = registeredPreferences.get(key);
if (preference != null) {
@ -273,6 +274,22 @@ public class OsmandSettings {
((LongPreference) preference).set((Long) value);
return true;
}
} else if (preference instanceof EnumIntPreference) {
EnumIntPreference enumPref = (EnumIntPreference) preference;
if (value instanceof String) {
String name = (String) value;
Enum enumValue = null;
for (int i = 0; i < enumPref.values.length; i++) {
if (name.equals(enumPref.values[i].name())) {
enumValue = enumPref.values[i];
break;
}
}
if (enumValue != null) {
return enumPref.set(enumValue);
}
return false;
}
}
}
return false;
@ -694,7 +711,7 @@ public class OsmandSettings {
}
private class EnumIntPreference<E extends Enum<E>> extends CommonPreference<E> {
public class EnumIntPreference<E extends Enum<E>> extends CommonPreference<E> {
private final E[] values;

View file

@ -51,6 +51,14 @@ public class ConfigureProfileFragment extends BaseSettingsFragment {
protected void createUI() {
PreferenceScreen screen = getPreferenceScreen();
Preference generalSettings = findAndRegisterPreference("general_settings");
Preference navigationSettings = findAndRegisterPreference("navigation_settings");
Preference configureMap = findAndRegisterPreference("configure_map");
generalSettings.setIcon(getContentIcon(R.drawable.ic_action_settings));
navigationSettings.setIcon(getContentIcon(R.drawable.ic_action_gdirections_dark));
configureMap.setIcon(getContentIcon(R.drawable.ic_action_layers_dark));
List<ConnectedApp> connectedApps = getMyApplication().getAidlApi().getConnectedApps();
List<OsmandPlugin> plugins = OsmandPlugin.getVisiblePlugins();

View file

@ -1,18 +1,31 @@
package net.osmand.plus.settings;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.preference.ListPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.support.v7.widget.AppCompatCheckedTextView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import net.osmand.data.PointDescription;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.base.MapViewTrackingUtilities;
import net.osmand.plus.views.ListIntPreference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ProfileGeneralSettings extends BaseSettingsFragment {
public static final String TAG = "ProfileGeneralSettings";
@ -44,18 +57,64 @@ public class ProfileGeneralSettings extends BaseSettingsFragment {
ListIntPreference appTheme = (ListIntPreference) findAndRegisterPreference(settings.OSMAND_THEME.getId());
appTheme.setEntries(new String[]{getString(R.string.dark_theme), getString(R.string.light_theme)});
appTheme.setEntryValues(new int[]{OsmandSettings.OSMAND_DARK_THEME, OsmandSettings.OSMAND_LIGHT_THEME});
updateAppThemePref();
ListIntPreference rotateMap = (ListIntPreference) findAndRegisterPreference(settings.ROTATE_MAP.getId());
rotateMap.setEntries(new String[]{getString(R.string.rotate_map_none_opt), getString(R.string.rotate_map_bearing_opt), getString(R.string.rotate_map_compass_opt)});
rotateMap.setEntryValues(new int[]{OsmandSettings.ROTATE_MAP_NONE, OsmandSettings.ROTATE_MAP_BEARING, OsmandSettings.ROTATE_MAP_COMPASS});
updateRotateMapPref();
ListIntPreference mapScreenOrientation = (ListIntPreference) findAndRegisterPreference(settings.MAP_SCREEN_ORIENTATION.getId());
mapScreenOrientation.setEntries(new String[]{getString(R.string.map_orientation_portrait), getString(R.string.map_orientation_landscape), getString(R.string.map_orientation_default)});
mapScreenOrientation.setEntryValues(new int[]{ActivityInfo.SCREEN_ORIENTATION_PORTRAIT, ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE, ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED});
updateMapScreenOrientationPref();
Preference defaultDrivingRegion = findAndRegisterPreference(settings.DRIVING_REGION.getId());
defaultDrivingRegion.setIcon(getContentIcon(R.drawable.ic_action_car_dark));
createUnitsOfLengthPref();
createCoordinatesFormatPref();
createAngularUnitsPref();
createExternalInputDevicePref();
}
private void createUnitsOfLengthPref() {
OsmandSettings.MetricsConstants[] mvls = OsmandSettings.MetricsConstants.values();
String[] entries = new String[mvls.length];
String[] entryValues = new String[mvls.length];
for (int i = 0; i < entries.length; i++) {
entries[i] = mvls[i].toHumanString(app);
entryValues[i] = mvls[i].name();
}
ListPreference unitsOfLength = (ListPreference) findAndRegisterPreference(settings.METRIC_SYSTEM.getId());
unitsOfLength.setEntries(entries);
unitsOfLength.setEntryValues(entryValues);
unitsOfLength.setIcon(getContentIcon(R.drawable.ic_action_ruler_unit));
}
private void createExternalInputDevicePref() {
String[] entries = new String[]{
getString(R.string.sett_no_ext_input),
getString(R.string.sett_generic_ext_input),
getString(R.string.sett_wunderlinq_ext_input),
getString(R.string.sett_parrot_ext_input)};
int[] entryValues = new int[]{
OsmandSettings.NO_EXTERNAL_DEVICE,
OsmandSettings.GENERIC_EXTERNAL_DEVICE,
OsmandSettings.WUNDERLINQ_EXTERNAL_DEVICE,
OsmandSettings.PARROT_EXTERNAL_DEVICE};
ListIntPreference externalInputDevice = (ListIntPreference) findAndRegisterPreference(settings.EXTERNAL_INPUT_DEVICE.getId());
externalInputDevice.setEntries(entries);
externalInputDevice.setEntryValues(entryValues);
int index = settings.EXTERNAL_INPUT_DEVICE.get();
externalInputDevice.setSummary(entries[index]);
}
private void createCoordinatesFormatPref() {
int[] cvls = new int[5];
cvls[0] = PointDescription.FORMAT_DEGREES;
cvls[1] = PointDescription.FORMAT_MINUTES;
@ -72,6 +131,156 @@ public class ProfileGeneralSettings extends BaseSettingsFragment {
ListIntPreference coordinatesFormat = (ListIntPreference) findAndRegisterPreference(settings.COORDINATES_FORMAT.getId());
coordinatesFormat.setEntries(entries);
coordinatesFormat.setEntryValues(cvls);
coordinatesFormat.setIcon(getContentIcon(R.drawable.ic_action_coordinates_widget));
}
private void createAngularUnitsPref() {
OsmandSettings.AngularConstants[] ac = OsmandSettings.AngularConstants.values();
String[] entries = new String[ac.length];
String[] entryValues = new String[ac.length];
for (int i = 0; i < entries.length; i++) {
if (ac[i] == OsmandSettings.AngularConstants.DEGREES) {
entries[i] = OsmandSettings.AngularConstants.DEGREES.toHumanString(app) + " 180";
entryValues[i] = OsmandSettings.AngularConstants.DEGREES.name();
} else if (ac[i] == OsmandSettings.AngularConstants.DEGREES360) {
entries[i] = OsmandSettings.AngularConstants.DEGREES.toHumanString(app) + " 360";
entryValues[i] = OsmandSettings.AngularConstants.DEGREES360.name();
} else {
entries[i] = ac[i].toHumanString(app);
entryValues[i] = OsmandSettings.AngularConstants.MILLIRADS.name();
}
}
ListPreference angularUnits = (ListPreference) findAndRegisterPreference(settings.ANGULAR_UNITS.getId());
angularUnits.setEntries(entries);
angularUnits.setEntryValues(entryValues);
angularUnits.setIcon(getContentIcon(R.drawable.ic_action_angular_unit));
}
private void updateAppThemePref() {
ListIntPreference appTheme = (ListIntPreference) findAndRegisterPreference(settings.OSMAND_THEME.getId());
int iconRes = settings.isLightContent() ? R.drawable.ic_action_sun : R.drawable.ic_action_moon;
appTheme.setIcon(getContentIcon(iconRes));
}
private void updateRotateMapPref() {
ListIntPreference rotateMap = (ListIntPreference) findAndRegisterPreference(settings.ROTATE_MAP.getId());
int iconRes;
if (settings.ROTATE_MAP.get() == OsmandSettings.ROTATE_MAP_NONE) {
iconRes = R.drawable.ic_action_direction_north;
} else if (settings.ROTATE_MAP.get() == OsmandSettings.ROTATE_MAP_BEARING) {
iconRes = R.drawable.ic_action_direction_movement;
} else {
iconRes = R.drawable.ic_action_direction_compass;
}
rotateMap.setIcon(getContentIcon(iconRes));
}
private void updateMapScreenOrientationPref() {
ListIntPreference mapScreenOrientation = (ListIntPreference) findAndRegisterPreference(settings.MAP_SCREEN_ORIENTATION.getId());
int iconRes;
if (settings.MAP_SCREEN_ORIENTATION.get() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
iconRes = R.drawable.ic_action_phone_portrait_orientation;
} else if (settings.MAP_SCREEN_ORIENTATION.get() == ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE) {
iconRes = R.drawable.ic_action_phone_landscape_orientation;
} else {
iconRes = R.drawable.ic_action_phone_device_orientation;
}
mapScreenOrientation.setIcon(getContentIcon(iconRes));
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
boolean changed = super.onPreferenceChange(preference, newValue);
if (changed) {
String key = preference.getKey();
if (key.equals(settings.OSMAND_THEME.getId())) {
updateAppThemePref();
} else if (key.equals(settings.ROTATE_MAP.getId())) {
updateRotateMapPref();
} else if (key.equals(settings.MAP_SCREEN_ORIENTATION.getId())) {
updateMapScreenOrientationPref();
}
}
return changed;
}
@Override
public boolean onPreferenceClick(Preference preference) {
if (preference.getKey().equals(settings.DRIVING_REGION.getId())) {
showDrivingRegionDialog();
return true;
}
return false;
}
private void showDrivingRegionDialog() {
final AlertDialog.Builder b = new AlertDialog.Builder(getActivity());
b.setTitle(getString(R.string.driving_region));
final List<OsmandSettings.DrivingRegion> drs = new ArrayList<>();
drs.add(null);
drs.addAll(Arrays.asList(OsmandSettings.DrivingRegion.values()));
int sel = -1;
OsmandSettings.DrivingRegion selectedDrivingRegion = settings.DRIVING_REGION.get();
if (settings.DRIVING_REGION_AUTOMATIC.get()) {
sel = 0;
}
for (int i = 1; i < drs.size(); i++) {
if (sel == -1 && drs.get(i) == selectedDrivingRegion) {
sel = i;
break;
}
}
final int selected = sel;
final ArrayAdapter<OsmandSettings.DrivingRegion> singleChoiceAdapter =
new ArrayAdapter<OsmandSettings.DrivingRegion>(getActivity(), R.layout.single_choice_description_item, R.id.text1, drs) {
@NonNull
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = getActivity().getLayoutInflater();
v = inflater.inflate(R.layout.single_choice_description_item, parent, false);
}
OsmandSettings.DrivingRegion item = getItem(position);
AppCompatCheckedTextView title = (AppCompatCheckedTextView) v.findViewById(R.id.text1);
TextView desc = (TextView) v.findViewById(R.id.description);
if (item != null) {
title.setText(getString(item.name));
desc.setVisibility(View.VISIBLE);
desc.setText(item.getDescription(v.getContext()));
} else {
title.setText(getString(R.string.driving_region_automatic));
desc.setVisibility(View.GONE);
}
title.setChecked(position == selected);
return v;
}
};
b.setAdapter(singleChoiceAdapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (drs.get(which) == null) {
settings.DRIVING_REGION_AUTOMATIC.set(true);
MapViewTrackingUtilities mapViewTrackingUtilities = getMyApplication().getMapViewTrackingUtilities();
if (mapViewTrackingUtilities != null) {
mapViewTrackingUtilities.resetDrivingRegionUpdate();
}
} else {
settings.DRIVING_REGION_AUTOMATIC.set(false);
settings.DRIVING_REGION.set(drs.get(which));
}
updateAllSettings();
}
});
b.setNegativeButton(R.string.shared_string_cancel, null);
b.show();
}
public static boolean showInstance(FragmentManager fragmentManager) {

View file

@ -82,4 +82,9 @@ public class ListFloatPreference extends ListPreference {
protected Object onGetDefaultValue(TypedArray a, int index) {
return String.valueOf(a.getFloat(index, 0f));
}
@Override
public boolean callChangeListener(Object newValue) {
return super.callChangeListener(Float.valueOf((String) newValue));
}
}

View file

@ -74,4 +74,9 @@ public class ListIntPreference extends ListPreference {
protected Object onGetDefaultValue(TypedArray a, int index) {
return String.valueOf(a.getInt(index, 0));
}
@Override
public boolean callChangeListener(Object newValue) {
return super.callChangeListener(Integer.valueOf((String) newValue));
}
}