mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-07-22 14:52:25 +02:00
Add support for Omnirom Omnijaws weather service.
This uses a contentobserver and doesn't do anything if it's not available. Should be feature equivalent to the LineageOS weather receiver.
This commit is contained in:
parent
64968d8a99
commit
888e7fa229
|
@ -21,6 +21,7 @@
|
|||
|
||||
<uses-permission android:name="cyanogenmod.permission.ACCESS_WEATHER_MANAGER" />
|
||||
<uses-permission android:name="cyanogenmod.permission.READ_WEATHER" />
|
||||
<uses-permission android:name="org.omnirom.omnijaws.READ_WEATHER" />
|
||||
|
||||
<uses-feature
|
||||
android:name="android.hardware.bluetooth"
|
||||
|
|
|
@ -0,0 +1,163 @@
|
|||
package nodomain.freeyourgadget.gadgetbridge.externalevents;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.PackageManager.NameNotFoundException;
|
||||
import android.database.ContentObserver;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.os.Handler;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.Weather;
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
|
||||
|
||||
|
||||
public class OmniJawsObserver extends ContentObserver {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(OmniJawsObserver.class);
|
||||
|
||||
private static final String SERVICE_PACKAGE = "org.omnirom.omnijaws";
|
||||
public static final Uri WEATHER_URI = Uri.parse("content://org.omnirom.omnijaws.provider/weather");
|
||||
private static final Uri SETTINGS_URI = Uri.parse("content://org.omnirom.omnijaws.provider/settings");
|
||||
|
||||
private Context mContext;
|
||||
private boolean mInstalled;
|
||||
private boolean mEnabled = false;
|
||||
private boolean mMetric = true;
|
||||
|
||||
|
||||
private final String[] WEATHER_PROJECTION = new String[]{
|
||||
"city",
|
||||
"condition", //unused, see below
|
||||
"condition_code",
|
||||
"temperature",
|
||||
"humidity",
|
||||
"forecast_low",
|
||||
"forecast_high",
|
||||
"forecast_condition",
|
||||
"forecast_condition_code",
|
||||
"time_stamp",
|
||||
"forecast_date"
|
||||
};
|
||||
|
||||
private final String[] SETTINGS_PROJECTION = new String[]{
|
||||
"enabled",
|
||||
"units"
|
||||
};
|
||||
|
||||
public OmniJawsObserver(Handler handler) throws NameNotFoundException {
|
||||
super(handler);
|
||||
mContext = GBApplication.getContext();
|
||||
mInstalled = isOmniJawsServiceAvailable();
|
||||
LOG.info("OmniJaws installation status: " + mInstalled);
|
||||
checkSettings();
|
||||
LOG.info("OmniJaws is enabled: " + mEnabled);
|
||||
if (!mEnabled) {
|
||||
throw new NameNotFoundException();
|
||||
}
|
||||
|
||||
updateWeather();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChange(boolean selfChange) {
|
||||
super.onChange(selfChange);
|
||||
LOG.info("Weather update received");
|
||||
checkSettings();
|
||||
if (!mEnabled) {
|
||||
LOG.info("Provider was disabled, ignoring.");
|
||||
return;
|
||||
}
|
||||
queryWeather();
|
||||
}
|
||||
|
||||
private void queryWeather() {
|
||||
Cursor c = mContext.getContentResolver().query(WEATHER_URI, WEATHER_PROJECTION, null, null, null);
|
||||
if (c != null) {
|
||||
try {
|
||||
|
||||
WeatherSpec weatherSpec = new WeatherSpec();
|
||||
weatherSpec.forecasts = new ArrayList<>();
|
||||
|
||||
int count = c.getCount();
|
||||
if (count > 0) {
|
||||
for (int i = 0; i < count; i++) {
|
||||
c.moveToPosition(i);
|
||||
if (i == 0) {
|
||||
|
||||
weatherSpec.location = c.getString(0);
|
||||
weatherSpec.currentConditionCode = Weather.mapToOpenWeatherMapCondition(c.getInt(2));
|
||||
weatherSpec.currentCondition = Weather.getConditionString(weatherSpec.currentConditionCode);
|
||||
//alternatively the following would also be possible
|
||||
//weatherSpec.currentCondition = c.getString(1);
|
||||
|
||||
weatherSpec.currentTemp = toKelvin(c.getFloat(3));
|
||||
weatherSpec.currentHumidity = (int) c.getFloat(4);
|
||||
weatherSpec.todayMinTemp = toKelvin(c.getFloat(5));
|
||||
weatherSpec.todayMaxTemp = toKelvin(c.getFloat(6));
|
||||
weatherSpec.timestamp = (int) (Long.valueOf(c.getString(9)) / 1000);
|
||||
} else {
|
||||
|
||||
WeatherSpec.Forecast gbForecast = new WeatherSpec.Forecast();
|
||||
gbForecast.minTemp = toKelvin(c.getFloat(5));
|
||||
gbForecast.maxTemp = toKelvin(c.getFloat(6));
|
||||
gbForecast.conditionCode = Weather.mapToOpenWeatherMapCondition(c.getInt(8));
|
||||
weatherSpec.forecasts.add(gbForecast);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Weather.getInstance().setWeatherSpec(weatherSpec);
|
||||
GBApplication.deviceService().onSendWeather(weatherSpec);
|
||||
|
||||
} finally {
|
||||
c.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void updateWeather() {
|
||||
Intent updateIntent = new Intent(Intent.ACTION_MAIN)
|
||||
.setClassName(SERVICE_PACKAGE, SERVICE_PACKAGE + ".WeatherService");
|
||||
updateIntent.setAction(SERVICE_PACKAGE + ".ACTION_UPDATE");
|
||||
mContext.startService(updateIntent);
|
||||
}
|
||||
|
||||
private boolean isOmniJawsServiceAvailable() throws NameNotFoundException {
|
||||
final PackageManager pm = mContext.getPackageManager();
|
||||
pm.getPackageInfo("org.omnirom.omnijaws", 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
private void checkSettings() {
|
||||
if (!mInstalled) {
|
||||
return;
|
||||
}
|
||||
final Cursor c = mContext.getContentResolver().query(SETTINGS_URI, SETTINGS_PROJECTION,
|
||||
null, null, null);
|
||||
if (c != null) {
|
||||
int count = c.getCount();
|
||||
if (count == 1) {
|
||||
c.moveToPosition(0);
|
||||
mEnabled = c.getInt(0) == 1 ? true : false;
|
||||
mMetric = c.getInt(1) == 0 ? true : false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private int toKelvin(double temperature) {
|
||||
if (mMetric) {
|
||||
return (int) (temperature + 273.15);
|
||||
}
|
||||
return (int) ((temperature - 32) * 0.5555555555555556D + 273.15);
|
||||
}
|
||||
|
||||
}
|
|
@ -30,6 +30,7 @@ import android.content.IntentFilter;
|
|||
import android.content.SharedPreferences;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Handler;
|
||||
import android.os.IBinder;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
|
@ -53,6 +54,7 @@ import nodomain.freeyourgadget.gadgetbridge.externalevents.BluetoothPairingReque
|
|||
import nodomain.freeyourgadget.gadgetbridge.externalevents.CMWeatherReceiver;
|
||||
import nodomain.freeyourgadget.gadgetbridge.externalevents.CalendarReceiver;
|
||||
import nodomain.freeyourgadget.gadgetbridge.externalevents.MusicPlaybackReceiver;
|
||||
import nodomain.freeyourgadget.gadgetbridge.externalevents.OmniJawsObserver;
|
||||
import nodomain.freeyourgadget.gadgetbridge.externalevents.PebbleReceiver;
|
||||
import nodomain.freeyourgadget.gadgetbridge.externalevents.PhoneCallReceiver;
|
||||
import nodomain.freeyourgadget.gadgetbridge.externalevents.SMSReceiver;
|
||||
|
@ -175,6 +177,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
|
|||
private AlarmReceiver mAlarmReceiver = null;
|
||||
private CalendarReceiver mCalendarReceiver = null;
|
||||
private CMWeatherReceiver mCMWeatherReceiver = null;
|
||||
private OmniJawsObserver mOmniJawsObserver = null;
|
||||
private Random mRandom = new Random();
|
||||
|
||||
private final String[] mMusicActions = {
|
||||
|
@ -588,7 +591,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
|
|||
LOG.info("Setting broadcast receivers to: " + enable);
|
||||
|
||||
if (enable && initialized && coordinator != null && coordinator.supportsCalendarEvents()) {
|
||||
if (mCalendarReceiver == null && getPrefs().getBoolean("enable_calendar_sync", true)) {
|
||||
if (mCalendarReceiver == null && getPrefs().getBoolean("enable_calendar_sync", true)) {
|
||||
if (!(GBApplication.isRunningMarshmallowOrLater() && ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) == PackageManager.PERMISSION_DENIED)) {
|
||||
IntentFilter calendarIntentFilter = new IntentFilter();
|
||||
calendarIntentFilter.addAction("android.intent.action.PROVIDER_CHANGED");
|
||||
|
@ -632,7 +635,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
|
|||
if (mMusicPlaybackReceiver == null) {
|
||||
mMusicPlaybackReceiver = new MusicPlaybackReceiver();
|
||||
IntentFilter filter = new IntentFilter();
|
||||
for (String action : mMusicActions){
|
||||
for (String action : mMusicActions) {
|
||||
filter.addAction(action);
|
||||
}
|
||||
registerReceiver(mMusicPlaybackReceiver, filter);
|
||||
|
@ -663,6 +666,14 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
|
|||
mCMWeatherReceiver = new CMWeatherReceiver();
|
||||
registerReceiver(mCMWeatherReceiver, new IntentFilter("GB_UPDATE_WEATHER"));
|
||||
}
|
||||
if (mOmniJawsObserver == null && coordinator != null && coordinator.supportsWeather()) {
|
||||
try {
|
||||
mOmniJawsObserver = new OmniJawsObserver(new Handler());
|
||||
getContentResolver().registerContentObserver(mOmniJawsObserver.WEATHER_URI, true, mOmniJawsObserver);
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
//Nothing wrong, it just means we're not running on omnirom.
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (mPhoneCallReceiver != null) {
|
||||
unregisterReceiver(mPhoneCallReceiver);
|
||||
|
@ -701,6 +712,9 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
|
|||
unregisterReceiver(mCMWeatherReceiver);
|
||||
mCMWeatherReceiver = null;
|
||||
}
|
||||
if (mOmniJawsObserver != null) {
|
||||
getContentResolver().unregisterContentObserver(mOmniJawsObserver);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user