Fix layout issue and auto start navigation

This commit is contained in:
Victor Shcherb 2014-06-09 22:56:41 +02:00
parent 516a70538a
commit 4e48598b54
4 changed files with 165 additions and 19 deletions

View file

@ -9,6 +9,8 @@
3. All your modified/created strings are in the top of the file (to make easier find what\'s translated).
PLEASE: Have a look at http://code.google.com/p/osmand/wiki/UIConsistency, it may really improve your and our work :-) Thx - Hardy
-->
<string name="selected_gpx_info_show">\n\nPress and hold to see on map</string>
<string name="delay_navigation_start">Start navigation with delay</string>
<string name="selected">selected</string>
<string name="gpx_split_interval">Select split interval</string>
<string name="local_index_gpx_info_show">\n\nPress and hold for options</string>
@ -115,6 +117,7 @@
* Supports GPX/KML import (convert from KML to GPX)
* GPX tracks has moved to \'My Data\'
* You can split GPX track by distance and check altitude difference/ speed
* Auto start navigation after delay
</string>
<string name="use_points_as_intermediates">Calculate route between points</string>
<string name="osmo_mode_restart">Restart OsMo session</string>

View file

@ -754,6 +754,8 @@ public class OsmandSettings {
AUTO_ZOOM_MAP.setModeDefaultValue(ApplicationMode.PEDESTRIAN, AutoZoomMap.NONE);
}
public final CommonPreference<Integer> DELAY_TO_START_NAVIGATION = new IntPreference("delay_to_start_navigation", 10).makeGlobal().cache();
public final CommonPreference<Boolean> SNAP_TO_ROAD = new BooleanPreference("snap_to_road", false).makeProfile().cache();
{
SNAP_TO_ROAD.setModeDefaultValue(ApplicationMode.CAR, true);

View file

@ -239,6 +239,7 @@ public class MapInfoLayer extends OsmandMapLayer {
public void createControls() {
// 1. Create view groups and controls
statusBar.setBackgroundDrawable(view.getResources().getDrawable(R.drawable.box_top));
statusBar.addView(createBackToLocation(new MapInfoWidgetsFactory(scaleCoefficient)));
rightStack = new StackWidgetView(view.getContext());
leftStack = new StackWidgetView(view.getContext());

View file

@ -1,14 +1,31 @@
package net.osmand.plus.views.controls;
import gnu.trove.list.array.TIntArrayList;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import net.osmand.data.RotatedTileBox;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.routing.RoutingHelper;
import net.osmand.plus.views.ShadowText;
import net.osmand.plus.views.OsmandMapLayer.DrawSettings;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.text.TextPaint;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
@ -16,11 +33,64 @@ import android.widget.FrameLayout;
public class MapNavigateControl extends MapControls {
private Button navigateButton;
private MapRouteInfoControl ri;
private Runnable delayStart;
private Drawable navigateShadow;
private Bitmap mapMagnifier;
private TextPaint counterTextPaint;
private Paint bitmapPaint;
private static AtomicInteger startCounter = new AtomicInteger();
public MapNavigateControl(MapRouteInfoControl ri, MapActivity mapActivity, Handler showUIHandler, float scaleCoefficient) {
super(mapActivity, showUIHandler, scaleCoefficient);
this.ri = ri;
counterTextPaint = new TextPaint();
counterTextPaint.setTextSize(18 * scaleCoefficient);
counterTextPaint.setAntiAlias(true);
counterTextPaint.setFakeBoldText(true);
}
public void startCounter() {
OsmandSettings settings = mapActivity.getMyApplication().getSettings();
startCounter.set(settings.DELAY_TO_START_NAVIGATION.get());
delayStart = new Runnable() {
@Override
public void run() {
int cnt = startCounter.decrementAndGet();
if (cnt == 0) {
startNavigation();
} else if (cnt > 0)
mapActivity.refreshMap();
showUIHandler.postDelayed(delayStart, 1000);
}
};
if(startCounter.get() > 0) {
delayStart.run();
}
}
private void startNavigation() {
startCounter.set(-1);
OsmandApplication app = mapActivity.getMyApplication();
RoutingHelper routingHelper = app.getRoutingHelper();
if(routingHelper.isFollowingMode()) {
routingHelper.setRoutePlanningMode(false);
MapActivity.getMapViewTrackingUtilities().switchToRoutePlanningMode();
} else {
OsmandApplication ctx = mapActivity.getMyApplication();
if(!ctx.getTargetPointsHelper().checkPointToNavigateShort()) {
ri.showDialog();
} else {
MapActivity.getMapViewTrackingUtilities().backToLocationImpl();
app.getSettings().FOLLOW_THE_ROUTE.set(true);
routingHelper.setFollowingMode(true);
routingHelper.setRoutePlanningMode(false);
MapActivity.getMapViewTrackingUtilities().switchToRoutePlanningMode();
routingHelper.setCurrentLocation(app.getLocationProvider().getLastKnownLocation(), false);
app.getRoutingHelper().notifyIfRouteIsCalculated();
}
}
}
@Override
@ -29,36 +99,106 @@ public class MapNavigateControl extends MapControls {
navigateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
OsmandApplication app = mapActivity.getMyApplication();
RoutingHelper routingHelper = app.getRoutingHelper();
if(routingHelper.isFollowingMode()) {
routingHelper.setRoutePlanningMode(false);
mapActivity.getMapViewTrackingUtilities().switchToRoutePlanningMode();
} else {
OsmandApplication ctx = mapActivity.getMyApplication();
if(!ctx.getTargetPointsHelper().checkPointToNavigateShort()) {
ri.showDialog();
} else {
mapActivity.getMapViewTrackingUtilities().backToLocationImpl();
app.getSettings().FOLLOW_THE_ROUTE.set(true);
routingHelper.setFollowingMode(true);
routingHelper.setRoutePlanningMode(false);
mapActivity.getMapViewTrackingUtilities().switchToRoutePlanningMode();
routingHelper.setCurrentLocation(app.getLocationProvider().getLastKnownLocation(), false);
app.getRoutingHelper().notifyIfRouteIsCalculated();
}
}
startNavigation();
}
});
if(!mapActivity.getRoutingHelper().isFollowingMode()) {
startCounter();
}
}
@Override
protected void initControls(FrameLayout layout) {
super.initControls(layout);
navigateShadow = mapActivity.getResources().getDrawable(R.drawable.zoom_background).mutate();
mapMagnifier = BitmapFactory.decodeResource(mapActivity.getResources(), R.drawable.map_magnifier);
bitmapPaint = new Paint();
}
@Override
public void hideControls(FrameLayout layout) {
removeButton(layout, navigateButton);
startCounter.set(-1);
}
@Override
public void onDraw(Canvas canvas, RotatedTileBox tileBox, DrawSettings nightMode) {
if(!mapActivity.getRoutingHelper().isFollowingMode()) {
drawCount(canvas, tileBox);
}
}
public boolean onSingleTap(PointF point, RotatedTileBox tileBox) {
startCounter.set(-1);
if (navigateShadow.getBounds().contains((int) point.x, (int) point.y)) {
startCounter.set(-1);
openDialog();
return true;
}
return false;
}
private void openDialog() {
Builder bld = new AlertDialog.Builder(mapActivity);
final TIntArrayList opt = new TIntArrayList();
List<String> items = new ArrayList<String>();
int[] checkedItem = new int[]{ -1};
Integer selected = mapActivity.getMyApplication().getSettings().DELAY_TO_START_NAVIGATION.get();
addOpt(-1, items, opt, checkedItem, selected);
addOpt(3, items, opt, checkedItem, selected);
addOpt(5, items, opt, checkedItem, selected);
addOpt(7, items, opt, checkedItem, selected);
addOpt(10, items, opt, checkedItem, selected);
addOpt(15, items, opt, checkedItem, selected);
addOpt(20, items, opt, checkedItem, selected);
bld.setTitle(R.string.delay_navigation_start);
bld.setSingleChoiceItems(items.toArray(new String[items.size()]), checkedItem[0], new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
mapActivity.getMyApplication().getSettings().DELAY_TO_START_NAVIGATION.set(opt.get(which));
startCounter();
}
});
bld.show();
}
private void addOpt(int i, List<String> items, TIntArrayList opt, int[] checkedItem, int selected) {
if(i == selected) {
checkedItem[0] = items.size();
}
opt.add(i);
if(i < 0) {
items.add(mapActivity.getString(R.string.none));
} else {
items.add(i + " " +mapActivity.getString(R.string.int_seconds));
}
}
private void drawCount(Canvas canvas, RotatedTileBox tb) {
if (navigateShadow.getBounds().width() == 0 && navigateButton.getWidth() > 0) {
navigateShadow.setBounds(navigateButton.getLeft() - 2, navigateButton.getTop()
- (int) (18 * scaleCoefficient), navigateButton.getRight(), navigateButton.getBottom());
}
if(navigateShadow.getBounds().width() > 0) {
navigateShadow.draw(canvas);
}
int get = startCounter.get();
if (get > 0) {
final String text = get + "";
float length = counterTextPaint.measureText(text);
ShadowText.draw(text, canvas, navigateButton.getLeft() + (navigateButton.getWidth() - length - 2) / 2,
navigateButton.getTop() + 4 * scaleCoefficient, counterTextPaint, shadowColor);
} else {
int size = (int) (16 * scaleCoefficient);
int top = (int) (navigateButton.getTop() - size - 4 * scaleCoefficient);
int left = (int) (navigateButton.getLeft() + (navigateButton.getWidth() - mapMagnifier.getWidth() - 2 * scaleCoefficient) / 2);
// canvas density /2 ? size * 2
canvas.drawBitmap(mapMagnifier, null, new Rect(left, top, left + size * 2, top + size * 2), bitmapPaint);
}
}
public int getWidth() {