From c249c05dc54ea9c7b095a23d33d4611b5367caa0 Mon Sep 17 00:00:00 2001 From: Denis_Bars Date: Mon, 29 Sep 2014 18:10:13 +0300 Subject: [PATCH] Refactor --- OsmAnd/res/values/strings.xml | 2 + .../src/net/osmand/plus/OsmandSettings.java | 2 + .../osmand/plus/activities/MapActivity.java | 72 ++++--- .../SettingsDevelopmentActivity.java | 2 + .../osmand/plus/views/OsmandMapTileView.java | 9 +- .../views/controllers/MapViewController.java | 203 ++++++++++++++++++ 6 files changed, 254 insertions(+), 36 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/views/controllers/MapViewController.java diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index 3c685b88d7..c2758f89a3 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -11,6 +11,8 @@ --> Test native render Starts activity with native render + Use native render + Use C++ render instead of Java Set the text size on the map. Text size Speed limit diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index 04c161b956..8015097e34 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -699,6 +699,8 @@ public class OsmandSettings { // magnetic field doesn'torkmost of the time on some phones public final OsmandPreference USE_MAGNETIC_FIELD_SENSOR_COMPASS = new BooleanPreference("use_magnetic_field_sensor_compass", false).makeGlobal().cache(); public final OsmandPreference USE_KALMAN_FILTER_FOR_COMPASS = new BooleanPreference("use_kalman_filter_compass", true).makeGlobal().cache(); + + public final OsmandPreference USE_NATIVE_RENDER = new BooleanPreference("use_native_render", false).makeGlobal().cache(); public final CommonPreference TEXT_SCALE = new FloatPreference("text_scale", 1f).makeProfile().cache(); { diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index 77daf3907f..e8430e7277 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -9,6 +9,7 @@ import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; +import android.opengl.GLSurfaceView; import net.osmand.Location; import net.osmand.StateChangedListener; import net.osmand.access.AccessibilityPlugin; @@ -42,6 +43,7 @@ import net.osmand.plus.routing.RoutingHelper.RouteCalculationProgressCallback; import net.osmand.plus.views.AnimateDraggingMapThread; import net.osmand.plus.views.OsmandMapLayer; import net.osmand.plus.views.OsmandMapTileView; +import net.osmand.plus.views.controllers.MapViewController; import net.osmand.render.RenderingRulesStorage; import net.osmand.util.Algorithms; import android.app.Dialog; @@ -78,8 +80,7 @@ public class MapActivity extends AccessibleActivity { private static MapViewTrackingUtilities mapViewTrackingUtilities; /** Called when the activity is first created. */ - private OsmandMapTileView mapView; - + private MapActivityActions mapActions; private MapActivityLayers mapLayers; @@ -100,6 +101,7 @@ public class MapActivity extends AccessibleActivity { private StateChangedListener applicationModeListener; private FrameLayout lockView; private GpxImportHelper gpxImportHelper; + private MapViewController mapViewController; private Notification getNotification() { @@ -126,32 +128,39 @@ public class MapActivity extends AccessibleActivity { requestWindowFeature(Window.FEATURE_NO_TITLE); // Full screen is not used here //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); - setContentView(R.layout.main); + startProgressDialog = new ProgressDialog(this); startProgressDialog.setCancelable(true); app.checkApplicationIsBeingInitialized(this, startProgressDialog); parseLaunchIntentLocation(); - - mapView = (OsmandMapTileView) findViewById(R.id.MapView); - mapView.setTrackBallDelegate(new OsmandMapTileView.OnTrackBallListener(){ + + if (settings.USE_NATIVE_RENDER.get()){ + setContentView(R.layout.main); + mapViewController = new MapViewController((OsmandMapTileView) findViewById(R.id.MapView), this); + } else { + setContentView(R.layout.activity_gl); + mapViewController = new MapViewController((GLSurfaceView) findViewById(R.id.glSurfaceView), this); + } + + mapViewController.setTrackBallDelegate(new MapViewController.OnTrackBallListener(){ @Override public boolean onTrackBallEvent(MotionEvent e) { showAndHideMapPosition(); return MapActivity.this.onTrackballEvent(e); } }); - mapView.setAccessibilityActions(new MapAccessibilityActions(this)); + mapViewController.setAccessibilityActions(new MapAccessibilityActions(this)); if(mapViewTrackingUtilities == null) { mapViewTrackingUtilities = new MapViewTrackingUtilities(app); } - mapViewTrackingUtilities.setMapView(mapView); + mapViewController.setTrackingUtilities(mapViewTrackingUtilities); // Do some action on close startProgressDialog.setOnDismissListener(new DialogInterface.OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { app.getResourceManager().getRenderer().clearCache(); - mapView.refreshMap(true); + mapViewController.refreshMap(true); } }); @@ -163,12 +172,12 @@ public class MapActivity extends AccessibleActivity { mgr.tileDownloaded(request); } if(request == null || !request.error){ - mapView.tileDownloaded(request); + mapViewController.tileDownloaded(request); } } }); createProgressBarForRouting(); - mapLayers.createLayers(mapView); + mapViewController.createLayers(mapLayers); // This situtation could be when navigation suddenly crashed and after restarting // it tries to continue the last route if (settings.FOLLOW_THE_ROUTE.get() && !app.getRoutingHelper().isRouteCalculated() @@ -180,14 +189,15 @@ public class MapActivity extends AccessibleActivity { // show first time when application ran net.osmand.Location location = app.getLocationProvider().getFirstTimeRunDefaultLocation(); if(location != null){ - mapView.setLatLon(location.getLatitude(), location.getLongitude()); - mapView.setIntZoom(14); + mapViewController.setLatLon(location.getLatitude(), location.getLongitude()); + mapViewController.setIntZoom(14); } } addDialogProvider(mapActions); OsmandPlugin.onMapActivityCreate(this); if(lockView != null) { - ((FrameLayout)mapView.getParent()).addView(lockView); + mapViewController.addView(lockView); + } gpxImportHelper = new GpxImportHelper(this, getMyApplication(), getMapView()); } @@ -197,7 +207,7 @@ public class MapActivity extends AccessibleActivity { } private void createProgressBarForRouting() { - FrameLayout parent = (FrameLayout) mapView.getParent(); + FrameLayout parent = (FrameLayout) mapViewController.getParentView(); FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL | Gravity.TOP); DisplayMetrics dm = getResources().getDisplayMetrics(); @@ -238,7 +248,7 @@ public class MapActivity extends AccessibleActivity { @Override public Object onRetainCustomNonConfigurationInstance() { LinkedHashMap l = new LinkedHashMap(); - for(OsmandMapLayer ml : mapView.getLayers() ) { + for(OsmandMapLayer ml : mapViewController.getLayers() ) { ml.onRetainNonConfigurationInstance(l); } return l; @@ -301,8 +311,8 @@ public class MapActivity extends AccessibleActivity { if (settings != null && settings.isLastKnownMapLocation()) { LatLon l = settings.getLastKnownMapLocation(); - mapView.setLatLon(l.getLatitude(), l.getLongitude()); - mapView.setIntZoom(settings.getLastKnownMapZoom()); + mapViewController.setLatLon(l.getLatitude(), l.getLongitude()); + mapViewController.setIntZoom(settings.getLastKnownMapZoom()); } settings.MAP_ACTIVITY_ENABLED.set(true); @@ -310,7 +320,7 @@ public class MapActivity extends AccessibleActivity { checkExternalStorage(); showAndHideMapPosition(); - LatLon cur = new LatLon(mapView.getLatitude(), mapView.getLongitude()); + LatLon cur = new LatLon(mapViewController.getLatitude(), mapViewController.getLongitude()); LatLon latLonToShow = settings.getAndClearMapLocationToShow(); String mapLabelToShow = settings.getAndClearMapLabelToShow(); Object toShow = settings.getAndClearObjectToShow(); @@ -318,8 +328,8 @@ public class MapActivity extends AccessibleActivity { if(status != 0){ // always enable and follow and let calculate it (i.e.GPS is not accessible in a garage) Location loc = new Location("map"); - loc.setLatitude(mapView.getLatitude()); - loc.setLongitude(mapView.getLongitude()); + loc.setLatitude(mapViewController.getLatitude()); + loc.setLongitude(mapViewController.getLongitude()); getMapActions().enterRoutePlanningMode(null, null, status == OsmandSettings.NAVIGATE_CURRENT_GPX); } if(mapLabelToShow != null && latLonToShow != null){ @@ -327,7 +337,7 @@ public class MapActivity extends AccessibleActivity { mapLayers.getContextMenuLayer().setLocation(latLonToShow, mapLabelToShow); } if (latLonToShow != null && !latLonToShow.equals(cur)) { - mapView.getAnimatedDraggingThread().startMoving(latLonToShow.getLatitude(), latLonToShow.getLongitude(), + mapViewController.startMoving(latLonToShow.getLatitude(), latLonToShow.getLongitude(), settings.getMapZoomToShow(), true); } if(latLonToShow != null) { @@ -376,7 +386,7 @@ public class MapActivity extends AccessibleActivity { } OsmandPlugin.onMapActivityResume(this); - mapView.refreshMap(true); + mapViewController.refreshMap(true); } @@ -417,8 +427,8 @@ public class MapActivity extends AccessibleActivity { // if (settings.AUTO_ZOOM_MAP.get() == AutoZoomMap.NONE) { // changeLocation = false; // } - final int newZoom = mapView.getZoom() + stp; - mapView.getAnimatedDraggingThread().startZooming(newZoom, changeLocation); + final int newZoom = mapViewController.getZoom() + stp; + mapViewController.startZooming(newZoom, changeLocation); if (app.accessibilityEnabled()) AccessibleToast.makeText(this, getString(R.string.zoomIs) + " " + newZoom, Toast.LENGTH_SHORT).show(); //$NON-NLS-1$ showAndHideMapPosition(); @@ -455,7 +465,7 @@ public class MapActivity extends AccessibleActivity { OsmandPlugin.getEnabledPlugin(AccessibilityPlugin.class) != null) { // Find more appropriate plugin for it? if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && event.getRepeatCount() == 0) { - if (mapView.isZooming()) { + if (mapViewController.isZooming()) { changeZoom(+ 2); } else { changeZoom(+ 1); @@ -470,7 +480,7 @@ public class MapActivity extends AccessibleActivity { } public void setMapLocation(double lat, double lon){ - mapView.setLatLon(lat, lon); + mapViewController.setLatLon(lat, lon); mapViewTrackingUtilities.locationChanged(lat, lon, this); } @@ -479,7 +489,7 @@ public class MapActivity extends AccessibleActivity { if(event.getAction() == MotionEvent.ACTION_MOVE && settings.USE_TRACKBALL_FOR_MOVEMENTS.get()){ float x = event.getX(); float y = event.getY(); - final RotatedTileBox tb = mapView.getCurrentRotatedTileBox(); + final RotatedTileBox tb = mapViewController.getCurrentRotatedTileBox(); final QuadPoint cp = tb.getCenterPixelPoint(); final LatLon l = tb.getLatLonFromPixel(cp.x + x * 15, cp.y + y * 15); setMapLocation(l.getLatitude(), l.getLongitude()); @@ -538,7 +548,7 @@ public class MapActivity extends AccessibleActivity { public LatLon getMapLocation(){ - return new LatLon(mapView.getLatitude(), mapView.getLongitude()); + return new LatLon(mapViewController.getLatitude(), mapViewController.getLongitude()); } // Duplicate methods to OsmAndApplication @@ -557,14 +567,14 @@ public class MapActivity extends AccessibleActivity { app.getDaynightHelper().stopSensorIfNeeded(); settings.APPLICATION_MODE.removeListener(applicationModeListener); - settings.setLastKnownMapLocation((float) mapView.getLatitude(), (float) mapView.getLongitude()); + settings.setLastKnownMapLocation((float) mapViewController.getLatitude(), (float) mapViewController.getLongitude()); AnimateDraggingMapThread animatedThread = mapView.getAnimatedDraggingThread(); if(animatedThread.isAnimating() && animatedThread.getTargetIntZoom() != 0){ settings.setMapLocationToShow(animatedThread.getTargetLatitude(), animatedThread.getTargetLongitude(), animatedThread.getTargetIntZoom()); } - settings.setLastKnownMapZoom(mapView.getZoom()); + settings.setLastKnownMapZoom(mapViewController.getZoom()); settings.MAP_ACTIVITY_ENABLED.set(false); app.setMapActivity(null); app.getResourceManager().interruptRendering(); diff --git a/OsmAnd/src/net/osmand/plus/development/SettingsDevelopmentActivity.java b/OsmAnd/src/net/osmand/plus/development/SettingsDevelopmentActivity.java index f46e8970e1..bfde20feb1 100644 --- a/OsmAnd/src/net/osmand/plus/development/SettingsDevelopmentActivity.java +++ b/OsmAnd/src/net/osmand/plus/development/SettingsDevelopmentActivity.java @@ -41,6 +41,8 @@ public class SettingsDevelopmentActivity extends SettingsBaseActivity { cat.addPreference(createCheckBoxPreference(settings.DISABLE_COMPLEX_ROUTING, R.string.disable_complex_routing, R.string.disable_complex_routing_descr)); cat.addPreference(createCheckBoxPreference(settings.USE_MAGNETIC_FIELD_SENSOR_COMPASS, R.string.use_magnetic_sensor, R.string.use_magnetic_sensor_descr)); + + cat.addPreference(createCheckBoxPreference(settings.USE_NATIVE_RENDER, R.string.use_native_render,R.string.use_native_render_descr)); Preference pref = new Preference(this); pref.setTitle(R.string.test_voice_prompts); diff --git a/OsmAnd/src/net/osmand/plus/views/OsmandMapTileView.java b/OsmAnd/src/net/osmand/plus/views/OsmandMapTileView.java index e34d874f3d..503a6ded41 100644 --- a/OsmAnd/src/net/osmand/plus/views/OsmandMapTileView.java +++ b/OsmAnd/src/net/osmand/plus/views/OsmandMapTileView.java @@ -26,6 +26,7 @@ import net.osmand.plus.R; import net.osmand.plus.helpers.SimpleTwoFingerTapDetector; import net.osmand.plus.views.MultiTouchSupport.MultiTouchZoomListener; import net.osmand.plus.views.OsmandMapLayer.DrawSettings; +import net.osmand.plus.views.controllers.MapViewController; import net.osmand.util.MapUtils; import org.apache.commons.logging.Log; @@ -81,9 +82,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall protected static final int emptyTileDivisor = 16; - public interface OnTrackBallListener { - public boolean onTrackBallEvent(MotionEvent e); - } + public interface OnLongClickListener { public boolean onLongPressEvent(PointF point); @@ -110,7 +109,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall private OnClickListener onClickListener; - private OnTrackBallListener trackBallDelegate; + private MapViewController.OnTrackBallListener trackBallDelegate; private AccessibilityActionsProvider accessibilityActions; @@ -746,7 +745,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall return super.onTrackballEvent(event); } - public void setTrackBallDelegate(OnTrackBallListener trackBallDelegate) { + public void setTrackBallDelegate(MapViewController.OnTrackBallListener trackBallDelegate) { this.trackBallDelegate = trackBallDelegate; } diff --git a/OsmAnd/src/net/osmand/plus/views/controllers/MapViewController.java b/OsmAnd/src/net/osmand/plus/views/controllers/MapViewController.java new file mode 100644 index 0000000000..0e14655379 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/views/controllers/MapViewController.java @@ -0,0 +1,203 @@ +package net.osmand.plus.views.controllers; + +import android.opengl.GLSurfaceView; +import android.view.MotionEvent; +import android.view.View; +import android.view.ViewParent; +import android.widget.FrameLayout; +import net.osmand.access.MapAccessibilityActions; +import net.osmand.data.RotatedTileBox; +import net.osmand.map.MapTileDownloader; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.OsmandSettings; +import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.activities.MapActivityLayers; +import net.osmand.plus.base.MapViewTrackingUtilities; +import net.osmand.plus.views.OsmandMapLayer; +import net.osmand.plus.views.OsmandMapTileView; + +import java.util.List; + +/** + * Created by Натали on 29.09.2014. + */ +public class MapViewController { + private GLSurfaceView glSurfaceView; + private OsmandMapTileView mapTileView; + private OsmandSettings settings; + private MapActivity mapActivity; + private boolean isNative = false; + + public void setAccessibilityActions(MapAccessibilityActions accessibilityActions) { + if (isNative){ + + } else { + mapTileView.setAccessibilityActions(accessibilityActions); + } + } + + public void refreshMap(boolean b) { + if (isNative){ + + } else { + mapTileView.refreshMap(b); + } + } + + public void createLayers(MapActivityLayers mapLayers) { + if (isNative) { + + } else { + mapLayers.createLayers(mapTileView); + } + } + + public void setLatLon(double latitude, double longitude) { + if (isNative){ + + } else { + mapTileView.setLatLon(latitude, longitude); + } + } + + public void setIntZoom(int i) { + if (isNative) { + + } else { + mapTileView.setIntZoom(i); + } + } + + public void addView(FrameLayout view) { + if (isNative) { + + } else { + ((FrameLayout)mapTileView.getParent()).addView(view); + } + } + + public void setTrackingUtilities(MapViewTrackingUtilities mapViewTrackingUtilities) { + if (isNative) { + + } else { + mapViewTrackingUtilities.setMapView(mapTileView); + } + } + + public void tileDownloaded(MapTileDownloader.DownloadRequest request) { + if (isNative) { + + } else { + mapTileView.tileDownloaded(request); + } + } + + public ViewParent getParentView() { + if (isNative) { + return glSurfaceView.getParent(); + } else { + return mapTileView.getParent(); + } + } + + public List getLayers() { + if (isNative) { + + } else { + return mapTileView.getLayers(); + } + return null; + } + + public double getLatitude() { + if (isNative){ + + } else { + return mapTileView.getLatitude(); + } + return 0; + } + + public double getLongitude() { + if (isNative){ + + } else { + return mapTileView.getLongitude(); + } + return 0; + } + + public void startMoving(double latitude, double longitude, int mapZoomToShow, boolean b) { + if (isNative){ + + } else { + mapTileView.getAnimatedDraggingThread().startMoving(latitude, longitude, + settings.getMapZoomToShow(), true); + } + + } + + public int getZoom() { + if (isNative) { + + } else { + return mapTileView.getZoom(); + } + return 0; + } + + public void startZooming(int newZoom, boolean changeLocation) { + if (isNative){ + + } else { + mapTileView.getAnimatedDraggingThread().startZooming(newZoom, changeLocation); + } + } + + public boolean isZooming() { + if (isNative){ + + } else { + return mapTileView.isZooming(); + } + return false; + } + + public RotatedTileBox getCurrentRotatedTileBox() { + if (isNative) { + + } else { + return mapTileView.getCurrentRotatedTileBox(); + } + return null; + } + + + public interface OnTrackBallListener { + public boolean onTrackBallEvent(MotionEvent e); + } + + public MapViewController(GLSurfaceView surfaceView, MapActivity activity){ + this.glSurfaceView = surfaceView; + this.settings = activity.getMyApplication().getSettings(); + this.mapActivity = activity; + isNative = true; + + } + + public MapViewController(OsmandMapTileView mapTileView, MapActivity activity){ + this.mapTileView = mapTileView; + this.settings = activity.getMyApplication().getSettings(); + this.mapActivity = activity; + isNative = false; + } + + public void setTrackBallDelegate(OnTrackBallListener trackBallDelegate) { + if (isNative){ + + } else { + mapTileView.setTrackBallDelegate(trackBallDelegate); + } + } + +}