Fix sunrise/sunset for longitude < 0

This commit is contained in:
Victor Shcherb 2012-08-22 19:05:30 +02:00
parent 1cd15df201
commit d1d190b338
4 changed files with 89 additions and 11 deletions

View file

@ -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
-->
<string name="monitor_preferences">Tracking</string>
<string name="day_night_info_description">Sunrise : %1$s \nSunset : %2$s</string>
<string name="day_night_info">Day/night info</string>
<string name="map_widget_day_night">Day/night map</string>
<string name="map_widget_vector_attributes">Rendering attributes:</string>
<string name="map_widget_renderer">Rendering style</string>

View file

@ -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;
}
}

View file

@ -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();

View file

@ -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);
}
}
}