16
16
import androidx .core .location .LocationManagerCompat ;
17
17
import androidx .localbroadcastmanager .content .LocalBroadcastManager ;
18
18
19
+ import com .google .gson .JsonArray ;
20
+ import com .google .gson .JsonObject ;
21
+ import com .google .gson .JsonParser ;
22
+ import com .google .gson .JsonSyntaxException ;
23
+
24
+ import java .io .IOException ;
19
25
import java .lang .ref .WeakReference ;
26
+ import java .util .Locale ;
20
27
import java .util .concurrent .Executors ;
21
28
29
+ import foundation .e .blisslauncher .R ;
22
30
import foundation .e .blisslauncher .core .Preferences ;
23
31
import lineageos .weather .LineageWeatherManager ;
24
32
import lineageos .weather .WeatherInfo ;
25
33
import lineageos .weather .WeatherLocation ;
34
+ import okhttp3 .Call ;
35
+ import okhttp3 .Callback ;
36
+ import okhttp3 .OkHttpClient ;
37
+ import okhttp3 .Request ;
38
+ import okhttp3 .Response ;
39
+ import okhttp3 .ResponseBody ;
26
40
27
41
public class WeatherUpdater {
28
42
@@ -153,6 +167,10 @@ private synchronized void onNewLocationFetched(@Nullable Location location) {
153
167
}
154
168
155
169
requestWeatherUpdate (getMostRecentLocation ());
170
+
171
+ if (!Preferences .useCustomWeatherLocation (mWeakContext .get ())) {
172
+ reverseGeocodeLocation (getMostRecentLocation ());
173
+ }
156
174
}
157
175
158
176
private void notifyUi (@ NonNull Context context , @ Nullable WeatherInfo weatherInfo , int status ) {
@@ -163,6 +181,7 @@ private void notifyUi(@NonNull Context context, @Nullable WeatherInfo weatherInf
163
181
}
164
182
165
183
Log .i (TAG , "WeatherInfo=" + weatherInfo );
184
+
166
185
long now = SystemClock .elapsedRealtime ();
167
186
Preferences .setCachedWeatherInfo (context , now , weatherInfo );
168
187
Preferences .setLastWeatherUpdateTimestamp (context , now );
@@ -188,4 +207,66 @@ private Location getMostRecentLocation() {
188
207
long networkTime = mNetworkLocation .getTime ();
189
208
return gpsTime >= networkTime ? mGpsLocation : mNetworkLocation ;
190
209
}
210
+
211
+ private void reverseGeocodeLocation (@ NonNull Location location ) {
212
+ Log .i (TAG , "Reverse geocoding location " + location );
213
+
214
+ final String url = "https://api.openweathermap.org/geo/1.0/reverse?lat=" + location .getLatitude () + "&lon="
215
+ + location .getLongitude () + "&limit=1&appid=" + mWeakContext .get ().getString (R .string .default_key );
216
+
217
+ final OkHttpClient okHttpClient = new OkHttpClient ();
218
+ final Request request = new Request .Builder ().url (url ).build ();
219
+ okHttpClient .newCall (request ).enqueue (mReverseGeocodeCallback );
220
+ }
221
+
222
+ private void onReverseGeocoded (@ NonNull Response response ) {
223
+ final ResponseBody body = response .body ();
224
+ if (body == null ) {
225
+ Log .w (TAG , "Reverse geocoding response is empty" );
226
+ return ;
227
+ }
228
+
229
+ JsonObject locales ;
230
+ try {
231
+ final String json = body .string ();
232
+ final JsonArray array = new JsonParser ().parse (json ).getAsJsonArray ();
233
+ locales = array .get (0 ).getAsJsonObject ().getAsJsonObject ("local_names" );
234
+ } catch (IOException | IllegalStateException | JsonSyntaxException exception ) {
235
+ Log .e (TAG , "Exception caught" , exception );
236
+ return ;
237
+ }
238
+
239
+ if (locales == null ) {
240
+ Log .e (TAG , "Could not get locales" );
241
+ return ;
242
+ }
243
+
244
+ String countryCode = Locale .getDefault ().getCountry ().toLowerCase (Locale .ROOT );
245
+ if (!locales .has (countryCode )) {
246
+ countryCode = locales .get ("en" ).getAsString ();
247
+ }
248
+
249
+ final String city = locales .get (countryCode ).getAsString ();
250
+ notifyUi (city );
251
+ }
252
+
253
+ private void notifyUi (@ NonNull String city ) {
254
+ Context context = mWeakContext .get ();
255
+ Preferences .setCachedCity (context , city );
256
+ final Intent intent = new Intent (WeatherUpdateService .ACTION_UPDATE_CITY_FINISHED );
257
+ intent .putExtra (WeatherUpdateService .EXTRA_UPDATE_CITY_KEY , city );
258
+ LocalBroadcastManager .getInstance (mWeakContext .get ()).sendBroadcast (intent );
259
+ }
260
+
261
+ private final Callback mReverseGeocodeCallback = new Callback () {
262
+ @ Override
263
+ public void onFailure (@ NonNull Call call , @ NonNull IOException e ) {
264
+ Log .e (TAG , "Could not reverse geocode location" , e );
265
+ }
266
+
267
+ @ Override
268
+ public void onResponse (@ NonNull Call call , @ NonNull Response response ) throws IOException {
269
+ onReverseGeocoded (response );
270
+ }
271
+ };
191
272
}
0 commit comments