wangzhibo
4 天以前 ae6f40460dcd56af6c5f60ba52c883854c3bac55
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*  Copyright (C) 2017-2024 Carsten Pfeiffer, Daniele Gobbetti, José Rebelo
 
    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 <https://www.gnu.org/licenses/>. */
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 java.util.Arrays;
import java.util.Collections;
 
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",
            "wind_speed",
            "wind_direction"
    };
 
    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.windSpeed = toKmh(c.getFloat(11));
                            weatherSpec.windDirection = c.getInt(12);
                            weatherSpec.timestamp = (int) (Long.parseLong(c.getString(9)) / 1000);
                        } else if (i == 1) {
                            weatherSpec.todayMinTemp = toKelvin(c.getFloat(5));
                            weatherSpec.todayMaxTemp = toKelvin(c.getFloat(6));
                        } else {
 
                            WeatherSpec.Daily gbForecast = new WeatherSpec.Daily();
                            gbForecast.minTemp = toKelvin(c.getFloat(5));
                            gbForecast.maxTemp = toKelvin(c.getFloat(6));
                            gbForecast.conditionCode = Weather.mapToOpenWeatherMapCondition(c.getInt(8));
                            weatherSpec.forecasts.add(gbForecast);
                        }
                    }
                }
 
                ArrayList<WeatherSpec> weatherSpecs = new ArrayList<>(Collections.singletonList(weatherSpec));
                Weather.getInstance().setWeatherSpec(weatherSpecs);
                GBApplication.deviceService().onSendWeather(weatherSpecs);
 
            } 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);
    }
 
    private float toKmh(float speed) {
        if (mMetric) {
            return speed;
        }
        return (speed * 1.61f);
    }
}