1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-26 18:45:49 +01:00

Weather: refactoring and support forecast for more than one day

This commit is contained in:
Andreas Shimokawa 2017-11-29 23:57:36 +01:00
parent b4bbd0323c
commit 879272deb7
12 changed files with 209 additions and 129 deletions

View File

@ -54,8 +54,13 @@ public class AlarmReceiver extends BroadcastReceiver {
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, new Intent("DAILY_ALARM"), 0); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, new Intent("DAILY_ALARM"), 0);
AlarmManager am = (AlarmManager) (context.getSystemService(Context.ALARM_SERVICE)); AlarmManager am = (AlarmManager) (context.getSystemService(Context.ALARM_SERVICE));
if (am != null) {
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis() + 10000, AlarmManager.INTERVAL_DAY, pendingIntent); am.setInexactRepeating(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis() + 10000, AlarmManager.INTERVAL_DAY, pendingIntent);
} }
else {
LOG.warn("could not get alarm manager!");
}
}
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {

View File

@ -35,7 +35,7 @@ public class WeatherNotificationReceiver extends BroadcastReceiver {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (!intent.getAction().contains("WEATHER_UPDATE_2")) { if (intent.getAction() == null || !intent.getAction().contains("WEATHER_UPDATE_2")) {
LOG.info("Wrong action"); LOG.info("Wrong action");
return; return;
} }
@ -58,9 +58,7 @@ public class WeatherNotificationReceiver extends BroadcastReceiver {
weatherSpec.currentConditionCode = weather.currentConditionCode; weatherSpec.currentConditionCode = weather.currentConditionCode;
weatherSpec.todayMaxTemp = weather.todayHighTemp; weatherSpec.todayMaxTemp = weather.todayHighTemp;
weatherSpec.todayMinTemp = weather.todayLowTemp; weatherSpec.todayMinTemp = weather.todayLowTemp;
weatherSpec.tomorrowConditionCode = weather.forecastConditionCode; weatherSpec.forecasts = weather.forecasts;
weatherSpec.tomorrowMaxTemp = weather.forecastHighTemp;
weatherSpec.tomorrowMinTemp = weather.forecastLowTemp;
Weather.getInstance().setWeatherSpec(weatherSpec); Weather.getInstance().setWeatherSpec(weatherSpec);
GBApplication.deviceService().onSendWeather(weatherSpec); GBApplication.deviceService().onSendWeather(weatherSpec);
} }

View File

@ -373,16 +373,7 @@ public class GBDeviceService implements DeviceService {
@Override @Override
public void onSendWeather(WeatherSpec weatherSpec) { public void onSendWeather(WeatherSpec weatherSpec) {
Intent intent = createIntent().setAction(ACTION_SEND_WEATHER) Intent intent = createIntent().setAction(ACTION_SEND_WEATHER)
.putExtra(EXTRA_WEATHER_TIMESTAMP, weatherSpec.timestamp) .putExtra(EXTRA_WEATHER, weatherSpec);
.putExtra(EXTRA_WEATHER_LOCATION, weatherSpec.location)
.putExtra(EXTRA_WEATHER_CURRENTTEMP, weatherSpec.currentTemp)
.putExtra(EXTRA_WEATHER_CURRENTCONDITIONCODE, weatherSpec.currentConditionCode)
.putExtra(EXTRA_WEATHER_CURRENTCONDITION, weatherSpec.currentCondition)
.putExtra(EXTRA_WEATHER_TODAYMAXTEMP, weatherSpec.todayMaxTemp)
.putExtra(EXTRA_WEATHER_TODAYMINTEMP, weatherSpec.todayMinTemp)
.putExtra(EXTRA_WEATHER_TOMORROWMAXTEMP, weatherSpec.tomorrowMaxTemp)
.putExtra(EXTRA_WEATHER_TOMORROWMINTEMP, weatherSpec.tomorrowMinTemp)
.putExtra(EXTRA_WEATHER_TOMORROWCONDITIONCODE, weatherSpec.tomorrowConditionCode);
invokeService(intent); invokeService(intent);
} }

View File

@ -104,16 +104,7 @@ public interface DeviceService extends EventHandler {
String EXTRA_BOOLEAN_ENABLE = "enable_realtime_steps"; String EXTRA_BOOLEAN_ENABLE = "enable_realtime_steps";
String EXTRA_INTERVAL_SECONDS = "interval_seconds"; String EXTRA_INTERVAL_SECONDS = "interval_seconds";
String EXTRA_WEATHER_TIMESTAMP = "weather_timestamp"; String EXTRA_WEATHER = "weather";
String EXTRA_WEATHER_LOCATION = "weather_location";
String EXTRA_WEATHER_CURRENTTEMP = "weather_currenttemp";
String EXTRA_WEATHER_CURRENTCONDITIONCODE = "weather_currentconditioncode";
String EXTRA_WEATHER_CURRENTCONDITION = "currentcondition";
String EXTRA_WEATHER_TODAYMAXTEMP = "weather_todaymaxtemp";
String EXTRA_WEATHER_TODAYMINTEMP = "weather_todaymintemp";
String EXTRA_WEATHER_TOMORROWMAXTEMP = "weather_tomorrowmaxtemp";
String EXTRA_WEATHER_TOMORROWMINTEMP = "weather_tomorrowmintemp";
String EXTRA_WEATHER_TOMORROWCONDITIONCODE = "weather_tomorrowconditioncode";
/** /**
* Use EXTRA_REALTIME_SAMPLE instead * Use EXTRA_REALTIME_SAMPLE instead

View File

@ -16,8 +16,24 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. */ along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.model; package nodomain.freeyourgadget.gadgetbridge.model;
import android.os.Parcel;
import android.os.Parcelable;
import java.util.ArrayList;
// FIXME: document me and my fields, including units // FIXME: document me and my fields, including units
public class WeatherSpec { public class WeatherSpec implements Parcelable {
public static final Creator<WeatherSpec> CREATOR = new Creator<WeatherSpec>() {
@Override
public WeatherSpec createFromParcel(Parcel in) {
return new WeatherSpec(in);
}
@Override
public WeatherSpec[] newArray(int size) {
return new WeatherSpec[size];
}
};
public int timestamp; public int timestamp;
public String location; public String location;
public int currentTemp; public int currentTemp;
@ -25,7 +41,81 @@ public class WeatherSpec {
public String currentCondition; public String currentCondition;
public int todayMaxTemp; public int todayMaxTemp;
public int todayMinTemp; public int todayMinTemp;
public int tomorrowMaxTemp; public ArrayList<Forecast> forecasts = new ArrayList<>();
public int tomorrowMinTemp;
public int tomorrowConditionCode; public WeatherSpec() {
}
protected WeatherSpec(Parcel in) {
timestamp = in.readInt();
location = in.readString();
currentTemp = in.readInt();
currentConditionCode = in.readInt();
currentCondition = in.readString();
todayMaxTemp = in.readInt();
todayMinTemp = in.readInt();
in.readList(forecasts, Forecast.class.getClassLoader());
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(timestamp);
dest.writeString(location);
dest.writeInt(currentTemp);
dest.writeInt(currentConditionCode);
dest.writeString(currentCondition);
dest.writeInt(todayMaxTemp);
dest.writeInt(todayMinTemp);
dest.writeList(forecasts);
}
public static class Forecast implements Parcelable {
public static final Creator<Forecast> CREATOR = new Creator<Forecast>() {
@Override
public Forecast createFromParcel(Parcel in) {
return new Forecast(in);
}
@Override
public Forecast[] newArray(int size) {
return new Forecast[size];
}
};
public int minTemp;
public int maxTemp;
public int conditionCode;
public Forecast() {
}
public Forecast(int minTemp, int maxTemp, int conditionCode) {
this.minTemp = minTemp;
this.maxTemp = maxTemp;
this.conditionCode = conditionCode;
}
Forecast(Parcel in) {
minTemp = in.readInt();
maxTemp = in.readInt();
conditionCode = in.readInt();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(minTemp);
dest.writeInt(maxTemp);
dest.writeInt(conditionCode);
}
}
} }

View File

@ -50,6 +50,7 @@ import nodomain.freeyourgadget.gadgetbridge.externalevents.AlarmClockReceiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.AlarmReceiver; import nodomain.freeyourgadget.gadgetbridge.externalevents.AlarmReceiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.BluetoothConnectReceiver; import nodomain.freeyourgadget.gadgetbridge.externalevents.BluetoothConnectReceiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.BluetoothPairingRequestReceiver; import nodomain.freeyourgadget.gadgetbridge.externalevents.BluetoothPairingRequestReceiver;
//import nodomain.freeyourgadget.gadgetbridge.externalevents.CMWeatherReceiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.CalendarReceiver; import nodomain.freeyourgadget.gadgetbridge.externalevents.CalendarReceiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.MusicPlaybackReceiver; import nodomain.freeyourgadget.gadgetbridge.externalevents.MusicPlaybackReceiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.PebbleReceiver; import nodomain.freeyourgadget.gadgetbridge.externalevents.PebbleReceiver;
@ -149,16 +150,7 @@ import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_NOT
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_NOTIFICATION_TYPE; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_NOTIFICATION_TYPE;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_URI; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_URI;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_VIBRATION_INTENSITY; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_VIBRATION_INTENSITY;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_WEATHER_CURRENTCONDITION; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_WEATHER;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_WEATHER_CURRENTCONDITIONCODE;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_WEATHER_CURRENTTEMP;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_WEATHER_LOCATION;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_WEATHER_TIMESTAMP;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_WEATHER_TODAYMAXTEMP;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_WEATHER_TODAYMINTEMP;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_WEATHER_TOMORROWCONDITIONCODE;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_WEATHER_TOMORROWMAXTEMP;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_WEATHER_TOMORROWMINTEMP;
public class DeviceCommunicationService extends Service implements SharedPreferences.OnSharedPreferenceChangeListener { public class DeviceCommunicationService extends Service implements SharedPreferences.OnSharedPreferenceChangeListener {
private static final Logger LOG = LoggerFactory.getLogger(DeviceCommunicationService.class); private static final Logger LOG = LoggerFactory.getLogger(DeviceCommunicationService.class);
@ -182,6 +174,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
private AlarmReceiver mAlarmReceiver = null; private AlarmReceiver mAlarmReceiver = null;
private CalendarReceiver mCalendarReceiver = null; private CalendarReceiver mCalendarReceiver = null;
//private CMWeatherReceiver mCMWeatherReceiver = null;
private Random mRandom = new Random(); private Random mRandom = new Random();
private final String[] mMusicActions = { private final String[] mMusicActions = {
@ -214,7 +207,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
String action = intent.getAction(); String action = intent.getAction();
if (action.equals(GBDevice.ACTION_DEVICE_CHANGED)) { if (GBDevice.ACTION_DEVICE_CHANGED.equals(action)) {
GBDevice device = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE); GBDevice device = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE);
if (mGBDevice != null && mGBDevice.equals(device)) { if (mGBDevice != null && mGBDevice.equals(device)) {
mGBDevice = device; mGBDevice = device;
@ -540,18 +533,10 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
break; break;
} }
case ACTION_SEND_WEATHER: { case ACTION_SEND_WEATHER: {
WeatherSpec weatherSpec = new WeatherSpec(); WeatherSpec weatherSpec = intent.getParcelableExtra(EXTRA_WEATHER);
weatherSpec.timestamp = intent.getIntExtra(EXTRA_WEATHER_TIMESTAMP, 0); if (weatherSpec != null) {
weatherSpec.location = intent.getStringExtra(EXTRA_WEATHER_LOCATION);
weatherSpec.currentTemp = intent.getIntExtra(EXTRA_WEATHER_CURRENTTEMP, 0);
weatherSpec.currentConditionCode = intent.getIntExtra(EXTRA_WEATHER_CURRENTCONDITIONCODE, 0);
weatherSpec.currentCondition = intent.getStringExtra(EXTRA_WEATHER_CURRENTCONDITION);
weatherSpec.todayMaxTemp = intent.getIntExtra(EXTRA_WEATHER_TODAYMAXTEMP, 0);
weatherSpec.todayMinTemp = intent.getIntExtra(EXTRA_WEATHER_TODAYMINTEMP, 0);
weatherSpec.tomorrowMaxTemp = intent.getIntExtra(EXTRA_WEATHER_TOMORROWMAXTEMP, 0);
weatherSpec.tomorrowMinTemp = intent.getIntExtra(EXTRA_WEATHER_TOMORROWMINTEMP, 0);
weatherSpec.tomorrowConditionCode = intent.getIntExtra(EXTRA_WEATHER_TOMORROWCONDITIONCODE, 0);
mDeviceSupport.onSendWeather(weatherSpec); mDeviceSupport.onSendWeather(weatherSpec);
}
break; break;
} }
} }
@ -674,6 +659,12 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
filter.addAction(AlarmClockReceiver.ALARM_DONE_ACTION); filter.addAction(AlarmClockReceiver.ALARM_DONE_ACTION);
registerReceiver(mAlarmClockReceiver, filter); registerReceiver(mAlarmClockReceiver, filter);
} }
/*
if (mCMWeatherReceiver == null) {
mCMWeatherReceiver = new CMWeatherReceiver();
registerReceiver(mCMWeatherReceiver, new IntentFilter("HOURLY_ALARM"));
}
*/
} else { } else {
if (mPhoneCallReceiver != null) { if (mPhoneCallReceiver != null) {
unregisterReceiver(mPhoneCallReceiver); unregisterReceiver(mPhoneCallReceiver);
@ -708,6 +699,12 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
unregisterReceiver(mAlarmClockReceiver); unregisterReceiver(mAlarmClockReceiver);
mAlarmClockReceiver = null; mAlarmClockReceiver = null;
} }
/*
if (mCMWeatherReceiver != null) {
unregisterReceiver(mCMWeatherReceiver);
mCMWeatherReceiver = null;
}
*/
} }
} }
@ -725,8 +722,10 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
setDeviceSupport(null); setDeviceSupport(null);
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (nm != null) {
nm.cancel(GB.NOTIFICATION_ID); // need to do this because the updated notification won't be cancelled when service stops nm.cancel(GB.NOTIFICATION_ID); // need to do this because the updated notification won't be cancelled when service stops
} }
}
@Override @Override
public IBinder onBind(Intent intent) { public IBinder onBind(Intent intent) {

View File

@ -137,7 +137,7 @@ public class AmazfitBipSupport extends MiBand2Support {
supportsConditionString = true; supportsConditionString = true;
} }
final byte NR_DAYS = 2; final byte NR_DAYS = (byte) (1 + weatherSpec.forecasts.size());
int bytesPerDay = 4; int bytesPerDay = 4;
int conditionsLength = 0; int conditionsLength = 0;
if (supportsConditionString) { if (supportsConditionString) {
@ -164,15 +164,18 @@ public class AmazfitBipSupport extends MiBand2Support {
buf.put(weatherSpec.currentCondition.getBytes()); buf.put(weatherSpec.currentCondition.getBytes());
buf.put((byte) 0); // buf.put((byte) 0); //
} }
condition = HuamiWeatherConditions.mapToAmazfitBipWeatherCode(weatherSpec.tomorrowConditionCode);
for (WeatherSpec.Forecast forecast : weatherSpec.forecasts) {
condition = HuamiWeatherConditions.mapToAmazfitBipWeatherCode(forecast.conditionCode);
buf.put(condition); buf.put(condition);
buf.put(condition); buf.put(condition);
buf.put((byte) (weatherSpec.tomorrowMaxTemp - 273)); buf.put((byte) (forecast.maxTemp - 273));
buf.put((byte) (weatherSpec.tomorrowMinTemp - 273)); buf.put((byte) (forecast.minTemp - 273));
if (supportsConditionString) { if (supportsConditionString) {
buf.put((byte) 0); // not yet in weatherspec buf.put((byte) 0); // not yet in weatherspec
} }
}
builder.write(getCharacteristic(AmazfitBipService.UUID_CHARACTERISTIC_WEATHER), buf.array()); builder.write(getCharacteristic(AmazfitBipService.UUID_CHARACTERISTIC_WEATHER), buf.array());
builder.queue(getQueue()); builder.queue(getQueue());

View File

@ -141,7 +141,12 @@ class AppMessageHandlerTimeStylePebble extends AppMessageHandler {
pairs.add(new Pair<>(messageKeys.get("WeatherUseNightIcon"), (Object) (isNight ? 1 : 0))); pairs.add(new Pair<>(messageKeys.get("WeatherUseNightIcon"), (Object) (isNight ? 1 : 0)));
pairs.add(new Pair<>(messageKeys.get("WeatherTemperature"), (Object) (weatherSpec.currentTemp - 273))); pairs.add(new Pair<>(messageKeys.get("WeatherTemperature"), (Object) (weatherSpec.currentTemp - 273)));
pairs.add(new Pair<>(messageKeys.get("WeatherCondition"), (Object) (getIconForConditionCode(weatherSpec.currentConditionCode, isNight)))); pairs.add(new Pair<>(messageKeys.get("WeatherCondition"), (Object) (getIconForConditionCode(weatherSpec.currentConditionCode, isNight))));
pairs.add(new Pair<>(messageKeys.get("WeatherForecastCondition"), (Object) (getIconForConditionCode(weatherSpec.tomorrowConditionCode, isNight))));
if (weatherSpec.forecasts.size() > 0) {
WeatherSpec.Forecast tomorrow = weatherSpec.forecasts.get(0);
pairs.add(new Pair<>(messageKeys.get("WeatherForecastCondition"), (Object) (getIconForConditionCode(tomorrow.conditionCode, isNight))));
}
pairs.add(new Pair<>(messageKeys.get("WeatherForecastHighTemp"), (Object) (weatherSpec.todayMaxTemp - 273))); pairs.add(new Pair<>(messageKeys.get("WeatherForecastHighTemp"), (Object) (weatherSpec.todayMaxTemp - 273)));
pairs.add(new Pair<>(messageKeys.get("WeatherForecastLowTemp"), (Object) (weatherSpec.todayMinTemp - 273))); pairs.add(new Pair<>(messageKeys.get("WeatherForecastLowTemp"), (Object) (weatherSpec.todayMinTemp - 273)));

View File

@ -1227,8 +1227,16 @@ public class PebbleProtocol extends GBDeviceProtocol {
short currentTemp = (short) (weatherSpec.currentTemp - 273); short currentTemp = (short) (weatherSpec.currentTemp - 273);
short todayMax = (short) (weatherSpec.todayMaxTemp - 273); short todayMax = (short) (weatherSpec.todayMaxTemp - 273);
short todayMin = (short) (weatherSpec.todayMinTemp - 273); short todayMin = (short) (weatherSpec.todayMinTemp - 273);
short tomorrowMax = (short) (weatherSpec.tomorrowMaxTemp - 273); short tomorrowMax = 0;
short tomorrowMin = (short) (weatherSpec.tomorrowMinTemp - 273); short tomorrowMin = 0;
int tomorrowConditionCode = 0;
if (weatherSpec.forecasts.size() > 0) {
WeatherSpec.Forecast tomorrow = weatherSpec.forecasts.get(0);
tomorrowMax = (short) (tomorrow.maxTemp - 273);
tomorrowMin = (short) (tomorrow.minTemp - 273);
tomorrowConditionCode = tomorrow.conditionCode;
}
String units = GBApplication.getPrefs().getString(SettingsActivity.PREF_MEASUREMENT_SYSTEM, GBApplication.getContext().getString(R.string.p_unit_metric)); String units = GBApplication.getPrefs().getString(SettingsActivity.PREF_MEASUREMENT_SYSTEM, GBApplication.getContext().getString(R.string.p_unit_metric));
if (units.equals(GBApplication.getContext().getString(R.string.p_unit_imperial))) { if (units.equals(GBApplication.getContext().getString(R.string.p_unit_imperial))) {
currentTemp = (short) (currentTemp * 1.8f + 32); currentTemp = (short) (currentTemp * 1.8f + 32);
@ -1261,7 +1269,7 @@ public class PebbleProtocol extends GBDeviceProtocol {
buf.put(Weather.mapToPebbleCondition(weatherSpec.currentConditionCode)); buf.put(Weather.mapToPebbleCondition(weatherSpec.currentConditionCode));
buf.putShort(todayMax); buf.putShort(todayMax);
buf.putShort(todayMin); buf.putShort(todayMin);
buf.put(Weather.mapToPebbleCondition(weatherSpec.tomorrowConditionCode)); buf.put(Weather.mapToPebbleCondition(tomorrowConditionCode));
buf.putShort(tomorrowMax); buf.putShort(tomorrowMax);
buf.putShort(tomorrowMin); buf.putShort(tomorrowMin);
buf.putInt(weatherSpec.timestamp); buf.putInt(weatherSpec.timestamp);

View File

@ -30,13 +30,16 @@ import org.slf4j.LoggerFactory;
import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs; import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
class CurrentPosition { public class CurrentPosition {
private static final Logger LOG = LoggerFactory.getLogger(CurrentPosition.class); private static final Logger LOG = LoggerFactory.getLogger(CurrentPosition.class);
private Location lastKnownLocation;
private float latitude, longitude;
long timestamp; long timestamp;
double altitude; double altitude;
float latitude, longitude, accuracy, speed; float accuracy, speed;
float getLatitude() { float getLatitude() {
return latitude; return latitude;
@ -46,10 +49,19 @@ class CurrentPosition {
return longitude; return longitude;
} }
CurrentPosition() { public Location getLastKnownLocation() {
return lastKnownLocation;
}
public CurrentPosition() {
Prefs prefs = GBApplication.getPrefs(); Prefs prefs = GBApplication.getPrefs();
this.latitude = prefs.getFloat("location_latitude", 0); this.latitude = prefs.getFloat("location_latitude", 0);
this.longitude = prefs.getFloat("location_longitude", 0); this.longitude = prefs.getFloat("location_longitude", 0);
lastKnownLocation = new Location("preferences");
lastKnownLocation.setLatitude(this.latitude);
lastKnownLocation.setLongitude(this.longitude);
LOG.info("got longitude/latitude from preferences: " + latitude + "/" + longitude); LOG.info("got longitude/latitude from preferences: " + latitude + "/" + longitude);
this.timestamp = System.currentTimeMillis() - 86400000; //let accessor know this value is really old this.timestamp = System.currentTimeMillis() - 86400000; //let accessor know this value is really old
@ -58,10 +70,14 @@ class CurrentPosition {
prefs.getBoolean("use_updated_location_if_available", false)) { prefs.getBoolean("use_updated_location_if_available", false)) {
LocationManager locationManager = (LocationManager) GBApplication.getContext().getSystemService(Context.LOCATION_SERVICE); LocationManager locationManager = (LocationManager) GBApplication.getContext().getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria(); Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, false); String provider = null;
if (locationManager != null) {
provider = locationManager.getBestProvider(criteria, false);
}
if (provider != null) { if (provider != null) {
Location lastKnownLocation = locationManager.getLastKnownLocation(provider); Location lastKnownLocation = locationManager.getLastKnownLocation(provider);
if (lastKnownLocation != null) { if (lastKnownLocation != null) {
this.lastKnownLocation = lastKnownLocation;
this.timestamp = lastKnownLocation.getTime(); this.timestamp = lastKnownLocation.getTime();
this.timestamp = System.currentTimeMillis() - 1000; //TODO: request updating the location and don't fake its age this.timestamp = System.currentTimeMillis() - 1000; //TODO: request updating the location and don't fake its age

View File

@ -227,8 +227,8 @@ public class JSInterface {
geoPosition.put("timestamp", currentPosition.timestamp); geoPosition.put("timestamp", currentPosition.timestamp);
coords.put("latitude", currentPosition.latitude); coords.put("latitude", currentPosition.getLatitude());
coords.put("longitude", currentPosition.longitude); coords.put("longitude", currentPosition.getLongitude());
coords.put("accuracy", currentPosition.accuracy); coords.put("accuracy", currentPosition.accuracy);
coords.put("altitude", currentPosition.altitude); coords.put("altitude", currentPosition.altitude);
coords.put("speed", currentPosition.speed); coords.put("speed", currentPosition.speed);

View File

@ -26,7 +26,10 @@ import org.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import nodomain.freeyourgadget.gadgetbridge.model.Weather; import nodomain.freeyourgadget.gadgetbridge.model.Weather;
import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
public class ParcelableWeather2 implements Parcelable { public class ParcelableWeather2 implements Parcelable {
private static final Logger LOG = LoggerFactory.getLogger(ParcelableWeather2.class); private static final Logger LOG = LoggerFactory.getLogger(ParcelableWeather2.class);
@ -42,12 +45,9 @@ public class ParcelableWeather2 implements Parcelable {
private String[] currentConditionType = null; private String[] currentConditionType = null;
public int currentConditionCode = 3200; public int currentConditionCode = 3200;
private String[] forecastConditionType = null;
public int forecastConditionCode = 3200;
public int todayLowTemp = 0; public int todayLowTemp = 0;
public int todayHighTemp = 0; public int todayHighTemp = 0;
public int forecastLowTemp = 0; public ArrayList<WeatherSpec.Forecast> forecasts = new ArrayList<>();
public int forecastHighTemp = 0;
public JSONObject reconstructedWeather = null; public JSONObject reconstructedWeather = null;
public JSONObject reconstructedForecast = null; public JSONObject reconstructedForecast = null;
@ -98,21 +98,23 @@ public class ParcelableWeather2 implements Parcelable {
e.printStackTrace(); e.printStackTrace();
} }
LOG.debug("Weather JSON for WEBVIEW: " + reconstructedWeather.toString()); LOG.debug("Weather JSON for WEBVIEW: " + reconstructedWeather.toString());
//fetch immediate next forecast //fetch forecasts
if (--conditions > 0) { int timeOffset = 0;
int timeOffset = 86400000; //manually determined
reconstructedForecast = new JSONObject();
JSONArray list = new JSONArray(); JSONArray list = new JSONArray();
JSONObject city = new JSONObject(); JSONObject city = new JSONObject();
while (--conditions > 0) {
timeOffset += 86400000; //manually determined
JSONObject item = new JSONObject(); JSONObject item = new JSONObject();
condition = new JSONObject(); condition = new JSONObject();
main = new JSONObject(); main = new JSONObject();
weather = new JSONArray(); weather = new JSONArray();
Bundle forecastBundle = in.readBundle(); Bundle forecastBundle = in.readBundle();
forecastConditionType = forecastBundle.getStringArray("weather_condition_types"); String[] forecastConditionType = forecastBundle.getStringArray("weather_condition_types");
forecastConditionCode = weatherConditionTypesToOpenWeatherMapIds(forecastConditionType[0]); int forecastConditionCode = weatherConditionTypesToOpenWeatherMapIds(forecastConditionType[0]);
forecastLowTemp = forecastBundle.getInt("weather_low_temp"); int forecastLowTemp = forecastBundle.getInt("weather_low_temp");
forecastHighTemp = forecastBundle.getInt("weather_high_temp"); int forecastHighTemp = forecastBundle.getInt("weather_high_temp");
forecasts.add(new WeatherSpec.Forecast(forecastLowTemp, forecastHighTemp, forecastConditionCode));
try { try {
condition.put("id", forecastConditionCode); condition.put("id", forecastConditionCode);
condition.put("main", forecastBundle.getString("weather_condition_text")); condition.put("main", forecastBundle.getString("weather_condition_text"));
@ -125,60 +127,31 @@ public class ParcelableWeather2 implements Parcelable {
main.put("temp_max", forecastHighTemp); main.put("temp_max", forecastHighTemp);
//forecast //forecast
item.put("dt", (time / 1000) + timeOffset);
item.put("main", main);
item.put("weather", weather);
list.put(item);
} catch (JSONException e) {
LOG.error("error while construction JSON", e);
}
}
try {
//"city":{"id":3181913,"name":"Bolzano","coord":{"lat":46.4927,"lon":11.3336},"country":"IT"} //"city":{"id":3181913,"name":"Bolzano","coord":{"lat":46.4927,"lon":11.3336},"country":"IT"}
city.put("name", location); city.put("name", location);
city.put("country", "World"); city.put("country", "World");
reconstructedForecast = new JSONObject();
reconstructedForecast.put("city", city); reconstructedForecast.put("city", city);
item.put("dt", (time / 1000) + timeOffset);
item.put("main", main);
item.put("weather", weather);
list.put(item);
} catch (JSONException e) {
e.printStackTrace();
}
// get the rest
while (--conditions > 0) {
conditionBundle = in.readBundle();
conditionBundle.getString("weather_condition_text");
weatherConditionTypesToOpenWeatherMapIds(conditionBundle.getStringArray("weather_condition_types")[0]);
conditionBundle.getInt("weather_current_temp");
item = new JSONObject();
condition = new JSONObject();
main = new JSONObject();
weather = new JSONArray();
timeOffset += 86400000;
try {
condition.put("id", weatherConditionTypesToOpenWeatherMapIds(conditionBundle.getStringArray("weather_condition_types")[0]));
condition.put("main", conditionBundle.getString("weather_condition_text"));
condition.put("icon", Weather.mapToOpenWeatherMapIcon(weatherConditionTypesToOpenWeatherMapIds(conditionBundle.getStringArray("weather_condition_types")[0])));
weather.put(condition);
main.put("temp", conditionBundle.getInt("weather_current_temp"));
main.put("humidity", conditionBundle.getInt("weather_humidity_value"));
main.put("temp_min", conditionBundle.getInt("weather_low_temp"));
main.put("temp_max", conditionBundle.getInt("weather_high_temp"));
item.put("dt", (time / 1000) + timeOffset);
item.put("main", main);
item.put("weather", weather);
list.put(item);
} catch (JSONException e) {
e.printStackTrace();
}
}
try {
reconstructedForecast.put("cnt", list.length()); reconstructedForecast.put("cnt", list.length());
reconstructedForecast.put("list", list); reconstructedForecast.put("list", list);
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); LOG.error("error while construction JSON", e);
} }
LOG.debug("Forecast JSON for WEBVIEW: " + reconstructedForecast.toString()); LOG.debug("Forecast JSON for WEBVIEW: " + reconstructedForecast.toString());
} }
} }
}
public static final Creator<ParcelableWeather2> CREATOR = new Creator<ParcelableWeather2>() { public static final Creator<ParcelableWeather2> CREATOR = new Creator<ParcelableWeather2>() {
@Override @Override
@ -309,4 +282,5 @@ public class ParcelableWeather2 implements Parcelable {
} }
return 3200; return 3200;
} }
} }