Skip to content

Commit

Permalink
make use of GeocodeDatabase
Browse files Browse the repository at this point in the history
  • Loading branch information
mar-v-in committed Dec 11, 2013
1 parent 2192b67 commit 41771a6
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 8 deletions.
4 changes: 2 additions & 2 deletions NetworkLocation/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.android.location"
android:versionCode="1301"
android:versionName="1.3.1">
android:versionCode="1302"
android:versionName="1.3.2">

<uses-sdk
android:minSdkVersion="9"
Expand Down
2 changes: 1 addition & 1 deletion NetworkLocation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.microg.networklocation</groupId>
<artifactId>NetworkLocation</artifactId>
<version>1.3.1</version>
<version>1.3.2</version>
<packaging>apk</packaging>
<name>NetworkLocation</name>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.microg.networklocation.data.LocationCalculator;
import org.microg.networklocation.data.LocationRetriever;
import org.microg.networklocation.data.WifiSpec;
import org.microg.networklocation.database.GeocodeDatabase;
import org.microg.networklocation.database.LocationDatabase;
import org.microg.networklocation.helper.Reflected;
import org.microg.networklocation.platform.PlatformFactory;
Expand Down Expand Up @@ -118,6 +119,8 @@ public void onCreate() {
wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
nlprovider = PlatformFactory.newNetworkLocationProvider();
geoprovider = PlatformFactory.newGeocodeProvider();
GeocodeDatabase geocodeDatabase = new GeocodeDatabase();
geoprovider.setGeocodeDatabase(geocodeDatabase);
WifiSpecRetriever wifiSpecRetriever = PlatformFactory.newWifiSpecRetriever(context);
CellSpecRetriever cellSpecRetriever = PlatformFactory.newCellSpecRetriever(context);
LocationDatabase locationDatabase = new LocationDatabase(context);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.microg.networklocation.database;

import android.location.Address;
import android.util.Log;
import org.microg.networklocation.MainService;

import java.util.HashMap;
import java.util.List;
Expand All @@ -10,6 +12,7 @@
* This provides basic caching capabilities until the real database is ready.
*/
public class GeocodeDatabase {
private static final String TAG = "GeocodeDatabase";
private Map<Long, List<Address>> intDb = new HashMap<Long, List<Address>>();
private Map<String, List<Address>> stringDb = new HashMap<String, List<Address>>();

Expand All @@ -22,18 +25,44 @@ private static long dbIdent(double latitude, double longitude) {
}

public List<Address> get(String locationName) {
return stringDb.get(locationName);
try {
return stringDb.get(locationName);
} catch (Throwable t) {
if (MainService.DEBUG) {
Log.w(TAG, t);
}
return null;
}
}

public List<Address> get(double latitude, double longitude) {
return intDb.get(dbIdent(latitude, longitude));
try {
return intDb.get(dbIdent(latitude, longitude));
} catch (Throwable t) {
if (MainService.DEBUG) {
Log.w(TAG, t);
}
return null;
}
}

public void put(double latitude, double longitude, List<Address> addresses) {
intDb.put(dbIdent(latitude, longitude), addresses);
try {
intDb.put(dbIdent(latitude, longitude), addresses);
} catch (Throwable t) {
if (MainService.DEBUG) {
Log.w(TAG, t);
}
}
}

public void put(String locationName, List<Address> addresses) {
stringDb.put(locationName, addresses);
try {
stringDb.put(locationName, addresses);
} catch (Throwable t) {
if (MainService.DEBUG) {
Log.w(TAG, t);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,34 @@ class GeocodeProvider extends internal.com.android.location.provider.GeocodeProv
private GeocodeDatabase geocodeDatabase;
private List<GeocodeSource> sources;

@Override
public void setGeocodeDatabase(GeocodeDatabase geocodeDatabase) {
this.geocodeDatabase = geocodeDatabase;
}

@Override
public String onGetFromLocation(double latitude, double longitude, int maxResults, GeocoderParams params,
List<Address> addrs) {
List<Address> addresses = geocodeDatabase.get(latitude, longitude);
if (MainService.DEBUG) {
Log.d(TAG, "Reverse request: " + latitude + "/" + longitude);
}
List<Address> addresses = null;
if (geocodeDatabase != null) {
addresses = geocodeDatabase.get(latitude, longitude);
}
if ((addresses != null) && !addresses.isEmpty()) {
// database hit
if (MainService.DEBUG) {
Log.d(TAG, "Using database entry: " + addrs.get(0));
}
addrs.addAll(addresses);
return null;
}
for (GeocodeSource source : sources) {
if (source.isSourceAvailable()) {
if (MainService.DEBUG) {
Log.d(TAG, "Try reverse using: " + source.getName());
}
try {
addresses =
source.getFromLocation(latitude, longitude, params.getClientPackage(), params.getLocale());
Expand All @@ -54,6 +71,9 @@ public String onGetFromLocation(double latitude, double longitude, int maxResult
public String onGetFromLocationName(String locationName, double lowerLeftLatitude, double lowerLeftLongitude,
double upperRightLatitude, double upperRightLongitude, int maxResults,
GeocoderParams params, List<Address> addrs) {
if (MainService.DEBUG) {
Log.d(TAG, "Forward request: " + locationName);
}
List<Address> addresses = geocodeDatabase.get(locationName);
if ((addresses != null) && !addresses.isEmpty()) {
// database hit
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package org.microg.networklocation.provider;

import android.os.IBinder;
import org.microg.networklocation.database.GeocodeDatabase;
import org.microg.networklocation.source.GeocodeSource;

import java.util.List;

public interface GeocodeProvider {
IBinder getBinder();

void setGeocodeDatabase(GeocodeDatabase geocodeDatabase);

void setSources(List<GeocodeSource> geocodeSources);
}

0 comments on commit 41771a6

Please sign in to comment.