Add brouter service

This commit is contained in:
vshcherb 2014-01-12 01:22:51 +01:00
parent 8b79c279ae
commit 1389c178d9
4 changed files with 62 additions and 57 deletions

View file

@ -0,0 +1,38 @@
package btools.routingapp;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
public class BRouterServiceConnection implements ServiceConnection {
IBRouterService brouterService;
public void onServiceConnected(ComponentName className, IBinder boundService) {
brouterService = IBRouterService.Stub.asInterface((IBinder) boundService);
}
public void onServiceDisconnected(ComponentName className) {
brouterService = null;
}
public void disconnect(Context ctx){
ctx.unbindService(this);
}
public IBRouterService getBrouterService() {
return brouterService;
}
public static BRouterServiceConnection connect(Context ctx){
BRouterServiceConnection conn = new BRouterServiceConnection();
Intent intent = new Intent();
intent.setClassName("btools.routingapp", "btools.routingapp.BRouterService");
boolean hasBRouter = ctx.bindService(intent, conn, Context.BIND_AUTO_CREATE);
if(!hasBRouter){
conn = null;
}
return conn;
}
}

View file

@ -57,6 +57,8 @@ import android.os.Handler;
import android.os.Message; import android.os.Message;
import android.text.format.DateFormat; import android.text.format.DateFormat;
import android.widget.Toast; import android.widget.Toast;
import btools.routingapp.BRouterServiceConnection;
import btools.routingapp.IBRouterService;
import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity; import com.actionbarsherlock.app.SherlockActivity;
@ -99,6 +101,7 @@ public class OsmandApplication extends Application implements ClientContext {
InternalToDoAPI internalToDoAPI; InternalToDoAPI internalToDoAPI;
InternalOsmAndAPI internalOsmAndAPI; InternalOsmAndAPI internalOsmAndAPI;
SQLiteAPI sqliteAPI; SQLiteAPI sqliteAPI;
BRouterServiceConnection bRouterServiceConnection;
@Override @Override
public void onCreate() { public void onCreate() {
@ -120,6 +123,12 @@ public class OsmandApplication extends Application implements ClientContext {
internalOsmAndAPI = new net.osmand.plus.api.InternalOsmAndAPIImpl(this); internalOsmAndAPI = new net.osmand.plus.api.InternalOsmAndAPIImpl(this);
sqliteAPI = new SQLiteAPIImpl(this); sqliteAPI = new SQLiteAPIImpl(this);
try {
bRouterServiceConnection = BRouterServiceConnection.connect(this);
} catch(Exception e) {
e.printStackTrace();
}
// settings used everywhere so they need to be created first // settings used everywhere so they need to be created first
osmandSettings = createOsmandSettingsInstance(); osmandSettings = createOsmandSettingsInstance();
// always update application mode to default // always update application mode to default
@ -737,6 +746,13 @@ public class OsmandApplication extends Application implements ClientContext {
} }
} }
public IBRouterService getBRouterService() {
if(bRouterServiceConnection == null) {
return null;
}
return bRouterServiceConnection.getBrouterService();
}
public void setLanguage(Context context) { public void setLanguage(Context context) {
if (prefferedLocale != null) { if (prefferedLocale != null) {
Configuration config = context.getResources().getConfiguration(); Configuration config = context.getResources().getConfiguration();

View file

@ -76,7 +76,7 @@ public class SettingsNavigationActivity extends SettingsBaseActivity {
} }
registerListPreference(settings.AUTO_FOLLOW_ROUTE, screen, entries, intValues); registerListPreference(settings.AUTO_FOLLOW_ROUTE, screen, entries, intValues);
RouteService[] vls = RouteService.getAvailableRouters(this); RouteService[] vls = RouteService.getAvailableRouters(getMyApplication());
entries = new String[vls.length]; entries = new String[vls.length];
for(int i=0; i<entries.length; i++){ for(int i=0; i<entries.length; i++){
entries[i] = vls[i].getName(); entries[i] = vls[i].getName();

View file

@ -60,14 +60,8 @@ import org.xml.sax.InputSource;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException;
import btools.routingapp.IBRouterService;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle; import android.os.Bundle;
import android.os.IBinder; import btools.routingapp.IBRouterService;
public class RouteProvider { public class RouteProvider {
@ -90,22 +84,14 @@ public class RouteProvider {
return this != OSMAND && this != BROUTER; return this != OSMAND && this != BROUTER;
} }
boolean isAvailable(Context ctx) { boolean isAvailable(OsmandApplication ctx) {
if (this == BROUTER) { if (this == BROUTER) {
try { return ctx.getBRouterService() != null;
BRouterServiceConnection c = BRouterServiceConnection.connect(ctx);
if(c != null) {
c.disconnect(ctx);
return true;
}
} catch (Exception e) {
return false;
}
} }
return true; return true;
} }
public static RouteService[] getAvailableRouters(Context ctx){ public static RouteService[] getAvailableRouters(OsmandApplication ctx){
List<RouteService> list = new ArrayList<RouteProvider.RouteService>(); List<RouteService> list = new ArrayList<RouteProvider.RouteService>();
for(RouteService r : values()) { for(RouteService r : values()) {
if(r.isAvailable(ctx)) { if(r.isAvailable(ctx)) {
@ -873,33 +859,6 @@ public class RouteProvider {
} }
static class BRouterServiceConnection implements ServiceConnection {
IBRouterService brouterService;
public void onServiceConnected(ComponentName className, IBinder boundService) {
brouterService = IBRouterService.Stub.asInterface((IBinder) boundService);
}
public void onServiceDisconnected(ComponentName className) {
brouterService = null;
}
public void disconnect(Context ctx){
ctx.unbindService(this);
}
public static BRouterServiceConnection connect(Context ctx){
BRouterServiceConnection conn = new BRouterServiceConnection();
Intent intent = new Intent();
intent.setClassName("btools.routingapp", "btools.routingapp.BRouterService");
boolean hasBRouter = ctx.bindService(intent, conn, Context.BIND_AUTO_CREATE);
if(!hasBRouter){
conn = null;
}
return conn;
}
};
protected RouteCalculationResult findBROUTERRoute(RouteCalculationParams params) throws MalformedURLException, protected RouteCalculationResult findBROUTERRoute(RouteCalculationParams params) throws MalformedURLException,
IOException, ParserConfigurationException, FactoryConfigurationError, SAXException { IOException, ParserConfigurationException, FactoryConfigurationError, SAXException {
double[] lats = new double[] { params.start.getLatitude(), params.end.getLatitude() }; double[] lats = new double[] { params.start.getLatitude(), params.end.getLatitude() };
@ -923,19 +882,12 @@ public class RouteProvider {
List<Location> res = new ArrayList<Location>(); List<Location> res = new ArrayList<Location>();
BRouterServiceConnection conn = BRouterServiceConnection.connect(ctx); IBRouterService brouterService = ctx.getBRouterService();
if (conn == null) { if (brouterService == null) {
return new RouteCalculationResult("BRouter service is not available"); return new RouteCalculationResult("BRouter service is not available");
} }
long begin = System.currentTimeMillis();
try { try {
while((System.currentTimeMillis() - begin) < 15000 && conn.brouterService == null) { String kmlMessage = brouterService.getTrackFromParams(bpars);
Thread.sleep(500);
}
if(conn.brouterService == null){
return new RouteCalculationResult("BRouter service is not available");
}
String kmlMessage = conn.brouterService.getTrackFromParams(bpars);
if (kmlMessage == null) if (kmlMessage == null)
kmlMessage = "no result from brouter"; kmlMessage = "no result from brouter";
if (!kmlMessage.startsWith("<")) { if (!kmlMessage.startsWith("<")) {
@ -976,7 +928,6 @@ public class RouteProvider {
return new RouteCalculationResult(item.getNodeValue()); return new RouteCalculationResult(item.getNodeValue());
} }
} }
conn.disconnect((Context) params.ctx);
} catch (Exception e) { } catch (Exception e) {
return new RouteCalculationResult("Exception calling BRouter: " + e); //$NON-NLS-1$ return new RouteCalculationResult("Exception calling BRouter: " + e); //$NON-NLS-1$
} }