basic progress dialog

This commit is contained in:
MadWasp79 2019-12-15 00:02:47 +02:00
parent 7839cb2c32
commit a21d25d5e6
5 changed files with 110 additions and 43 deletions

View file

@ -21,6 +21,7 @@ import net.osmand.util.MapUtils;
import org.apache.commons.logging.Log;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
@ -143,10 +144,17 @@ public class GeocodingUtilities {
}
}
public Map<RouteDataObject, Location> multipleReverseGeocodingSearch(RoutingContext ctx, List<Location> points, boolean allowEmptyNames) throws IOException {
public interface RouteSearchProgressCallback {
void onRouteFoundProgress(int percent);
}
public Map<RouteDataObject, Location> multipleReverseGeocodingSearch(RoutingContext ctx, List<Location> points, boolean allowEmptyNames, WeakReference<RouteSearchProgressCallback> progressCallbackWeakRef) throws IOException {
RoutePlannerFrontEnd rp = new RoutePlannerFrontEnd();
Map<RouteDataObject, Location> result = new HashMap<>();
for (Location point : points) {
int prevProgress = 0;
int batchSize = points.size();
for (int i = 0; i < batchSize; i++) {
Location point = points.get(i);
List<GeocodingResult> lst = new ArrayList<>();
List<RouteSegmentPoint> listR = new ArrayList<>();
rp.findRouteSegment(point.getLatitude(), point.getLongitude(), ctx, listR);
@ -185,6 +193,13 @@ public class GeocodingUtilities {
Collections.sort(lst, GeocodingUtilities.DISTANCE_COMPARATOR);
if (lst.size() > 0) {
result.put(lst.get(0).point.getRoad(), point);
if (progressCallbackWeakRef.get() != null) {
int progress = (int) (i * 100.0/batchSize);
if (progress > prevProgress) {
progressCallbackWeakRef.get().onRouteFoundProgress(progress);
prevProgress = progress;
}
}
// log.debug(String.format("Road %s", lst.get(0).point.getRoad()));
}
}

View file

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/avoid_route_pb"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/list_content_padding"
/>
</LinearLayout>

View file

@ -23,6 +23,7 @@ import net.osmand.util.MapUtils;
import org.apache.commons.logging.Log;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@ -60,8 +61,9 @@ public class CurrentPositionHelper {
public boolean getMultipleRouteSegmentsIds(List<Location> points,
@Nullable ApplicationMode appMode,
boolean cancelPreviousSearch,
ResultMatcher<Map<RouteDataObject, Location>> result) {
return scheduleMultipleRouteSegmentFind(points, false, true, cancelPreviousSearch, null, result, appMode);
ResultMatcher<Map<RouteDataObject, Location>> result,
final WeakReference<GeocodingUtilities.RouteSearchProgressCallback> progressCallbackWeakRef) {
return scheduleMultipleRouteSegmentFind(points, false, true, cancelPreviousSearch, null, result, appMode, progressCallbackWeakRef);
}
@ -132,7 +134,8 @@ public class CurrentPositionHelper {
final boolean cancelPreviousSearch,
@Nullable final ResultMatcher<GeocodingResult> geoCoding,
@Nullable final ResultMatcher<Map<RouteDataObject, Location>> result,
@Nullable final ApplicationMode appMode) {
@Nullable final ApplicationMode appMode,
final WeakReference<GeocodingUtilities.RouteSearchProgressCallback> progressCallbackWeakRef) {
boolean res = false;
if (points.get(0) != null) {
long requestKey = getRequestNumberKey(storeFound, allowEmptyNames);
@ -146,7 +149,7 @@ public class CurrentPositionHelper {
singleThreadExecutor.submit(new Runnable() {
@Override
public void run() {
processMultipleGeocoding(points, geoCoding, storeFound, allowEmptyNames, result, appMode, request, finalRequestNumber, cancelPreviousSearch);
processMultipleGeocoding(points, geoCoding, storeFound, allowEmptyNames, result, appMode, request, finalRequestNumber, cancelPreviousSearch, progressCallbackWeakRef);
}
});
res = true;
@ -162,14 +165,15 @@ public class CurrentPositionHelper {
@Nullable ApplicationMode appMode,
int request,
@NonNull AtomicInteger requestNumber,
boolean cancelPreviousSearch) {
boolean cancelPreviousSearch,
WeakReference<GeocodingUtilities.RouteSearchProgressCallback> progressCallbackWeakRef) {
if (cancelPreviousSearch && request != requestNumber.get()) {
return;
}
final Map<RouteDataObject, Location> gr = runUpdateInThreadBatch(points,
geoCoding != null, allowEmptyNames, appMode);
geoCoding != null, allowEmptyNames, appMode, progressCallbackWeakRef);
if(result != null) {
app.runInUIThread(new Runnable() {
@ -186,7 +190,8 @@ public class CurrentPositionHelper {
private Map<RouteDataObject, Location> runUpdateInThreadBatch(List<Location> points,
boolean geocoding,
boolean allowEmptyNames,
@Nullable ApplicationMode appMode) {
@Nullable ApplicationMode appMode,
WeakReference<GeocodingUtilities.RouteSearchProgressCallback> progressCallbackWeakRef) {
List<BinaryMapReaderResource> checkReaders = usedReaders;
for (Location loc : points) {
@ -203,7 +208,7 @@ public class CurrentPositionHelper {
}
}
try {
return new GeocodingUtilities().multipleReverseGeocodingSearch(geocoding ? defCtx : ctx, points, allowEmptyNames);
return new GeocodingUtilities().multipleReverseGeocodingSearch(geocoding ? defCtx : ctx, points, allowEmptyNames,progressCallbackWeakRef);
} catch (Exception e) {
log.error("Exception happened during runUpdateInThread", e);
return null;

View file

@ -2,6 +2,8 @@ package net.osmand.plus;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
@ -14,6 +16,7 @@ import net.osmand.GeoidAltitudeCorrection;
import net.osmand.PlatformUtil;
import net.osmand.ResultMatcher;
import net.osmand.access.NavigationInfo;
import net.osmand.binary.GeocodingUtilities;
import net.osmand.binary.GeocodingUtilities.GeocodingResult;
import net.osmand.binary.RouteDataObject;
import net.osmand.data.LatLon;
@ -884,8 +887,9 @@ public class OsmAndLocationProvider implements SensorEventListener {
public boolean getMultipleRouteSegmentsIds(List<net.osmand.Location> points,
@Nullable ApplicationMode appMode,
boolean cancelPreviousSearch,
ResultMatcher<Map<RouteDataObject, net.osmand.Location>> result) {
return currentPositionHelper.getMultipleRouteSegmentsIds(points, appMode, cancelPreviousSearch, result);
ResultMatcher<Map<RouteDataObject, net.osmand.Location>> result,
final WeakReference<GeocodingUtilities.RouteSearchProgressCallback> progressCallbackWeakRef) {
return currentPositionHelper.getMultipleRouteSegmentsIds(points, appMode, cancelPreviousSearch, result, progressCallbackWeakRef);
}
public boolean getGeocodingResult(net.osmand.Location loc, ResultMatcher<GeocodingResult> result) {

View file

@ -6,6 +6,7 @@ import android.content.DialogInterface;
import android.os.AsyncTask;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
@ -17,6 +18,7 @@ import net.osmand.IndexConstants;
import net.osmand.Location;
import net.osmand.PlatformUtil;
import net.osmand.ResultMatcher;
import net.osmand.binary.GeocodingUtilities;
import net.osmand.binary.RouteDataObject;
import net.osmand.data.QuadRect;
import net.osmand.data.QuadTree;
@ -60,9 +62,11 @@ public class AvoidRoadsHelper {
private final List<Location> parsedPoints;
private final OsmandApplication app;
private final ApplicationMode appMode;
private CompletionCallback completionCallback;
private ParsingProgressCallback ppc;
private GeocodingUtilities.RouteSearchProgressCallback ppc;
private ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
private CompletionCallback completionCallback;
private UpdateProgressCallback progressDialogCallback;
private WeakReference<MapActivity> mapActivityWeakReference = null;
private static boolean saveResultToFile = false;
@ -72,6 +76,8 @@ public class AvoidRoadsHelper {
private long timeParsePoints;
private long timeFindSegments;
private AlertDialog pDialog;
private final int[] pointsToProcess = {-1};
// private String inputFileName = "points_500.json";
// private String inputFileName = "point_100.json";
@ -90,13 +96,15 @@ public class AvoidRoadsHelper {
@Override
public void onRDOSearchComplete() {
timeFindSegments = System.currentTimeMillis();
if (pDialog.isShowing()) {
pDialog.dismiss();
}
pDialog = null;
if (saveResultToFile) {
File out = new File (app.getAppPath(IndexConstants.AVOID_ROADS_DIR).getAbsolutePath() + "/"
+ "processed_ids.json");
File out = new File (app.getAppPath(IndexConstants.AVOID_ROADS_DIR).getAbsolutePath() + "/processed_ids.json");
saveRoadsToJson(roadsToAvoid, out);
}
app.getAvoidRoadsHelper().addResultToImpassibleRoads(roadsToAvoid);
MapActivity mapActivity = mapActivityWeakReference.get();
if (mapActivity != null) {
app.getAvoidRoadsHelper().showParsingCompleteDialog(mapActivity);
@ -110,11 +118,49 @@ public class AvoidRoadsHelper {
app.getRoutingConfig().clearImpassableRoads();
parsedPoints.clear();
parsedPoints.addAll(result);
final MapActivity mapActivity = mapActivityWeakReference.get();
if (mapActivity != null) {
if (pDialog == null) {
pDialog = buildProgressDialog(mapActivity).show();
}
}
convertPointsToRDO(parsedPoints);
}
};
ppc = new GeocodingUtilities.RouteSearchProgressCallback() {
@Override
public void onRouteFoundProgress(int percent) {
LOG.debug("Progress: " + percent);
final MapActivity mapActivity = mapActivityWeakReference.get();
if (mapActivity != null) {
// if (!pDialog.isShowing()) {
// pDialog.show();
// }
final ProgressBar pb = (ProgressBar) ((AlertDialog) pDialog).findViewById(R.id.avoid_route_pb);
pb.setProgress(percent);
}
}
};
}
public void testRun() {
String in = "";
switch (SOURCE) {
case FROM_STORAGE:
in = app.getAppPath(IndexConstants.AVOID_ROADS_DIR).getAbsolutePath() + "/" + inputFileName;
LOG.debug(String.format("Input json from file: %s", in));
break;
case FROM_URL:
LOG.debug(String.format("Loading json from url: %s", in));
break;
}
loadJson(in, SOURCE);
}
public Map<RouteDataObject, Location> getRoadsToAvoid() {
return roadsToAvoid;
}
@ -123,16 +169,15 @@ public class AvoidRoadsHelper {
app.getRoutingConfig().addMultipleImpassableRoads(result);
}
public void progressDialog(final MapActivity activity) {
public AlertDialog.Builder buildProgressDialog(final MapActivity activity) {
AlertDialog.Builder adb = new AlertDialog.Builder(activity);
adb.setMessage("Searching Roads to Avoid...");
adb.setMessage("Determining Roads to Avoid...");
final View dialogProgressView = activity.getLayoutInflater().inflate(R.layout.avoid_roads_progress_dialog, null);
adb.setView(dialogProgressView);
return adb;
ppc = new ParsingProgressCallback() {
@Override
public void onParsingProgress(int percent) {
}
};
}
public void showUrlDialog(final MapActivity activity) {
@ -215,21 +260,6 @@ public class AvoidRoadsHelper {
db.show();
}
public void testRun() {
String in = "";
switch (SOURCE) {
case FROM_STORAGE:
in = app.getAppPath(IndexConstants.AVOID_ROADS_DIR).getAbsolutePath() + "/" + inputFileName;
LOG.debug(String.format("Input json from file: %s", in));
break;
case FROM_URL:
LOG.debug(String.format("Loading json from url: %s", in));
break;
}
loadJson(in, SOURCE);
}
private void convertPointsToRDO(final List<Location> parsedPoints) {
this.roadsToAvoid.clear();
app.getLocationProvider().getMultipleRouteSegmentsIds(parsedPoints, appMode, false,
@ -250,7 +280,7 @@ public class AvoidRoadsHelper {
public boolean isCancelled() {
return false;
}
});
}, new WeakReference<GeocodingUtilities.RouteSearchProgressCallback>(ppc));
}
@ -376,10 +406,11 @@ public class AvoidRoadsHelper {
void onPointsParsed(List<Location> result);
}
interface ParsingProgressCallback {
void onParsingProgress(int percent);
interface UpdateProgressCallback {
void onProgressUpdate(int progress);
}
class GeoJSON {
@SerializedName("type")
@Expose
@ -423,7 +454,6 @@ public class AvoidRoadsHelper {
}
class RoadToAvoid {
@SerializedName("road_id")
@Expose
long roadId;