Use onClick handler to start download

This commit is contained in:
Manuel Fuhr 2021-10-15 17:19:32 +02:00
parent 51ef5c6aad
commit da7569b0a0
2 changed files with 102 additions and 69 deletions

View file

@ -1,5 +1,10 @@
package btools.routingapp; 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.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.Dialog; import android.app.Dialog;
@ -18,20 +23,23 @@ import android.widget.TextView;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Locale;
import btools.router.RoutingHelper;
public class BInstallerActivity extends Activity { public class BInstallerActivity extends Activity {
public static final String DOWNLOAD_ACTION = "btools.routingapp.download"; public static final String DOWNLOAD_ACTION = "btools.routingapp.download";
private static final int DIALOG_CONFIRM_DELETE_ID = 1; private static final int DIALOG_CONFIRM_DELETE_ID = 1;
public static boolean downloadCanceled = false; public static boolean downloadCanceled = false;
private File baseDir; private File mBaseDir;
private BInstallerView mBInstallerView; private BInstallerView mBInstallerView;
private DownloadReceiver downloadReceiver; private DownloadReceiver downloadReceiver;
private View mDownloadInfo; private View mDownloadInfo;
private TextView mDownloadInfoText; private TextView mDownloadInfoText;
private Button mButtonDownloadCancel; private Button mButtonDownloadCancel;
static public long getAvailableSpace(String baseDir) { public static long getAvailableSpace(String baseDir) {
StatFs stat = new StatFs(baseDir); StatFs stat = new StatFs(baseDir);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { 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); setContentView(R.layout.activity_binstaller);
mBInstallerView = findViewById(R.id.BInstallerView); mBInstallerView = findViewById(R.id.BInstallerView);
mBInstallerView.setOnClickListener(
view -> mBInstallerView.toggleDownload()
);
mDownloadInfo = findViewById(R.id.view_download_progress); mDownloadInfo = findViewById(R.id.view_download_progress);
mDownloadInfoText = findViewById(R.id.textViewDownloadProgress); mDownloadInfoText = findViewById(R.id.textViewDownloadProgress);
mButtonDownloadCancel = findViewById(R.id.buttonDownloadCancel); mButtonDownloadCancel = findViewById(R.id.buttonDownloadCancel);
@ -57,7 +68,8 @@ public class BInstallerActivity extends Activity {
cancelDownload(); cancelDownload();
}); });
baseDir = ConfigHelper.getBaseDir(this); mBaseDir = ConfigHelper.getBaseDir(this);
scanExistingFiles();
} }
private String baseNameForTile(int tileIndex) { private String baseNameForTile(int tileIndex) {
@ -69,7 +81,7 @@ public class BInstallerActivity extends Activity {
} }
private void deleteRawTracks() { private void deleteRawTracks() {
File modeDir = new File(baseDir, "brouter/modes"); File modeDir = new File(mBaseDir, "brouter/modes");
String[] fileNames = modeDir.list(); String[] fileNames = modeDir.list();
if (fileNames == null) return; if (fileNames == null) return;
for (String fileName : fileNames) { for (String fileName : fileNames) {
@ -97,7 +109,7 @@ public class BInstallerActivity extends Activity {
mDownloadInfoText.setText(R.string.download_info_start); mDownloadInfoText.setText(R.string.download_info_start);
Intent intent = new Intent(this, DownloadService.class); Intent intent = new Intent(this, DownloadService.class);
intent.putExtra("dir", baseDir.getAbsolutePath() + "/brouter/"); intent.putExtra("dir", mBaseDir.getAbsolutePath() + "/brouter/");
intent.putExtra("urlparts", urlparts); intent.putExtra("urlparts", urlparts);
startService(intent); startService(intent);
@ -154,6 +166,54 @@ public class BInstallerActivity extends Activity {
showDialog(DIALOG_CONFIRM_DELETE_ID); 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 { public class DownloadReceiver extends BroadcastReceiver {
@Override @Override

View file

@ -18,15 +18,14 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Locale;
import btools.router.RoutingHelper; import btools.router.RoutingHelper;
public class BInstallerView extends View { public class BInstallerView extends View {
private static final int MASK_SELECTED_RD5 = 1; public static final int MASK_SELECTED_RD5 = 1;
private static final int MASK_DELETED_RD5 = 2; public static final int MASK_DELETED_RD5 = 2;
private static final int MASK_INSTALLED_RD5 = 4; public static final int MASK_INSTALLED_RD5 = 4;
private static final int MASK_CURRENT_RD5 = 8; public static final int MASK_CURRENT_RD5 = 8;
private final File baseDir; private final File baseDir;
private final File segmentDir; private final File segmentDir;
private final Matrix mat; private final Matrix mat;
@ -54,6 +53,7 @@ public class BInstallerView extends View {
private long totalSize = 0; private long totalSize = 0;
private long rd5Tiles = 0; private long rd5Tiles = 0;
private long delTiles = 0; private long delTiles = 0;
private OnClickListener mOnClickListener;
public BInstallerView(Context context, AttributeSet attrs) { public BInstallerView(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
@ -97,6 +97,33 @@ public class BInstallerView extends View {
mat.postScale(viewscale, viewscale); 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) { protected String baseNameForTile(int tileIndex) {
int lon = (tileIndex % 72) * 5 - 180; int lon = (tileIndex % 72) * 5 - 180;
int lat = (tileIndex / 72) * 5 - 90; 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); return (35 - iy) * 72 + (ix >= 70 ? ix - 70 : ix + 2);
} }
private int tileForBaseName(String basename) { public void toggleDownload() {
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() {
if (delTiles > 0) { if (delTiles > 0) {
((BInstallerActivity) getContext()).showConfirmDelete(); ((BInstallerActivity) getContext()).showConfirmDelete();
return; return;
@ -164,15 +176,6 @@ public class BInstallerView extends View {
return -1; 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 // get back the current image scale
private float currentScale() { private float currentScale() {
testVector[1] = 1.f; testVector[1] = 1.f;
@ -180,38 +183,6 @@ public class BInstallerView extends View {
return testVector[1] / viewscale; 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 @Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) { protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh); super.onSizeChanged(w, h, oldw, oldh);
@ -394,7 +365,7 @@ public class BInstallerView extends View {
boolean tilesv = currentScale() >= 3.f; boolean tilesv = currentScale() >= 3.f;
if (tilesVisible && !tilesv) { if (tilesVisible && !tilesv) {
clearTileSelection(MASK_SELECTED_RD5 | MASK_DELETED_RD5); clearAllTilesStatus(MASK_SELECTED_RD5 | MASK_DELETED_RD5);
} }
tilesVisible = tilesv; tilesVisible = tilesv;
} }
@ -432,7 +403,9 @@ public class BInstallerView extends View {
} }
} }
} }
toggleDownload(); if (mOnClickListener != null) {
mOnClickListener.onClick(null);
}
invalidate(); invalidate();
break; break;
} }