From 11f104afe9d9cbde714ea09eedb3882d61047fe6 Mon Sep 17 00:00:00 2001 From: Manuel Fuhr Date: Sun, 24 Apr 2022 14:23:58 +0200 Subject: [PATCH 01/15] Rearange BRouterActivity/BRouterView --- .../btools/routingapp/BRouterActivity.java | 47 +++++++------------ .../java/btools/routingapp/BRouterView.java | 31 +++++------- 2 files changed, 29 insertions(+), 49 deletions(-) diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java b/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java index c3253ce..a03f65d 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java @@ -48,10 +48,27 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat private static final int DIALOG_SHOW_REPEAT_TIMEOUT_HELP_ID = 16; private static final int DIALOG_SHOW_API23_HELP_ID = 17; - private BRouterView mBRouterView; private PowerManager mPowerManager; private WakeLock mWakeLock; + private String[] availableProfiles; + private String selectedProfile = null; + private List availableBasedirs; + private String[] basedirOptions; + private int selectedBasedir; + private String[] availableWaypoints; + private String[] routingModes; + private boolean[] routingModesChecked; + private String defaultbasedir = null; + private String message = null; + private String[] availableVias; + private Set selectedVias; + private List nogoList; + private String maptoolDirCandidate; + private Set dialogIds = new HashSet(); + private String errorMessage; + private String title; + private int wpCount; /** * Called when the activity is first created. @@ -359,28 +376,6 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat return checked; } - private String[] availableProfiles; - private String selectedProfile = null; - - private List availableBasedirs; - private String[] basedirOptions; - private int selectedBasedir; - - private String[] availableWaypoints; - - private String[] routingModes; - private boolean[] routingModesChecked; - - private String defaultbasedir = null; - private String message = null; - - private String[] availableVias; - private Set selectedVias; - - private List nogoList; - - private String maptoolDirCandidate; - @SuppressWarnings("deprecation") public void selectProfile(String[] items) { availableProfiles = items; @@ -486,8 +481,6 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat showDialog(DIALOG_NOGOSELECT_ID); } - private Set dialogIds = new HashSet(); - private void showNewDialog(int id) { if (dialogIds.contains(Integer.valueOf(id))) { removeDialog(id); @@ -496,10 +489,6 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat showDialog(id); } - private String errorMessage; - private String title; - private int wpCount; - @SuppressWarnings("deprecation") public void showErrorMessage(String msg) { errorMessage = msg; diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java b/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java index 2551d25..4fcdc10 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java @@ -47,10 +47,11 @@ import btools.router.RoutingHelper; import btools.util.CheapRuler; public class BRouterView extends View { + + public boolean canAccessSdCard; RoutingEngine cr; private int imgw; private int imgh; - private int centerLon; private int centerLat; private double scaleLon; // ilon -> pixel @@ -60,7 +61,6 @@ public class BRouterView extends View { private List nogoList; private List nogoVetoList; private OsmTrack rawTrack; - private File retryBaseDir; private File modesDir; private File tracksDir; @@ -73,32 +73,26 @@ public class BRouterView extends View { private boolean waitingForMigration = false; private String rawTrackPath; private String oldMigrationPath; - private boolean needsViaSelection; private boolean needsNogoSelection; private boolean needsWaypointSelection; - private WpDatabaseScanner dataBaseScanner; - private long lastDataTime = System.currentTimeMillis(); - private CoordinateReader cor; - private int[] imgPixels; - private int memoryClass; - - public boolean canAccessSdCard; - - public void stopRouting() { - if (cr != null) cr.terminate(); - } + private long lastTs = System.currentTimeMillis(); + private long startTime = 0L; public BRouterView(Context context, int memoryClass) { super(context); this.memoryClass = memoryClass; } + public void stopRouting() { + if (cr != null) cr.terminate(); + } + public void init() { try { // get base dir from private file @@ -114,9 +108,9 @@ public class BRouterView extends View { // don't ask twice String version = "v" + getContext().getString(R.string.app_version); - File vFile = new File(brd, "profiles2/"+version ); + File vFile = new File(brd, "profiles2/" + version); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q - && vFile.exists()) { + && vFile.exists()) { startSetup(baseDir, false); return; } @@ -568,7 +562,7 @@ public class BRouterView extends View { // for profile remote, use ref-track logic same as service interface rc.rawTrackPath = rawTrackPath; - cr = new RoutingEngine(tracksDir.getAbsolutePath()+"/brouter", null, segmentDir, wpList, rc); + cr = new RoutingEngine(tracksDir.getAbsolutePath() + "/brouter", null, segmentDir, wpList, rc); cr.start(); invalidate(); @@ -703,9 +697,6 @@ public class BRouterView extends View { lastDataTime += 4000; // give time for the toast before exiting } - private long lastTs = System.currentTimeMillis(); - private long startTime = 0L; - @Override protected void onDraw(Canvas canvas) { try { From 1d2809de7079c9c893a64475f30e08c6348fba41 Mon Sep 17 00:00:00 2001 From: Manuel Fuhr Date: Sun, 24 Apr 2022 14:25:12 +0200 Subject: [PATCH 02/15] Silent code cleanup --- .../main/java/btools/routingapp/BRouterActivity.java | 2 +- .../src/main/java/btools/routingapp/BRouterView.java | 11 ++++------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java b/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java index a03f65d..e4862da 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java @@ -65,7 +65,7 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat private Set selectedVias; private List nogoList; private String maptoolDirCandidate; - private Set dialogIds = new HashSet(); + private final Set dialogIds = new HashSet(); private String errorMessage; private String title; private int wpCount; diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java b/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java index 4fcdc10..df787c3 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java @@ -80,7 +80,7 @@ public class BRouterView extends View { private long lastDataTime = System.currentTimeMillis(); private CoordinateReader cor; private int[] imgPixels; - private int memoryClass; + private final int memoryClass; private long lastTs = System.currentTimeMillis(); private long startTime = 0L; @@ -203,10 +203,7 @@ public class BRouterView extends View { int deviceLevel = Build.VERSION.SDK_INT; int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion; - canAccessSdCard = true; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && !Environment.isExternalStorageLegacy()) { - canAccessSdCard = false; - } + canAccessSdCard = Build.VERSION.SDK_INT < Build.VERSION_CODES.Q || Environment.isExternalStorageLegacy(); if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { canAccessSdCard = false; } @@ -393,7 +390,7 @@ public class BRouterView extends View { try { cor.readAllPoints(); } catch (Exception e) { - msg = "Error reading waypoints: " + e.toString(); + msg = "Error reading waypoints: " + e; } int size = cor.allpoints.size(); @@ -962,7 +959,7 @@ public class BRouterView extends View { for (ServiceModeConfig smc : map.values()) { bw.write(smc.toLine()); bw.write('\n'); - msg.append(smc.toString()).append('\n'); + msg.append(smc).append('\n'); } } catch (Exception e) { } finally { From fc9deccad74d3f5ef118045461ae9cb0227ff84c Mon Sep 17 00:00:00 2001 From: Manuel Fuhr Date: Sun, 24 Apr 2022 14:33:28 +0200 Subject: [PATCH 03/15] Fix some AndroidStudio warnings --- .../btools/routingapp/BRouterActivity.java | 52 ++++---- .../java/btools/routingapp/BRouterView.java | 113 +++++++++--------- 2 files changed, 75 insertions(+), 90 deletions(-) diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java b/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java index e4862da..d26ce2e 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java @@ -14,6 +14,7 @@ import android.os.PowerManager.WakeLock; import android.os.StatFs; import android.widget.EditText; +import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.os.EnvironmentCompat; @@ -22,6 +23,7 @@ import java.io.File; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -65,7 +67,7 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat private Set selectedVias; private List nogoList; private String maptoolDirCandidate; - private final Set dialogIds = new HashSet(); + private final Set dialogIds = new HashSet<>(); private String errorMessage; private String title; private int wpCount; @@ -74,7 +76,6 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat * Called when the activity is first created. */ @Override - @SuppressWarnings("deprecation") public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -94,7 +95,6 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat } @Override - @SuppressWarnings("deprecation") protected Dialog onCreateDialog(int id) { AlertDialog.Builder builder; builder = new AlertDialog.Builder(this); @@ -372,11 +372,10 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat private boolean[] getCheckedBooleanArray(int size) { boolean[] checked = new boolean[size]; - for (int i = 0; i < checked.length; i++) checked[i] = true; + Arrays.fill(checked, true); return checked; } - @SuppressWarnings("deprecation") public void selectProfile(String[] items) { availableProfiles = items; @@ -384,7 +383,6 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat showDialog(DIALOG_MAINACTION_ID); } - @SuppressWarnings("deprecation") public void startDownloadManager() { if (!mBRouterView.hasUpToDateLookups()) { showDialog(DIALOG_OLDDATAHINT_ID); @@ -393,19 +391,18 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat } } - @SuppressWarnings("deprecation") public void selectBasedir(ArrayList items, String defaultBasedir, String message) { this.defaultbasedir = defaultBasedir; this.message = message; availableBasedirs = items; - ArrayList dirFreeSizes = new ArrayList(); + ArrayList dirFreeSizes = new ArrayList<>(); for (File f : items) { long size = 0L; try { StatFs stat = new StatFs(f.getAbsolutePath()); size = (long) stat.getAvailableBlocks() * stat.getBlockSize(); } catch (Exception e) { /* ignore */ } - dirFreeSizes.add(Long.valueOf(size)); + dirFreeSizes.add(size); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { @@ -426,7 +423,6 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat showDialog(DIALOG_SELECTBASEDIR_ID); } - @SuppressWarnings("deprecation") public void selectRoutingModes(String[] modes, boolean[] modesChecked, String message) { routingModes = modes; routingModesChecked = modesChecked; @@ -434,28 +430,23 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat showDialog(DIALOG_ROUTINGMODES_ID); } - @SuppressWarnings("deprecation") public void showModeConfigOverview(String message) { this.message = message; showDialog(DIALOG_MODECONFIGOVERVIEW_ID); } - @SuppressWarnings("deprecation") public void selectVias(String[] items) { availableVias = items; - selectedVias = new HashSet(availableVias.length); - for (String via : items) - selectedVias.add(via); + selectedVias = new HashSet<>(availableVias.length); + Collections.addAll(selectedVias, items); showDialog(DIALOG_VIASELECT_ID); } - @SuppressWarnings("deprecation") public void selectWaypoint(String[] items) { availableWaypoints = items; showNewDialog(DIALOG_PICKWAYPOINT_ID); } - @SuppressWarnings("deprecation") public void showWaypointDatabaseHelp() { if (mBRouterView.canAccessSdCard) { showNewDialog(DIALOG_SHOW_WP_HELP_ID); @@ -464,38 +455,33 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat } } - @SuppressWarnings("deprecation") public void showRepeatTimeoutHelp() { showNewDialog(DIALOG_SHOW_REPEAT_TIMEOUT_HELP_ID); } - @SuppressWarnings("deprecation") public void showWpDatabaseScanSuccess(String bestGuess) { maptoolDirCandidate = bestGuess; showNewDialog(DIALOG_SHOW_WP_SCANRESULT_ID); } - @SuppressWarnings("deprecation") public void selectNogos(List nogoList) { this.nogoList = nogoList; showDialog(DIALOG_NOGOSELECT_ID); } private void showNewDialog(int id) { - if (dialogIds.contains(Integer.valueOf(id))) { + if (dialogIds.contains(id)) { removeDialog(id); } - dialogIds.add(Integer.valueOf(id)); + dialogIds.add(id); showDialog(id); } - @SuppressWarnings("deprecation") public void showErrorMessage(String msg) { errorMessage = msg; showNewDialog(DIALOG_EXCEPTION_ID); } - @SuppressWarnings("deprecation") public void showResultMessage(String title, String msg, int wpCount) { errorMessage = msg; this.title = title; @@ -535,16 +521,18 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat public ArrayList getStorageDirectories() { ArrayList list = null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - list = new ArrayList(Arrays.asList(getExternalMediaDirs())); + list = new ArrayList<>(Arrays.asList(getExternalMediaDirs())); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { - list = new ArrayList(Arrays.asList(getExternalFilesDirs(null))); + list = new ArrayList<>(Arrays.asList(getExternalFilesDirs(null))); } - ArrayList res = new ArrayList(); + ArrayList res = new ArrayList<>(); - for (File f : list) { - if (f != null) { - if (getStorageState(f).equals(Environment.MEDIA_MOUNTED)) - res.add(f); + if (list != null) { + for (File f : list) { + if (f != null) { + if (getStorageState(f).equals(Environment.MEDIA_MOUNTED)) + res.add(f); + } } } @@ -577,7 +565,7 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat } @Override - public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == 0) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java b/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java index df787c3..3edae99 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java @@ -66,7 +66,6 @@ public class BRouterView extends View { private File tracksDir; private File segmentDir; private File profileDir; - private String profilePath; private String profileName; private String sourceHint; private boolean waitingForSelection = false; @@ -109,8 +108,7 @@ public class BRouterView extends View { // don't ask twice String version = "v" + getContext().getString(R.string.app_version); File vFile = new File(brd, "profiles2/" + version); - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q - && vFile.exists()) { + if (vFile.exists()) { startSetup(baseDir, false); return; } @@ -120,11 +118,10 @@ public class BRouterView extends View { waitingForSelection = true; waitingForMigration = true; oldMigrationPath = brd.getAbsolutePath(); - return; } else { startSetup(baseDir, false); - return; } + return; } } String message = baseDir == null ? "(no basedir configured previously)" : "(previous basedir " + baseDir @@ -212,7 +209,7 @@ public class BRouterView extends View { wpList = cor.waypoints; nogoList = cor.nogopoints; - nogoVetoList = new ArrayList(); + nogoVetoList = new ArrayList<>(); sourceHint = "(dev/trgt=" + deviceLevel + "/" + targetSdkVersion + " coordinate-source: " + cor.basedir + cor.rootdir + ")"; @@ -248,15 +245,17 @@ public class BRouterView extends View { } String[] fileNames = profileDir.list(); - ArrayList profiles = new ArrayList(); + ArrayList profiles = new ArrayList<>(); boolean lookupsFound = false; - for (String fileName : fileNames) { - if (fileName.endsWith(".brf")) { - profiles.add(fileName.substring(0, fileName.length() - 4)); + if (fileNames != null) { + for (String fileName : fileNames) { + if (fileName.endsWith(".brf")) { + profiles.add(fileName.substring(0, fileName.length() - 4)); + } + if (fileName.equals("lookups.dat")) + lookupsFound = true; } - if (fileName.equals("lookups.dat")) - lookupsFound = true; } // add a "last timeout" dummy profile @@ -298,24 +297,26 @@ public class BRouterView extends View { private void moveFolders(String oldMigrationPath, String basedir) { File oldDir = new File(oldMigrationPath); File[] oldFiles = oldDir.listFiles(); - for (File f : oldFiles) { - if (f.isDirectory()) { - int index = f.getAbsolutePath().lastIndexOf("/"); - String tmpdir = basedir + f.getAbsolutePath().substring(index); - moveFolders(f.getAbsolutePath(), tmpdir); - } else { - if (!f.getName().startsWith("v1.6")) { - moveFile(oldMigrationPath, f.getName(), basedir); + if (oldFiles != null) { + for (File f : oldFiles) { + if (f.isDirectory()) { + int index = f.getAbsolutePath().lastIndexOf("/"); + String tmpdir = basedir + f.getAbsolutePath().substring(index); + moveFolders(f.getAbsolutePath(), tmpdir); + } else { + if (!f.getName().startsWith("v1.6")) { + moveFile(oldMigrationPath, f.getName(), basedir); + } } - } + } } } private void moveFile(String inputPath, String inputFile, String outputPath) { - InputStream in = null; - OutputStream out = null; + InputStream in; + OutputStream out; try { //create output directory if it doesn't exist @@ -334,12 +335,10 @@ public class BRouterView extends View { out.write(buffer, 0, read); } in.close(); - in = null; // write the output file out.flush(); out.close(); - out = null; // delete the original file new File(inputPath + "/" + inputFile).delete(); @@ -365,7 +364,7 @@ public class BRouterView extends View { } public void updateViaList(Set selectedVias) { - ArrayList filtered = new ArrayList(wpList.size()); + ArrayList filtered = new ArrayList<>(wpList.size()); for (OsmNodeNamed n : wpList) { String name = n.name; if ("from".equals(name) || "to".equals(name) || selectedVias.contains(name)) @@ -442,7 +441,7 @@ public class BRouterView extends View { private List readWpList(BufferedReader br, boolean isNogo) throws Exception { int cnt = Integer.parseInt(br.readLine()); - List res = new ArrayList(cnt); + List res = new ArrayList<>(cnt); for (int i = 0; i < cnt; i++) { OsmNodeNamed wp = OsmNodeNamed.decodeNogo(br.readLine()); wp.isNogo = isNogo; @@ -471,7 +470,7 @@ public class BRouterView extends View { rawTrackPath = modesDir + "/remote_rawtrack.dat"; } - profilePath = profileDir + "/" + profile + ".brf"; + String profilePath = profileDir + "/" + profile + ".brf"; profileName = profile; if (needsViaSelection) { @@ -490,15 +489,15 @@ public class BRouterView extends View { } if (needsWaypointSelection) { - String msg; + StringBuilder msg; if (wpList.size() == 0) { - msg = "Expecting waypoint selection\n" + sourceHint; + msg = new StringBuilder("Expecting waypoint selection\n" + sourceHint); } else { - msg = "current waypoint selection:\n"; + msg = new StringBuilder("current waypoint selection:\n"); for (int i = 0; i < wpList.size(); i++) - msg += (i > 0 ? "->" : "") + wpList.get(i).name; + msg.append(i > 0 ? "->" : "").append(wpList.get(i).name); } - ((BRouterActivity) getContext()).showResultMessage("Select Action", msg, wpList.size()); + ((BRouterActivity) getContext()).showResultMessage("Select Action", msg.toString(), wpList.size()); return; } @@ -578,7 +577,7 @@ public class BRouterView extends View { File vtag = new File(path, versionTag); try { exists = !vtag.createNewFile(); - } catch (IOException io) { + } catch (IOException ignored) { } // well.. } @@ -599,7 +598,7 @@ public class BRouterView extends View { } String name = ze.getName(); File outfile = new File(path, name); - if (!outfile.exists()) { + if (!outfile.exists() && outfile.getParentFile() != null) { outfile.getParentFile().mkdirs(); FileOutputStream fos = new FileOutputStream(outfile); @@ -647,7 +646,7 @@ public class BRouterView extends View { int ir = (int) (n.radius * scaleMeter2Pixel); if (ir > minradius) { Paint paint = new Paint(); - paint.setColor(Color.RED); + paint.setColor(color); paint.setStyle(Paint.Style.STROKE); canvas.drawCircle((float) x, (float) y, (float) ir, paint); } @@ -705,7 +704,7 @@ public class BRouterView extends View { cr = null; try { Thread.sleep(2000); - } catch (InterruptedException ie) { + } catch (InterruptedException ignored) { } ((BRouterActivity) getContext()).showErrorMessage(t.toString()); waitingForSelection = true; @@ -715,7 +714,7 @@ public class BRouterView extends View { private void showDatabaseScanning(Canvas canvas) { try { Thread.sleep(100); - } catch (InterruptedException ie) { + } catch (InterruptedException ignored) { } Paint paint1 = new Paint(); paint1.setColor(Color.WHITE); @@ -768,7 +767,7 @@ public class BRouterView extends View { try { Thread.sleep(sleeptime); - } catch (InterruptedException ie) { + } catch (InterruptedException ignored) { } lastTs = System.currentTimeMillis(); @@ -776,9 +775,6 @@ public class BRouterView extends View { if (cr != null) { if (cr.getErrorMessage() != null) { ((BRouterActivity) getContext()).showErrorMessage(cr.getErrorMessage()); - cr = null; - waitingForSelection = true; - return; } else { String memstat = memoryClass + "mb pathPeak " + ((cr.getPathPeak() + 500) / 1000) + "k"; String result = "version = BRouter-" + getContext().getString(R.string.app_version) + "\n" + "mem = " + memstat + "\ndistance = " + cr.getDistance() / 1000. + " km\n" + "filtered ascend = " + cr.getAscend() @@ -796,10 +792,10 @@ public class BRouterView extends View { title += " / " + cr.getAlternativeIndex() + ". Alternative"; ((BRouterActivity) getContext()).showResultMessage(title, result, rawTrackPath == null ? -1 : -3); - cr = null; - waitingForSelection = true; - return; } + cr = null; + waitingForSelection = true; + return; } else if (System.currentTimeMillis() > lastDataTime) { System.exit(0); } @@ -812,17 +808,18 @@ public class BRouterView extends View { paintPosition(openSet[si], openSet[si + 1], 0xffffff, 1); } // paint nogos on top (red) + int minradius = 4; for (int ngi = 0; ngi < nogoList.size(); ngi++) { OsmNodeNamed n = nogoList.get(ngi); int color = 0xff0000; - paintPosition(n.ilon, n.ilat, color, 4); + paintPosition(n.ilon, n.ilat, color, minradius); } // paint start/end/vias on top (yellow/green/blue) for (int wpi = 0; wpi < wpList.size(); wpi++) { OsmNodeNamed n = wpList.get(wpi); int color = wpi == 0 ? 0xffff00 : wpi < wpList.size() - 1 ? 0xff : 0xff00; - paintPosition(n.ilon, n.ilat, color, 4); + paintPosition(n.ilon, n.ilat, color, minradius); } canvas.drawBitmap(imgPixels, 0, imgw, (float) 0., (float) 0., imgw, imgh, false, null); @@ -831,10 +828,10 @@ public class BRouterView extends View { for (int ngi = 0; ngi < nogoList.size(); ngi++) { OsmNodeNamed n = nogoList.get(ngi); if (n instanceof OsmNogoPolygon) { - paintPolygon(canvas, (OsmNogoPolygon) n, 4); + paintPolygon(canvas, (OsmNogoPolygon) n, minradius); } else { - int color = 0xff0000; - paintCircle(canvas, n, color, 4); + int color = Color.RED; + paintCircle(canvas, n, color, minradius); } } @@ -857,7 +854,7 @@ public class BRouterView extends View { File basedir = Environment.getExternalStorageDirectory(); try { File bd2 = new File(basedir, "external_sd"); - ArrayList basedirGuesses = new ArrayList(); + ArrayList basedirGuesses = new ArrayList<>(); basedirGuesses.add(basedir.getAbsolutePath()); if (bd2.exists()) { @@ -865,7 +862,7 @@ public class BRouterView extends View { basedirGuesses.add(basedir.getAbsolutePath()); } - ArrayList rl = new ArrayList(); + ArrayList rl = new ArrayList<>(); for (String bdg : basedirGuesses) { rl.add(new CoordinateReaderOsmAnd(bdg)); rl.add(new CoordinateReaderLocus(bdg)); @@ -897,7 +894,7 @@ public class BRouterView extends View { if (rawTrack != null) { try { rawTrack.writeBinary(rawTrackPath); - } catch (Exception e) { + } catch (Exception ignored) { } } else { new File(rawTrackPath).delete(); @@ -916,7 +913,7 @@ public class BRouterView extends View { public void configureService(String[] routingModes, boolean[] checkedModes) { // read in current config - TreeMap map = new TreeMap(); + TreeMap map = new TreeMap<>(); BufferedReader br = null; String modesFile = modesDir + "/serviceconfig.dat"; try { @@ -928,12 +925,12 @@ public class BRouterView extends View { ServiceModeConfig smc = new ServiceModeConfig(line); map.put(smc.mode, smc); } - } catch (Exception e) { + } catch (Exception ignored) { } finally { if (br != null) try { br.close(); - } catch (Exception ee) { + } catch (Exception ignored) { } } @@ -961,12 +958,12 @@ public class BRouterView extends View { bw.write('\n'); msg.append(smc).append('\n'); } - } catch (Exception e) { + } catch (Exception ignored) { } finally { if (bw != null) try { bw.close(); - } catch (Exception ee) { + } catch (Exception ignored) { } } ((BRouterActivity) getContext()).showModeConfigOverview(msg.toString()); From b5895e1fd8b66d90be9eb464754c0565ff9da1c0 Mon Sep 17 00:00:00 2001 From: Manuel Fuhr Date: Sat, 7 May 2022 07:54:14 +0200 Subject: [PATCH 04/15] Split functions --- .../btools/routingapp/BRouterActivity.java | 6 +-- .../java/btools/routingapp/BRouterView.java | 40 ++++++++++--------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java b/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java index d26ce2e..be63b17 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java @@ -514,10 +514,6 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat mWakeLock.release(); } - private String getStorageState(File f) { - return EnvironmentCompat.getStorageState(f); //Environment.MEDIA_MOUNTED - } - public ArrayList getStorageDirectories() { ArrayList list = null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { @@ -530,7 +526,7 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat if (list != null) { for (File f : list) { if (f != null) { - if (getStorageState(f).equals(Environment.MEDIA_MOUNTED)) + if (EnvironmentCompat.getStorageState(f).equals(Environment.MEDIA_MOUNTED)) res.add(f); } } diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java b/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java index 3edae99..ada4a39 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java @@ -171,24 +171,7 @@ public class BRouterView extends View { String basedir = baseDir.getAbsolutePath(); AppLogger.log("using basedir: " + basedir); - String version = "v" + getContext().getString(R.string.app_version); - - // create missing directories - assertDirectoryExists("project directory", new File(basedir, "brouter"), null, null); - segmentDir = new File(basedir, "/brouter/segments4"); - if (assertDirectoryExists("data directory", segmentDir, "segments4.zip", null)) { - ConfigMigration.tryMigrateStorageConfig( - new File(basedir + "/brouter/segments3/storageconfig.txt"), - new File(basedir + "/brouter/segments4/storageconfig.txt")); - } - profileDir = new File(basedir, "brouter/profiles2"); - assertDirectoryExists("profile directory", profileDir, "profiles2.zip", version); - modesDir = new File(basedir, "/brouter/modes"); - assertDirectoryExists("modes directory", modesDir, "modes.zip", version); - assertDirectoryExists("readmes directory", new File(basedir, "brouter/readmes"), "readmes.zip", version); - - File inputDir = new File(basedir, "brouter/import"); - assertDirectoryExists("input directory", inputDir, null, version); + populateBasedir(basedir); // new init is done move old files if (waitingForMigration) { @@ -294,6 +277,27 @@ public class BRouterView extends View { waitingForSelection = true; } + private void populateBasedir(String basedir) { + String version = "v" + getContext().getString(R.string.app_version); + + // create missing directories + assertDirectoryExists("project directory", new File(basedir, "brouter"), null, null); + segmentDir = new File(basedir, "/brouter/segments4"); + if (assertDirectoryExists("data directory", segmentDir, "segments4.zip", null)) { + ConfigMigration.tryMigrateStorageConfig( + new File(basedir + "/brouter/segments3/storageconfig.txt"), + new File(basedir + "/brouter/segments4/storageconfig.txt")); + } + profileDir = new File(basedir, "brouter/profiles2"); + assertDirectoryExists("profile directory", profileDir, "profiles2.zip", version); + modesDir = new File(basedir, "/brouter/modes"); + assertDirectoryExists("modes directory", modesDir, "modes.zip", version); + assertDirectoryExists("readmes directory", new File(basedir, "brouter/readmes"), "readmes.zip", version); + + File inputDir = new File(basedir, "brouter/import"); + assertDirectoryExists("input directory", inputDir, null, version); + } + private void moveFolders(String oldMigrationPath, String basedir) { File oldDir = new File(oldMigrationPath); File[] oldFiles = oldDir.listFiles(); From fc524fb4db00ce91339c3afe3a96cc3878d0ca71 Mon Sep 17 00:00:00 2001 From: Manuel Fuhr Date: Wed, 4 May 2022 17:01:37 +0200 Subject: [PATCH 05/15] Remove WpDatabaseScanner --- .../btools/routingapp/BRouterActivity.java | 52 +-------- .../java/btools/routingapp/BRouterView.java | 56 --------- .../btools/routingapp/WpDatabaseScanner.java | 107 ------------------ 3 files changed, 2 insertions(+), 213 deletions(-) delete mode 100644 brouter-routing-app/src/main/java/btools/routingapp/WpDatabaseScanner.java diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java b/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java index be63b17..7063d71 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java @@ -45,11 +45,9 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat private static final int DIALOG_SELECTBASEDIR_ID = 11; private static final int DIALOG_MAINACTION_ID = 12; private static final int DIALOG_OLDDATAHINT_ID = 13; - private static final int DIALOG_SHOW_WP_HELP_ID = 14; - private static final int DIALOG_SHOW_WP_SCANRESULT_ID = 15; private static final int DIALOG_SHOW_REPEAT_TIMEOUT_HELP_ID = 16; private static final int DIALOG_SHOW_API23_HELP_ID = 17; - + private final Set dialogIds = new HashSet<>(); private BRouterView mBRouterView; private PowerManager mPowerManager; private WakeLock mWakeLock; @@ -66,8 +64,6 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat private String[] availableVias; private Set selectedVias; private List nogoList; - private String maptoolDirCandidate; - private final Set dialogIds = new HashSet<>(); private String errorMessage; private String title; private int wpCount; @@ -148,27 +144,6 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat } }); return builder.create(); - case DIALOG_SHOW_WP_HELP_ID: - builder - .setTitle("No Waypoint Database found") - .setMessage( - "The simple scan did not find any map-tool directory including a waypoint database. " - + "Reason could be there is no map-tool installed (osmand, locus or oruxmaps), or at an " - + "unusual path, or it contains no waypoints yet. That's o.k. if you want to use BRouter " - + "in server-mode only - in that case you can still use the 'Server-Mode' button to " - + "configure the profile mapping. But you will not be able to use nogo-points or do " - + "long distance calculations. If you know the path to your map-tool, you can manually " - + "configure it in 'storageconfig.txt'. Or I can do an extended scan searching " - + "your sd-card for a valid waypoint database").setPositiveButton("Scan", new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - mBRouterView.startWpDatabaseScan(); - } - }).setNegativeButton("Exit", new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - finish(); - } - }); - return builder.create(); case DIALOG_SHOW_API23_HELP_ID: builder .setTitle("Android >=6 limitations") @@ -202,20 +177,6 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat } }); return builder.create(); - case DIALOG_SHOW_WP_SCANRESULT_ID: - builder - .setTitle("Waypoint Database ") - .setMessage("Found Waypoint-Database(s) for maptool-dir: " + maptoolDirCandidate - + " Configure that?").setPositiveButton("Yes", new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - mBRouterView.saveMaptoolDir(maptoolDirCandidate); - } - }).setNegativeButton("No", new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - finish(); - } - }); - return builder.create(); case DIALOG_OLDDATAHINT_ID: builder .setTitle("Local setup needs reset") @@ -448,22 +409,13 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat } public void showWaypointDatabaseHelp() { - if (mBRouterView.canAccessSdCard) { - showNewDialog(DIALOG_SHOW_WP_HELP_ID); - } else { - showNewDialog(DIALOG_SHOW_API23_HELP_ID); - } + showNewDialog(DIALOG_SHOW_API23_HELP_ID); } public void showRepeatTimeoutHelp() { showNewDialog(DIALOG_SHOW_REPEAT_TIMEOUT_HELP_ID); } - public void showWpDatabaseScanSuccess(String bestGuess) { - maptoolDirCandidate = bestGuess; - showNewDialog(DIALOG_SHOW_WP_SCANRESULT_ID); - } - public void selectNogos(List nogoList) { this.nogoList = nogoList; showDialog(DIALOG_NOGOSELECT_ID); diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java b/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java index ada4a39..5455db0 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java @@ -75,7 +75,6 @@ public class BRouterView extends View { private boolean needsViaSelection; private boolean needsNogoSelection; private boolean needsWaypointSelection; - private WpDatabaseScanner dataBaseScanner; private long lastDataTime = System.currentTimeMillis(); private CoordinateReader cor; private int[] imgPixels; @@ -428,17 +427,6 @@ public class BRouterView extends View { } } - public void startWpDatabaseScan() { - dataBaseScanner = new WpDatabaseScanner(); - dataBaseScanner.start(); - invalidate(); - } - - public void saveMaptoolDir(String dir) { - ConfigMigration.saveAdditionalMaptoolDir(segmentDir, dir); - ((BRouterActivity) getContext()).showResultMessage("Success", "please restart to use new config", -1); - } - public void finishWaypointSelection() { needsWaypointSelection = false; } @@ -715,51 +703,7 @@ public class BRouterView extends View { } } - private void showDatabaseScanning(Canvas canvas) { - try { - Thread.sleep(100); - } catch (InterruptedException ignored) { - } - Paint paint1 = new Paint(); - paint1.setColor(Color.WHITE); - paint1.setTextSize(20); - - Paint paint2 = new Paint(); - paint2.setColor(Color.WHITE); - paint2.setTextSize(10); - - String currentDir = dataBaseScanner.getCurrentDir(); - String bestGuess = dataBaseScanner.getBestGuess(); - - if (currentDir == null) // scan finished - { - if (bestGuess.length() == 0) { - ((BRouterActivity) getContext()).showErrorMessage("scan did not find any possible waypoint database"); - } else { - ((BRouterActivity) getContext()).showWpDatabaseScanSuccess(bestGuess); - } - cr = null; - dataBaseScanner = null; - waitingForSelection = true; - return; - } - - canvas.drawText("Scanning:", 10, 30, paint1); - canvas.drawText(currentDir, 0, 60, paint2); - canvas.drawText("Best Guess:", 10, 90, paint1); - canvas.drawText(bestGuess, 0, 120, paint2); - canvas.drawText("Last Error:", 10, 150, paint1); - canvas.drawText(dataBaseScanner.getLastError(), 0, 180, paint2); - - invalidate(); - } - private void _onDraw(Canvas canvas) { - if (dataBaseScanner != null) { - showDatabaseScanning(canvas); - return; - } - if (waitingForSelection) return; diff --git a/brouter-routing-app/src/main/java/btools/routingapp/WpDatabaseScanner.java b/brouter-routing-app/src/main/java/btools/routingapp/WpDatabaseScanner.java deleted file mode 100644 index 0c91da0..0000000 --- a/brouter-routing-app/src/main/java/btools/routingapp/WpDatabaseScanner.java +++ /dev/null @@ -1,107 +0,0 @@ -package btools.routingapp; - -import java.io.File; - -public class WpDatabaseScanner extends Thread { - private String currentDir = ""; - private String bestGuess = ""; - private String lastError = ""; - - private Object currentDirSync = new Object(); - - private long maxtimestamp = 0; - - public String getCurrentDir() { - synchronized (currentDirSync) { - return currentDir; - } - } - - private void setCurrentDir(String dir) { - synchronized (currentDirSync) { - currentDir = dir; - } - } - - public String getBestGuess() { - synchronized (currentDirSync) { - return bestGuess; - } - } - - public String getLastError() { - synchronized (currentDirSync) { - return lastError; - } - } - - private void setLastError(String msg) { - synchronized (currentDirSync) { - lastError = msg; - } - } - - private static String[] vetos = new String[]{"dev", "sys", "system", "proc", "etc", "init", "d", "cache", "acct", "data"}; - - private void scan(File dir, int level) { - if (level > 8) { - return; - } - - try { - if (dir.isDirectory()) { - if (level == 1) { - String name = dir.getName(); - for (String veto : vetos) { - if (veto.equals(name)) { - return; - } - } - } - - testPath(dir.getPath()); - File[] childs = dir.listFiles(); - if (childs == null) { - return; - } - for (File child : childs) { - scan(child, level + 1); - } - } - } catch (Exception e) { - setLastError(e.toString()); - } - } - - private void testPath(String path) throws Exception { - setCurrentDir(path); - - testReader(new CoordinateReaderOsmAnd(path)); - testReader(new CoordinateReaderOsmAnd(path, true)); - testReader(new CoordinateReaderLocus(path)); - testReader(new CoordinateReaderOrux(path)); - } - - private void testReader(CoordinateReader cor) throws Exception { - long ts = cor.getTimeStamp(); - if (ts > maxtimestamp) { - maxtimestamp = ts; - synchronized (currentDirSync) { - bestGuess = cor.basedir; - } - } else if (ts > 0 && ts == maxtimestamp) { - synchronized (currentDirSync) { - if (cor.basedir.length() < bestGuess.length()) { - bestGuess = cor.basedir; - } - } - } - } - - @Override - public void run() { - scan(new File("/"), 0); - setCurrentDir(null); - - } -} From 924a33ccb5661c49facf8feed0aa6a4385d23e2b Mon Sep 17 00:00:00 2001 From: Manuel Fuhr Date: Sat, 7 May 2022 07:50:54 +0200 Subject: [PATCH 06/15] Remove wakelock --- .../src/main/AndroidManifest.xml | 1 - .../btools/routingapp/BRouterActivity.java | 29 +------------------ 2 files changed, 1 insertion(+), 29 deletions(-) diff --git a/brouter-routing-app/src/main/AndroidManifest.xml b/brouter-routing-app/src/main/AndroidManifest.xml index 76f6b51..3c8f22e 100644 --- a/brouter-routing-app/src/main/AndroidManifest.xml +++ b/brouter-routing-app/src/main/AndroidManifest.xml @@ -7,7 +7,6 @@ - diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java b/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java index 7063d71..ac20455 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java @@ -9,8 +9,6 @@ import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; import android.os.Environment; -import android.os.PowerManager; -import android.os.PowerManager.WakeLock; import android.os.StatFs; import android.widget.EditText; @@ -49,8 +47,6 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat private static final int DIALOG_SHOW_API23_HELP_ID = 17; private final Set dialogIds = new HashSet<>(); private BRouterView mBRouterView; - private PowerManager mPowerManager; - private WakeLock mWakeLock; private String[] availableProfiles; private String selectedProfile = null; private List availableBasedirs; @@ -75,12 +71,6 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - // Get an instance of the PowerManager - mPowerManager = (PowerManager) getSystemService(POWER_SERVICE); - - // Create a bright wake lock - mWakeLock = mPowerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, getClass().getName()); - ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE); int memoryClass = am.getMemoryClass(); @@ -441,29 +431,12 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat showNewDialog(DIALOG_SHOWRESULT_ID); } - @Override - protected void onResume() { - super.onResume(); - /* - * when the activity is resumed, we acquire a wake-lock so that the screen - * stays on, since the user will likely not be fiddling with the screen or - * buttons. - */ - mWakeLock.acquire(); - } - @Override protected void onPause() { super.onPause(); - /* - * When the activity is paused, we make sure to stop the router - */ - // Stop the simulation + // When the activity is paused, we make sure to stop the router mBRouterView.stopRouting(); - - // and release our wake-lock - mWakeLock.release(); } public ArrayList getStorageDirectories() { From 01ad4dc09a7ddd0be72e3f8a907edca5d8947b83 Mon Sep 17 00:00:00 2001 From: Manuel Fuhr Date: Sat, 7 May 2022 08:04:13 +0200 Subject: [PATCH 07/15] Remove app specific coordinate readers --- .../btools/routingapp/BRouterActivity.java | 6 +- .../btools/routingapp/BRouterService.java | 41 ++--- .../java/btools/routingapp/BRouterView.java | 61 +------ .../btools/routingapp/CoordinateReader.java | 69 +------- .../routingapp/CoordinateReaderLocus.java | 68 -------- .../routingapp/CoordinateReaderOrux.java | 63 ------- .../routingapp/CoordinateReaderOsmAnd.java | 163 ------------------ 7 files changed, 23 insertions(+), 448 deletions(-) delete mode 100644 brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderLocus.java delete mode 100644 brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderOrux.java delete mode 100644 brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderOsmAnd.java diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java b/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java index ac20455..2ff2a69 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java @@ -55,7 +55,6 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat private String[] availableWaypoints; private String[] routingModes; private boolean[] routingModesChecked; - private String defaultbasedir = null; private String message = null; private String[] availableVias; private Set selectedVias; @@ -206,7 +205,7 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat builder.setTitle("Enter SDCARD base dir:"); builder.setMessage(message); final EditText input = new EditText(this); - input.setText(defaultbasedir); + // input.setText(defaultbasedir); builder.setView(input); builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { @@ -342,8 +341,7 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat } } - public void selectBasedir(ArrayList items, String defaultBasedir, String message) { - this.defaultbasedir = defaultBasedir; + public void selectBasedir(ArrayList items, String message) { this.message = message; availableBasedirs = items; ArrayList dirFreeSizes = new ArrayList<>(); diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BRouterService.java b/brouter-routing-app/src/main/java/btools/routingapp/BRouterService.java index f7c96b6..0e33316 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BRouterService.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BRouterService.java @@ -1,7 +1,16 @@ package btools.routingapp; +import android.app.Service; +import android.content.Intent; +import android.os.Bundle; +import android.os.IBinder; +import android.os.RemoteException; +import android.util.Base64; +import android.util.Log; + import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -10,26 +19,9 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; -import java.io.ByteArrayOutputStream; import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; -import java.util.List; -import java.util.zip.GZIPOutputStream; import java.util.ArrayList; - -import android.Manifest; -import android.app.Service; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.os.Build; -import android.os.Bundle; -import android.os.Environment; -import android.os.IBinder; -import android.os.RemoteException; -import android.util.Log; -import android.util.Base64; - -import androidx.core.content.ContextCompat; +import java.util.zip.GZIPOutputStream; import btools.router.OsmNodeNamed; @@ -185,18 +177,7 @@ public class BRouterService extends Service { private void readNogos(BRouterWorker worker, String baseDir) throws Exception { // add nogos from waypoint database - int deviceLevel = android.os.Build.VERSION.SDK_INT; - int targetSdkVersion = getApplicationInfo().targetSdkVersion; - boolean canAccessSdCard = true; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && !Environment.isExternalStorageLegacy()) { - canAccessSdCard = false; - } - if (ContextCompat.checkSelfPermission(BRouterService.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { - canAccessSdCard = false; - } - AppLogger.log("dev/target=" + deviceLevel + "/" + targetSdkVersion + " canAccessSdCard=" + canAccessSdCard); - - CoordinateReader cor = CoordinateReader.obtainValidReader(baseDir, worker.segmentDir, canAccessSdCard, true); + CoordinateReader cor = CoordinateReader.obtainValidReader(baseDir, true); worker.nogoList = new ArrayList(cor.nogopoints); worker.nogoPolygonsList = new ArrayList(); } diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java b/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java index 5455db0..541b960 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java @@ -7,8 +7,6 @@ import android.content.res.AssetManager; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; -import android.os.Build; -import android.os.Environment; import android.util.Log; import android.view.View; import android.widget.Toast; @@ -48,7 +46,7 @@ import btools.util.CheapRuler; public class BRouterView extends View { - public boolean canAccessSdCard; + private final int memoryClass; RoutingEngine cr; private int imgw; private int imgh; @@ -67,7 +65,6 @@ public class BRouterView extends View { private File segmentDir; private File profileDir; private String profileName; - private String sourceHint; private boolean waitingForSelection = false; private boolean waitingForMigration = false; private String rawTrackPath; @@ -78,7 +75,6 @@ public class BRouterView extends View { private long lastDataTime = System.currentTimeMillis(); private CoordinateReader cor; private int[] imgPixels; - private final int memoryClass; private long lastTs = System.currentTimeMillis(); private long startTime = 0L; @@ -113,7 +109,7 @@ public class BRouterView extends View { } String message = "(previous basedir " + baseDir + " has to migrate )"; - ((BRouterActivity) getContext()).selectBasedir(((BRouterActivity) getContext()).getStorageDirectories(), guessBaseDir(), message); + ((BRouterActivity) getContext()).selectBasedir(((BRouterActivity) getContext()).getStorageDirectories(), message); waitingForSelection = true; waitingForMigration = true; oldMigrationPath = brd.getAbsolutePath(); @@ -126,7 +122,7 @@ public class BRouterView extends View { String message = baseDir == null ? "(no basedir configured previously)" : "(previous basedir " + baseDir + (bdValid ? " does not contain 'brouter' subfolder)" : " is not valid)"); - ((BRouterActivity) getContext()).selectBasedir(((BRouterActivity) getContext()).getStorageDirectories(), guessBaseDir(), message); + ((BRouterActivity) getContext()).selectBasedir(((BRouterActivity) getContext()).getStorageDirectories(), message); waitingForSelection = true; } catch (Exception e) { String msg = e instanceof IllegalArgumentException ? e.getMessage() : e.toString(); @@ -158,7 +154,7 @@ public class BRouterView extends View { retryBaseDir = baseDir; ActivityCompat.requestPermissions((BRouterActivity) getContext(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0); } else { - ((BRouterActivity) getContext()).selectBasedir(((BRouterActivity) getContext()).getStorageDirectories(), guessBaseDir(), "Cannot access " + baseDir.getAbsolutePath() + "; select another"); + ((BRouterActivity) getContext()).selectBasedir(((BRouterActivity) getContext()).getStorageDirectories(), "Cannot access " + baseDir.getAbsolutePath() + "; select another"); } return; } @@ -180,21 +176,12 @@ public class BRouterView extends View { waitingForMigration = false; } - int deviceLevel = Build.VERSION.SDK_INT; - int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion; - canAccessSdCard = Build.VERSION.SDK_INT < Build.VERSION_CODES.Q || Environment.isExternalStorageLegacy(); - if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { - canAccessSdCard = false; - } - - cor = CoordinateReader.obtainValidReader(basedir, segmentDir, canAccessSdCard); + cor = CoordinateReader.obtainValidReader(basedir); wpList = cor.waypoints; nogoList = cor.nogopoints; nogoVetoList = new ArrayList<>(); - sourceHint = "(dev/trgt=" + deviceLevel + "/" + targetSdkVersion + " coordinate-source: " + cor.basedir + cor.rootdir + ")"; - needsViaSelection = wpList.size() > 2; needsNogoSelection = nogoList.size() > 0; needsWaypointSelection = wpList.size() == 0; @@ -483,7 +470,7 @@ public class BRouterView extends View { if (needsWaypointSelection) { StringBuilder msg; if (wpList.size() == 0) { - msg = new StringBuilder("Expecting waypoint selection\n" + sourceHint); + msg = new StringBuilder("Expecting waypoint selection\n" + "(coordinate-source: " + cor.basedir + cor.rootdir + ")"); } else { msg = new StringBuilder("current waypoint selection:\n"); for (int i = 0; i < wpList.size(); i++) @@ -798,42 +785,6 @@ public class BRouterView extends View { invalidate(); } - private String guessBaseDir() { - File basedir = Environment.getExternalStorageDirectory(); - try { - File bd2 = new File(basedir, "external_sd"); - ArrayList basedirGuesses = new ArrayList<>(); - basedirGuesses.add(basedir.getAbsolutePath()); - - if (bd2.exists()) { - basedir = bd2; - basedirGuesses.add(basedir.getAbsolutePath()); - } - - ArrayList rl = new ArrayList<>(); - for (String bdg : basedirGuesses) { - rl.add(new CoordinateReaderOsmAnd(bdg)); - rl.add(new CoordinateReaderLocus(bdg)); - rl.add(new CoordinateReaderOrux(bdg)); - } - long tmax = 0; - CoordinateReader cor = null; - for (CoordinateReader r : rl) { - long t = r.getTimeStamp(); - if (t > tmax) { - tmax = t; - cor = r; - } - } - if (cor != null) { - return cor.basedir; - } - } catch (Exception e) { - System.out.println("guessBaseDir:" + e); - } - return basedir.getAbsolutePath(); - } - private void writeRawTrackToMode(String mode) { writeRawTrackToPath(modesDir + "/" + mode + "_rawtrack.dat"); } diff --git a/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReader.java b/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReader.java index 8251e1e..5809e70 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReader.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReader.java @@ -1,8 +1,6 @@ package btools.routingapp; -import java.io.File; import java.util.ArrayList; -import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -10,10 +8,7 @@ import java.util.Map; import java.util.Set; import java.util.TreeMap; -import android.os.Environment; - import btools.router.OsmNodeNamed; -import btools.router.RoutingHelper; /** * Read coordinates from a gpx-file @@ -125,68 +120,12 @@ public abstract class CoordinateReader { protected abstract void readPointmap() throws Exception; - public static CoordinateReader obtainValidReader(String basedir, File segmentDir, boolean canAccessSdCard) throws Exception { - return obtainValidReader(basedir, segmentDir, canAccessSdCard, false); + public static CoordinateReader obtainValidReader(String basedir) throws Exception { + return obtainValidReader(basedir, false); } - public static CoordinateReader obtainValidReader(String basedir, File segmentDir, boolean canAccessSdCard, boolean nogosOnly) throws Exception { - CoordinateReader cor = null; - ArrayList rl = new ArrayList(); - - if (canAccessSdCard) { - AppLogger.log("adding standard maptool-base: " + basedir); - rl.add(new CoordinateReaderOsmAnd(basedir)); - rl.add(new CoordinateReaderLocus(basedir)); - rl.add(new CoordinateReaderOrux(basedir)); - - // eventually add standard-sd - File standardbase = Environment.getExternalStorageDirectory(); - if (standardbase != null) { - String base2 = standardbase.getAbsolutePath(); - if (!base2.equals(basedir)) { - AppLogger.log("adding internal sd maptool-base: " + base2); - rl.add(new CoordinateReaderOsmAnd(base2)); - rl.add(new CoordinateReaderLocus(base2)); - rl.add(new CoordinateReaderOrux(base2)); - } - } - - // eventually add explicit directory - File additional = RoutingHelper.getAdditionalMaptoolDir(segmentDir); - if (additional != null) { - String base3 = additional.getAbsolutePath(); - - AppLogger.log("adding maptool-base from storage-config: " + base3); - - rl.add(new CoordinateReaderOsmAnd(base3)); - rl.add(new CoordinateReaderOsmAnd(base3, true)); - rl.add(new CoordinateReaderLocus(base3)); - rl.add(new CoordinateReaderOrux(base3)); - } - - long tmax = 0; - for (CoordinateReader r : rl) { - if (AppLogger.isLogging()) { - AppLogger.log("reading timestamp at systime " + new Date()); - } - - long t = r.getTimeStamp(); - - if (t != 0) { - if (AppLogger.isLogging()) { - AppLogger.log("found coordinate source at " + r.basedir + r.rootdir + " with timestamp " + new Date(t)); - } - } - - if (t > tmax) { - tmax = t; - cor = r; - } - } - } - if (cor == null) { - cor = new CoordinateReaderInternal(basedir); - } + public static CoordinateReader obtainValidReader(String basedir, boolean nogosOnly) throws Exception { + CoordinateReader cor = new CoordinateReaderInternal(basedir); cor.nogosOnly = nogosOnly; cor.readFromTo(); return cor; diff --git a/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderLocus.java b/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderLocus.java deleted file mode 100644 index 105de7f..0000000 --- a/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderLocus.java +++ /dev/null @@ -1,68 +0,0 @@ -package btools.routingapp; - -import java.io.File; - -import android.database.Cursor; -import android.database.sqlite.SQLiteDatabase; - -import btools.router.OsmNodeNamed; - -/** - * Read coordinates from a gpx-file - */ -public class CoordinateReaderLocus extends CoordinateReader { - public CoordinateReaderLocus(String basedir) { - super(basedir); - tracksdir = "/Locus/mapItems"; - rootdir = "/Locus"; - } - - @Override - public long getTimeStamp() throws Exception { - File f = new File(basedir + "/Locus/data/database/waypoints.db"); - long t1 = f.lastModified(); - // Android 10 delivers file size but can't read it - boolean canRead = f.canRead(); - return canRead ? t1 : 0L; - } - - @Override - public int getTurnInstructionMode() { - return 2; // locus style - } - - /* - * read the from and to position from a ggx-file - * (with hardcoded name for now) - */ - @Override - public void readPointmap() throws Exception { - _readPointmap(basedir + "/Locus/data/database/waypoints.db"); - } - - private void _readPointmap(String filename) throws Exception { - SQLiteDatabase myDataBase = null; - try { - myDataBase = SQLiteDatabase.openDatabase(filename, null, SQLiteDatabase.OPEN_READONLY); - } catch (Exception e) { - // not open, do not produce an error - return; - } - - Cursor c = myDataBase.rawQuery("SELECT c.name, w.name, w.longitude, w.latitude FROM waypoints w, categories c where w.parent_id = c._id", null); - if (c.getCount() == 0) { - c.close(); - c = myDataBase.rawQuery("SELECT c.name, w.name, w.longitude, w.latitude FROM waypoints w, groups c where w.parent_id = c._id;", null); - } - while (c.moveToNext()) { - OsmNodeNamed n = new OsmNodeNamed(); - String category = c.getString(0); - n.name = c.getString(1); - n.ilon = (int) ((c.getDouble(2) + 180.) * 1000000. + 0.5); - n.ilat = (int) ((c.getDouble(3) + 90.) * 1000000. + 0.5); - checkAddPoint(category, n); - } - c.close(); - myDataBase.close(); - } -} diff --git a/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderOrux.java b/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderOrux.java deleted file mode 100644 index c4628f4..0000000 --- a/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderOrux.java +++ /dev/null @@ -1,63 +0,0 @@ -package btools.routingapp; - -import java.io.File; - -import android.database.Cursor; -import android.database.sqlite.SQLiteDatabase; - -import btools.router.OsmNodeNamed; - -/** - * Read coordinates from a gpx-file - */ -public class CoordinateReaderOrux extends CoordinateReader { - public CoordinateReaderOrux(String basedir) { - super(basedir); - tracksdir = "/oruxmaps/tracklogs"; - rootdir = "/oruxmaps"; - } - - @Override - public long getTimeStamp() throws Exception { - File f = new File(basedir + "/oruxmaps/tracklogs/oruxmapstracks.db"); - long t1 = f.lastModified(); - // Android 10 delivers file size but can't read it - boolean canRead = f.canRead(); - return canRead ? t1 : 0L; - } - - @Override - public int getTurnInstructionMode() { - return 0; // none - } - - /* - * read the from and to position from a ggx-file - * (with hardcoded name for now) - */ - @Override - public void readPointmap() throws Exception { - _readPointmap(basedir + "/oruxmaps/tracklogs/oruxmapstracks.db"); - } - - private void _readPointmap(String filename) throws Exception { - SQLiteDatabase myDataBase = null; - try { - myDataBase = SQLiteDatabase.openDatabase(filename, null, SQLiteDatabase.OPEN_READONLY); - } catch (Exception e) { - // not open, do not produce an error - return; - } - Cursor c = myDataBase.rawQuery("SELECT poiname, poilon, poilat, poifolder FROM pois", null); - while (c.moveToNext()) { - OsmNodeNamed n = new OsmNodeNamed(); - n.name = c.getString(0); - n.ilon = (int) ((c.getDouble(1) + 180.) * 1000000. + 0.5); - n.ilat = (int) ((c.getDouble(2) + 90.) * 1000000. + 0.5); - String category = c.getString(3); - checkAddPoint(category, n); - } - c.close(); - myDataBase.close(); - } -} diff --git a/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderOsmAnd.java b/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderOsmAnd.java deleted file mode 100644 index bf6423b..0000000 --- a/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderOsmAnd.java +++ /dev/null @@ -1,163 +0,0 @@ -package btools.routingapp; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileReader; -import java.io.IOException; -import java.io.InputStreamReader; - -import org.xmlpull.v1.XmlPullParser; -import org.xmlpull.v1.XmlPullParserFactory; - -import btools.router.OsmNodeNamed; -import btools.router.OsmNogoPolygon; - -/** - * Read coordinates from a gpx-file - */ -public class CoordinateReaderOsmAnd extends CoordinateReader { - private String osmandDir; - - public CoordinateReaderOsmAnd(String basedir) { - this(basedir, false); - } - - public CoordinateReaderOsmAnd(String basedir, boolean shortPath) { - super(basedir); - if (shortPath) { - osmandDir = basedir; - tracksdir = "/tracks"; - rootdir = ""; - } else { - osmandDir = basedir + "/osmand"; - tracksdir = "/osmand/tracks"; - rootdir = "/osmand"; - } - } - - @Override - public long getTimeStamp() throws Exception { - File f1 = new File(osmandDir + "/favourites_bak.gpx"); - File f2 = new File(osmandDir + "/favourites.gpx"); - long t1 = f1.canRead() ? f1.lastModified() : 0L; - long t2 = f2.canRead() ? f2.lastModified() : 0L; - return t1 > t2 ? t1 : t2; - } - - @Override - public int getTurnInstructionMode() { - return 3; // osmand style - } - - /* - * read the from and to position from a gpx-file - * (with hardcoded name for now) - */ - @Override - public void readPointmap() throws Exception { - try { - _readPointmap(osmandDir + "/favourites_bak.gpx"); - } catch (Exception e) { - _readPointmap(osmandDir + "/favourites.gpx"); - } - try { - _readNogoLines(basedir + tracksdir); - } catch (IOException ioe) { - } - } - - private void _readPointmap(String filename) throws Exception { - BufferedReader br = new BufferedReader( - new InputStreamReader( - new FileInputStream(filename))); - OsmNodeNamed n = null; - - for (; ; ) { - String line = br.readLine(); - if (line == null) break; - - int idx0 = line.indexOf(""); - if (idx0 >= 0) { - n = new OsmNodeNamed(); - idx0 += 10; - int idx1 = line.indexOf('"', idx0); - n.ilat = (int) ((Double.parseDouble(line.substring(idx0, idx1)) + 90.) * 1000000. + 0.5); - int idx2 = line.indexOf(" lon=\""); - if (idx2 < 0) continue; - idx2 += 6; - int idx3 = line.indexOf('"', idx2); - n.ilon = (int) ((Double.parseDouble(line.substring(idx2, idx3)) + 180.) * 1000000. + 0.5); - continue; - } - if (n != null && idx10 >= 0) { - idx10 += 6; - int idx11 = line.indexOf("", idx10); - if (idx11 >= 0) { - n.name = line.substring(idx10, idx11).trim(); - checkAddPoint("(one-for-all)", n); - } - } - } - br.close(); - } - - private void _readNogoLines(String dirname) throws IOException { - - File dir = new File(dirname); - - if (dir.exists() && dir.isDirectory()) { - for (final File file : dir.listFiles()) { - final String name = file.getName(); - if (name.startsWith("nogo") && name.endsWith(".gpx")) { - try { - _readNogoLine(file); - } catch (Exception e) { - } - } - } - } - } - - private void _readNogoLine(File file) throws Exception { - XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); - factory.setNamespaceAware(false); - XmlPullParser xpp = factory.newPullParser(); - - xpp.setInput(new FileReader(file)); - OsmNogoPolygon nogo = new OsmNogoPolygon(false); - int eventType = xpp.getEventType(); - int numSeg = 0; - while (eventType != XmlPullParser.END_DOCUMENT) { - switch (eventType) { - case XmlPullParser.START_TAG: { - if (xpp.getName().equals("trkpt")) { - final String lon = xpp.getAttributeValue(null, "lon"); - final String lat = xpp.getAttributeValue(null, "lat"); - if (lon != null && lat != null) { - nogo.addVertex( - (int) ((Double.parseDouble(lon) + 180.) * 1000000. + 0.5), - (int) ((Double.parseDouble(lat) + 90.) * 1000000. + 0.5)); - } - } - break; - } - case XmlPullParser.END_TAG: { - if (xpp.getName().equals("trkseg")) { - nogo.calcBoundingCircle(); - final String name = file.getName(); - nogo.name = name.substring(0, name.length() - 4); - if (numSeg > 0) { - nogo.name += Integer.toString(numSeg + 1); - } - numSeg++; - checkAddPoint("(one-for-all)", nogo); - } - break; - } - } - eventType = xpp.next(); - } - } -} From 9d93d25a84fe7f0fa45b0942ef55f4eefc3991f7 Mon Sep 17 00:00:00 2001 From: Manuel Fuhr Date: Sat, 7 May 2022 08:27:22 +0200 Subject: [PATCH 08/15] Remove abstract CoordinateReader --- .../btools/routingapp/CoordinateReader.java | 211 ++++++++++++++---- .../routingapp/CoordinateReaderInternal.java | 186 --------------- 2 files changed, 173 insertions(+), 224 deletions(-) delete mode 100644 brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderInternal.java diff --git a/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReader.java b/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReader.java index 5809e70..468b910 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReader.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReader.java @@ -1,5 +1,17 @@ package btools.routingapp; +import android.graphics.Point; + +import org.xmlpull.v1.XmlPullParser; +import org.xmlpull.v1.XmlPullParserFactory; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; @@ -9,43 +21,181 @@ import java.util.Set; import java.util.TreeMap; import btools.router.OsmNodeNamed; +import btools.router.OsmNogoPolygon; /** * Read coordinates from a gpx-file */ -public abstract class CoordinateReader { +public class CoordinateReader { + protected static String[] posnames + = new String[]{"from", "via1", "via2", "via3", "via4", "via5", "via6", "via7", "via8", "via9", "to"}; + private final String internalDir; public List waypoints; public List nogopoints; public String basedir; public String rootdir; public String tracksdir; - - private boolean nogosOnly; - - private Map> allpointsMap; public List allpoints; - + private boolean nogosOnly; + private Map> allpointsMap; private HashMap pointmap; - protected static String[] posnames - = new String[]{"from", "via1", "via2", "via3", "via4", "via5", "via6", "via7", "via8", "via9", "to"}; - public CoordinateReader(String basedir) { this.basedir = basedir; + internalDir = basedir + "/brouter/import"; + tracksdir = "/brouter/import/tracks"; + rootdir = "/brouter/import"; } - public abstract long getTimeStamp() throws Exception; + public static CoordinateReader obtainValidReader(String basedir) throws IOException { + return obtainValidReader(basedir, false); + } - public abstract int getTurnInstructionMode(); + public static CoordinateReader obtainValidReader(String basedir, boolean nogosOnly) throws IOException { + CoordinateReader cor = new CoordinateReader(basedir); + cor.nogosOnly = nogosOnly; + cor.readFromTo(); + return cor; + } + + public int getTurnInstructionMode() { + return 4; // comment style + } + + /* + * read the from and to position from a gpx-file + * (with hardcoded name for now) + */ + public void readPointmap() throws IOException { + if (!_readPointmap(internalDir + "/favourites_bak.gpx")) { + _readPointmap(internalDir + "/favourites.gpx"); + } + + _readNogoLines(basedir + tracksdir); + } + + private boolean _readPointmap(String filename) throws IOException { + BufferedReader br; + try { + br = new BufferedReader( + new InputStreamReader( + new FileInputStream(filename))); + } catch (FileNotFoundException e) { + // ignore until it's reading error + return false; + } + OsmNodeNamed n = null; + + for (; ; ) { + String line = br.readLine(); + if (line == null) break; + + int idx0 = line.indexOf(" lat=\""); + int idx10 = line.indexOf(""); + if (idx0 >= 0) { + n = new OsmNodeNamed(); + idx0 += 6; + int idx1 = line.indexOf('"', idx0); + n.ilat = (int) ((Double.parseDouble(line.substring(idx0, idx1)) + 90.) * 1000000. + 0.5); + int idx2 = line.indexOf(" lon=\""); + if (idx2 < 0) continue; + idx2 += 6; + int idx3 = line.indexOf('"', idx2); + n.ilon = (int) ((Double.parseDouble(line.substring(idx2, idx3)) + 180.) * 1000000. + 0.5); + if (idx3 < 0) continue; + } + if (n != null && idx10 >= 0) { + idx10 += 6; + int idx11 = line.indexOf("", idx10); + if (idx11 >= 0) { + n.name = line.substring(idx10, idx11).trim(); + checkAddPoint("(one-for-all)", n); + } + } + } + br.close(); + return true; + } + + private void _readNogoLines(String dirname) { + File dir = new File(dirname); + + if (dir.exists() && dir.isDirectory()) { + for (final File file : dir.listFiles()) { + final String name = file.getName(); + if (name.startsWith("nogo") && name.endsWith(".gpx")) { + try { + _readNogoLine(file); + } catch (Exception ignored) { + } + } + } + } + } + + private void _readNogoLine(File file) throws Exception { + XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); + factory.setNamespaceAware(false); + XmlPullParser xpp = factory.newPullParser(); + + xpp.setInput(new FileReader(file)); + + List tmpPts = new ArrayList<>(); + int eventType = xpp.getEventType(); + int numSeg = 0; + while (eventType != XmlPullParser.END_DOCUMENT) { + switch (eventType) { + case XmlPullParser.START_TAG: { + if (xpp.getName().equals("trkpt") || xpp.getName().equals("rtept")) { + final String lon = xpp.getAttributeValue(null, "lon"); + final String lat = xpp.getAttributeValue(null, "lat"); + if (lon != null && lat != null) { + tmpPts.add(new Point( + (int) ((Double.parseDouble(lon) + 180.) * 1000000. + 0.5), + (int) ((Double.parseDouble(lat) + 90.) * 1000000. + 0.5))); + } + } + break; + } + case XmlPullParser.END_TAG: { + if (xpp.getName().equals("trkseg") || xpp.getName().equals("rte")) { // rte has no segment + OsmNogoPolygon nogo; + if (tmpPts.size() > 0) { + if (tmpPts.get(0).x == tmpPts.get(tmpPts.size() - 1).x && + tmpPts.get(0).y == tmpPts.get(tmpPts.size() - 1).y) { + nogo = new OsmNogoPolygon(true); + } else { + nogo = new OsmNogoPolygon(false); + } + for (Point p : tmpPts) { + nogo.addVertex(p.x, p.y); + } + nogo.calcBoundingCircle(); + final String name = file.getName(); + nogo.name = name.substring(0, name.length() - 4); + if (numSeg > 0) { + nogo.name += Integer.toString(numSeg + 1); + } + numSeg++; + checkAddPoint("(one-for-all)", nogo); + } + tmpPts.clear(); + } + break; + } + } + eventType = xpp.next(); + } + } public void readAllPoints() throws Exception { - allpointsMap = new TreeMap>(); + allpointsMap = new TreeMap<>(); readFromTo(); - allpoints = new ArrayList(); - Set names = new HashSet(); + allpoints = new ArrayList<>(); + Set names = new HashSet<>(); for (String category : allpointsMap.keySet()) { Map cat = allpointsMap.get(category); - if (cat.size() < 101) { + if (cat != null && cat.size() < 101) { for (OsmNodeNamed wp : cat.values()) { if (names.add(wp.name)) { allpoints.add(wp); @@ -62,14 +212,13 @@ public abstract class CoordinateReader { /* * read the from, to and via-positions from a gpx-file */ - public void readFromTo() throws Exception { - pointmap = new HashMap(); - waypoints = new ArrayList(); - nogopoints = new ArrayList(); + public void readFromTo() throws IOException { + pointmap = new HashMap<>(); + waypoints = new ArrayList<>(); + nogopoints = new ArrayList<>(); readPointmap(); boolean fromToMissing = false; - for (int i = 0; i < posnames.length; i++) { - String name = posnames[i]; + for (String name : posnames) { OsmNodeNamed n = pointmap.get(name); if (n != null) { waypoints.add(n); @@ -86,7 +235,7 @@ public abstract class CoordinateReader { if (category == null) category = ""; Map cat = allpointsMap.get(category); if (cat == null) { - cat = new TreeMap(); + cat = new TreeMap<>(); allpointsMap.put(category, cat); } if (cat.size() < 101) { @@ -96,8 +245,8 @@ public abstract class CoordinateReader { } boolean isKnown = false; - for (int i = 0; i < posnames.length; i++) { - if (posnames[i].equals(n.name)) { + for (String posname : posnames) { + if (posname.equals(n.name)) { isKnown = true; break; } @@ -116,18 +265,4 @@ public abstract class CoordinateReader { } } - - protected abstract void readPointmap() throws Exception; - - - public static CoordinateReader obtainValidReader(String basedir) throws Exception { - return obtainValidReader(basedir, false); - } - - public static CoordinateReader obtainValidReader(String basedir, boolean nogosOnly) throws Exception { - CoordinateReader cor = new CoordinateReaderInternal(basedir); - cor.nogosOnly = nogosOnly; - cor.readFromTo(); - return cor; - } } diff --git a/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderInternal.java b/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderInternal.java deleted file mode 100644 index ccae01d..0000000 --- a/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReaderInternal.java +++ /dev/null @@ -1,186 +0,0 @@ -package btools.routingapp; - -import android.graphics.Point; - -import org.xmlpull.v1.XmlPullParser; -import org.xmlpull.v1.XmlPullParserFactory; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.util.ArrayList; -import java.util.List; - -import btools.router.OsmNodeNamed; -import btools.router.OsmNogoPolygon; - -/** - * Read coordinates from a gpx-file - */ -public class CoordinateReaderInternal extends CoordinateReader { - private String internalDir; - - public CoordinateReaderInternal(String basedir) { - this(basedir, false); - } - - public CoordinateReaderInternal(String basedir, boolean shortPath) { - super(basedir); - if (shortPath) { - internalDir = basedir; - tracksdir = "/tracks"; - rootdir = ""; - } else { - internalDir = basedir + "/brouter/import"; - tracksdir = "/brouter/import/tracks"; - rootdir = "/brouter/import"; - } - } - - @Override - public long getTimeStamp() throws Exception { - long t1 = new File(internalDir + "/favourites_bak.gpx").lastModified(); - long t2 = new File(internalDir + "/favourites.gpx").lastModified(); - return t1 > t2 ? t1 : t2; - } - - @Override - public int getTurnInstructionMode() { - return 4; // comment style - } - - /* - * read the from and to position from a gpx-file - * (with hardcoded name for now) - */ - @Override - public void readPointmap() throws Exception { - if (!_readPointmap(internalDir + "/favourites_bak.gpx")) { - _readPointmap(internalDir + "/favourites.gpx"); - } - - try { - _readNogoLines(basedir + tracksdir); - } catch (IOException ioe) { - } - } - - private boolean _readPointmap(String filename) throws Exception { - BufferedReader br = null; - try { - br = new BufferedReader( - new InputStreamReader( - new FileInputStream(filename))); - } catch (FileNotFoundException e) { - // ignore until it's reading error - return false; - } - OsmNodeNamed n = null; - - for (; ; ) { - String line = br.readLine(); - if (line == null) break; - - int idx0 = line.indexOf(" lat=\""); - int idx10 = line.indexOf(""); - if (idx0 >= 0) { - n = new OsmNodeNamed(); - idx0 += 6; - int idx1 = line.indexOf('"', idx0); - n.ilat = (int) ((Double.parseDouble(line.substring(idx0, idx1)) + 90.) * 1000000. + 0.5); - int idx2 = line.indexOf(" lon=\""); - if (idx2 < 0) continue; - idx2 += 6; - int idx3 = line.indexOf('"', idx2); - n.ilon = (int) ((Double.parseDouble(line.substring(idx2, idx3)) + 180.) * 1000000. + 0.5); - if (idx3 < 0) continue; - } - if (n != null && idx10 >= 0) { - idx10 += 6; - int idx11 = line.indexOf("", idx10); - if (idx11 >= 0) { - n.name = line.substring(idx10, idx11).trim(); - checkAddPoint("(one-for-all)", n); - } - } - } - br.close(); - return true; - } - - private void _readNogoLines(String dirname) throws IOException { - - File dir = new File(dirname); - - if (dir.exists() && dir.isDirectory()) { - for (final File file : dir.listFiles()) { - final String name = file.getName(); - if (name.startsWith("nogo") && name.endsWith(".gpx")) { - try { - _readNogoLine(file); - } catch (Exception e) { - } - } - } - } - } - - private void _readNogoLine(File file) throws Exception { - XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); - factory.setNamespaceAware(false); - XmlPullParser xpp = factory.newPullParser(); - - xpp.setInput(new FileReader(file)); - - List tmpPts = new ArrayList(); - int eventType = xpp.getEventType(); - int numSeg = 0; - while (eventType != XmlPullParser.END_DOCUMENT) { - switch (eventType) { - case XmlPullParser.START_TAG: { - if (xpp.getName().equals("trkpt") || xpp.getName().equals("rtept")) { - final String lon = xpp.getAttributeValue(null, "lon"); - final String lat = xpp.getAttributeValue(null, "lat"); - if (lon != null && lat != null) { - tmpPts.add(new Point( - (int) ((Double.parseDouble(lon) + 180.) * 1000000. + 0.5), - (int) ((Double.parseDouble(lat) + 90.) * 1000000. + 0.5))); - } - } - break; - } - case XmlPullParser.END_TAG: { - if (xpp.getName().equals("trkseg") || xpp.getName().equals("rte")) { // rte has no segment - OsmNogoPolygon nogo = null; - if (tmpPts.size() >= 0) { - if (tmpPts.get(0).x == tmpPts.get(tmpPts.size() - 1).x && - tmpPts.get(0).y == tmpPts.get(tmpPts.size() - 1).y) { - nogo = new OsmNogoPolygon(true); - } else { - nogo = new OsmNogoPolygon(false); - } - for (Point p : tmpPts) { - nogo.addVertex(p.x, p.y); - } - nogo.calcBoundingCircle(); - final String name = file.getName(); - nogo.name = name.substring(0, name.length() - 4); - if (numSeg > 0) { - nogo.name += Integer.toString(numSeg + 1); - } - numSeg++; - checkAddPoint("(one-for-all)", nogo); - } - tmpPts.clear(); - } - break; - } - } - eventType = xpp.next(); - } - } -} From 56b7c108e452a2ce672d6370eec0fb187b364884 Mon Sep 17 00:00:00 2001 From: Manuel Fuhr Date: Sat, 7 May 2022 22:24:31 +0200 Subject: [PATCH 09/15] Remove redirect handling --- .../java/btools/routingapp/BRouterView.java | 30 ------------------- 1 file changed, 30 deletions(-) diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java b/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java index 541b960..d075546 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java @@ -24,7 +24,6 @@ import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; @@ -189,25 +188,6 @@ public class BRouterView extends View { if (cor.tracksdir != null) { tracksDir = new File(cor.basedir, cor.tracksdir); assertDirectoryExists("track directory", tracksDir, null, null); - - // output redirect: look for a pointerfile in tracksdir - File tracksDirPointer = new File(tracksDir, "brouter.redirect"); - if (tracksDirPointer.isFile()) { - String tracksDirStr = readSingleLineFile(tracksDirPointer); - if (tracksDirStr == null) - throw new IllegalArgumentException("redirect pointer file is empty: " + tracksDirPointer); - tracksDir = new File(tracksDirStr); - if (!(tracksDir.isDirectory())) - throw new IllegalArgumentException("redirect pointer file " + tracksDirPointer + " does not point to a directory: " + tracksDir); - } else { - File writeTest = new File(tracksDir + "/brouter.writetest"); - try { - writeTest.createNewFile(); - writeTest.delete(); - } catch (Exception e) { - tracksDir = null; - } - } } if (tracksDir == null) { tracksDir = new File(basedir, "brouter"); // fallback @@ -868,14 +848,4 @@ public class BRouterView extends View { ((BRouterActivity) getContext()).showModeConfigOverview(msg.toString()); } - private String readSingleLineFile(File f) { - try (FileInputStream fis = new FileInputStream(f); - InputStreamReader isr = new InputStreamReader(fis); - BufferedReader br = new BufferedReader(isr)) { - return br.readLine(); - } catch (Exception e) { - return null; - } - } - } From 3cfa1d954bd53aeed598e66ae6af11930377118f Mon Sep 17 00:00:00 2001 From: Manuel Fuhr Date: Mon, 9 May 2022 07:00:51 +0200 Subject: [PATCH 10/15] Test CoordinateReader --- .../routingapp/CoordinateReaderTest.java | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 brouter-routing-app/src/androidTest/java/btools/routingapp/CoordinateReaderTest.java diff --git a/brouter-routing-app/src/androidTest/java/btools/routingapp/CoordinateReaderTest.java b/brouter-routing-app/src/androidTest/java/btools/routingapp/CoordinateReaderTest.java new file mode 100644 index 0000000..b9edb7a --- /dev/null +++ b/brouter-routing-app/src/androidTest/java/btools/routingapp/CoordinateReaderTest.java @@ -0,0 +1,82 @@ +package btools.routingapp; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.closeTo; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.notNullValue; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.File; +import java.io.FileWriter; + + +public class CoordinateReaderTest { + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + @Test + public void readNogoRoute() throws Exception { + File importFolder = temporaryFolder.newFolder("brouter", "import", "tracks"); + File tempFile = new File(importFolder, "nogo_test.gpx"); + try (FileWriter writer = new FileWriter(tempFile)) { + writer.write("\n" + + "\n" + + " Nogo Route\n" + + " \n" + + " \n" + + "\n" + + " Nogo Oststadt\n" + + " Red\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + "\n" + + ""); + } + + CoordinateReader coordinateReader = CoordinateReader.obtainValidReader(temporaryFolder.getRoot().getAbsolutePath(), false); + assertThat(coordinateReader.nogopoints, hasSize(1)); + // Name should return "Nogo Oststadt", "Nogo Route" or "nogo_test.gpx" + assertThat(coordinateReader.nogopoints.get(0).name, equalTo("nogo_test")); + assertThat(coordinateReader.nogopoints.get(0).radius, closeTo(810.0, 5.0)); + } + + @Test + public void readWaypoints() throws Exception { + File importFolder = temporaryFolder.newFolder("brouter", "import"); + File tempFile = new File(importFolder, "favourites.gpx"); + try (FileWriter writer = new FileWriter(tempFile)) { + // https://en.wikipedia.org/wiki/GPS_Exchange_Format#Sample_GPX_document + writer.write("\n" + + "\n" + + "3.4from\n" + + "3.4to\n" + + "3.4via1\n" + + "3.4nogo100 Test\n" + + ""); + } + + CoordinateReader coordinateReader = CoordinateReader.obtainValidReader(temporaryFolder.getRoot().getAbsolutePath(), false); + assertThat(coordinateReader, notNullValue()); + // from, to, viaX are parsed into waypoints + assertThat(coordinateReader.waypoints, hasSize(3)); + assertThat(coordinateReader.nogopoints, hasSize(1)); + coordinateReader.readAllPoints(); + assertThat(coordinateReader.allpoints, hasSize(4)); + } +} From a767ed6dbb95a83ce1d0c841de737f34d437b642 Mon Sep 17 00:00:00 2001 From: Manuel Fuhr Date: Mon, 23 May 2022 18:21:33 +0200 Subject: [PATCH 11/15] Sort profiles --- .../src/main/java/btools/routingapp/BRouterActivity.java | 1 + 1 file changed, 1 insertion(+) diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java b/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java index 2ff2a69..5306206 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java @@ -328,6 +328,7 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat public void selectProfile(String[] items) { availableProfiles = items; + Arrays.sort(availableProfiles); // show main dialog showDialog(DIALOG_MAINACTION_ID); From bdecc2e1b91d06d5d4f86b286c4b62d421ea8295 Mon Sep 17 00:00:00 2001 From: Manuel Fuhr Date: Tue, 24 May 2022 22:56:36 +0200 Subject: [PATCH 12/15] Provide only single targetSdkVersion build Remove api19 build and default to api30 because we no longer access files of other apps. --- brouter-routing-app/build.gradle | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/brouter-routing-app/build.gradle b/brouter-routing-app/build.gradle index aa4c375..3266fd5 100644 --- a/brouter-routing-app/build.gradle +++ b/brouter-routing-app/build.gradle @@ -17,6 +17,7 @@ android { setProperty("archivesBaseName", "BRouterApp." + defaultConfig.versionName) minSdkVersion 14 + targetSdkVersion 30 testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } @@ -73,21 +74,6 @@ android { targetCompatibility JavaVersion.VERSION_1_8 } - - flavorDimensions "api" - productFlavors { - api19 { - dimension "api" - - targetSdkVersion 19 - } - api30 { - dimension "api" - - targetSdkVersion 30 - } - } - applicationVariants.all { variant -> { From 669ea28d1b0a0981816130cc884fe584524c5c10 Mon Sep 17 00:00:00 2001 From: Manuel Fuhr Date: Fri, 27 May 2022 07:51:24 +0200 Subject: [PATCH 13/15] Share track via intent --- .../java/btools/router/RoutingEngine.java | 5 + .../src/main/AndroidManifest.xml | 10 ++ .../btools/routingapp/BRouterActivity.java | 100 +++++++++++------- .../java/btools/routingapp/BRouterView.java | 39 ++++--- .../src/main/res/xml/filepaths.xml | 5 + 5 files changed, 106 insertions(+), 53 deletions(-) create mode 100644 brouter-routing-app/src/main/res/xml/filepaths.xml diff --git a/brouter-core/src/main/java/btools/router/RoutingEngine.java b/brouter-core/src/main/java/btools/router/RoutingEngine.java index a5022ba..bf059fe 100644 --- a/brouter-core/src/main/java/btools/router/RoutingEngine.java +++ b/brouter-core/src/main/java/btools/router/RoutingEngine.java @@ -63,6 +63,7 @@ public class RoutingEngine extends Thread private Object[] extract; private boolean directWeaving = !Boolean.getBoolean( "disableDirectWeaving" ); + private String outfile; public RoutingEngine( String outfileBase, String logfileBase, File segmentDir, List waypoints, RoutingContext rc ) @@ -185,6 +186,7 @@ public class RoutingEngine extends Thread track.writeGpx( filename ); foundTrack = track; alternativeIndex = i; + outfile = filename; } else { @@ -1369,4 +1371,7 @@ public class RoutingEngine extends Thread return terminated; } + public String getOutfile() { + return outfile; + } } diff --git a/brouter-routing-app/src/main/AndroidManifest.xml b/brouter-routing-app/src/main/AndroidManifest.xml index 3c8f22e..17957da 100644 --- a/brouter-routing-app/src/main/AndroidManifest.xml +++ b/brouter-routing-app/src/main/AndroidManifest.xml @@ -91,5 +91,15 @@ android:exported="true" android:process=":brouter_service" /> + + + + diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java b/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java index 5306206..b9af03c 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java @@ -122,16 +122,16 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat "*** Attention: ***\n\n" + "The Download Manager is used to download routing-data " + "files which can be up to 170MB each. Do not start the Download Manager " + "on a cellular data connection without a data plan! " + "Download speed is restricted to 16 MBit/s.").setPositiveButton("I know", new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - Intent intent = new Intent(BRouterActivity.this, BInstallerActivity.class); - startActivity(intent); - showNewDialog(DIALOG_MAINACTION_ID); - } - }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - finish(); - } - }); + public void onClick(DialogInterface dialog, int id) { + Intent intent = new Intent(BRouterActivity.this, BInstallerActivity.class); + startActivity(intent); + showNewDialog(DIALOG_MAINACTION_ID); + } + }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + finish(); + } + }); return builder.create(); case DIALOG_SHOW_API23_HELP_ID: builder @@ -148,10 +148,10 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat + "the APK of the BRouter App from the release page ( http://brouter.de/brouter/revisions.html ), which " + "is still built against Android API 10, and does not have these limitations. " ).setNegativeButton("Exit", new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - finish(); - } - }); + public void onClick(DialogInterface dialog, int id) { + finish(); + } + }); return builder.create(); case DIALOG_SHOW_REPEAT_TIMEOUT_HELP_ID: builder @@ -161,10 +161,10 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat + "when started from your map-tool. If you repeat the same request from your " + "maptool, with the exact same destination point and a close-by starting point, " + "this request is guaranteed not to time out.").setNegativeButton("Exit", new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - finish(); - } - }); + public void onClick(DialogInterface dialog, int id) { + finish(); + } + }); return builder.create(); case DIALOG_OLDDATAHINT_ID: builder @@ -272,31 +272,49 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat }); return builder.create(); case DIALOG_SHOWRESULT_ID: - String leftLabel = wpCount < 0 ? (wpCount != -2 ? "Exit" : "Help") : (wpCount == 0 ? "Select from" : "Select to/via"); - String rightLabel = wpCount < 2 ? (wpCount == -3 ? "Help" : "Server-Mode") : "Calc Route"; + // -3: Repeated route calculation + // -2: No waypoints? + // -1: Route calculated + // other: Select waypoints for route calculation + builder.setTitle(title).setMessage(errorMessage); - builder.setTitle(title).setMessage(errorMessage).setPositiveButton(leftLabel, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - if (wpCount == -2) { - showWaypointDatabaseHelp(); - } else if (wpCount == -1 || wpCount == -3) { - finish(); - } else { - mBRouterView.pickWaypoints(); - } - } - }).setNegativeButton(rightLabel, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - if (wpCount == -3) { - showRepeatTimeoutHelp(); - } else if (wpCount < 2) { - mBRouterView.startConfigureService(); - } else { - mBRouterView.finishWaypointSelection(); - mBRouterView.startProcessing(selectedProfile); - } - } + // Neutral button + if (wpCount == 0) { + builder.setNeutralButton("Server-Mode", (dialog, which) -> { + mBRouterView.startConfigureService(); + }); + } else if (wpCount == -3) { + builder.setNeutralButton("Info", (dialog, which) -> { + showRepeatTimeoutHelp(); + }); + } else if (wpCount == -2) { + builder.setNeutralButton("Help", (dialog, which) -> { + showWaypointDatabaseHelp(); + }); + } else if (wpCount >= 2) { + builder.setNeutralButton("Calc Route", (dialog, which) -> { + mBRouterView.finishWaypointSelection(); + mBRouterView.startProcessing(selectedProfile); + }); + } + + // Positive button + if (wpCount == -3 || wpCount == -1) { + builder.setPositiveButton("Share GPX", (dialog, which) -> { + mBRouterView.shareTrack(); + }); + } else if (wpCount >= 0) { + String selectLabel = wpCount == 0 ? "Select from" : "Select to/via"; + builder.setPositiveButton(selectLabel, (dialog, which) -> { + mBRouterView.pickWaypoints(); + }); + } + + // Negative button + builder.setNegativeButton("Exit", (dialog, which) -> { + finish(); }); + return builder.create(); case DIALOG_MODECONFIGOVERVIEW_ID: builder.setTitle("Success").setMessage(message).setPositiveButton("Exit", new DialogInterface.OnClickListener() { diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java b/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java index d075546..8c6f52e 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java @@ -2,6 +2,7 @@ package btools.routingapp; import android.Manifest; import android.content.Context; +import android.content.Intent; import android.content.pm.PackageManager; import android.content.res.AssetManager; import android.graphics.Canvas; @@ -13,6 +14,7 @@ import android.widget.Toast; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; +import androidx.core.content.FileProvider; import java.io.BufferedReader; import java.io.BufferedWriter; @@ -68,6 +70,7 @@ public class BRouterView extends View { private boolean waitingForMigration = false; private String rawTrackPath; private String oldMigrationPath; + private String trackOutfile; private boolean needsViaSelection; private boolean needsNogoSelection; private boolean needsWaypointSelection; @@ -283,21 +286,19 @@ public class BRouterView extends View { } } - private void moveFile(String inputPath, String inputFile, String outputPath) { - + private void copyFile(String inputPath, String inputFile, String outputPath) { InputStream in; OutputStream out; - try { + try { //create output directory if it doesn't exist File dir = new File(outputPath); if (!dir.exists()) { dir.mkdirs(); } - - in = new FileInputStream(inputPath + "/" + inputFile); - out = new FileOutputStream(outputPath + "/" + inputFile); + in = new FileInputStream(new File(inputPath, inputFile)); + out = new FileOutputStream(new File(outputPath, inputFile)); byte[] buffer = new byte[1024]; int read; @@ -310,16 +311,17 @@ public class BRouterView extends View { out.flush(); out.close(); - // delete the original file - new File(inputPath + "/" + inputFile).delete(); - - - } catch (FileNotFoundException fnfe1) { - Log.e("tag", fnfe1.getMessage()); + } catch (FileNotFoundException fileNotFoundException) { + Log.e("tag", fileNotFoundException.getMessage()); } catch (Exception e) { Log.e("tag", e.getMessage()); } + } + private void moveFile(String inputPath, String inputFile, String outputPath) { + copyFile(inputPath, inputFile, outputPath); + // delete the original file + new File(inputPath, inputFile).delete(); } public boolean hasUpToDateLookups() { @@ -707,6 +709,7 @@ public class BRouterView extends View { title += " / " + cr.getAlternativeIndex() + ". Alternative"; ((BRouterActivity) getContext()).showResultMessage(title, result, rawTrackPath == null ? -1 : -3); + trackOutfile = cr.getOutfile(); } cr = null; waitingForSelection = true; @@ -848,4 +851,16 @@ public class BRouterView extends View { ((BRouterActivity) getContext()).showModeConfigOverview(msg.toString()); } + public void shareTrack() { + File track = new File(trackOutfile); + // Copy file to cache to ensure FileProvider allows sharing the file + File cacheDir = getContext().getCacheDir(); + copyFile(track.getParent(), track.getName(), cacheDir.getAbsolutePath()); + Intent intent = new Intent(); + intent.setDataAndType(FileProvider.getUriForFile(getContext(), "btools.routing.fileprovider", new File(cacheDir, track.getName())), + "application/gpx+xml"); + intent.setAction(Intent.ACTION_VIEW); + intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + getContext().startActivity(intent); + } } diff --git a/brouter-routing-app/src/main/res/xml/filepaths.xml b/brouter-routing-app/src/main/res/xml/filepaths.xml new file mode 100644 index 0000000..5b58665 --- /dev/null +++ b/brouter-routing-app/src/main/res/xml/filepaths.xml @@ -0,0 +1,5 @@ + + + From f42f10ba4bb7763f0a079edcd2f0ccc6f4b77e4a Mon Sep 17 00:00:00 2001 From: Manuel Fuhr Date: Wed, 8 Jun 2022 06:49:34 +0200 Subject: [PATCH 14/15] Remove help dialog for api10 --- .../btools/routingapp/BRouterActivity.java | 31 +------------------ 1 file changed, 1 insertion(+), 30 deletions(-) diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java b/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java index b9af03c..c2a365e 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java @@ -44,7 +44,6 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat private static final int DIALOG_MAINACTION_ID = 12; private static final int DIALOG_OLDDATAHINT_ID = 13; private static final int DIALOG_SHOW_REPEAT_TIMEOUT_HELP_ID = 16; - private static final int DIALOG_SHOW_API23_HELP_ID = 17; private final Set dialogIds = new HashSet<>(); private BRouterView mBRouterView; private String[] availableProfiles; @@ -133,26 +132,6 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat } }); return builder.create(); - case DIALOG_SHOW_API23_HELP_ID: - builder - .setTitle("Android >=6 limitations") - .setMessage( - "You are using the BRouter APP on Android >= 6, where classic mode is no longer supported. " - + "Reason is that security policy does not permit any longer to read the waypoint databases of other apps. " - + "That's o.k. if you want to use BRouter in server-mode only, where the apps actively send the waypoints " - + "via a remote procedure call to BRouter (And Locus can also send nogo areas). " - + "So the only functions you need to start the BRouter App are 1) to initially define the base directory " - + "2) to download routing data files and 3) to configure the profile mapping via the 'Server-Mode' button. " - + "You will eventually not be able to define nogo-areas (OsmAnd, Orux) or to do " - + "very long distance calculations. If you want to get classic mode back, you can manually install " - + "the APK of the BRouter App from the release page ( http://brouter.de/brouter/revisions.html ), which " - + "is still built against Android API 10, and does not have these limitations. " - ).setNegativeButton("Exit", new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - finish(); - } - }); - return builder.create(); case DIALOG_SHOW_REPEAT_TIMEOUT_HELP_ID: builder .setTitle("Successfully prepared a timeout-free calculation") @@ -273,7 +252,7 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat return builder.create(); case DIALOG_SHOWRESULT_ID: // -3: Repeated route calculation - // -2: No waypoints? + // -2: Unused? // -1: Route calculated // other: Select waypoints for route calculation builder.setTitle(title).setMessage(errorMessage); @@ -287,10 +266,6 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat builder.setNeutralButton("Info", (dialog, which) -> { showRepeatTimeoutHelp(); }); - } else if (wpCount == -2) { - builder.setNeutralButton("Help", (dialog, which) -> { - showWaypointDatabaseHelp(); - }); } else if (wpCount >= 2) { builder.setNeutralButton("Calc Route", (dialog, which) -> { mBRouterView.finishWaypointSelection(); @@ -415,10 +390,6 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat showNewDialog(DIALOG_PICKWAYPOINT_ID); } - public void showWaypointDatabaseHelp() { - showNewDialog(DIALOG_SHOW_API23_HELP_ID); - } - public void showRepeatTimeoutHelp() { showNewDialog(DIALOG_SHOW_REPEAT_TIMEOUT_HELP_ID); } From 08df1d9909ca2558351fda125a62b1abc0320b58 Mon Sep 17 00:00:00 2001 From: Manuel Fuhr Date: Wed, 8 Jun 2022 06:52:50 +0200 Subject: [PATCH 15/15] Finish after sharing track --- .../src/main/java/btools/routingapp/BRouterActivity.java | 1 + 1 file changed, 1 insertion(+) diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java b/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java index c2a365e..d147d7e 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java @@ -277,6 +277,7 @@ public class BRouterActivity extends AppCompatActivity implements ActivityCompat if (wpCount == -3 || wpCount == -1) { builder.setPositiveButton("Share GPX", (dialog, which) -> { mBRouterView.shareTrack(); + finish(); }); } else if (wpCount >= 0) { String selectLabel = wpCount == 0 ? "Select from" : "Select to/via";