1717package org .microg .nlp .backend .nominatim ;
1818
1919import android .content .Context ;
20+ import android .content .SharedPreferences ;
21+ import android .content .pm .PackageManager ;
2022import android .location .Address ;
2123import android .net .Uri ;
24+ import android .os .Build ;
25+ import android .preference .PreferenceManager ;
2226import android .util .Log ;
2327
2428import org .json .JSONArray ;
3842import java .util .List ;
3943import java .util .Locale ;
4044import java .util .Map ;
45+ import java .util .Objects ;
4146import java .util .concurrent .atomic .AtomicBoolean ;
4247
4348import static android .os .Build .VERSION .RELEASE ;
4449import static org .microg .nlp .backend .nominatim .BuildConfig .VERSION_NAME ;
4550
4651public class BackendService extends GeocoderBackendService {
47- private static final String TAG = "NominatimBackend" ;
48- private static final String SERVICE_URL_MAPQUEST = "http://open.mapquestapi.com/nominatim/v1/" ;
49- private static final String SERVICE_URL_OSM = "http://nominatim.openstreetmap.org/" ;
52+ private static final String TAG = "NominatimGeocoder" ;
53+
54+ private static final String SERVICE_URL_MAPQUEST = "https://open.mapquestapi.com/nominatim/v1" ;
55+ private static final String SERVICE_URL_OSM = "https://nominatim.openstreetmap.org" ;
56+
5057 private static final String REVERSE_GEOCODE_URL =
51- "%sreverse?format =json&accept-language=%s&lat=%f&lon=%f" ;
58+ "%s/reverse?%sformat =json&accept-language=%s&lat=%f&lon=%f" ;
5259 private static final String SEARCH_GEOCODE_URL =
53- "%ssearch?format =json&accept-language=%s&addressdetails=1&bounded=1&q=%s&limit=%d" ;
60+ "%s/search?%sformat =json&accept-language=%s&addressdetails=1&bounded=1&q=%s&limit=%d" ;
5461 private static final String SEARCH_GEOCODE_WITH_BOX_URL =
55- "%ssearch?format=json&accept-language=%s&addressdetails=1&bounded=1&q=%s&limit=%d" +
56- "&viewbox=%f,%f,%f,%f" ;
62+ SEARCH_GEOCODE_URL + "&viewbox=%f,%f,%f,%f" ;
63+
5764 private static final String WIRE_LATITUDE = "lat" ;
5865 private static final String WIRE_LONGITUDE = "lon" ;
5966 private static final String WIRE_ADDRESS = "address" ;
@@ -70,6 +77,9 @@ public class BackendService extends GeocoderBackendService {
7077
7178 private Formatter formatter ;
7279
80+ private String mApiUrl ;
81+ private String mAPIKey ;
82+
7383 @ Override
7484 public void onCreate () {
7585 super .onCreate ();
@@ -78,12 +88,34 @@ public void onCreate() {
7888 } catch (IOException e ) {
7989 Log .w (TAG , "Could not initialize address formatter" , e );
8090 }
91+ readPrefs ();
92+ }
93+
94+ @ Override
95+ protected void onOpen () {
96+ super .onOpen ();
97+ readPrefs ();
8198 }
8299
100+ private void readPrefs () {
101+ SharedPreferences sp = PreferenceManager .getDefaultSharedPreferences (getApplicationContext ());
102+
103+ if (sp .getString (SettingsActivity .PrefsFragment .apiChoiceToken , "OSM" ).equals ("OSM" )) {
104+ mApiUrl = SERVICE_URL_OSM ;
105+ // No API key for OSM
106+ mAPIKey = "" ;
107+ } else {
108+ mApiUrl = SERVICE_URL_MAPQUEST ;
109+ mAPIKey = "key=" + sp .getString (SettingsActivity .PrefsFragment .mapQuestApiKeyToken , "NA" )
110+ + "&" ;
111+ }
112+ }
113+
114+
83115 @ Override
84116 protected List <Address > getFromLocation (double latitude , double longitude , int maxResults ,
85- String locale ) {
86- String url = String .format (Locale .US , REVERSE_GEOCODE_URL , SERVICE_URL_OSM ,
117+ String locale ) {
118+ String url = String .format (Locale .US , REVERSE_GEOCODE_URL , mApiUrl , mAPIKey ,
87119 locale .split ("_" )[0 ], latitude , longitude );
88120 try {
89121 JSONObject result = new JSONObject (new AsyncGetRequest (this ,
@@ -120,10 +152,10 @@ protected List<Address> getFromLocationName(String locationName, int maxResults,
120152 String url ;
121153 if (lowerLeftLatitude == 0 && lowerLeftLongitude == 0 && upperRightLatitude == 0 &&
122154 upperRightLongitude == 0 ) {
123- url = String .format (Locale .US , SEARCH_GEOCODE_URL , SERVICE_URL_OSM ,
155+ url = String .format (Locale .US , SEARCH_GEOCODE_URL , mApiUrl , mAPIKey ,
124156 locale .split ("_" )[0 ], query , maxResults );
125157 } else {
126- url = String .format (Locale .US , SEARCH_GEOCODE_WITH_BOX_URL , SERVICE_URL_OSM ,
158+ url = String .format (Locale .US , SEARCH_GEOCODE_WITH_BOX_URL , mApiUrl , mAPIKey ,
127159 locale .split ("_" )[0 ], query , maxResults , lowerLeftLongitude ,
128160 upperRightLatitude , upperRightLongitude , lowerLeftLatitude );
129161 }
@@ -202,8 +234,8 @@ public Iterator<T> iterator() {
202234 }
203235
204236 private class AsyncGetRequest extends Thread {
205- public static final String USER_AGENT = "User-Agent" ;
206- public static final String USER_AGENT_TEMPLATE = "UnifiedNlp/%s (Linux; Android %s)" ;
237+ static final String USER_AGENT = "User-Agent" ;
238+ static final String USER_AGENT_TEMPLATE = "UnifiedNlp/%s (Linux; Android %s)" ;
207239 private final AtomicBoolean done = new AtomicBoolean (false );
208240 private final Context context ;
209241 private final String url ;
@@ -232,12 +264,12 @@ public void run() {
232264 }
233265 }
234266
235- public AsyncGetRequest asyncStart () {
267+ AsyncGetRequest asyncStart () {
236268 start ();
237269 return this ;
238270 }
239271
240- public byte [] retrieveAllBytes () {
272+ byte [] retrieveAllBytes () {
241273 if (!done .get ()) {
242274 synchronized (done ) {
243275 while (!done .get ()) {
@@ -252,7 +284,7 @@ public byte[] retrieveAllBytes() {
252284 return result ;
253285 }
254286
255- public String retrieveString () {
287+ String retrieveString () {
256288 return new String (retrieveAllBytes ());
257289 }
258290
0 commit comments