Skip to content

Commit 719c68e

Browse files
committed
Allow user to choose between OSM and MapQuest API server
* MapQuest server needs a developper API key. See : https://developer.mapquest.com/
1 parent 874e8ac commit 719c68e

File tree

5 files changed

+97
-36
lines changed

5 files changed

+97
-36
lines changed

src/main/java/org/microg/nlp/backend/nominatim/BackendService.java

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.ArrayList;
2323
import java.util.List;
2424
import java.util.Locale;
25+
import java.util.Objects;
2526
import java.util.concurrent.atomic.AtomicBoolean;
2627

2728
import javax.net.ssl.HttpsURLConnection;
@@ -33,12 +34,12 @@ public class BackendService extends GeocoderBackendService {
3334
private static final String SERVICE_URL_OSM = "https://nominatim.openstreetmap.org/";
3435

3536
private static final String REVERSE_GEOCODE_URL =
36-
"%sreverse?format=json&key=%s&accept-language=%s&lat=%f&lon=%f";
37+
"%s/reverse?%sformat=json&accept-language=%s&lat=%f&lon=%f";
3738
private static final String SEARCH_GEOCODE_URL =
38-
"%ssearch?format=json&key=%s&accept-language=%s&addressdetails=1&bounded=1&q=%s&limit=%d";
39+
"%s/search?%sformat=json&accept-language=%s&addressdetails=1&bounded=1&q=%s&limit=%d";
3940
private static final String SEARCH_GEOCODE_WITH_BOX_URL =
40-
"%ssearch?format=json&key=%s&accept-language=%s&addressdetails=1&bounded=1&q=%s&limit=%d" +
41-
"&viewbox=%f,%f,%f,%f";
41+
SEARCH_GEOCODE_URL + "&viewbox=%f,%f,%f,%f";
42+
4243
private static final String WIRE_LATITUDE = "lat";
4344
private static final String WIRE_LONGITUDE = "lon";
4445
private static final String WIRE_ADDRESS = "address";
@@ -53,15 +54,31 @@ public class BackendService extends GeocoderBackendService {
5354
private static final String WIRE_COUNTRYNAME = "country";
5455
private static final String WIRE_COUNTRYCODE = "country_code";
5556

57+
private String mApiUrl;
58+
private String mAPIKey;
59+
60+
private void readPrefs() {
61+
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
62+
63+
if (sp.getString(SettingsActivity.PrefsFragment.apiChoiceToken, "OSM").equals("OSM")) {
64+
mApiUrl = SERVICE_URL_OSM;
65+
// No API key for OSM
66+
mAPIKey = "";
67+
} else {
68+
mApiUrl = SERVICE_URL_MAPQUEST;
69+
mAPIKey = "key=" + sp.getString(SettingsActivity.PrefsFragment.mapQuestApiKeyToken, "NA")
70+
+ "&";
71+
}
72+
}
73+
74+
5675
@Override
5776
protected List<Address> getFromLocation(double latitude, double longitude, int maxResults,
5877
String locale) {
78+
readPrefs();
5979

60-
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
61-
String mapquestApiKey = SP.getString("api_preference", "NA");
62-
63-
String url = String.format(Locale.US, REVERSE_GEOCODE_URL, SERVICE_URL_MAPQUEST,
64-
mapquestApiKey, locale.split("_")[0], latitude, longitude);
80+
String url = String.format(Locale.US, REVERSE_GEOCODE_URL, mApiUrl, mAPIKey,
81+
locale.split("_")[0], latitude, longitude);
6582
try {
6683
JSONObject result = new JSONObject(new AsyncGetRequest(this,
6784
url).asyncStart().retrieveString());
@@ -93,19 +110,17 @@ private static Locale localeFromLocaleString(String localeString) {
93110
protected List<Address> getFromLocationName(String locationName, int maxResults,
94111
double lowerLeftLatitude, double lowerLeftLongitude, double upperRightLatitude,
95112
double upperRightLongitude, String locale) {
96-
97-
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
98-
String mapquestApiKey = SP.getString("api_preference", "NA");
113+
readPrefs();
99114

100115
String query = Uri.encode(locationName);
101116
String url;
102117
if (lowerLeftLatitude == 0 && lowerLeftLongitude == 0 && upperRightLatitude == 0 &&
103118
upperRightLongitude == 0) {
104-
url = String.format(Locale.US, SEARCH_GEOCODE_URL, SERVICE_URL_MAPQUEST,
105-
mapquestApiKey, locale.split("_")[0], query, maxResults);
119+
url = String.format(Locale.US, SEARCH_GEOCODE_URL, mApiUrl, mAPIKey,
120+
locale.split("_")[0], query, maxResults);
106121
} else {
107-
url = String.format(Locale.US, SEARCH_GEOCODE_WITH_BOX_URL, SERVICE_URL_MAPQUEST,
108-
mapquestApiKey, locale.split("_")[0], query, maxResults, lowerLeftLongitude,
122+
url = String.format(Locale.US, SEARCH_GEOCODE_WITH_BOX_URL, mApiUrl, mAPIKey,
123+
locale.split("_")[0], query, maxResults, lowerLeftLongitude,
109124
upperRightLatitude, upperRightLongitude, lowerLeftLatitude);
110125
}
111126
try {

src/main/java/org/microg/nlp/backend/nominatim/SettingsActivity.java

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import android.preference.PreferenceFragment;
88
import android.os.Bundle;
99

10+
import java.util.Objects;
11+
1012
public class SettingsActivity extends PreferenceActivity {
1113
@Override
1214
protected void onCreate(Bundle savedInstanceState) {
@@ -18,41 +20,71 @@ protected void onCreate(Bundle savedInstanceState) {
1820

1921
}
2022

21-
public static class PrefsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
23+
public static class PrefsFragment extends PreferenceFragment {
24+
public static final String catApiKeyToken = "cat_api_preference";
25+
public static final String apiChoiceToken = "api_server_choice";
26+
public static final String mapQuestApiKeyToken = "api_preference";
27+
28+
private SharedPreferences shPref;
29+
private SharedPreferences.OnSharedPreferenceChangeListener listener;
30+
31+
private Preference mApiChoicePref;
32+
private Preference mCatAPIKeyPref;
33+
private Preference mMapQuestApiKeyPref;
2234

2335
@Override
2436
public void onCreate(Bundle savedInstanceState) {
2537
super.onCreate(savedInstanceState);
2638

39+
shPref = getPreferenceManager().getSharedPreferences();
40+
2741
// Load the preferences from an XML resource
2842
addPreferencesFromResource(R.xml.preferences);
29-
setSummaries();
30-
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
31-
}
3243

33-
@Override
34-
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
35-
updatePreference(findPreference(key), key);
36-
}
44+
mApiChoicePref = findPreference(apiChoiceToken);
45+
mCatAPIKeyPref = findPreference(catApiKeyToken);
46+
mMapQuestApiKeyPref = findPreference(mapQuestApiKeyToken);
3747

38-
private void setSummaries(){
48+
refreshPrefs();
3949

40-
final SharedPreferences sh = getPreferenceManager().getSharedPreferences() ;
50+
// Need explicit reference.
51+
// See :
52+
// http://stackoverflow.com/a/3104265
53+
listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
54+
@Override
55+
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
56+
updatePreference(findPreference(key), key);
57+
}
58+
};
59+
shPref.registerOnSharedPreferenceChangeListener(listener);
4160

42-
Preference stylePref = findPreference("api_preference");
43-
stylePref.setSummary(sh.getString("api_preference", ""));
61+
mApiChoicePref.setSummary(shPref.getString(apiChoiceToken, "OSM"));
62+
mMapQuestApiKeyPref.setSummary(shPref.getString(mapQuestApiKeyToken, ""));
63+
}
4464

65+
private void refreshPrefs() {
66+
String apiServer = shPref.getString(apiChoiceToken, "OSM");
67+
if (apiServer.equals("OSM")) {
68+
getPreferenceScreen().removePreference(mCatAPIKeyPref);
69+
} else {
70+
getPreferenceScreen().addPreference(mCatAPIKeyPref);
71+
}
4572
}
73+
4674
private void updatePreference(Preference preference, String key) {
47-
if (preference == null) return;
75+
refreshPrefs();
76+
77+
if (preference == null) {
78+
return;
79+
}
80+
4881
if (preference instanceof ListPreference) {
4982
ListPreference listPreference = (ListPreference) preference;
5083
listPreference.setSummary(listPreference.getEntry());
5184
return;
5285
}
53-
SharedPreferences sharedPrefs = getPreferenceManager().getSharedPreferences();
54-
preference.setSummary(sharedPrefs.getString(key, "Default"));
86+
87+
preference.setSummary(shPref.getString(key, "Default"));
5588
}
5689
}
57-
58-
}
90+
}

src/main/res/values/arrays.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
3-
</resources>
3+
<string-array name="ApiChoice">
4+
<item>OSM</item>
5+
<item>MapQuest</item>
6+
</string-array>
7+
</resources>

src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<string name="app_name">Nominatim Geocoder Backend</string>
44
<string name="backend_name">Nominatim</string>
55

6+
<string name="choose_server">Choose Nominatim API Server</string>
67
<string name="server_access">MapQuest Server Access</string>
78
<string name="api_title">MapQuest Developer API Key</string>
89
<string name="api_summary">MapQuest Developer API Key</string>

src/main/res/xml/preferences.xml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
33

4+
<ListPreference
5+
android:defaultValue="OSM"
6+
android:title="@string/choose_server"
7+
android:entries="@array/ApiChoice"
8+
android:entryValues="@array/ApiChoice"
9+
android:key="api_server_choice"
10+
/>
11+
412
<PreferenceCategory
13+
android:key="cat_api_preference"
514
android:title="@string/server_access">
615

716
<EditTextPreference
817
android:key="api_preference"
918
android:title="@string/api_title"
10-
android:summary=""
1119
android:dialogTitle="@string/api_dialog_title"
12-
android:defaultValue=""/>
20+
android:defaultValue=""
21+
/>
1322

1423
</PreferenceCategory>
1524

0 commit comments

Comments
 (0)