code cleanup
This commit is contained in:
parent
c81e5a88ae
commit
656a316f87
10 changed files with 105 additions and 149 deletions
|
@ -8,6 +8,7 @@ public interface OsmAndCustomizationConstants {
|
||||||
String DRAWER_MAP_MARKERS_ID = DRAWER_ITEM_ID_SCHEME + "map_markers";
|
String DRAWER_MAP_MARKERS_ID = DRAWER_ITEM_ID_SCHEME + "map_markers";
|
||||||
String DRAWER_MY_PLACES_ID = DRAWER_ITEM_ID_SCHEME + "my_places";
|
String DRAWER_MY_PLACES_ID = DRAWER_ITEM_ID_SCHEME + "my_places";
|
||||||
String DRAWER_SEARCH_ID = DRAWER_ITEM_ID_SCHEME + "search";
|
String DRAWER_SEARCH_ID = DRAWER_ITEM_ID_SCHEME + "search";
|
||||||
|
String DRAWER_WEBSERVER_ID = DRAWER_ITEM_ID_SCHEME + "server";
|
||||||
String DRAWER_DIRECTIONS_ID = DRAWER_ITEM_ID_SCHEME + "directions";
|
String DRAWER_DIRECTIONS_ID = DRAWER_ITEM_ID_SCHEME + "directions";
|
||||||
String DRAWER_CONFIGURE_MAP_ID = DRAWER_ITEM_ID_SCHEME + "configure_map";
|
String DRAWER_CONFIGURE_MAP_ID = DRAWER_ITEM_ID_SCHEME + "configure_map";
|
||||||
String DRAWER_DOWNLOAD_MAPS_ID = DRAWER_ITEM_ID_SCHEME + "download_maps";
|
String DRAWER_DOWNLOAD_MAPS_ID = DRAWER_ITEM_ID_SCHEME + "download_maps";
|
||||||
|
|
|
@ -1009,20 +1009,6 @@ public class MapActivityActions implements DialogProvider {
|
||||||
}).createItem());
|
}).createItem());
|
||||||
*/
|
*/
|
||||||
|
|
||||||
optionsMenuHelper.addItem(new ItemBuilder().setTitleId(R.string.web_server, mapActivity)
|
|
||||||
.setId("SERVER_ID")
|
|
||||||
.setIcon(R.drawable.mm_shop_computer)
|
|
||||||
.setListener(new ItemClickListener() {
|
|
||||||
@Override
|
|
||||||
public boolean onContextMenuClick(ArrayAdapter<ContextMenuItem> adapter, int itemId, int pos, boolean isChecked, int[] viewCoordinates) {
|
|
||||||
app.logEvent("drawer_help_open");
|
|
||||||
Intent intent = new Intent(mapActivity, ServerActivity.class);
|
|
||||||
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
|
|
||||||
mapActivity.startActivity(intent);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}).createItem());
|
|
||||||
|
|
||||||
optionsMenuHelper.addItem(new ItemBuilder().setTitleId(R.string.shared_string_help, mapActivity)
|
optionsMenuHelper.addItem(new ItemBuilder().setTitleId(R.string.shared_string_help, mapActivity)
|
||||||
.setId(DRAWER_HELP_ID)
|
.setId(DRAWER_HELP_ID)
|
||||||
.setIcon(R.drawable.ic_action_help)
|
.setIcon(R.drawable.ic_action_help)
|
||||||
|
|
|
@ -73,7 +73,7 @@ public class ServerActivity extends AppCompatActivity {
|
||||||
OsmAndHttpServer.HOSTNAME = getDeviceAddress();
|
OsmAndHttpServer.HOSTNAME = getDeviceAddress();
|
||||||
try {
|
try {
|
||||||
server = new OsmAndHttpServer();
|
server = new OsmAndHttpServer();
|
||||||
server.setAndroidApplication((OsmandApplication) this.getApplication());
|
server.setApplication((OsmandApplication) this.getApplication());
|
||||||
initialized = true;
|
initialized = true;
|
||||||
updateTextView("Server started at: http://" + getDeviceAddress() + ":" + OsmAndHttpServer.PORT);
|
updateTextView("Server started at: http://" + getDeviceAddress() + ":" + OsmAndHttpServer.PORT);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|
|
@ -13,6 +13,7 @@ import net.osmand.plus.R;
|
||||||
import net.osmand.plus.Version;
|
import net.osmand.plus.Version;
|
||||||
import net.osmand.plus.activities.ContributionVersionActivity;
|
import net.osmand.plus.activities.ContributionVersionActivity;
|
||||||
import net.osmand.plus.activities.MapActivity;
|
import net.osmand.plus.activities.MapActivity;
|
||||||
|
import net.osmand.plus.activities.ServerActivity;
|
||||||
import net.osmand.plus.dashboard.tools.DashFragmentData;
|
import net.osmand.plus.dashboard.tools.DashFragmentData;
|
||||||
import net.osmand.plus.settings.fragments.BaseSettingsFragment;
|
import net.osmand.plus.settings.fragments.BaseSettingsFragment;
|
||||||
import net.osmand.plus.views.layers.MapInfoLayer;
|
import net.osmand.plus.views.layers.MapInfoLayer;
|
||||||
|
@ -21,6 +22,7 @@ import net.osmand.plus.views.OsmandMapTileView;
|
||||||
import net.osmand.plus.views.mapwidgets.widgets.TextInfoWidget;
|
import net.osmand.plus.views.mapwidgets.widgets.TextInfoWidget;
|
||||||
|
|
||||||
import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_BUILDS_ID;
|
import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_BUILDS_ID;
|
||||||
|
import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_WEBSERVER_ID;
|
||||||
|
|
||||||
public class OsmandDevelopmentPlugin extends OsmandPlugin {
|
public class OsmandDevelopmentPlugin extends OsmandPlugin {
|
||||||
|
|
||||||
|
@ -71,8 +73,20 @@ public class OsmandDevelopmentPlugin extends OsmandPlugin {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}).createItem());
|
}).createItem());
|
||||||
}
|
|
||||||
|
|
||||||
|
helper.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.web_server, mapActivity)
|
||||||
|
.setId(DRAWER_WEBSERVER_ID)
|
||||||
|
.setIcon(R.drawable.mm_shop_computer)
|
||||||
|
.setListener(new ContextMenuAdapter.ItemClickListener() {
|
||||||
|
@Override
|
||||||
|
public boolean onContextMenuClick(ArrayAdapter<ContextMenuItem> adapter, int itemId, int pos, boolean isChecked, int[] viewCoordinates) {
|
||||||
|
Intent intent = new Intent(mapActivity, ServerActivity.class);
|
||||||
|
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
|
||||||
|
mapActivity.startActivity(intent);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}).createItem());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -1,18 +1,10 @@
|
||||||
package net.osmand.plus.server;
|
package net.osmand.plus.server;
|
||||||
|
|
||||||
import java.util.concurrent.Callable;
|
|
||||||
|
|
||||||
import fi.iki.elonen.NanoHTTPD;
|
import fi.iki.elonen.NanoHTTPD;
|
||||||
|
import net.osmand.plus.OsmandApplication;
|
||||||
|
|
||||||
public class ApiEndpoint{
|
public interface ApiEndpoint {
|
||||||
public String uri = "";
|
NanoHTTPD.Response process(NanoHTTPD.IHTTPSession session);
|
||||||
public ApiCall apiCall;
|
|
||||||
|
|
||||||
public NanoHTTPD.Response run(NanoHTTPD.IHTTPSession session){
|
void setApplication(OsmandApplication application);
|
||||||
return apiCall.call(session);
|
|
||||||
}
|
|
||||||
|
|
||||||
public interface ApiCall{
|
|
||||||
NanoHTTPD.Response call(NanoHTTPD.IHTTPSession session);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,87 +5,29 @@ import android.webkit.MimeTypeMap;
|
||||||
import fi.iki.elonen.NanoHTTPD;
|
import fi.iki.elonen.NanoHTTPD;
|
||||||
import net.osmand.PlatformUtil;
|
import net.osmand.PlatformUtil;
|
||||||
import net.osmand.plus.OsmandApplication;
|
import net.osmand.plus.OsmandApplication;
|
||||||
import net.osmand.plus.server.endpoints.TileEndpoint;
|
|
||||||
import net.osmand.util.Algorithms;
|
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.nio.charset.Charset;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import static fi.iki.elonen.NanoHTTPD.newFixedLengthResponse;
|
import static fi.iki.elonen.NanoHTTPD.newFixedLengthResponse;
|
||||||
|
|
||||||
public class ApiRouter {
|
public class ApiRouter {
|
||||||
private OsmandApplication application;
|
private static final String FOLDER_NAME = "server";
|
||||||
private final String FOLDER_NAME = "server";
|
|
||||||
private final Map<String, ApiEndpoint> endpoints = new HashMap<>();
|
|
||||||
private static final org.apache.commons.logging.Log LOG = PlatformUtil.getLog(ApiRouter.class);
|
private static final org.apache.commons.logging.Log LOG = PlatformUtil.getLog(ApiRouter.class);
|
||||||
|
private final Map<String, ApiEndpoint> endpoints = new HashMap<>();
|
||||||
public ApiRouter() {
|
private OsmandApplication application;
|
||||||
initRoutes();
|
|
||||||
}
|
|
||||||
|
|
||||||
public OsmandApplication getApplication() {
|
public OsmandApplication getApplication() {
|
||||||
return application;
|
return application;
|
||||||
}
|
}
|
||||||
|
|
||||||
final TileEndpoint tileEndpoint = new TileEndpoint(application);
|
|
||||||
|
|
||||||
private void initRoutes() {
|
|
||||||
endpoints.put(tileEndpoint.uri, tileEndpoint);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setApplication(OsmandApplication application) {
|
public void setApplication(OsmandApplication application) {
|
||||||
this.application = application;
|
this.application = application;
|
||||||
tileEndpoint.setApplication(application);
|
for (String s : endpoints.keySet()) {
|
||||||
}
|
endpoints.get(s).setApplication(application);
|
||||||
|
|
||||||
public NanoHTTPD.Response route(NanoHTTPD.IHTTPSession session) {
|
|
||||||
Log.d("SERVER", "URI: " + session.getUri());
|
|
||||||
String uri = session.getUri();
|
|
||||||
if (uri.equals("/")) {
|
|
||||||
return getStatic("/go.html");
|
|
||||||
}
|
}
|
||||||
if (uri.contains("/scripts/") ||
|
|
||||||
uri.contains("/images/") ||
|
|
||||||
uri.contains("/css/") ||
|
|
||||||
uri.contains("/fonts/") ||
|
|
||||||
uri.contains("/favicon.ico")
|
|
||||||
) {
|
|
||||||
return getStatic(uri);
|
|
||||||
}
|
|
||||||
if (isApiUrl(uri)) {
|
|
||||||
return routeApi(session);
|
|
||||||
} else {
|
|
||||||
return getStatic(session.getUri());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private NanoHTTPD.Response routeApi(NanoHTTPD.IHTTPSession session) {
|
|
||||||
String uri = session.getUri();
|
|
||||||
int pathEnd = uri.indexOf("/", 1);
|
|
||||||
if (pathEnd != -1) {
|
|
||||||
uri = uri.substring(0, pathEnd);
|
|
||||||
}
|
|
||||||
ApiEndpoint endpoint = endpoints.get(uri);
|
|
||||||
if (endpoint != null) {
|
|
||||||
return endpoint.apiCall.call(session);
|
|
||||||
}
|
|
||||||
return ErrorResponses.response404;
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isApiUrl(String uri) {
|
|
||||||
for (String endpoint : endpoints.keySet()) {
|
|
||||||
int stringLength = endpoint.length();
|
|
||||||
if (uri.startsWith(endpoint) &&
|
|
||||||
(uri.length() == endpoint.length() || uri.charAt(stringLength) == '/')) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public NanoHTTPD.Response getStatic(String uri) {
|
public NanoHTTPD.Response getStatic(String uri) {
|
||||||
|
@ -109,6 +51,48 @@ public class ApiRouter {
|
||||||
return ErrorResponses.response500;
|
return ErrorResponses.response500;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public NanoHTTPD.Response route(NanoHTTPD.IHTTPSession session) {
|
||||||
|
Log.d("SERVER", "URI: " + session.getUri());
|
||||||
|
String uri = session.getUri();
|
||||||
|
if (uri.equals("/")) {
|
||||||
|
return getStatic("/go.html");
|
||||||
|
}
|
||||||
|
if (isApiUrl(uri)) {
|
||||||
|
return routeApi(session);
|
||||||
|
}
|
||||||
|
return getStatic(uri);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void register(String path, ApiEndpoint endpoint) {
|
||||||
|
endpoint.setApplication(application);
|
||||||
|
endpoints.put(path, endpoint);
|
||||||
|
}
|
||||||
|
|
||||||
|
private NanoHTTPD.Response routeApi(NanoHTTPD.IHTTPSession session) {
|
||||||
|
String uri = session.getUri();
|
||||||
|
int pathEnd = uri.indexOf("/", 1);
|
||||||
|
if (pathEnd != -1) {
|
||||||
|
uri = uri.substring(0, pathEnd);
|
||||||
|
}
|
||||||
|
ApiEndpoint endpoint = endpoints.get(uri);
|
||||||
|
if (endpoint != null) {
|
||||||
|
return endpoint.process(session);
|
||||||
|
}
|
||||||
|
return ErrorResponses.response404;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isApiUrl(String uri) {
|
||||||
|
for (String endpoint : endpoints.keySet()) {
|
||||||
|
int stringLength = endpoint.length();
|
||||||
|
if (uri.startsWith(endpoint) &&
|
||||||
|
(uri.length() == endpoint.length() || uri.charAt(stringLength) == '/')) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private String parseMimeType(String url) {
|
private String parseMimeType(String url) {
|
||||||
String type = "text/plain";
|
String type = "text/plain";
|
||||||
if (url.endsWith(".js")) return "text/javascript";
|
if (url.endsWith(".js")) return "text/javascript";
|
||||||
|
|
|
@ -1,34 +1,38 @@
|
||||||
package net.osmand.plus.server;
|
package net.osmand.plus.server;
|
||||||
|
|
||||||
|
import fi.iki.elonen.NanoHTTPD;
|
||||||
import net.osmand.plus.OsmandApplication;
|
import net.osmand.plus.OsmandApplication;
|
||||||
|
import net.osmand.plus.server.endpoints.TileEndpoint;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import fi.iki.elonen.NanoHTTPD;
|
|
||||||
|
|
||||||
public class OsmAndHttpServer extends NanoHTTPD {
|
public class OsmAndHttpServer extends NanoHTTPD {
|
||||||
public static int PORT = 24990;
|
public static int PORT = 24990;
|
||||||
public static String HOSTNAME = "0.0.0.0";
|
public static String HOSTNAME = "0.0.0.0";
|
||||||
|
private final ApiRouter router = new ApiRouter();
|
||||||
private ServerSessionHandler sessionHandler = new ServerSessionHandler();
|
private OsmandApplication application;
|
||||||
private OsmandApplication androidApplication;
|
|
||||||
|
|
||||||
public OsmandApplication getAndroidApplication() {
|
|
||||||
return androidApplication;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAndroidApplication(OsmandApplication androidApplication) {
|
|
||||||
this.androidApplication = androidApplication;
|
|
||||||
sessionHandler.setAndroidApplication(androidApplication);
|
|
||||||
}
|
|
||||||
|
|
||||||
public OsmAndHttpServer() throws IOException {
|
public OsmAndHttpServer() throws IOException {
|
||||||
super(HOSTNAME,PORT);
|
super(HOSTNAME, PORT);
|
||||||
start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
|
start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
|
||||||
|
registerEndpoints();
|
||||||
|
}
|
||||||
|
|
||||||
|
public OsmandApplication getApplication() {
|
||||||
|
return application;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setApplication(OsmandApplication application) {
|
||||||
|
this.application = application;
|
||||||
|
router.setApplication(application);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void registerEndpoints() {
|
||||||
|
router.register("/tile", new TileEndpoint(application));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Response serve(IHTTPSession session) {
|
public Response serve(IHTTPSession session) {
|
||||||
return sessionHandler.handle(session);
|
return router.route(session);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,24 +0,0 @@
|
||||||
package net.osmand.plus.server;
|
|
||||||
|
|
||||||
import net.osmand.plus.OsmandApplication;
|
|
||||||
|
|
||||||
import fi.iki.elonen.NanoHTTPD;
|
|
||||||
|
|
||||||
public class ServerSessionHandler {
|
|
||||||
private OsmandApplication androidApplication;
|
|
||||||
|
|
||||||
private ApiRouter router = new ApiRouter();
|
|
||||||
|
|
||||||
public OsmandApplication getAndroidApplication() {
|
|
||||||
return androidApplication;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAndroidApplication(OsmandApplication androidApplication) {
|
|
||||||
this.androidApplication = androidApplication;
|
|
||||||
router.setApplication(androidApplication);
|
|
||||||
}
|
|
||||||
|
|
||||||
public NanoHTTPD.Response handle(NanoHTTPD.IHTTPSession session) {
|
|
||||||
return router.route(session);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -22,27 +22,30 @@ import java.util.concurrent.*;
|
||||||
|
|
||||||
import static fi.iki.elonen.NanoHTTPD.newFixedLengthResponse;
|
import static fi.iki.elonen.NanoHTTPD.newFixedLengthResponse;
|
||||||
|
|
||||||
public class TileEndpoint extends ApiEndpoint {
|
public class TileEndpoint implements ApiEndpoint {
|
||||||
|
private static final Log LOG = PlatformUtil.getLog(TileEndpoint.class);
|
||||||
ExecutorService executor = Executors.newFixedThreadPool(3);
|
ExecutorService executor = Executors.newFixedThreadPool(3);
|
||||||
Map<RotatedTileBox, Bitmap> hashMap = new HashMap<>();
|
Map<RotatedTileBox, Bitmap> hashMap = new HashMap<>();
|
||||||
Map<RotatedTileBox, Bitmap> map = Collections.synchronizedMap(hashMap);
|
Map<RotatedTileBox, Bitmap> map = Collections.synchronizedMap(hashMap);
|
||||||
OsmandApplication application;
|
OsmandApplication application;
|
||||||
private static final Log LOG = PlatformUtil.getLog(TileEndpoint.class);
|
|
||||||
|
|
||||||
public TileEndpoint(OsmandApplication application) {
|
public TileEndpoint(OsmandApplication application) {
|
||||||
this.application = application;
|
this.application = application;
|
||||||
this.uri = "/tile";
|
}
|
||||||
this.apiCall = new ApiEndpoint.ApiCall() {
|
|
||||||
@Override
|
@Override
|
||||||
public NanoHTTPD.Response call(NanoHTTPD.IHTTPSession session) {
|
public NanoHTTPD.Response process(NanoHTTPD.IHTTPSession session) {
|
||||||
try {
|
try {
|
||||||
return tileApiCall(session);
|
return tileApiCall(session);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LOG.error("Exception", e);
|
LOG.error("Exception", e);
|
||||||
}
|
}
|
||||||
return ApiRouter.ErrorResponses.response500;
|
return ApiRouter.ErrorResponses.response500;
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
@Override
|
||||||
|
public void setApplication(OsmandApplication application) {
|
||||||
|
this.application = application;
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized NanoHTTPD.Response tileApiCall(NanoHTTPD.IHTTPSession session) {
|
private synchronized NanoHTTPD.Response tileApiCall(NanoHTTPD.IHTTPSession session) {
|
||||||
|
@ -96,8 +99,4 @@ public class TileEndpoint extends ApiEndpoint {
|
||||||
return ApiRouter.ErrorResponses.response500;
|
return ApiRouter.ErrorResponses.response500;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setApplication(OsmandApplication application) {
|
|
||||||
this.application = application;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Binary file not shown.
Loading…
Reference in a new issue