Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 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:minSdkVersion="11"
android:targetSdkVersion="22" />

<application
android:allowBackup="false"
android:icon="@drawable/ic_launcher"
Expand All @@ -17,8 +18,18 @@
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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
</application>

</manifest>
22 changes: 16 additions & 6 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 @@ -28,11 +30,11 @@ public class BackendService extends GeocoderBackendService {
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 REVERSE_GEOCODE_URL =
"%sreverse?format=json&accept-language=%s&lat=%f&lon=%f";
"%sreverse?format=json&key=%s&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";
"%ssearch?format=json&key=%s&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" +
"%ssearch?format=json&key=%s&accept-language=%s&addressdetails=1&bounded=1&q=%s&limit=%d" +
"&viewbox=%f,%f,%f,%f";
private static final String WIRE_LATITUDE = "lat";
private static final String WIRE_LONGITUDE = "lon";
Expand All @@ -51,8 +53,12 @@ public class BackendService extends GeocoderBackendService {
@Override
protected List<Address> getFromLocation(double latitude, double longitude, int maxResults,
String locale) {

SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String mapquestApiKey = SP.getString("api_preference", "NA");

String url = String.format(Locale.US, REVERSE_GEOCODE_URL, SERVICE_URL_MAPQUEST,
locale.split("_")[0], latitude, longitude);
mapquestApiKey, locale.split("_")[0], latitude, longitude);
try {
JSONObject result = new JSONObject(new AsyncGetRequest(this,
url).asyncStart().retrieveString());
Expand Down Expand Up @@ -84,15 +90,19 @@ private static Locale localeFromLocaleString(String localeString) {
protected List<Address> getFromLocationName(String locationName, int maxResults,
double lowerLeftLatitude, double lowerLeftLongitude, double upperRightLatitude,
double upperRightLongitude, String locale) {

SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String mapquestApiKey = SP.getString("api_preference", "NA");

String query = Uri.encode(locationName);
String url;
if (lowerLeftLatitude == 0 && lowerLeftLongitude == 0 && upperRightLatitude == 0 &&
upperRightLongitude == 0) {
url = String.format(Locale.US, SEARCH_GEOCODE_URL, SERVICE_URL_MAPQUEST,
locale.split("_")[0], query, maxResults);
mapquestApiKey, locale.split("_")[0], query, maxResults);
} else {
url = String.format(Locale.US, SEARCH_GEOCODE_WITH_BOX_URL, SERVICE_URL_MAPQUEST,
locale.split("_")[0], query, maxResults, lowerLeftLongitude,
mapquestApiKey, locale.split("_")[0], query, maxResults, lowerLeftLongitude,
upperRightLatitude, upperRightLongitude, lowerLeftLatitude);
}
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.microg.nlp.backend.nominatim;

import android.content.SharedPreferences;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.os.Bundle;

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

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

}

public static class PrefsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
setSummaries();
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
updatePreference(findPreference(key), key);
}

private void setSummaries(){

final SharedPreferences sh = getPreferenceManager().getSharedPreferences() ;

Preference stylePref = findPreference("api_preference");
stylePref.setSummary(sh.getString("api_preference", ""));

}
private void updatePreference(Preference preference, String key) {
if (preference == null) return;
if (preference instanceof ListPreference) {
ListPreference listPreference = (ListPreference) preference;
listPreference.setSummary(listPreference.getEntry());
return;
}
SharedPreferences sharedPrefs = getPreferenceManager().getSharedPreferences();
preference.setSummary(sharedPrefs.getString(key, "Default"));
}
}

}
7 changes: 6 additions & 1 deletion src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?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="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>
16 changes: 16 additions & 0 deletions src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

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

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

</PreferenceCategory>

</PreferenceScreen>