-
Notifications
You must be signed in to change notification settings - Fork 338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable Gadgetbridge support. #680
Open
KamaleiZestri
wants to merge
2
commits into
martykan:master
Choose a base branch
from
KamaleiZestri:to-gadgetbridge
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
app/src/main/java/cz/martykan/forecastie/Broadcaster.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package cz.martykan.forecastie; | ||
|
||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.SharedPreferences; | ||
import android.preference.PreferenceManager; | ||
import android.widget.Toast; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Calendar; | ||
import java.util.Date; | ||
import java.util.List; | ||
|
||
import cz.martykan.forecastie.models.Weather; | ||
import cz.martykan.forecastie.utils.UnitConvertor; | ||
import cz.martykan.forecastie.weatherapi.WeatherStorage; | ||
import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec; | ||
|
||
public class Broadcaster { | ||
public static void sendWeather(Context context) { | ||
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); | ||
boolean run = sp.getBoolean("sendToGadgetbridge", false); | ||
|
||
if(run) { | ||
WeatherStorage storage = new WeatherStorage(context); | ||
|
||
Intent intent = new Intent(); | ||
intent.setAction("de.kaffeemitkoffein.broadcast.WEATHERDATA"); | ||
intent.setPackage("nodomain.freeyourgadget.gadgetbridge"); | ||
intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); | ||
intent.putExtra("WeatherSpec", generateWeatherSpec(storage)); | ||
context.sendBroadcast(intent); | ||
} | ||
} | ||
|
||
private static WeatherSpec generateWeatherSpec(WeatherStorage storage) { | ||
WeatherSpec spec = new WeatherSpec(); | ||
Weather weather = storage.getLastToday(); | ||
List<Weather> multi = storage.getLastLongTerm(); | ||
List<WeatherSpec.Forecast> forecasts = generateForecasts(weather, multi); | ||
|
||
spec.timestamp = (int) weather.getLastUpdated(); | ||
spec.location = weather.getCity() + ", " +weather.getCountry(); | ||
spec.currentTemp = (int) weather.getTemperature(); //Gadgetbridge does conversion. slight loss in accuracy here. | ||
spec.currentConditionCode = weather.getWeatherId(); | ||
spec.currentCondition = weather.getDescription(); | ||
spec.currentHumidity = weather.getHumidity(); | ||
spec.todayMaxTemp = forecasts.get(0).maxTemp; | ||
spec.todayMinTemp = forecasts.get(0).minTemp; | ||
spec.windSpeed = (float) weather.getWind(); //km per hour. | ||
spec.windDirection = weather.getWindDirectionDegree().intValue(); | ||
for (int i=1;i<forecasts.size();i++) | ||
spec.forecasts.add(forecasts.get(i)); | ||
|
||
return spec; | ||
} | ||
|
||
private static List<WeatherSpec.Forecast> generateForecasts(Weather weather, List<Weather> manyWeathers) { | ||
List<WeatherSpec.Forecast> forecasts = new ArrayList<WeatherSpec.Forecast>(); | ||
manyWeathers.add(0,weather); | ||
List<Weather> allWeathers = manyWeathers; | ||
|
||
//seperate weathers out based on day. assuming already ordered, no further sorting based on day is needed. | ||
List<List<Integer>> sortedWeathers = new ArrayList<List<Integer>>(); | ||
Calendar currCal; | ||
Calendar currDate; | ||
int count=0; | ||
|
||
while(count<allWeathers.size()) { | ||
currCal = getWeatherCalendar(allWeathers.get(count)); | ||
sortedWeathers.add(new ArrayList<Integer>()); | ||
|
||
for(int i=0;i<allWeathers.size();i++) { | ||
currDate = getWeatherCalendar(allWeathers.get(i)); | ||
if (currCal.get(Calendar.DATE) == currDate.get(Calendar.DATE)) { | ||
sortedWeathers.get(sortedWeathers.size() - 1).add(i); | ||
count++; | ||
} | ||
} | ||
} | ||
|
||
//2. get the 4 values based on day. min/max temp, average humidity, and weather cond. | ||
for(List<Integer> dayNumber : sortedWeathers) | ||
{ | ||
ArrayList<Double> temps = new ArrayList<Double>(); | ||
int totalHumid = 0; | ||
|
||
for (Integer occ : dayNumber) { | ||
temps.add(allWeathers.get(occ).getTemperature()); | ||
totalHumid += allWeathers.get(occ).getHumidity(); | ||
} | ||
|
||
int humidity = totalHumid / dayNumber.size(); | ||
//TODO also the option to take mode of weather conditions for the day instead of just getting mid value | ||
int condition = allWeathers.get(dayNumber.get(dayNumber.size() / 2)).getWeatherId(); | ||
double maxTemp = temps.get(0); | ||
double minTemp = temps.get(0); | ||
|
||
for(double temp : temps) | ||
{ | ||
if(temp < minTemp) | ||
minTemp = temp; | ||
if(temp > maxTemp) | ||
maxTemp = temp; | ||
} | ||
|
||
WeatherSpec.Forecast forecast = new WeatherSpec.Forecast( | ||
(int) minTemp, | ||
(int) maxTemp, | ||
condition, | ||
humidity); | ||
forecasts.add(forecast); | ||
} | ||
return forecasts; | ||
} | ||
//method from LongTermWeatherList | ||
private static Calendar getWeatherCalendar(Weather weather) { | ||
Calendar weatherCalendar = Calendar.getInstance(); | ||
weatherCalendar.setTimeInMillis(weather.getDate().getTime()); | ||
|
||
return weatherCalendar; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/WeatherSpec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
/* Copyright (C) 2016-2020 Andreas Shimokawa, Carsten Pfeiffer, Daniele | ||
Gobbetti | ||
|
||
This file is part of Gadgetbridge. | ||
|
||
Gadgetbridge is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Affero General Public License as published | ||
by the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
|
||
Gadgetbridge is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Affero General Public License for more details. | ||
|
||
You should have received a copy of the GNU Affero General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. */ | ||
|
||
package nodomain.freeyourgadget.gadgetbridge.model; | ||
|
||
import android.os.Parcel; | ||
import android.os.Parcelable; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
|
||
// FIXME: document me and my fields, including units | ||
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 static final int VERSION = 2; | ||
public int timestamp; | ||
public String location; | ||
public int currentTemp; | ||
public int currentConditionCode = 3200; | ||
public String currentCondition; | ||
public int currentHumidity; | ||
public int todayMaxTemp; | ||
public int todayMinTemp; | ||
public float windSpeed; //km per hour | ||
public int windDirection; //deg | ||
|
||
public ArrayList<Forecast> forecasts = new ArrayList<>(); | ||
|
||
public WeatherSpec() { | ||
|
||
} | ||
|
||
// Lower bounds of beaufort regions 1 to 12 | ||
// Values from https://en.wikipedia.org/wiki/Beaufort_scale | ||
static final float[] beaufort = new float[] { 2, 6, 12, 20, 29, 39, 50, 62, 75, 89, 103, 118 }; | ||
// level: 0 1 2 3 4 5 6 7 8 9 10 11 12 | ||
|
||
public int windSpeedAsBeaufort() { | ||
int l = 0; | ||
while (l < beaufort.length && beaufort[l] < this.windSpeed) { | ||
l++; | ||
} | ||
return l; | ||
} | ||
|
||
protected WeatherSpec(Parcel in) { | ||
int version = in.readInt(); | ||
if (version == VERSION) { | ||
timestamp = in.readInt(); | ||
location = in.readString(); | ||
currentTemp = in.readInt(); | ||
currentConditionCode = in.readInt(); | ||
currentCondition = in.readString(); | ||
currentHumidity = in.readInt(); | ||
todayMaxTemp = in.readInt(); | ||
todayMinTemp = in.readInt(); | ||
windSpeed = in.readFloat(); | ||
windDirection = in.readInt(); | ||
in.readList(forecasts, Forecast.class.getClassLoader()); | ||
} | ||
} | ||
|
||
@Override | ||
public int describeContents() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public void writeToParcel(Parcel dest, int flags) { | ||
dest.writeInt(VERSION); | ||
dest.writeInt(timestamp); | ||
dest.writeString(location); | ||
dest.writeInt(currentTemp); | ||
dest.writeInt(currentConditionCode); | ||
dest.writeString(currentCondition); | ||
dest.writeInt(currentHumidity); | ||
dest.writeInt(todayMaxTemp); | ||
dest.writeInt(todayMinTemp); | ||
dest.writeFloat(windSpeed); | ||
dest.writeInt(windDirection); | ||
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 int humidity; | ||
|
||
public Forecast() { | ||
} | ||
|
||
public Forecast(int minTemp, int maxTemp, int conditionCode, int humidity) { | ||
this.minTemp = minTemp; | ||
this.maxTemp = maxTemp; | ||
this.conditionCode = conditionCode; | ||
this.humidity = humidity; | ||
} | ||
|
||
Forecast(Parcel in) { | ||
minTemp = in.readInt(); | ||
maxTemp = in.readInt(); | ||
conditionCode = in.readInt(); | ||
humidity = 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); | ||
dest.writeInt(humidity); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wondering if this should be configurable, since there are a few different flavors now (eg. nightly, bangle.js).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense also for future compatibility. It is done.