1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-28 12:56:49 +01:00

Added GenericWeatherProvider, shows "Activate" and "Deactivate" regardless of if "Weather Notification" is installed

Using objects instead of primitives, reading from correct JSON

Added unregisterReceiver for GenericWeatherReceiver

Added GenericWeatherReceiver to manifest
This commit is contained in:
TylerWilliamson 2022-08-21 20:30:13 -04:00 committed by Gitea
parent cc27409726
commit a86894b052
4 changed files with 122 additions and 2 deletions

View File

@ -366,6 +366,14 @@
android:name=".devices.pinetime.PineTimeDFUService"
android:label="PineTime Nordic DFU service" />
<receiver
android:name=".externalevents.GenericWeatherReceiver"
android:enabled="true">
<intent-filter>
<action android:name="nodomain.freeyourgadget.gadgetbridge.ACTION_GENERIC_WEATHER" />
</intent-filter>
</receiver>
<receiver
android:name=".externalevents.WeatherNotificationReceiver"
android:enabled="true">

View File

@ -441,8 +441,8 @@ public abstract class AbstractAppManagerFragment extends Fragment {
pm.getPackageInfo("ru.gelin.android.weather.notification", PackageManager.GET_ACTIVITIES);
menu.removeItem(R.id.appmanager_weather_install_provider);
} catch (PackageManager.NameNotFoundException e) {
menu.removeItem(R.id.appmanager_weather_activate);
menu.removeItem(R.id.appmanager_weather_deactivate);
//menu.removeItem(R.id.appmanager_weather_activate);
//menu.removeItem(R.id.appmanager_weather_deactivate);
}
}

View File

@ -0,0 +1,102 @@
/* Copyright (C) 2020 Andreas Shimokawa
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.externalevents;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.model.Weather;
import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
public class GenericWeatherReceiver extends BroadcastReceiver {
public final static String ACTION_GENERIC_WEATHER = "nodomain.freeyourgadget.gadgetbridge.ACTION_GENERIC_WEATHER";
public final static String EXTRA_WEATHER_JSON = "WeatherJson";
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null && ACTION_GENERIC_WEATHER.equals(intent.getAction())) {
Bundle bundle = intent.getExtras();
if (bundle != null && bundle.containsKey(EXTRA_WEATHER_JSON)) {
try {
JSONObject weatherJson = new JSONObject(bundle.getString(EXTRA_WEATHER_JSON));
WeatherSpec weatherSpec = new WeatherSpec();
weatherSpec.timestamp = safelyGet(weatherJson, Integer.class, "timestamp", (int) (System.currentTimeMillis() / 1000));
weatherSpec.location = safelyGet(weatherJson, String.class, "location", "");
weatherSpec.currentTemp = safelyGet(weatherJson, Integer.class, "currentTemp", 0);
weatherSpec.todayMinTemp = safelyGet(weatherJson, Integer.class, "todayMinTemp", 0);
weatherSpec.todayMaxTemp = safelyGet(weatherJson, Integer.class, "todayMaxTemp", 0);
weatherSpec.currentCondition = safelyGet(weatherJson, String.class, "currentCondition", "");
weatherSpec.currentConditionCode = safelyGet(weatherJson, Integer.class, "currentConditionCode", 0);
weatherSpec.currentHumidity = safelyGet(weatherJson, Integer.class, "currentHumidity", 0);
weatherSpec.windSpeed = safelyGet(weatherJson, Float.class, "windSpeed", 0f);
weatherSpec.windDirection = safelyGet(weatherJson, Integer.class, "windDirection", 0);
if (weatherJson.has("forecasts")) {
JSONArray forecastArray = weatherJson.getJSONArray("forecasts");
weatherSpec.forecasts = new ArrayList<>();
for (int i = 0, l = forecastArray.length(); i < l; i++) {
JSONObject forecastJson = forecastArray.getJSONObject(i);
WeatherSpec.Forecast forecast = new WeatherSpec.Forecast();
forecast.conditionCode = safelyGet(forecastJson, Integer.class, "conditionCode", 0);
forecast.humidity = safelyGet(forecastJson, Integer.class, "humidity", 0);
forecast.maxTemp = safelyGet(forecastJson, Integer.class, "maxTemp", 0);
forecast.minTemp = safelyGet(forecastJson, Integer.class, "minTemp", 0);
weatherSpec.forecasts.add(forecast);
}
}
Weather.getInstance().setWeatherSpec(weatherSpec);
GBApplication.deviceService().onSendWeather(weatherSpec);
} catch (Exception e) {
GB.toast("Gadgetbridge received broken or incompatible weather data", Toast.LENGTH_SHORT, GB.ERROR, e);
}
}
}
}
private <T> T safelyGet(JSONObject jsonObject, Class<T> tClass, String name, T defaultValue) {
try {
if (jsonObject.has(name)) {
Object value = jsonObject.get(name);
if (tClass.isInstance(value)) {
return (T) value;
}
}
} catch (Exception e) {
//
}
return defaultValue;
}
}

View File

@ -62,6 +62,7 @@ import nodomain.freeyourgadget.gadgetbridge.externalevents.BluetoothPairingReque
import nodomain.freeyourgadget.gadgetbridge.externalevents.CMWeatherReceiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.CalendarReceiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.DeviceSettingsReceiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.GenericWeatherReceiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.LineageOsWeatherReceiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.MusicPlaybackReceiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.OmniJawsObserver;
@ -327,6 +328,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
private CMWeatherReceiver mCMWeatherReceiver = null;
private LineageOsWeatherReceiver mLineageOsWeatherReceiver = null;
private TinyWeatherForecastGermanyReceiver mTinyWeatherForecastGermanyReceiver = null;
private GenericWeatherReceiver mGenericWeatherReceiver = null;
private OmniJawsObserver mOmniJawsObserver = null;
private final DeviceSettingsReceiver deviceSettingsReceiver = new DeviceSettingsReceiver();
@ -1188,6 +1190,10 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
mTinyWeatherForecastGermanyReceiver = new TinyWeatherForecastGermanyReceiver();
registerReceiver(mTinyWeatherForecastGermanyReceiver, new IntentFilter("de.kaffeemitkoffein.broadcast.WEATHERDATA"));
}
if (mGenericWeatherReceiver == null) {
mGenericWeatherReceiver = new GenericWeatherReceiver();
registerReceiver(mGenericWeatherReceiver, new IntentFilter(GenericWeatherReceiver.ACTION_GENERIC_WEATHER));
}
if (mOmniJawsObserver == null) {
try {
mOmniJawsObserver = new OmniJawsObserver(new Handler());
@ -1253,6 +1259,10 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
unregisterReceiver(mGBAutoFetchReceiver);
mGBAutoFetchReceiver = null;
}
if (mGenericWeatherReceiver != null) {
unregisterReceiver(mGenericWeatherReceiver);
mGenericWeatherReceiver = null;
}
}
}