Fix osmand plugins updates and improve settings bottom sheets
This commit is contained in:
parent
fc44cb8506
commit
0275b22aaa
13 changed files with 233 additions and 280 deletions
|
@ -377,7 +377,7 @@ public class OsmandAidlApi {
|
|||
}
|
||||
|
||||
private void registerReceiver(BroadcastReceiver rec, MapActivity ma,
|
||||
String filter) {
|
||||
String filter) {
|
||||
receivers.put(filter, rec);
|
||||
ma.registerReceiver(rec, new IntentFilter(filter));
|
||||
}
|
||||
|
@ -1254,7 +1254,7 @@ public class OsmandAidlApi {
|
|||
@SuppressLint("StaticFieldLeak")
|
||||
private void finishGpxImport(boolean destinationExists, File destination, String color, boolean show) {
|
||||
int col = ConfigureMapMenu.GpxAppearanceAdapter.parseTrackColor(
|
||||
app.getRendererRegistry().getCurrentSelectedRenderer(), color);
|
||||
app.getRendererRegistry().getCurrentSelectedRenderer(), color);
|
||||
if (!destinationExists) {
|
||||
GpxDataItem gpxDataItem = new GpxDataItem(destination, col);
|
||||
gpxDataItem.setApiImported(true);
|
||||
|
@ -1472,19 +1472,19 @@ public class OsmandAidlApi {
|
|||
List<GpxDataItem> gpxDataItems = app.getGpxDatabase().getItems();
|
||||
for (GpxDataItem dataItem : gpxDataItems) {
|
||||
//if (dataItem.isApiImported()) {
|
||||
File file = dataItem.getFile();
|
||||
if (file.exists()) {
|
||||
String fileName = file.getName();
|
||||
boolean active = app.getSelectedGpxHelper().getSelectedFileByPath(file.getAbsolutePath()) != null;
|
||||
long modifiedTime = dataItem.getFileLastModifiedTime();
|
||||
long fileSize = file.length();
|
||||
AGpxFileDetails details = null;
|
||||
GPXTrackAnalysis analysis = dataItem.getAnalysis();
|
||||
if (analysis != null) {
|
||||
details = createGpxFileDetails(analysis);
|
||||
File file = dataItem.getFile();
|
||||
if (file.exists()) {
|
||||
String fileName = file.getName();
|
||||
boolean active = app.getSelectedGpxHelper().getSelectedFileByPath(file.getAbsolutePath()) != null;
|
||||
long modifiedTime = dataItem.getFileLastModifiedTime();
|
||||
long fileSize = file.length();
|
||||
AGpxFileDetails details = null;
|
||||
GPXTrackAnalysis analysis = dataItem.getAnalysis();
|
||||
if (analysis != null) {
|
||||
details = createGpxFileDetails(analysis);
|
||||
}
|
||||
files.add(new AGpxFile(fileName, modifiedTime, fileSize, active, details));
|
||||
}
|
||||
files.add(new AGpxFile(fileName, modifiedTime, fileSize, active, details));
|
||||
}
|
||||
//}
|
||||
}
|
||||
return true;
|
||||
|
@ -1661,8 +1661,8 @@ public class OsmandAidlApi {
|
|||
}
|
||||
|
||||
boolean navigate(String startName, double startLat, double startLon,
|
||||
String destName, double destLat, double destLon,
|
||||
String profile, boolean force) {
|
||||
String destName, double destLat, double destLon,
|
||||
String profile, boolean force) {
|
||||
Intent intent = new Intent();
|
||||
intent.setAction(AIDL_NAVIGATE);
|
||||
intent.putExtra(AIDL_START_NAME, startName);
|
||||
|
@ -1678,8 +1678,8 @@ public class OsmandAidlApi {
|
|||
}
|
||||
|
||||
boolean navigateSearch(String startName, double startLat, double startLon,
|
||||
String searchQuery, double searchLat, double searchLon,
|
||||
String profile, boolean force) {
|
||||
String searchQuery, double searchLat, double searchLon,
|
||||
String profile, boolean force) {
|
||||
Intent intent = new Intent();
|
||||
intent.setAction(AIDL_NAVIGATE_SEARCH);
|
||||
intent.putExtra(AIDL_START_NAME, startName);
|
||||
|
@ -1740,7 +1740,7 @@ public class OsmandAidlApi {
|
|||
}
|
||||
|
||||
boolean search(final String searchQuery, final int searchType, final double latitude, final double longitude,
|
||||
final int radiusLevel, final int totalLimit, final SearchCompleteCallback callback) {
|
||||
final int radiusLevel, final int totalLimit, final SearchCompleteCallback callback) {
|
||||
if (Algorithms.isEmpty(searchQuery) || latitude == 0 || longitude == 0 || callback == null) {
|
||||
return false;
|
||||
}
|
||||
|
@ -1762,7 +1762,7 @@ public class OsmandAidlApi {
|
|||
}
|
||||
|
||||
boolean registerForOsmandInitialization(final OsmandAppInitCallback callback)
|
||||
throws RemoteException {
|
||||
throws RemoteException {
|
||||
if (app.isApplicationInitializing()) {
|
||||
app.getAppInitializer().addListener(new AppInitializeListener() {
|
||||
@Override
|
||||
|
@ -1819,22 +1819,6 @@ public class OsmandAidlApi {
|
|||
return res;
|
||||
}
|
||||
|
||||
public void updateConnectedApps() {
|
||||
try {
|
||||
JSONArray array = new JSONArray(app.getSettings().API_CONNECTED_APPS_JSON.get());
|
||||
for (int i = 0; i < array.length(); i++) {
|
||||
JSONObject obj = array.getJSONObject(i);
|
||||
String pack = obj.optString(ConnectedApp.PACK_KEY, "");
|
||||
ConnectedApp connectedApp = connectedApps.get(pack);
|
||||
if (connectedApp != null) {
|
||||
connectedApp.enabled = obj.optBoolean(ConnectedApp.ENABLED_KEY, true);
|
||||
}
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean switchEnabled(@NonNull ConnectedApp app) {
|
||||
app.enabled = !app.enabled;
|
||||
return saveConnectedApps();
|
||||
|
@ -1866,7 +1850,7 @@ public class OsmandAidlApi {
|
|||
return false;
|
||||
}
|
||||
|
||||
private void loadConnectedApps() {
|
||||
public void loadConnectedApps() {
|
||||
try {
|
||||
JSONArray array = new JSONArray(app.getSettings().API_CONNECTED_APPS_JSON.get());
|
||||
for (int i = 0; i < array.length(); i++) {
|
||||
|
@ -2090,7 +2074,7 @@ public class OsmandAidlApi {
|
|||
}
|
||||
|
||||
boolean getBitmapForGpx(final Uri gpxUri, final float density, final int widthPixels,
|
||||
final int heightPixels, final int color, final GpxBitmapCreatedCallback callback) {
|
||||
final int heightPixels, final int color, final GpxBitmapCreatedCallback callback) {
|
||||
if (gpxUri == null || callback == null) {
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -156,19 +156,6 @@ public abstract class OsmandPlugin {
|
|||
activatePlugins(app, enabledPlugins);
|
||||
}
|
||||
|
||||
public static void updateActivatedPlugins(@NonNull OsmandApplication app) {
|
||||
Set<String> enabledPlugins = app.getSettings().getEnabledPlugins();
|
||||
for (OsmandPlugin plugin : allPlugins) {
|
||||
try {
|
||||
if (plugin.init(app, null)) {
|
||||
plugin.setActive(enabledPlugins.contains(plugin.getId()));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.error("Plugin initialization failed " + plugin.getId(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void activatePlugins(OsmandApplication app, Set<String> enabledPlugins) {
|
||||
for (OsmandPlugin plugin : allPlugins) {
|
||||
if (enabledPlugins.contains(plugin.getId()) || plugin.isActive()) {
|
||||
|
|
|
@ -8,8 +8,6 @@ import android.content.SharedPreferences;
|
|||
import android.content.res.Configuration;
|
||||
import android.hardware.Sensor;
|
||||
import android.hardware.SensorManager;
|
||||
import android.media.AudioAttributes;
|
||||
import android.media.AudioManager;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.os.Build;
|
||||
|
@ -21,9 +19,9 @@ import android.support.v4.util.Pair;
|
|||
import android.support.v7.preference.PreferenceDataStore;
|
||||
|
||||
import net.osmand.IndexConstants;
|
||||
import net.osmand.PlatformUtil;
|
||||
import net.osmand.StateChangedListener;
|
||||
import net.osmand.ValueHolder;
|
||||
import net.osmand.aidl.OsmandAidlApi;
|
||||
import net.osmand.data.LatLon;
|
||||
import net.osmand.data.PointDescription;
|
||||
import net.osmand.map.ITileSource;
|
||||
|
@ -44,8 +42,6 @@ import net.osmand.plus.voice.CommandPlayer;
|
|||
import net.osmand.render.RenderingRulesStorage;
|
||||
import net.osmand.util.Algorithms;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.ref.WeakReference;
|
||||
|
@ -247,30 +243,45 @@ public class OsmandSettings {
|
|||
DEFAULT_APPLICATION_MODE.set(appMode);
|
||||
return true;
|
||||
}
|
||||
} else if (value instanceof ApplicationMode) {
|
||||
DEFAULT_APPLICATION_MODE.set((ApplicationMode) value);
|
||||
}
|
||||
} else if (preference == METRIC_SYSTEM) {
|
||||
MetricsConstants metricSystem = null;
|
||||
if (value instanceof String) {
|
||||
String metricSystemName = (String) value;
|
||||
try {
|
||||
metricSystem = MetricsConstants.valueOf(metricSystemName);
|
||||
} catch (IllegalArgumentException e) {
|
||||
return false;
|
||||
}
|
||||
} else if (value instanceof Integer) {
|
||||
int index = (Integer) value;
|
||||
if (index >= 0 && index < MetricsConstants.values().length) {
|
||||
metricSystem = MetricsConstants.values()[index];
|
||||
}
|
||||
}
|
||||
if (metricSystem != null) {
|
||||
METRIC_SYSTEM.set(metricSystem);
|
||||
return true;
|
||||
}
|
||||
} else if (preference == METRIC_SYSTEM && value instanceof String) {
|
||||
String metricSystemName = (String) value;
|
||||
MetricsConstants metricSystem;
|
||||
try {
|
||||
metricSystem = MetricsConstants.valueOf(metricSystemName);
|
||||
} catch (IllegalArgumentException e) {
|
||||
return false;
|
||||
} else if (preference == SPEED_SYSTEM) {
|
||||
SpeedConstants speedSystem = null;
|
||||
if (value instanceof String) {
|
||||
String speedSystemName = (String) value;
|
||||
try {
|
||||
speedSystem = SpeedConstants.valueOf(speedSystemName);
|
||||
} catch (IllegalArgumentException e) {
|
||||
return false;
|
||||
}
|
||||
} else if (value instanceof Integer) {
|
||||
int index = (Integer) value;
|
||||
if (index >= 0 && index < SpeedConstants.values().length) {
|
||||
speedSystem = SpeedConstants.values()[index];
|
||||
}
|
||||
}
|
||||
METRIC_SYSTEM.setModeValue(mode, metricSystem);
|
||||
return true;
|
||||
} else if (preference == SPEED_SYSTEM && value instanceof String) {
|
||||
String speedSystemName = (String) value;
|
||||
SpeedConstants speedSystem;
|
||||
try {
|
||||
speedSystem = SpeedConstants.valueOf(speedSystemName);
|
||||
} catch (IllegalArgumentException e) {
|
||||
return false;
|
||||
if (speedSystem != null) {
|
||||
SPEED_SYSTEM.set(speedSystem);
|
||||
return true;
|
||||
}
|
||||
SPEED_SYSTEM.setModeValue(mode, speedSystem);
|
||||
return true;
|
||||
} else if (preference instanceof BooleanPreference) {
|
||||
if (value instanceof Boolean) {
|
||||
((BooleanPreference) preference).setModeValue(mode, (Boolean) value);
|
||||
|
@ -349,13 +360,20 @@ public class OsmandSettings {
|
|||
@Override
|
||||
public boolean set(ApplicationMode val) {
|
||||
ApplicationMode oldMode = currentMode;
|
||||
boolean changed = settingsAPI.edit(globalPreferences).putString(getId(), val.getStringKey()).commit();
|
||||
if (changed) {
|
||||
boolean valueSaved = settingsAPI.edit(globalPreferences).putString(getId(), val.getStringKey()).commit();
|
||||
if (valueSaved) {
|
||||
currentMode = val;
|
||||
profilePreferences = getProfilePreferences(currentMode);
|
||||
|
||||
OsmandAidlApi aidlApi = ctx.getAidlApi();
|
||||
if (aidlApi != null) {
|
||||
aidlApi.loadConnectedApps();
|
||||
OsmandPlugin.initPlugins(ctx);
|
||||
}
|
||||
|
||||
fireEvent(oldMode);
|
||||
}
|
||||
return changed;
|
||||
return valueSaved;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -475,15 +493,15 @@ public class OsmandSettings {
|
|||
if (global) {
|
||||
return set(obj);
|
||||
}
|
||||
Object profilePrefs = getProfilePreferences(mode);
|
||||
boolean changed = setValue(profilePrefs, obj);
|
||||
|
||||
if (changed && cache && cachedPreference == profilePrefs) {
|
||||
Object profilePrefs = getProfilePreferences(mode);
|
||||
boolean valueSaved = setValue(profilePrefs, obj);
|
||||
if (valueSaved && cache && cachedPreference == profilePrefs) {
|
||||
cachedValue = obj;
|
||||
}
|
||||
|
||||
fireEvent(obj);
|
||||
return changed;
|
||||
|
||||
return valueSaved;
|
||||
}
|
||||
|
||||
public T getProfileDefaultValue(ApplicationMode mode) {
|
||||
|
@ -1241,11 +1259,11 @@ public class OsmandSettings {
|
|||
public final CommonPreference<Boolean> ENABLE_PROXY = new BooleanPreference("enable_proxy", false) {
|
||||
@Override
|
||||
protected boolean setValue(Object prefs, Boolean val) {
|
||||
boolean valueSet = super.setValue(prefs, val);
|
||||
if (valueSet) {
|
||||
boolean valueSaved = super.setValue(prefs, val);
|
||||
if (valueSaved) {
|
||||
NetworkUtils.setProxy(val ? PROXY_HOST.get() : null, val ? PROXY_PORT.get() : 0);
|
||||
}
|
||||
return valueSet;
|
||||
return valueSaved;
|
||||
}
|
||||
}.makeGlobal();
|
||||
|
||||
|
@ -1327,36 +1345,36 @@ public class OsmandSettings {
|
|||
public final OsmandPreference<Boolean> ANNOUNCE_WPT = new BooleanPreference("announce_wpt", true) {
|
||||
@Override
|
||||
protected boolean setValue(Object prefs, Boolean val) {
|
||||
boolean changed = super.setValue(prefs, val);
|
||||
if (changed) {
|
||||
boolean valueSaved = super.setValue(prefs, val);
|
||||
if (valueSaved) {
|
||||
SHOW_WPT.set(val);
|
||||
}
|
||||
|
||||
return changed;
|
||||
return valueSaved;
|
||||
}
|
||||
}.makeProfile().cache();
|
||||
|
||||
public final OsmandPreference<Boolean> ANNOUNCE_NEARBY_FAVORITES = new BooleanPreference("announce_nearby_favorites", false) {
|
||||
@Override
|
||||
protected boolean setValue(Object prefs, Boolean val) {
|
||||
boolean changed = super.setValue(prefs, val);
|
||||
if (changed) {
|
||||
boolean valueSaved = super.setValue(prefs, val);
|
||||
if (valueSaved) {
|
||||
SHOW_NEARBY_FAVORITES.set(val);
|
||||
}
|
||||
|
||||
return changed;
|
||||
return valueSaved;
|
||||
}
|
||||
}.makeProfile().cache();
|
||||
|
||||
public final OsmandPreference<Boolean> ANNOUNCE_NEARBY_POI = new BooleanPreference("announce_nearby_poi", false) {
|
||||
@Override
|
||||
protected boolean setValue(Object prefs, Boolean val) {
|
||||
boolean changed = super.setValue(prefs, val);
|
||||
if (changed) {
|
||||
boolean valueSaved = super.setValue(prefs, val);
|
||||
if (valueSaved) {
|
||||
SHOW_NEARBY_POI.set(val);
|
||||
}
|
||||
|
||||
return changed;
|
||||
return valueSaved;
|
||||
}
|
||||
}.makeProfile().cache();
|
||||
|
||||
|
@ -1537,25 +1555,25 @@ public class OsmandSettings {
|
|||
public final OsmandPreference<Integer> AUDIO_STREAM_GUIDANCE = new IntPreference("audio_stream", 3/*AudioManager.STREAM_MUSIC*/) {
|
||||
@Override
|
||||
protected boolean setValue(Object prefs, Integer stream) {
|
||||
boolean valueSet = super.setValue(prefs, stream);
|
||||
boolean valueSaved = super.setValue(prefs, stream);
|
||||
|
||||
if (valueSet) {
|
||||
if (valueSaved) {
|
||||
CommandPlayer player = ctx.getPlayer();
|
||||
if (player != null) {
|
||||
player.updateAudioStream(get());
|
||||
}
|
||||
// Sync corresponding AUDIO_USAGE value
|
||||
ApplicationMode mode = APPLICATION_MODE.get();
|
||||
if (stream == AudioManager.STREAM_MUSIC) {
|
||||
AUDIO_USAGE.setModeValue(mode, AudioAttributes.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE);
|
||||
} else if (stream == AudioManager.STREAM_NOTIFICATION) {
|
||||
AUDIO_USAGE.setModeValue(mode, AudioAttributes.USAGE_NOTIFICATION);
|
||||
} else if (stream == AudioManager.STREAM_VOICE_CALL) {
|
||||
AUDIO_USAGE.setModeValue(mode, AudioAttributes.USAGE_VOICE_COMMUNICATION);
|
||||
if (stream == 3 /*AudioManager.STREAM_MUSIC*/) {
|
||||
AUDIO_USAGE.setModeValue(mode, 12 /*AudioAttributes.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE*/);
|
||||
} else if (stream == 5 /*AudioManager.STREAM_NOTIFICATION*/) {
|
||||
AUDIO_USAGE.setModeValue(mode, 5 /*AudioAttributes.USAGE_NOTIFICATION*/);
|
||||
} else if (stream == 0 /*AudioManager.STREAM_VOICE_CALL*/) {
|
||||
AUDIO_USAGE.setModeValue(mode, 2 /*AudioAttributes.USAGE_VOICE_COMMUNICATION*/);
|
||||
}
|
||||
}
|
||||
|
||||
return valueSet;
|
||||
return valueSaved;
|
||||
}
|
||||
}.makeProfile();
|
||||
|
||||
|
@ -3176,47 +3194,38 @@ public class OsmandSettings {
|
|||
|
||||
public class PreferencesDataStore extends PreferenceDataStore {
|
||||
|
||||
private final Log log = PlatformUtil.getLog(PreferencesDataStore.class);
|
||||
|
||||
@Override
|
||||
public void putString(String key, @Nullable String value) {
|
||||
setPreference(key, value);
|
||||
log.debug("putString key " + key + " value " + value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putStringSet(String key, @Nullable Set<String> values) {
|
||||
setPreference(key, values);
|
||||
log.debug("putStringSet key " + key + " value " + values);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putInt(String key, int value) {
|
||||
setPreference(key, value);
|
||||
log.debug("putInt key " + key + " value " + value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putLong(String key, long value) {
|
||||
setPreference(key, value);
|
||||
log.debug("putLong key " + key + " value " + value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putFloat(String key, float value) {
|
||||
setPreference(key, value);
|
||||
log.debug("putFloat key " + key + " value " + value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putBoolean(String key, boolean value) {
|
||||
setPreference(key, value);
|
||||
log.debug("putBoolean key " + key + " value " + value);
|
||||
}
|
||||
|
||||
public void putValue(String key, Object value) {
|
||||
setPreference(key, value);
|
||||
log.debug("putValue key " + key + " value " + value);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
|
|
@ -231,7 +231,7 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
|
|||
private boolean stopped = true;
|
||||
|
||||
private ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
|
||||
|
||||
|
||||
private LockHelper lockHelper;
|
||||
|
||||
@Override
|
||||
|
@ -351,7 +351,7 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
|
|||
registerReceiver(screenOffReceiver, filter);
|
||||
|
||||
app.getAidlApi().onCreateMapActivity(this);
|
||||
|
||||
|
||||
lockHelper.setLockUIAdapter(this);
|
||||
|
||||
mIsDestroyed = false;
|
||||
|
@ -1415,8 +1415,6 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
|
|||
public void updateApplicationModeSettings() {
|
||||
changeKeyguardFlags();
|
||||
updateMapSettings();
|
||||
OsmandPlugin.updateActivatedPlugins(app);
|
||||
getMyApplication().getAidlApi().updateConnectedApps();
|
||||
mapViewTrackingUtilities.updateSettings();
|
||||
//app.getRoutingHelper().setAppMode(settings.getApplicationMode());
|
||||
if (mapLayers.getMapInfoLayer() != null) {
|
||||
|
@ -2080,7 +2078,7 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
|
|||
}
|
||||
|
||||
public void showQuickSearch(@NonNull ShowQuickSearchMode mode, boolean showCategories,
|
||||
@NonNull String searchQuery, @Nullable LatLon searchLocation) {
|
||||
@NonNull String searchQuery, @Nullable LatLon searchLocation) {
|
||||
if (mode == ShowQuickSearchMode.CURRENT) {
|
||||
mapContextMenu.close();
|
||||
} else {
|
||||
|
@ -2133,7 +2131,7 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
|
|||
}
|
||||
|
||||
public void showQuickSearch(@NonNull ShowQuickSearchMode mode, QuickSearchTab showSearchTab,
|
||||
@NonNull String searchQuery, @Nullable LatLon searchLocation) {
|
||||
@NonNull String searchQuery, @Nullable LatLon searchLocation) {
|
||||
if (mode == ShowQuickSearchMode.CURRENT) {
|
||||
mapContextMenu.close();
|
||||
} else {
|
||||
|
|
|
@ -39,6 +39,10 @@ import net.osmand.plus.activities.OsmandActionBarActivity;
|
|||
import net.osmand.plus.activities.OsmandInAppPurchaseActivity;
|
||||
import net.osmand.plus.profiles.AppProfileArrayAdapter;
|
||||
import net.osmand.plus.profiles.ProfileDataObject;
|
||||
import net.osmand.plus.settings.bottomsheets.BooleanPreferenceBottomSheet;
|
||||
import net.osmand.plus.settings.bottomsheets.EditTextPreferenceBottomSheet;
|
||||
import net.osmand.plus.settings.bottomsheets.MultiSelectPreferencesBottomSheet;
|
||||
import net.osmand.plus.settings.bottomsheets.SingleSelectPreferenceBottomSheet;
|
||||
import net.osmand.plus.settings.preferences.ListPreferenceEx;
|
||||
import net.osmand.plus.settings.preferences.MultiSelectBooleanPreference;
|
||||
import net.osmand.plus.settings.preferences.SwitchPreferenceEx;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package net.osmand.plus.settings;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v7.preference.CheckBoxPreference;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
|
@ -23,6 +23,7 @@ import net.osmand.Location;
|
|||
import net.osmand.data.PointDescription;
|
||||
import net.osmand.plus.OsmAndFormatter;
|
||||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.settings.bottomsheets.ChangeGeneralProfilesPrefBottomSheet;
|
||||
|
||||
|
||||
public class CoordinatesFormatFragment extends BaseSettingsFragment {
|
||||
|
@ -158,8 +159,6 @@ public class CoordinatesFormatFragment extends BaseSettingsFragment {
|
|||
spannableBuilder.append(" ");
|
||||
spannableBuilder.append(getString(R.string.shared_string_read_more));
|
||||
|
||||
final int color = ContextCompat.getColor(app, isNightMode() ? R.color.active_color_primary_dark : R.color.active_color_primary_light);
|
||||
|
||||
ClickableSpan clickableSpan = new ClickableSpan() {
|
||||
@Override
|
||||
public void onClick(@NonNull View widget) {
|
||||
|
@ -168,7 +167,7 @@ public class CoordinatesFormatFragment extends BaseSettingsFragment {
|
|||
|
||||
@Override
|
||||
public void updateDrawState(@NonNull TextPaint ds) {
|
||||
ds.setColor(color);
|
||||
super.updateDrawState(ds);
|
||||
ds.setUnderlineText(false);
|
||||
}
|
||||
};
|
||||
|
@ -180,18 +179,19 @@ public class CoordinatesFormatFragment extends BaseSettingsFragment {
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceClick(Preference preference) {
|
||||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||
String key = preference.getKey();
|
||||
|
||||
updateSelectedFormatPrefs(key);
|
||||
|
||||
int newFormat = getCoordinatesFormatForKey(key);
|
||||
if (newFormat != -1) {
|
||||
settings.COORDINATES_FORMAT.set(newFormat);
|
||||
return true;
|
||||
FragmentManager fragmentManager = getFragmentManager();
|
||||
if (fragmentManager != null) {
|
||||
ChangeGeneralProfilesPrefBottomSheet.showInstance(fragmentManager, settings.COORDINATES_FORMAT.getId(), newFormat, this);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return super.onPreferenceClick(preference);
|
||||
return super.onPreferenceChange(preference, newValue);
|
||||
}
|
||||
|
||||
private void updateSelectedFormatPrefs(String key) {
|
||||
|
|
|
@ -19,6 +19,7 @@ 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.settings.bottomsheets.ChangeGeneralProfilesPrefBottomSheet;
|
||||
import net.osmand.plus.settings.preferences.ListPreferenceEx;
|
||||
import net.osmand.plus.settings.preferences.SwitchPreferenceEx;
|
||||
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
package net.osmand.plus.settings.bottomsheets;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.preference.DialogPreference.TargetFragment;
|
||||
import android.support.v7.preference.Preference;
|
||||
|
||||
import net.osmand.plus.base.MenuBottomSheetDialogFragment;
|
||||
|
||||
public abstract class BasePreferenceBottomSheet extends MenuBottomSheetDialogFragment {
|
||||
|
||||
public static final String PREFERENCE_ID = "preference_id";
|
||||
|
||||
private Preference preference;
|
||||
|
||||
public Preference getPreference() {
|
||||
if (preference == null) {
|
||||
Bundle args = getArguments();
|
||||
if (args != null && args.containsKey(PREFERENCE_ID)) {
|
||||
String prefId = args.getString(PREFERENCE_ID);
|
||||
TargetFragment targetFragment = (TargetFragment) getTargetFragment();
|
||||
|
||||
if (targetFragment != null) {
|
||||
preference = targetFragment.findPreference(prefId);
|
||||
}
|
||||
}
|
||||
}
|
||||
return preference;
|
||||
}
|
||||
}
|
|
@ -1,10 +1,9 @@
|
|||
package net.osmand.plus.settings;
|
||||
package net.osmand.plus.settings.bottomsheets;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v7.preference.DialogPreference;
|
||||
import android.view.View;
|
||||
|
||||
import net.osmand.PlatformUtil;
|
||||
|
@ -13,7 +12,6 @@ import net.osmand.plus.OsmandSettings;
|
|||
import net.osmand.plus.OsmandSettings.BooleanPreference;
|
||||
import net.osmand.plus.OsmandSettings.OsmandPreference;
|
||||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.base.MenuBottomSheetDialogFragment;
|
||||
import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem;
|
||||
import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithCompoundButton;
|
||||
import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithDescription;
|
||||
|
@ -22,37 +20,28 @@ import net.osmand.plus.settings.preferences.SwitchPreferenceEx;
|
|||
|
||||
import org.apache.commons.logging.Log;
|
||||
|
||||
public class BooleanPreferenceBottomSheet extends MenuBottomSheetDialogFragment {
|
||||
public class BooleanPreferenceBottomSheet extends BasePreferenceBottomSheet {
|
||||
|
||||
public static final String TAG = BooleanPreferenceBottomSheet.class.getSimpleName();
|
||||
|
||||
private static final String PREFERENCE_ID = "preference_id";
|
||||
|
||||
private static final Log LOG = PlatformUtil.getLog(BooleanPreferenceBottomSheet.class);
|
||||
|
||||
private SwitchPreferenceEx switchPreference;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
switchPreference = getListPreference();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createMenuItems(Bundle savedInstanceState) {
|
||||
OsmandApplication app = getMyApplication();
|
||||
Bundle args = getArguments();
|
||||
if (app == null || args == null || switchPreference == null) {
|
||||
if (app == null) {
|
||||
return;
|
||||
}
|
||||
final SwitchPreferenceEx switchPreference = getSwitchPreferenceEx();
|
||||
if (switchPreference == null) {
|
||||
return;
|
||||
}
|
||||
String title = switchPreference.getTitle().toString();
|
||||
String description = switchPreference.getDescription();
|
||||
|
||||
OsmandPreference preference = app.getSettings().getPreference(switchPreference.getKey());
|
||||
if (!(preference instanceof BooleanPreference)) {
|
||||
return;
|
||||
}
|
||||
|
||||
String title = switchPreference.getTitle().toString();
|
||||
items.add(new TitleItem(title));
|
||||
|
||||
final OsmandSettings.BooleanPreference pref = (BooleanPreference) preference;
|
||||
|
@ -79,6 +68,7 @@ public class BooleanPreferenceBottomSheet extends MenuBottomSheetDialogFragment
|
|||
.create();
|
||||
items.add(preferenceBtn[0]);
|
||||
|
||||
String description = switchPreference.getDescription();
|
||||
if (description != null) {
|
||||
BaseBottomSheetItem preferenceDescription = new BottomSheetItemWithDescription.Builder()
|
||||
.setDescription(description)
|
||||
|
@ -88,20 +78,15 @@ public class BooleanPreferenceBottomSheet extends MenuBottomSheetDialogFragment
|
|||
}
|
||||
}
|
||||
|
||||
private SwitchPreferenceEx getListPreference() {
|
||||
if (switchPreference == null) {
|
||||
final String key = getArguments().getString(PREFERENCE_ID);
|
||||
final DialogPreference.TargetFragment fragment = (DialogPreference.TargetFragment) getTargetFragment();
|
||||
switchPreference = (SwitchPreferenceEx) fragment.findPreference(key);
|
||||
}
|
||||
return switchPreference;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getDismissButtonTextId() {
|
||||
return R.string.shared_string_close;
|
||||
}
|
||||
|
||||
private SwitchPreferenceEx getSwitchPreferenceEx() {
|
||||
return (SwitchPreferenceEx) getPreference();
|
||||
}
|
||||
|
||||
public static void showInstance(@NonNull FragmentManager fm, String prefId, Fragment target) {
|
||||
try {
|
||||
if (fm.findFragmentByTag(BooleanPreferenceBottomSheet.TAG) == null) {
|
|
@ -1,4 +1,4 @@
|
|||
package net.osmand.plus.settings;
|
||||
package net.osmand.plus.settings.bottomsheets;
|
||||
|
||||
import android.graphics.Typeface;
|
||||
import android.os.Bundle;
|
||||
|
@ -14,11 +14,11 @@ import net.osmand.plus.OsmandApplication;
|
|||
import net.osmand.plus.OsmandSettings.CommonPreference;
|
||||
import net.osmand.plus.OsmandSettings.OsmandPreference;
|
||||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.base.MenuBottomSheetDialogFragment;
|
||||
import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem;
|
||||
import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem;
|
||||
import net.osmand.plus.base.bottomsheetmenu.simpleitems.LongDescriptionItem;
|
||||
import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem;
|
||||
import net.osmand.plus.settings.BaseSettingsFragment;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
|
||||
|
@ -26,12 +26,10 @@ import java.util.ArrayList;
|
|||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class ChangeGeneralProfilesPrefBottomSheet extends MenuBottomSheetDialogFragment {
|
||||
public class ChangeGeneralProfilesPrefBottomSheet extends BasePreferenceBottomSheet {
|
||||
|
||||
public static final String TAG = "ChangeGeneralProfilesPrefBottomSheet";
|
||||
|
||||
private static final String PREFERENCE_ID = "preference_id";
|
||||
|
||||
private static final Log LOG = PlatformUtil.getLog(ChangeGeneralProfilesPrefBottomSheet.class);
|
||||
|
||||
private Object newValue;
|
|
@ -1,54 +1,44 @@
|
|||
package net.osmand.plus.settings;
|
||||
package net.osmand.plus.settings.bottomsheets;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v7.preference.DialogPreference;
|
||||
import android.widget.EditText;
|
||||
|
||||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.base.MenuBottomSheetDialogFragment;
|
||||
import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem;
|
||||
import net.osmand.plus.base.bottomsheetmenu.simpleitems.LongDescriptionItem;
|
||||
import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem;
|
||||
import net.osmand.plus.settings.preferences.EditTextPreferenceEx;
|
||||
import net.osmand.util.Algorithms;
|
||||
|
||||
public class EditTextPreferenceBottomSheet extends MenuBottomSheetDialogFragment {
|
||||
public class EditTextPreferenceBottomSheet extends BasePreferenceBottomSheet {
|
||||
|
||||
public static final String TAG = EditTextPreferenceBottomSheet.class.getSimpleName();
|
||||
|
||||
private static final String PREFERENCE_ID = "preference_id";
|
||||
private static final String EDIT_TEXT_PREFERENCE_KEY = "edit_text_preference_key";
|
||||
|
||||
private EditTextPreferenceEx editTextPreference;
|
||||
|
||||
private EditText editText;
|
||||
private String text;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
editTextPreference = getEditTextPreference();
|
||||
if (savedInstanceState == null) {
|
||||
text = getEditTextPreference().getText();
|
||||
} else {
|
||||
text = savedInstanceState.getString(EDIT_TEXT_PREFERENCE_KEY);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createMenuItems(Bundle savedInstanceState) {
|
||||
final Context context = getContext();
|
||||
if (context == null || editTextPreference == null) {
|
||||
Context ctx = getContext();
|
||||
EditTextPreferenceEx editTextPreference = getEditTextPreference();
|
||||
if (ctx == null || editTextPreference == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
items.add(new TitleItem(editTextPreference.getDialogTitle().toString()));
|
||||
String text;
|
||||
if (savedInstanceState != null) {
|
||||
text = savedInstanceState.getString(EDIT_TEXT_PREFERENCE_KEY);
|
||||
} else {
|
||||
text = editTextPreference.getText();
|
||||
}
|
||||
|
||||
editText = new EditText(context);
|
||||
editText = new EditText(ctx);
|
||||
editText.setText(text);
|
||||
items.add(new SimpleBottomSheetItem.Builder().setCustomView(editText).create());
|
||||
|
||||
|
@ -61,7 +51,7 @@ public class EditTextPreferenceBottomSheet extends MenuBottomSheetDialogFragment
|
|||
@Override
|
||||
public void onSaveInstanceState(@NonNull Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
outState.putString(EDIT_TEXT_PREFERENCE_KEY, text);
|
||||
outState.putString(EDIT_TEXT_PREFERENCE_KEY, editText.getText().toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -76,26 +66,19 @@ public class EditTextPreferenceBottomSheet extends MenuBottomSheetDialogFragment
|
|||
|
||||
@Override
|
||||
protected void onRightBottomButtonClick() {
|
||||
String value = editText.getText().toString();
|
||||
if (editTextPreference.callChangeListener(value)) {
|
||||
editTextPreference.setText(value);
|
||||
EditTextPreferenceEx editTextPreference = getEditTextPreference();
|
||||
if (editTextPreference != null) {
|
||||
String value = editText.getText().toString();
|
||||
if (editTextPreference.callChangeListener(value)) {
|
||||
editTextPreference.setText(value);
|
||||
}
|
||||
}
|
||||
|
||||
dismiss();
|
||||
}
|
||||
|
||||
private EditTextPreferenceEx getEditTextPreference() {
|
||||
if (editTextPreference == null) {
|
||||
Bundle args = getArguments();
|
||||
if (args != null) {
|
||||
final String key = args.getString(PREFERENCE_ID);
|
||||
Fragment targetFragment = getTargetFragment();
|
||||
if (targetFragment instanceof DialogPreference.TargetFragment) {
|
||||
DialogPreference.TargetFragment fragment = (DialogPreference.TargetFragment) targetFragment;
|
||||
editTextPreference = (EditTextPreferenceEx) fragment.findPreference(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
return editTextPreference;
|
||||
return (EditTextPreferenceEx) getPreference();
|
||||
}
|
||||
|
||||
public static boolean showInstance(@NonNull FragmentManager fragmentManager, String key, Fragment target) {
|
|
@ -1,15 +1,14 @@
|
|||
package net.osmand.plus.settings;
|
||||
package net.osmand.plus.settings.bottomsheets;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v7.preference.DialogPreference;
|
||||
import android.view.View;
|
||||
|
||||
import net.osmand.PlatformUtil;
|
||||
import net.osmand.plus.OsmandApplication;
|
||||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.base.MenuBottomSheetDialogFragment;
|
||||
import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem;
|
||||
import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithCompoundButton;
|
||||
import net.osmand.plus.base.bottomsheetmenu.simpleitems.LongDescriptionItem;
|
||||
|
@ -17,15 +16,18 @@ import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem;
|
|||
import net.osmand.plus.settings.preferences.MultiSelectBooleanPreference;
|
||||
import net.osmand.util.Algorithms;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class MultiSelectPreferencesBottomSheet extends MenuBottomSheetDialogFragment {
|
||||
public class MultiSelectPreferencesBottomSheet extends BasePreferenceBottomSheet {
|
||||
|
||||
public static final String TAG = MultiSelectPreferencesBottomSheet.class.getSimpleName();
|
||||
|
||||
private static final String PREFERENCE_ID = "preference_id";
|
||||
private static final Log LOG = PlatformUtil.getLog(MultiSelectPreferencesBottomSheet.class);
|
||||
|
||||
private static final String PREFERENCES_IDS = "preferences_ids";
|
||||
private static final String PREFERENCE_CHANGED = "preference_changed";
|
||||
private static final String PREFERENCES_ENTRIES = "preferences_entries";
|
||||
|
@ -37,27 +39,27 @@ public class MultiSelectPreferencesBottomSheet extends MenuBottomSheetDialogFrag
|
|||
private CharSequence[] entries;
|
||||
private Set<String> enabledPrefs = new HashSet<>();
|
||||
|
||||
private boolean preferenceChanged;
|
||||
private boolean prefChanged;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
multiSelectBooleanPreference = getListPreference();
|
||||
|
||||
if (savedInstanceState == null) {
|
||||
if (multiSelectBooleanPreference.getEntries() == null || multiSelectBooleanPreference.getPrefsIds() == null) {
|
||||
throw new IllegalStateException("MultiSelectListPreference requires an entries array and an entryValues array.");
|
||||
LOG.error("MultiSelectListPreference requires an entries array and an entryValues array.");
|
||||
return;
|
||||
}
|
||||
enabledPrefs.clear();
|
||||
enabledPrefs.addAll(multiSelectBooleanPreference.getValues());
|
||||
preferenceChanged = false;
|
||||
prefChanged = false;
|
||||
entries = multiSelectBooleanPreference.getEntries();
|
||||
prefsIds = multiSelectBooleanPreference.getPrefsIds();
|
||||
} else {
|
||||
enabledPrefs.clear();
|
||||
enabledPrefs.addAll(savedInstanceState.getStringArrayList(ENABLED_PREFERENCES_IDS));
|
||||
preferenceChanged = savedInstanceState.getBoolean(PREFERENCE_CHANGED, false);
|
||||
prefChanged = savedInstanceState.getBoolean(PREFERENCE_CHANGED, false);
|
||||
entries = savedInstanceState.getCharSequenceArray(PREFERENCES_ENTRIES);
|
||||
prefsIds = savedInstanceState.getStringArray(PREFERENCES_IDS);
|
||||
}
|
||||
|
@ -66,7 +68,7 @@ public class MultiSelectPreferencesBottomSheet extends MenuBottomSheetDialogFrag
|
|||
@Override
|
||||
public void createMenuItems(Bundle savedInstanceState) {
|
||||
final OsmandApplication app = getMyApplication();
|
||||
if (app == null || multiSelectBooleanPreference == null) {
|
||||
if (app == null || multiSelectBooleanPreference == null || prefsIds == null || entries == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -95,9 +97,9 @@ public class MultiSelectPreferencesBottomSheet extends MenuBottomSheetDialogFrag
|
|||
public void onClick(View v) {
|
||||
boolean checked = !item[0].isChecked();
|
||||
if (checked) {
|
||||
preferenceChanged |= enabledPrefs.add(prefsIds[index]);
|
||||
prefChanged |= enabledPrefs.add(prefsIds[index]);
|
||||
} else {
|
||||
preferenceChanged |= enabledPrefs.remove(prefsIds[index]);
|
||||
prefChanged |= enabledPrefs.remove(prefsIds[index]);
|
||||
}
|
||||
item[0].setChecked(checked);
|
||||
}
|
||||
|
@ -122,10 +124,10 @@ public class MultiSelectPreferencesBottomSheet extends MenuBottomSheetDialogFrag
|
|||
@Override
|
||||
public void onSaveInstanceState(@NonNull Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
outState.putStringArrayList(ENABLED_PREFERENCES_IDS, new ArrayList<>(enabledPrefs));
|
||||
outState.putBoolean(PREFERENCE_CHANGED, preferenceChanged);
|
||||
outState.putCharSequenceArray(PREFERENCES_ENTRIES, entries);
|
||||
outState.putBoolean(PREFERENCE_CHANGED, prefChanged);
|
||||
outState.putStringArray(PREFERENCES_IDS, prefsIds);
|
||||
outState.putStringArrayList(ENABLED_PREFERENCES_IDS, new ArrayList<>(enabledPrefs));
|
||||
outState.putCharSequenceArray(PREFERENCES_ENTRIES, entries);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -135,13 +137,13 @@ public class MultiSelectPreferencesBottomSheet extends MenuBottomSheetDialogFrag
|
|||
|
||||
@Override
|
||||
protected void onRightBottomButtonClick() {
|
||||
if (preferenceChanged) {
|
||||
if (prefChanged) {
|
||||
final Set<String> values = enabledPrefs;
|
||||
if (multiSelectBooleanPreference.callChangeListener(values)) {
|
||||
multiSelectBooleanPreference.setValues(values);
|
||||
}
|
||||
}
|
||||
preferenceChanged = false;
|
||||
prefChanged = false;
|
||||
dismiss();
|
||||
}
|
||||
|
||||
|
@ -149,18 +151,6 @@ public class MultiSelectPreferencesBottomSheet extends MenuBottomSheetDialogFrag
|
|||
return (MultiSelectBooleanPreference) getPreference();
|
||||
}
|
||||
|
||||
public DialogPreference getPreference() {
|
||||
Bundle args = getArguments();
|
||||
if (multiSelectBooleanPreference == null && args != null) {
|
||||
final String key = args.getString(PREFERENCE_ID);
|
||||
final DialogPreference.TargetFragment targetFragment = (DialogPreference.TargetFragment) getTargetFragment();
|
||||
if (targetFragment != null) {
|
||||
multiSelectBooleanPreference = (MultiSelectBooleanPreference) targetFragment.findPreference(key);
|
||||
}
|
||||
}
|
||||
return multiSelectBooleanPreference;
|
||||
}
|
||||
|
||||
public static boolean showInstance(@NonNull FragmentManager fragmentManager, String prefId, Fragment target) {
|
||||
try {
|
||||
Bundle args = new Bundle();
|
|
@ -1,71 +1,65 @@
|
|||
package net.osmand.plus.settings;
|
||||
package net.osmand.plus.settings.bottomsheets;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v7.preference.DialogPreference;
|
||||
import android.view.View;
|
||||
|
||||
import net.osmand.AndroidUtils;
|
||||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.base.MenuBottomSheetDialogFragment;
|
||||
import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem;
|
||||
import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithCompoundButton;
|
||||
import net.osmand.plus.base.bottomsheetmenu.simpleitems.LongDescriptionItem;
|
||||
import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem;
|
||||
import net.osmand.plus.settings.OnPreferenceChanged;
|
||||
import net.osmand.plus.settings.preferences.ListPreferenceEx;
|
||||
import net.osmand.util.Algorithms;
|
||||
|
||||
public class SingleSelectPreferenceBottomSheet extends MenuBottomSheetDialogFragment {
|
||||
public class SingleSelectPreferenceBottomSheet extends BasePreferenceBottomSheet {
|
||||
|
||||
public static final String TAG = SingleSelectPreferenceBottomSheet.class.getSimpleName();
|
||||
|
||||
private static final String PREFERENCE_ID = "preference_id";
|
||||
private static final String SELECTED_ENTRY_INDEX_KEY = "selected_entry_index_key";
|
||||
|
||||
private ListPreferenceEx listPreference;
|
||||
|
||||
private String[] entries;
|
||||
private Object[] entryValues;
|
||||
private int selectedEntryIndex = -1;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
listPreference = getListPreference();
|
||||
if (listPreference != null) {
|
||||
entries = listPreference.getEntries();
|
||||
entryValues = listPreference.getEntryValues();
|
||||
|
||||
if (savedInstanceState != null) {
|
||||
selectedEntryIndex = savedInstanceState.getInt(SELECTED_ENTRY_INDEX_KEY);
|
||||
} else {
|
||||
selectedEntryIndex = listPreference.findIndexOfValue(listPreference.getValue());
|
||||
}
|
||||
if (savedInstanceState != null) {
|
||||
selectedEntryIndex = savedInstanceState.getInt(SELECTED_ENTRY_INDEX_KEY);
|
||||
} else if (listPreference != null) {
|
||||
selectedEntryIndex = listPreference.findIndexOfValue(listPreference.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createMenuItems(Bundle savedInstanceState) {
|
||||
final Context context = getContext();
|
||||
if (context == null || listPreference == null || entries == null || entryValues == null) {
|
||||
Context ctx = getContext();
|
||||
if (ctx == null || listPreference == null || listPreference.getEntries() == null || listPreference.getEntryValues() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
items.add(new TitleItem(listPreference.getDialogTitle().toString()));
|
||||
String title = listPreference.getDialogTitle().toString();
|
||||
items.add(new TitleItem(title));
|
||||
|
||||
String description = listPreference.getDescription();
|
||||
if (!Algorithms.isEmpty(description)) {
|
||||
items.add(new LongDescriptionItem(description));
|
||||
}
|
||||
|
||||
String[] entries = listPreference.getEntries();
|
||||
|
||||
for (int i = 0; i < entries.length; i++) {
|
||||
final BaseBottomSheetItem[] preferenceItem = new BottomSheetItemWithCompoundButton[1];
|
||||
preferenceItem[0] = new BottomSheetItemWithCompoundButton.Builder()
|
||||
.setChecked(i == selectedEntryIndex)
|
||||
.setButtonTintList(AndroidUtils.createCheckedColorStateList(context, R.color.icon_color_default_light, getActiveColorId()))
|
||||
.setButtonTintList(AndroidUtils.createCheckedColorStateList(ctx, R.color.icon_color_default_light, getActiveColorId()))
|
||||
.setTitle(entries[i])
|
||||
.setTag(i)
|
||||
.setLayoutId(R.layout.bottom_sheet_item_with_radio_btn_left)
|
||||
|
@ -93,30 +87,6 @@ public class SingleSelectPreferenceBottomSheet extends MenuBottomSheetDialogFrag
|
|||
outState.putInt(SELECTED_ENTRY_INDEX_KEY, selectedEntryIndex);
|
||||
}
|
||||
|
||||
private void updateItems() {
|
||||
for (BaseBottomSheetItem item : items) {
|
||||
if (item instanceof BottomSheetItemWithCompoundButton) {
|
||||
boolean checked = item.getTag().equals(selectedEntryIndex);
|
||||
((BottomSheetItemWithCompoundButton) item).setChecked(checked);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private ListPreferenceEx getListPreference() {
|
||||
if (listPreference == null) {
|
||||
Bundle args = getArguments();
|
||||
if (args != null) {
|
||||
final String key = args.getString(PREFERENCE_ID);
|
||||
Fragment targetFragment = getTargetFragment();
|
||||
if (targetFragment instanceof DialogPreference.TargetFragment) {
|
||||
DialogPreference.TargetFragment fragment = (DialogPreference.TargetFragment) targetFragment;
|
||||
listPreference = (ListPreferenceEx) fragment.findPreference(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
return listPreference;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getDismissButtonTextId() {
|
||||
return R.string.shared_string_close;
|
||||
|
@ -129,19 +99,34 @@ public class SingleSelectPreferenceBottomSheet extends MenuBottomSheetDialogFrag
|
|||
|
||||
@Override
|
||||
protected void onRightBottomButtonClick() {
|
||||
if (selectedEntryIndex >= 0) {
|
||||
Object[] entryValues = listPreference.getEntryValues();
|
||||
if (entryValues != null && selectedEntryIndex >= 0) {
|
||||
Object value = entryValues[selectedEntryIndex];
|
||||
if (listPreference.callChangeListener(value)) {
|
||||
listPreference.setValue(value);
|
||||
}
|
||||
Fragment target = getTargetFragment();
|
||||
if (target instanceof OnPreferenceChanged) {
|
||||
((OnPreferenceChanged) target).onPreferenceChanged(listPreference.getKey());
|
||||
}
|
||||
}
|
||||
Fragment target = getTargetFragment();
|
||||
if (target instanceof OnPreferenceChanged) {
|
||||
((OnPreferenceChanged) target).onPreferenceChanged(listPreference.getKey());
|
||||
}
|
||||
|
||||
dismiss();
|
||||
}
|
||||
|
||||
private void updateItems() {
|
||||
for (BaseBottomSheetItem item : items) {
|
||||
if (item instanceof BottomSheetItemWithCompoundButton) {
|
||||
boolean checked = item.getTag().equals(selectedEntryIndex);
|
||||
((BottomSheetItemWithCompoundButton) item).setChecked(checked);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private ListPreferenceEx getListPreference() {
|
||||
return (ListPreferenceEx) getPreference();
|
||||
}
|
||||
|
||||
public static boolean showInstance(@NonNull FragmentManager fragmentManager, String key, Fragment target) {
|
||||
try {
|
||||
Bundle args = new Bundle();
|
Loading…
Reference in a new issue