From d1d190b338ccb765f6651f99c001d24ff8d26a15 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Wed, 22 Aug 2012 19:05:30 +0200 Subject: [PATCH] Fix sunrise/sunset for longitude < 0 --- OsmAnd/res/values/strings.xml | 2 + .../plus/activities/DayNightHelper.java | 25 ++++---- .../plus/activities/SettingsActivity.java | 58 +++++++++++++++++++ .../development/OsmandDevelopmentPlugin.java | 15 ++++- 4 files changed, 89 insertions(+), 11 deletions(-) diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index f85d23d87a..76050d12c0 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -10,6 +10,8 @@ PLEASE: Have a look at http://code.google.com/p/osmand/wiki/UIConsistency, it may really improve your and our work :-) Thx - Hardy --> Tracking + Sunrise : %1$s \nSunset : %2$s + Day/night info Day/night map Rendering attributes: Rendering style diff --git a/OsmAnd/src/net/osmand/plus/activities/DayNightHelper.java b/OsmAnd/src/net/osmand/plus/activities/DayNightHelper.java index 34c43849c3..5cacec94bd 100644 --- a/OsmAnd/src/net/osmand/plus/activities/DayNightHelper.java +++ b/OsmAnd/src/net/osmand/plus/activities/DayNightHelper.java @@ -81,15 +81,7 @@ public class DayNightHelper implements SensorEventListener { if (currentTime - lastAutoCall > 60000) { lastAutoCall = System.currentTimeMillis(); try { - Location lastKnownLocation = getLocation(); - if (lastKnownLocation == null) { - return null; - } - double longitude = lastKnownLocation.getLongitude(); - Date actualTime = new Date(); - SunriseSunset daynightSwitch = new SunriseSunset(lastKnownLocation.getLatitude(), - longitude < 0 ? 360 - longitude : longitude, actualTime, - TimeZone.getDefault()); + SunriseSunset daynightSwitch = getSunriseSunset(); boolean daytime = daynightSwitch.isDaytime(); log.debug("Sunrise/sunset setting to day: " + daytime); //$NON-NLS-1$ lastAutoValue = Boolean.valueOf(daytime); @@ -109,6 +101,19 @@ public class DayNightHelper implements SensorEventListener { } return null; } + + public SunriseSunset getSunriseSunset(){ + Location lastKnownLocation = getLocation(); + if (lastKnownLocation == null) { + return null; + } + double longitude = lastKnownLocation.getLongitude(); + Date actualTime = new Date(); + SunriseSunset daynightSwitch = new SunriseSunset(lastKnownLocation.getLatitude(), + longitude < 0 ? 360 + longitude : longitude, actualTime, + TimeZone.getDefault()); + return daynightSwitch; + } private Location getLocation() { Location lastKnownLocation = null; @@ -124,7 +129,7 @@ public class DayNightHelper implements SensorEventListener { //find location for (String provider : providers) { lastKnownLocation = locationProvider.getLastKnownLocation(provider); - if (lastKnownLocation == null) { + if (lastKnownLocation != null) { break; } } diff --git a/OsmAnd/src/net/osmand/plus/activities/SettingsActivity.java b/OsmAnd/src/net/osmand/plus/activities/SettingsActivity.java index 24bf7df44f..44770111df 100644 --- a/OsmAnd/src/net/osmand/plus/activities/SettingsActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/SettingsActivity.java @@ -25,6 +25,7 @@ import net.osmand.plus.R; import net.osmand.plus.ResourceManager; import net.osmand.plus.activities.CustomTitleBar.CustomTitleBarView; import net.osmand.plus.render.NativeOsmandLibrary; +import net.osmand.plus.routing.RoutingHelper; import net.osmand.plus.routing.RouteProvider.RouteService; import net.osmand.plus.views.SeekBarPreference; import net.osmand.render.RenderingRulesStorage; @@ -35,6 +36,7 @@ import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; +import android.content.DialogInterface.OnDismissListener; import android.content.DialogInterface.OnMultiChoiceClickListener; import android.content.Intent; import android.content.pm.ActivityInfo; @@ -50,8 +52,10 @@ import android.preference.PreferenceActivity; import android.preference.PreferenceCategory; import android.preference.PreferenceScreen; import android.view.View; +import android.widget.CompoundButton; import android.widget.ListView; import android.widget.Toast; +import android.widget.ToggleButton; public class SettingsActivity extends PreferenceActivity implements OnPreferenceChangeListener, OnPreferenceClickListener { @@ -636,12 +640,66 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference String title = ""; if (preference.getKey() != null && preference instanceof PreferenceScreen && SettingsActivity.SCREEN_ID_NAVIGATION_SETTINGS.equals(preference.getKey())) { + final ApplicationMode appMode = osmandSettings.getApplicationMode(); PreferenceScreen scr = (PreferenceScreen) preference; title = scr.getTitle().toString(); if (title.startsWith("-")) { title = title.substring(1); } + Builder builder = new AlertDialog.Builder(this); + View view = getLayoutInflater().inflate(R.layout.calculate_route, null); + builder.setView(view); + final AlertDialog dlg = builder.show(); + + final ToggleButton[] buttons = new ToggleButton[ApplicationMode.values().length]; + buttons[ApplicationMode.CAR.ordinal()] = (ToggleButton) view.findViewById(R.id.CarButton); + buttons[ApplicationMode.BICYCLE.ordinal()] = (ToggleButton) view.findViewById(R.id.BicycleButton); + buttons[ApplicationMode.PEDESTRIAN.ordinal()] = (ToggleButton) view.findViewById(R.id.PedestrianButton); + for (int i = 0; i < buttons.length; i++) { + if (buttons[i] != null) { + final int ind = i; + ToggleButton b = buttons[i]; + b.setChecked(appMode == ApplicationMode.values()[i]); + b.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + if (isChecked) { + for (int j = 0; j < buttons.length; j++) { + if (buttons[j] != null) { + if (buttons[j].isChecked() != (ind == j)) { + buttons[j].setChecked(ind == j); + } + } + } + } else { + // revert state + boolean revert = true; + for (int j = 0; j < buttons.length; j++) { + if (buttons[j] != null) { + if (buttons[j].isChecked()) { + revert = false; + break; + } + } + } + if (revert) { + buttons[ind].setChecked(true); + } + } + dlg.dismiss(); + } + }); + } + } + + scr.getDialog().setTitle(" " + title + " [" + osmandSettings.APPLICATION_MODE.get().toHumanString(this) + "]"); + scr.getDialog().setOnDismissListener(new OnDismissListener() { + @Override + public void onDismiss(DialogInterface dialog) { + osmandSettings.APPLICATION_MODE.set(appMode); + } + }); } else if (preference instanceof PreferenceScreen) { final PreferenceScreen scr = (PreferenceScreen) preference; title = scr.getTitle().toString(); diff --git a/OsmAnd/src/net/osmand/plus/development/OsmandDevelopmentPlugin.java b/OsmAnd/src/net/osmand/plus/development/OsmandDevelopmentPlugin.java index edd67649bd..a02d8438a2 100644 --- a/OsmAnd/src/net/osmand/plus/development/OsmandDevelopmentPlugin.java +++ b/OsmAnd/src/net/osmand/plus/development/OsmandDevelopmentPlugin.java @@ -1,5 +1,8 @@ package net.osmand.plus.development; +import java.text.SimpleDateFormat; + +import net.osmand.SunriseSunset; import net.osmand.plus.OptionsMenuHelper; import net.osmand.plus.OptionsMenuHelper.OnOptionsMenuClick; import net.osmand.plus.OsmandApplication; @@ -120,9 +123,19 @@ public class OsmandDevelopmentPlugin extends OsmandPlugin { pref.setSummary(activity.getString(R.string.native_app_allocated_memory_descr , mem.nativePrivateDirty / 1024, mem.dalvikPrivateDirty / 1024 , mem.otherPrivateDirty / 1024 , mem.nativePss / 1024, mem.dalvikPss / 1024 , mem.otherPss / 1024)); - pref.setKey("test_voice_commands"); cat.addPreference(pref); + SunriseSunset sunriseSunset = app.getDaynightHelper().getSunriseSunset(); + if (sunriseSunset != null) { + pref = new Preference(app); + pref.setTitle(R.string.day_night_info); + SimpleDateFormat prt = new SimpleDateFormat("dd.MM.yyyy HH:MM"); + pref.setSummary(activity.getString(R.string.day_night_info_description, prt.format(sunriseSunset.getSunrise()), + prt.format(sunriseSunset.getSunset()))); + cat.addPreference(pref); + } + + } }