Skip to content
Merged
9 changes: 5 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

buildscript {
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.3'
classpath 'com.android.tools.build:gradle:2.2.3'
}
}

Expand All @@ -31,11 +31,12 @@ repositories {

dependencies {
compile 'org.microg:unifiednlp-api:1.3.2'
compile 'com.android.support:preference-v7:25.1.0'
}

android {
compileSdkVersion 22
buildToolsVersion "22.0.0"
compileSdkVersion 25
buildToolsVersion "25.0.2"
}

if (file('user.gradle').exists()) {
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Jan 14 11:55:04 CET 2015
#Fri Jan 27 15:08:17 CET 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
25 changes: 19 additions & 6 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionName="1.1.1"
android:versionCode="10101"
package="org.microg.nlp.backend.nominatim">
android:versionName="1.1.1"
android:versionCode="10101"
package="org.microg.nlp.backend.nominatim">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.INTERNET" />

<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="22"/>
android:targetSdkVersion="25" />

<application
android:allowBackup="false"
android:icon="@drawable/ic_launcher"
Expand All @@ -17,8 +18,20 @@
android:name=".BackendService"
android:label="@string/backend_name">
<intent-filter>
<action android:name="org.microg.nlp.GEOCODER_BACKEND"/>
<action android:name="org.microg.nlp.GEOCODER_BACKEND" />
</intent-filter>
<meta-data
android:name="org.microg.nlp.BACKEND_SETTINGS_ACTIVITY"
android:value="org.microg.nlp.backend.nominatim.SettingsActivity" />
</service>

<activity android:name=".SettingsActivity"
android:theme="@style/SettingsTheme"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
</application>

</manifest>
65 changes: 50 additions & 15 deletions src/main/java/org/microg/nlp/backend/nominatim/BackendService.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.microg.nlp.backend.nominatim;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.location.Address;
import android.net.Uri;
import android.os.Build;
import android.preference.PreferenceManager;
import android.util.Log;

import org.json.JSONArray;
Expand All @@ -21,19 +23,22 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;

public class BackendService extends GeocoderBackendService {
private static final String TAG = "NominatimGeocoderBackend";
private static final String SERVICE_URL_MAPQUEST = "http://open.mapquestapi.com/nominatim/v1/";
private static final String SERVICE_URL_OSM = " http://nominatim.openstreetmap.org/";
private static final String TAG = "NominatimGeocoder";

private static final String SERVICE_URL_MAPQUEST = "https://open.mapquestapi.com/nominatim/v1";
private static final String SERVICE_URL_OSM = "https://nominatim.openstreetmap.org";

private static final String REVERSE_GEOCODE_URL =
"%sreverse?format=json&accept-language=%s&lat=%f&lon=%f";
"%s/reverse?%sformat=json&accept-language=%s&lat=%f&lon=%f";
private static final String SEARCH_GEOCODE_URL =
"%ssearch?format=json&accept-language=%s&addressdetails=1&bounded=1&q=%s&limit=%d";
"%s/search?%sformat=json&accept-language=%s&addressdetails=1&bounded=1&q=%s&limit=%d";
private static final String SEARCH_GEOCODE_WITH_BOX_URL =
"%ssearch?format=json&accept-language=%s&addressdetails=1&bounded=1&q=%s&limit=%d" +
"&viewbox=%f,%f,%f,%f";
SEARCH_GEOCODE_URL + "&viewbox=%f,%f,%f,%f";

private static final String WIRE_LATITUDE = "lat";
private static final String WIRE_LONGITUDE = "lon";
private static final String WIRE_ADDRESS = "address";
Expand All @@ -48,10 +53,40 @@ public class BackendService extends GeocoderBackendService {
private static final String WIRE_COUNTRYNAME = "country";
private static final String WIRE_COUNTRYCODE = "country_code";

private String mApiUrl;
private String mAPIKey;

@Override
public void onCreate() {
super.onCreate();
readPrefs();
}

@Override
protected void onOpen() {
super.onOpen();
readPrefs();
}

private void readPrefs() {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

if (sp.getString(SettingsActivity.PrefsFragment.apiChoiceToken, "OSM").equals("OSM")) {
mApiUrl = SERVICE_URL_OSM;
// No API key for OSM
mAPIKey = "";
} else {
mApiUrl = SERVICE_URL_MAPQUEST;
mAPIKey = "key=" + sp.getString(SettingsActivity.PrefsFragment.mapQuestApiKeyToken, "NA")
+ "&";
}
}


@Override
protected List<Address> getFromLocation(double latitude, double longitude, int maxResults,
String locale) {
String url = String.format(Locale.US, REVERSE_GEOCODE_URL, SERVICE_URL_MAPQUEST,
String url = String.format(Locale.US, REVERSE_GEOCODE_URL, mApiUrl, mAPIKey,
locale.split("_")[0], latitude, longitude);
try {
JSONObject result = new JSONObject(new AsyncGetRequest(this,
Expand Down Expand Up @@ -88,10 +123,10 @@ protected List<Address> getFromLocationName(String locationName, int maxResults,
String url;
if (lowerLeftLatitude == 0 && lowerLeftLongitude == 0 && upperRightLatitude == 0 &&
upperRightLongitude == 0) {
url = String.format(Locale.US, SEARCH_GEOCODE_URL, SERVICE_URL_MAPQUEST,
url = String.format(Locale.US, SEARCH_GEOCODE_URL, mApiUrl, mAPIKey,
locale.split("_")[0], query, maxResults);
} else {
url = String.format(Locale.US, SEARCH_GEOCODE_WITH_BOX_URL, SERVICE_URL_MAPQUEST,
url = String.format(Locale.US, SEARCH_GEOCODE_WITH_BOX_URL, mApiUrl, mAPIKey,
locale.split("_")[0], query, maxResults, lowerLeftLongitude,
upperRightLatitude, upperRightLongitude, lowerLeftLatitude);
}
Expand Down Expand Up @@ -165,8 +200,8 @@ private Address parseResponse(Locale locale, JSONObject result) throws JSONExcep
}

private class AsyncGetRequest extends Thread {
public static final String USER_AGENT = "User-Agent";
public static final String USER_AGENT_TEMPLATE = "UnifiedNlp/%s (Linux; Android %s)";
static final String USER_AGENT = "User-Agent";
static final String USER_AGENT_TEMPLATE = "UnifiedNlp/%s (Linux; Android %s)";
private final AtomicBoolean done = new AtomicBoolean(false);
private final Context context;
private final String url;
Expand Down Expand Up @@ -196,12 +231,12 @@ public void run() {
}
}

public AsyncGetRequest asyncStart() {
AsyncGetRequest asyncStart() {
start();
return this;
}

public byte[] retrieveAllBytes() {
byte[] retrieveAllBytes() {
if (!done.get()) {
synchronized (done) {
while (!done.get()) {
Expand All @@ -216,7 +251,7 @@ public byte[] retrieveAllBytes() {
return result;
}

public String retrieveString() {
String retrieveString() {
return new String(retrieveAllBytes());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package org.microg.nlp.backend.nominatim;

import android.content.SharedPreferences;
import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;
import android.support.v7.preference.ListPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;

public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (getSupportActionBar() != null) {
getSupportActionBar().setTitle(R.string.app_name);
getSupportActionBar().show();
}

// Display the fragment as the main content.
getSupportFragmentManager()
.beginTransaction()
.replace(android.R.id.content, new PrefsFragment())
.commit();
}

public static class PrefsFragment extends PreferenceFragmentCompat {
public static final String catApiKeyToken = "cat_api_preference";
public static final String apiChoiceToken = "api_server_choice";
public static final String mapQuestApiKeyToken = "api_preference";

private SharedPreferences shPref;
private SharedPreferences.OnSharedPreferenceChangeListener listener;

private Preference mApiChoicePref;
private Preference mCatAPIKeyPref;
private Preference mMapQuestApiKeyPref;

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
shPref = getPreferenceManager().getSharedPreferences();

// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);

mApiChoicePref = findPreference(apiChoiceToken);
mCatAPIKeyPref = findPreference(catApiKeyToken);
mMapQuestApiKeyPref = findPreference(mapQuestApiKeyToken);

refreshPrefs();

// Need explicit reference.
// See :
// http://stackoverflow.com/a/3104265
listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
updatePreference(findPreference(key), key);
}
};
shPref.registerOnSharedPreferenceChangeListener(listener);

mApiChoicePref.setSummary(shPref.getString(apiChoiceToken, "OSM"));
mMapQuestApiKeyPref.setSummary(shPref.getString(mapQuestApiKeyToken, ""));
}

private void refreshPrefs() {
String apiServer = shPref.getString(apiChoiceToken, "OSM");
if (apiServer.equals("OSM")) {
getPreferenceScreen().removePreference(mCatAPIKeyPref);
} else {
getPreferenceScreen().addPreference(mCatAPIKeyPref);
}
}

private void updatePreference(Preference preference, String key) {
refreshPrefs();

if (preference == null) {
return;
}

if (preference instanceof ListPreference) {
ListPreference listPreference = (ListPreference) preference;
listPreference.setSummary(listPreference.getEntry());
return;
}

preference.setSummary(shPref.getString(key, "Default"));
}
}
}
7 changes: 7 additions & 0 deletions src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="ApiChoice">
<item>OSM</item>
<item>MapQuest</item>
</string-array>
</resources>
8 changes: 7 additions & 1 deletion src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">NominatimGeocoderBackend</string>
<string name="app_name">Nominatim Geocoder Backend</string>
<string name="backend_name">Nominatim</string>

<string name="choose_server">Choose Nominatim API Server</string>
<string name="server_access">MapQuest Server Access</string>
<string name="api_title">MapQuest Developer API Key</string>
<string name="api_summary">MapQuest Developer API Key</string>
<string name="api_dialog_title">Enter your MapQuest developer API key.</string>
</resources>
6 changes: 6 additions & 0 deletions src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="SettingsTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="preferenceTheme">@style/PreferenceFragment</item>
</style>
</resources>
25 changes: 25 additions & 0 deletions src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

<ListPreference
android:defaultValue="OSM"
android:title="@string/choose_server"
android:entries="@array/ApiChoice"
android:entryValues="@array/ApiChoice"
android:key="api_server_choice"
/>

<PreferenceCategory
android:key="cat_api_preference"
android:title="@string/server_access">

<EditTextPreference
android:key="api_preference"
android:title="@string/api_title"
android:dialogTitle="@string/api_dialog_title"
android:defaultValue=""
/>

</PreferenceCategory>

</PreferenceScreen>