From da7569b0a04a9fe5168f6bdbb59ff899ba396325 Mon Sep 17 00:00:00 2001 From: Manuel Fuhr Date: Fri, 15 Oct 2021 17:19:32 +0200 Subject: [PATCH] Use onClick handler to start download --- .../btools/routingapp/BInstallerActivity.java | 70 +++++++++++- .../btools/routingapp/BInstallerView.java | 101 +++++++----------- 2 files changed, 102 insertions(+), 69 deletions(-) diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BInstallerActivity.java b/brouter-routing-app/src/main/java/btools/routingapp/BInstallerActivity.java index f2ec66b..31320dc 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BInstallerActivity.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BInstallerActivity.java @@ -1,5 +1,10 @@ package btools.routingapp; +import static btools.routingapp.BInstallerView.MASK_CURRENT_RD5; +import static btools.routingapp.BInstallerView.MASK_DELETED_RD5; +import static btools.routingapp.BInstallerView.MASK_INSTALLED_RD5; +import static btools.routingapp.BInstallerView.MASK_SELECTED_RD5; + import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; @@ -18,20 +23,23 @@ import android.widget.TextView; import java.io.File; import java.util.ArrayList; +import java.util.Locale; + +import btools.router.RoutingHelper; public class BInstallerActivity extends Activity { public static final String DOWNLOAD_ACTION = "btools.routingapp.download"; private static final int DIALOG_CONFIRM_DELETE_ID = 1; public static boolean downloadCanceled = false; - private File baseDir; + private File mBaseDir; private BInstallerView mBInstallerView; private DownloadReceiver downloadReceiver; private View mDownloadInfo; private TextView mDownloadInfoText; private Button mButtonDownloadCancel; - static public long getAvailableSpace(String baseDir) { + public static long getAvailableSpace(String baseDir) { StatFs stat = new StatFs(baseDir); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { @@ -50,6 +58,9 @@ public class BInstallerActivity extends Activity { setContentView(R.layout.activity_binstaller); mBInstallerView = findViewById(R.id.BInstallerView); + mBInstallerView.setOnClickListener( + view -> mBInstallerView.toggleDownload() + ); mDownloadInfo = findViewById(R.id.view_download_progress); mDownloadInfoText = findViewById(R.id.textViewDownloadProgress); mButtonDownloadCancel = findViewById(R.id.buttonDownloadCancel); @@ -57,7 +68,8 @@ public class BInstallerActivity extends Activity { cancelDownload(); }); - baseDir = ConfigHelper.getBaseDir(this); + mBaseDir = ConfigHelper.getBaseDir(this); + scanExistingFiles(); } private String baseNameForTile(int tileIndex) { @@ -69,7 +81,7 @@ public class BInstallerActivity extends Activity { } private void deleteRawTracks() { - File modeDir = new File(baseDir, "brouter/modes"); + File modeDir = new File(mBaseDir, "brouter/modes"); String[] fileNames = modeDir.list(); if (fileNames == null) return; for (String fileName : fileNames) { @@ -97,7 +109,7 @@ public class BInstallerActivity extends Activity { mDownloadInfoText.setText(R.string.download_info_start); Intent intent = new Intent(this, DownloadService.class); - intent.putExtra("dir", baseDir.getAbsolutePath() + "/brouter/"); + intent.putExtra("dir", mBaseDir.getAbsolutePath() + "/brouter/"); intent.putExtra("urlparts", urlparts); startService(intent); @@ -154,6 +166,54 @@ public class BInstallerActivity extends Activity { showDialog(DIALOG_CONFIRM_DELETE_ID); } + private void scanExistingFiles() { + mBInstallerView.clearAllTilesStatus(MASK_INSTALLED_RD5 | MASK_CURRENT_RD5); + + scanExistingFiles(new File(mBaseDir, "brouter/segments4")); + + File secondary = RoutingHelper.getSecondarySegmentDir(new File(mBaseDir, "brouter/segments4")); + if (secondary != null) { + scanExistingFiles(secondary); + } + + long availableSize = -1; + try { + availableSize = getAvailableSpace(mBaseDir.getAbsolutePath()); + } catch (Exception e) { /* ignore */ } + mBInstallerView.setAvailableSize(availableSize); + } + + private void scanExistingFiles(File dir) { + String[] fileNames = dir.list(); + if (fileNames == null) return; + String suffix = ".rd5"; + for (String fileName : fileNames) { + if (fileName.endsWith(suffix)) { + String basename = fileName.substring(0, fileName.length() - suffix.length()); + int tileIndex = tileForBaseName(basename); + mBInstallerView.setTileStatus(tileIndex, MASK_INSTALLED_RD5); + + long age = System.currentTimeMillis() - new File(dir, fileName).lastModified(); + if (age < 10800000) mBInstallerView.setTileStatus(tileIndex, MASK_CURRENT_RD5); // 3 hours + } + } + } + + private int tileForBaseName(String basename) { + String uname = basename.toUpperCase(Locale.ROOT); + int idx = uname.indexOf("_"); + if (idx < 0) return -1; + String slon = uname.substring(0, idx); + String slat = uname.substring(idx + 1); + int ilon = slon.charAt(0) == 'W' ? -Integer.parseInt(slon.substring(1)) : + (slon.charAt(0) == 'E' ? Integer.parseInt(slon.substring(1)) : -1); + int ilat = slat.charAt(0) == 'S' ? -Integer.parseInt(slat.substring(1)) : + (slat.charAt(0) == 'N' ? Integer.parseInt(slat.substring(1)) : -1); + if (ilon < -180 || ilon >= 180 || ilon % 5 != 0) return -1; + if (ilat < -90 || ilat >= 90 || ilat % 5 != 0) return -1; + return (ilon + 180) / 5 + 72 * ((ilat + 90) / 5); + } + public class DownloadReceiver extends BroadcastReceiver { @Override diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BInstallerView.java b/brouter-routing-app/src/main/java/btools/routingapp/BInstallerView.java index 8d3a101..af90c5c 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BInstallerView.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BInstallerView.java @@ -18,15 +18,14 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; -import java.util.Locale; import btools.router.RoutingHelper; public class BInstallerView extends View { - private static final int MASK_SELECTED_RD5 = 1; - private static final int MASK_DELETED_RD5 = 2; - private static final int MASK_INSTALLED_RD5 = 4; - private static final int MASK_CURRENT_RD5 = 8; + public static final int MASK_SELECTED_RD5 = 1; + public static final int MASK_DELETED_RD5 = 2; + public static final int MASK_INSTALLED_RD5 = 4; + public static final int MASK_CURRENT_RD5 = 8; private final File baseDir; private final File segmentDir; private final Matrix mat; @@ -54,6 +53,7 @@ public class BInstallerView extends View { private long totalSize = 0; private long rd5Tiles = 0; private long delTiles = 0; + private OnClickListener mOnClickListener; public BInstallerView(Context context, AttributeSet attrs) { super(context, attrs); @@ -97,6 +97,33 @@ public class BInstallerView extends View { mat.postScale(viewscale, viewscale); } + private void scanExistingFiles() { + + } + + public void setAvailableSize(long availableSize) { + this.availableSize = availableSize; + } + + public void setTileStatus(int tileIndex, int tileMask) { + tileStatus[tileIndex] |= tileMask; + } + + public void clearAllTilesStatus(int tileMask) { + for (int ix = 0; ix < 72; ix++) { + for (int iy = 0; iy < 36; iy++) { + int tileIndex = gridPos2Tileindex(ix, iy); + tileStatus[tileIndex] ^= tileStatus[tileIndex] & tileMask; + } + } + invalidate(); + } + + @Override + public void setOnClickListener(OnClickListener listener) { + mOnClickListener = listener; + } + protected String baseNameForTile(int tileIndex) { int lon = (tileIndex % 72) * 5 - 180; int lat = (tileIndex / 72) * 5 - 90; @@ -109,22 +136,7 @@ public class BInstallerView extends View { return (35 - iy) * 72 + (ix >= 70 ? ix - 70 : ix + 2); } - private int tileForBaseName(String basename) { - String uname = basename.toUpperCase(Locale.ROOT); - int idx = uname.indexOf("_"); - if (idx < 0) return -1; - String slon = uname.substring(0, idx); - String slat = uname.substring(idx + 1); - int ilon = slon.charAt(0) == 'W' ? -Integer.parseInt(slon.substring(1)) : - (slon.charAt(0) == 'E' ? Integer.parseInt(slon.substring(1)) : -1); - int ilat = slat.charAt(0) == 'S' ? -Integer.parseInt(slat.substring(1)) : - (slat.charAt(0) == 'N' ? Integer.parseInt(slat.substring(1)) : -1); - if (ilon < -180 || ilon >= 180 || ilon % 5 != 0) return -1; - if (ilat < -90 || ilat >= 90 || ilat % 5 != 0) return -1; - return (ilon + 180) / 5 + 72 * ((ilat + 90) / 5); - } - - private void toggleDownload() { + public void toggleDownload() { if (delTiles > 0) { ((BInstallerActivity) getContext()).showConfirmDelete(); return; @@ -164,15 +176,6 @@ public class BInstallerView extends View { return -1; } - private void clearTileSelection(int mask) { - // clear selection if zooming out - for (int ix = 0; ix < 72; ix++) - for (int iy = 0; iy < 36; iy++) { - int tidx = gridPos2Tileindex(ix, iy); - tileStatus[tidx] ^= tileStatus[tidx] & mask; - } - } - // get back the current image scale private float currentScale() { testVector[1] = 1.f; @@ -180,38 +183,6 @@ public class BInstallerView extends View { return testVector[1] / viewscale; } - private void scanExistingFiles() { - clearTileSelection(MASK_INSTALLED_RD5 | MASK_CURRENT_RD5); - - scanExistingFiles(new File(baseDir, "brouter/segments4")); - - File secondary = RoutingHelper.getSecondarySegmentDir(new File(baseDir, "brouter/segments4")); - if (secondary != null) { - scanExistingFiles(secondary); - } - - availableSize = -1; - try { - availableSize = BInstallerActivity.getAvailableSpace(baseDir.getAbsolutePath()); - } catch (Exception e) { /* ignore */ } - } - - private void scanExistingFiles(File dir) { - String[] fileNames = dir.list(); - if (fileNames == null) return; - String suffix = ".rd5"; - for (String fileName : fileNames) { - if (fileName.endsWith(suffix)) { - String basename = fileName.substring(0, fileName.length() - suffix.length()); - int tidx = tileForBaseName(basename); - tileStatus[tidx] |= MASK_INSTALLED_RD5; - - long age = System.currentTimeMillis() - new File(dir, fileName).lastModified(); - if (age < 10800000) tileStatus[tidx] |= MASK_CURRENT_RD5; // 3 hours - } - } - } - @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); @@ -394,7 +365,7 @@ public class BInstallerView extends View { boolean tilesv = currentScale() >= 3.f; if (tilesVisible && !tilesv) { - clearTileSelection(MASK_SELECTED_RD5 | MASK_DELETED_RD5); + clearAllTilesStatus(MASK_SELECTED_RD5 | MASK_DELETED_RD5); } tilesVisible = tilesv; } @@ -432,7 +403,9 @@ public class BInstallerView extends View { } } } - toggleDownload(); + if (mOnClickListener != null) { + mOnClickListener.onClick(null); + } invalidate(); break; }