use of net.osmand.Location in project Osmand by osmandapp.
the class WaypointDialogHelper method switchStartAndFinish.
// switch start & finish
private static void switchStartAndFinish(TargetPointsHelper targetPointsHelper, TargetPoint finish, Activity ctx, TargetPoint start, OsmandApplication app, WaypointDialogHelper helper) {
targetPointsHelper.setStartPoint(new LatLon(finish.getLatitude(), finish.getLongitude()), false, finish.getPointDescription(ctx));
if (start == null) {
Location loc = app.getLocationProvider().getLastKnownLocation();
if (loc != null) {
targetPointsHelper.navigateToPoint(new LatLon(loc.getLatitude(), loc.getLongitude()), true, -1);
}
} else {
targetPointsHelper.navigateToPoint(new LatLon(start.getLatitude(), start.getLongitude()), true, -1, start.getPointDescription(ctx));
}
updateControls(ctx, helper);
}
use of net.osmand.Location in project Osmand by osmandapp.
the class WaypointDialogHelper method sortAllTargets.
@SuppressLint("StaticFieldLeak")
public static void sortAllTargets(final OsmandApplication app, final Activity activity, final WaypointDialogHelper helper) {
new AsyncTask<Void, Void, int[]>() {
ProgressDialog dlg = null;
long startDialogTime = 0;
List<TargetPoint> intermediates;
protected void onPreExecute() {
startDialogTime = System.currentTimeMillis();
dlg = new ProgressDialog(activity);
dlg.setTitle("");
dlg.setMessage(activity.getResources().getString(R.string.intermediate_items_sort_by_distance));
dlg.show();
}
protected int[] doInBackground(Void[] params) {
TargetPointsHelper targets = app.getTargetPointsHelper();
intermediates = targets.getIntermediatePointsWithTarget();
Location cll = app.getLocationProvider().getLastKnownLocation();
ArrayList<TargetPoint> lt = new ArrayList<>(intermediates);
TargetPoint start;
if (cll != null) {
LatLon ll = new LatLon(cll.getLatitude(), cll.getLongitude());
start = TargetPoint.create(ll, null);
} else if (app.getTargetPointsHelper().getPointToStart() != null) {
TargetPoint ps = app.getTargetPointsHelper().getPointToStart();
LatLon ll = new LatLon(ps.getLatitude(), ps.getLongitude());
start = TargetPoint.create(ll, null);
} else {
start = lt.get(0);
}
TargetPoint end = lt.remove(lt.size() - 1);
ArrayList<LatLon> al = new ArrayList<>();
for (TargetPoint p : lt) {
al.add(p.point);
}
return new TspAnt().readGraph(al, start.point, end.point).solve();
}
protected void onPostExecute(int[] result) {
if (dlg != null) {
long t = System.currentTimeMillis();
if (t - startDialogTime < 500) {
app.runInUIThread(new Runnable() {
@Override
public void run() {
dlg.dismiss();
}
}, 500 - (t - startDialogTime));
} else {
dlg.dismiss();
}
}
List<TargetPoint> alocs = new ArrayList<>();
for (int i : result) {
if (i > 0) {
TargetPoint loc = intermediates.get(i - 1);
alocs.add(loc);
}
}
intermediates.clear();
intermediates.addAll(alocs);
TargetPointsHelper targets = app.getTargetPointsHelper();
List<TargetPoint> cur = targets.getIntermediatePointsWithTarget();
boolean eq = true;
for (int j = 0; j < cur.size() && j < intermediates.size(); j++) {
if (cur.get(j) != intermediates.get(j)) {
eq = false;
break;
}
}
if (!eq) {
targets.reorderAllTargetPoints(intermediates, true);
}
if (helper.helperCallbacks != null) {
helper.helperCallbacks.reloadAdapter();
}
updateRouteInfoMenu(activity);
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
use of net.osmand.Location in project Osmand by osmandapp.
the class WaypointDialogHelper method getStandardPoints.
protected List<Object> getStandardPoints() {
final List<Object> points = new ArrayList<>();
boolean rc = waypointHelper.isRouteCalculated();
for (int i = 0; i < WaypointHelper.MAX; i++) {
List<LocationPointWrapper> tp = waypointHelper.getWaypoints(i);
if ((rc || i == WaypointHelper.WAYPOINTS || i == WaypointHelper.TARGETS) && waypointHelper.isTypeVisible(i)) {
if (points.size() > 0) {
points.add(true);
}
points.add(i);
if (i == WaypointHelper.TARGETS) {
TargetPoint start = app.getTargetPointsHelper().getPointToStart();
if (start == null) {
LatLon latLon;
Location loc = app.getLocationProvider().getLastKnownLocation();
if (loc != null) {
latLon = new LatLon(loc.getLatitude(), loc.getLongitude());
} else {
latLon = new LatLon(mapActivity.getMapView().getLatitude(), mapActivity.getMapView().getLongitude());
}
start = TargetPoint.createStartPoint(latLon, new PointDescription(PointDescription.POINT_TYPE_MY_LOCATION, mapActivity.getString(R.string.shared_string_my_location)));
} else {
String oname = start.getOnlyName().length() > 0 ? start.getOnlyName() : (mapActivity.getString(R.string.route_descr_map_location) + " " + mapActivity.getString(R.string.route_descr_lat_lon, start.getLatitude(), start.getLongitude()));
start = TargetPoint.createStartPoint(new LatLon(start.getLatitude(), start.getLongitude()), new PointDescription(PointDescription.POINT_TYPE_LOCATION, oname));
}
points.add(new LocationPointWrapper(null, WaypointHelper.TARGETS, start, 0f, 0));
} else if ((i == WaypointHelper.POI || i == WaypointHelper.FAVORITES || i == WaypointHelper.WAYPOINTS) && rc) {
if (waypointHelper.isTypeEnabled(i)) {
points.add(new RadiusItem(i));
}
}
if (tp != null && tp.size() > 0) {
points.addAll(tp);
}
points.add(false);
}
}
return points;
}
use of net.osmand.Location in project Osmand by osmandapp.
the class WaypointHelper method getMostImportantAlarm.
public AlarmInfo getMostImportantAlarm(MetricsConstants mc, boolean showCameras) {
Location lastProjection = app.getRoutingHelper().getLastProjection();
float mxspeed = route.getCurrentMaxSpeed();
float delta = app.getSettings().SPEED_LIMIT_EXCEED.get() / 3.6f;
AlarmInfo speedAlarm = createSpeedAlarm(mc, mxspeed, lastProjection, delta);
if (speedAlarm != null) {
getVoiceRouter().announceSpeedAlarm(speedAlarm.getIntValue(), lastProjection.getSpeed());
}
AlarmInfo mostImportant = speedAlarm;
int value = speedAlarm != null ? speedAlarm.updateDistanceAndGetPriority(0, 0) : Integer.MAX_VALUE;
float speed = lastProjection != null && lastProjection.hasSpeed() ? lastProjection.getSpeed() : 0;
if (ALARMS < pointsProgress.size()) {
int kIterator = pointsProgress.get(ALARMS);
List<LocationPointWrapper> lp = locationPoints.get(ALARMS);
while (kIterator < lp.size()) {
AlarmInfo inf = (AlarmInfo) lp.get(kIterator).point;
int currentRoute = route.getCurrentRoute();
if (inf.getLocationIndex() < currentRoute && inf.getLastLocationIndex() != -1 && inf.getLastLocationIndex() < currentRoute) {
// skip
} else {
if (inf.getType() == AlarmInfoType.TUNNEL && inf.getLastLocationIndex() != -1 && currentRoute > inf.getLocationIndex() && currentRoute < inf.getLastLocationIndex()) {
inf.setFloatValue(route.getDistanceToPoint(inf.getLastLocationIndex()));
}
int d = route.getDistanceToPoint(inf.getLocationIndex());
if (d > LONG_ANNOUNCE_RADIUS) {
break;
}
float time = speed > 0 ? d / speed : Integer.MAX_VALUE;
int vl = inf.updateDistanceAndGetPriority(time, d);
if (vl < value && (showCameras || inf.getType() != AlarmInfoType.SPEED_CAMERA)) {
mostImportant = inf;
value = vl;
}
}
kIterator++;
}
}
return mostImportant;
}
use of net.osmand.Location in project Osmand by osmandapp.
the class FirstUsageWizardFragment method onStart.
@Override
public void onStart() {
super.onStart();
final OsmandApplication app = getMyApplication();
switch(wizardType) {
case SEARCH_LOCATION:
if (searchLocationByIp) {
final Map<String, String> pms = new LinkedHashMap<>();
pms.put("version", Version.getFullVersion(app));
try {
pms.put("aid", Secure.getString(app.getContentResolver(), Secure.ANDROID_ID));
} catch (Exception e) {
e.printStackTrace();
}
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
try {
return AndroidNetworkUtils.sendRequest(app, "https://osmand.net/api/geo-ip", pms, "Requesting location by IP...", false, false);
} catch (Exception e) {
logError("Requesting location by IP error: ", e);
return null;
}
}
@Override
protected void onPostExecute(String response) {
if (response != null) {
try {
JSONObject obj = new JSONObject(response);
double latitude = obj.getDouble("latitude");
double longitude = obj.getDouble("longitude");
if (latitude == 0 && longitude == 0) {
showNoLocationFragment(getActivity());
} else {
location = new Location("geo-ip");
location.setLatitude(latitude);
location.setLongitude(longitude);
showSearchMapFragment(getActivity());
}
} catch (Exception e) {
logError("JSON parsing error: ", e);
showNoLocationFragment(getActivity());
}
} else {
showNoLocationFragment(getActivity());
}
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
FragmentActivity activity = getActivity();
if (!OsmAndLocationProvider.isLocationPermissionAvailable(activity)) {
ActivityCompat.requestPermissions(activity, new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, FIRST_USAGE_LOCATION_PERMISSION);
} else {
app.getLocationProvider().addLocationListener(this);
locationSearchTimer = new Timer();
locationSearchTimer.schedule(new TimerTask() {
@Override
public void run() {
FragmentActivity a = getActivity();
if (a != null) {
showNoLocationFragment(a);
}
}
}, 1000 * 10);
}
}
break;
case NO_INTERNET:
break;
case NO_LOCATION:
break;
case SEARCH_MAP:
if (app.isApplicationInitializing()) {
app.getAppInitializer().addListener(this);
} else {
if (!downloadThread.getIndexes().isDownloadedFromInternet) {
waitForIndexes = true;
downloadThread.runReloadIndexFilesSilent();
} else {
searchMap();
}
}
break;
case MAP_FOUND:
break;
case MAP_DOWNLOAD:
if (!startDownload(0)) {
startDownload(1);
}
if (localDownloadRegion != null) {
downloadThread.initSettingsFirstMap(localDownloadRegion);
}
break;
}
}
Aggregations