From 1389c178d98bb9a2294f9a36af1b6bf3161951b1 Mon Sep 17 00:00:00 2001 From: vshcherb Date: Sun, 12 Jan 2014 01:22:51 +0100 Subject: [PATCH] Add brouter service --- .../routingapp/BRouterServiceConnection.java | 38 +++++++++++ .../net/osmand/plus/OsmandApplication.java | 16 +++++ .../SettingsNavigationActivity.java | 2 +- .../osmand/plus/routing/RouteProvider.java | 63 +++---------------- 4 files changed, 62 insertions(+), 57 deletions(-) create mode 100644 OsmAnd/src/btools/routingapp/BRouterServiceConnection.java diff --git a/OsmAnd/src/btools/routingapp/BRouterServiceConnection.java b/OsmAnd/src/btools/routingapp/BRouterServiceConnection.java new file mode 100644 index 0000000000..b9a9fb6afb --- /dev/null +++ b/OsmAnd/src/btools/routingapp/BRouterServiceConnection.java @@ -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; + } + } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/OsmandApplication.java b/OsmAnd/src/net/osmand/plus/OsmandApplication.java index 7ff4d161e9..ec73dfbbb4 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandApplication.java +++ b/OsmAnd/src/net/osmand/plus/OsmandApplication.java @@ -57,6 +57,8 @@ import android.os.Handler; import android.os.Message; import android.text.format.DateFormat; import android.widget.Toast; +import btools.routingapp.BRouterServiceConnection; +import btools.routingapp.IBRouterService; import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.app.SherlockActivity; @@ -99,6 +101,7 @@ public class OsmandApplication extends Application implements ClientContext { InternalToDoAPI internalToDoAPI; InternalOsmAndAPI internalOsmAndAPI; SQLiteAPI sqliteAPI; + BRouterServiceConnection bRouterServiceConnection; @Override public void onCreate() { @@ -119,6 +122,12 @@ public class OsmandApplication extends Application implements ClientContext { internalToDoAPI = new net.osmand.plus.api.InternalToDoAPIImpl(this); internalOsmAndAPI = new net.osmand.plus.api.InternalOsmAndAPIImpl(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 osmandSettings = createOsmandSettingsInstance(); @@ -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) { if (prefferedLocale != null) { Configuration config = context.getResources().getConfiguration(); diff --git a/OsmAnd/src/net/osmand/plus/activities/SettingsNavigationActivity.java b/OsmAnd/src/net/osmand/plus/activities/SettingsNavigationActivity.java index 22f97abedb..816a5e1495 100644 --- a/OsmAnd/src/net/osmand/plus/activities/SettingsNavigationActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/SettingsNavigationActivity.java @@ -76,7 +76,7 @@ public class SettingsNavigationActivity extends SettingsBaseActivity { } registerListPreference(settings.AUTO_FOLLOW_ROUTE, screen, entries, intValues); - RouteService[] vls = RouteService.getAvailableRouters(this); + RouteService[] vls = RouteService.getAvailableRouters(getMyApplication()); entries = new String[vls.length]; for(int i=0; i list = new ArrayList(); for(RouteService r : values()) { 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, IOException, ParserConfigurationException, FactoryConfigurationError, SAXException { double[] lats = new double[] { params.start.getLatitude(), params.end.getLatitude() }; @@ -923,19 +882,12 @@ public class RouteProvider { List res = new ArrayList(); - BRouterServiceConnection conn = BRouterServiceConnection.connect(ctx); - if (conn == null) { + IBRouterService brouterService = ctx.getBRouterService(); + if (brouterService == null) { return new RouteCalculationResult("BRouter service is not available"); } - long begin = System.currentTimeMillis(); try { - while((System.currentTimeMillis() - begin) < 15000 && conn.brouterService == null) { - Thread.sleep(500); - } - if(conn.brouterService == null){ - return new RouteCalculationResult("BRouter service is not available"); - } - String kmlMessage = conn.brouterService.getTrackFromParams(bpars); + String kmlMessage = brouterService.getTrackFromParams(bpars); if (kmlMessage == null) kmlMessage = "no result from brouter"; if (!kmlMessage.startsWith("<")) { @@ -976,7 +928,6 @@ public class RouteProvider { return new RouteCalculationResult(item.getNodeValue()); } } - conn.disconnect((Context) params.ctx); } catch (Exception e) { return new RouteCalculationResult("Exception calling BRouter: " + e); //$NON-NLS-1$ }