Show download progress in different view

This commit is contained in:
Manuel Fuhr 2021-10-30 09:25:51 +02:00
parent 6045a18a61
commit 51ef5c6aad
5 changed files with 124 additions and 91 deletions

View file

@ -12,14 +12,24 @@ import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.StatFs;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.File;
import java.util.ArrayList;
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 BInstallerView mBInstallerView;
private DownloadReceiver downloadReceiver;
private View mDownloadInfo;
private TextView mDownloadInfoText;
private Button mButtonDownloadCancel;
static public long getAvailableSpace(String baseDir) {
StatFs stat = new StatFs(baseDir);
@ -40,6 +50,58 @@ public class BInstallerActivity extends Activity {
setContentView(R.layout.activity_binstaller);
mBInstallerView = findViewById(R.id.BInstallerView);
mDownloadInfo = findViewById(R.id.view_download_progress);
mDownloadInfoText = findViewById(R.id.textViewDownloadProgress);
mButtonDownloadCancel = findViewById(R.id.buttonDownloadCancel);
mButtonDownloadCancel.setOnClickListener(view -> {
cancelDownload();
});
baseDir = ConfigHelper.getBaseDir(this);
}
private String baseNameForTile(int tileIndex) {
int lon = (tileIndex % 72) * 5 - 180;
int lat = (tileIndex / 72) * 5 - 90;
String slon = lon < 0 ? "W" + (-lon) : "E" + lon;
String slat = lat < 0 ? "S" + (-lat) : "N" + lat;
return slon + "_" + slat;
}
private void deleteRawTracks() {
File modeDir = new File(baseDir, "brouter/modes");
String[] fileNames = modeDir.list();
if (fileNames == null) return;
for (String fileName : fileNames) {
if (fileName.endsWith("_rawtrack.dat")) {
File f = new File(modeDir, fileName);
f.delete();
}
}
}
private void cancelDownload() {
downloadCanceled = true;
mDownloadInfoText.setText(getString(R.string.download_info_cancel));
}
public void downloadAll(ArrayList<Integer> downloadList) {
ArrayList<String> urlparts = new ArrayList<>();
for (Integer i : downloadList) {
urlparts.add(baseNameForTile(i));
}
mBInstallerView.setVisibility(View.GONE);
mDownloadInfo.setVisibility(View.VISIBLE);
downloadCanceled = false;
mDownloadInfoText.setText(R.string.download_info_start);
Intent intent = new Intent(this, DownloadService.class);
intent.putExtra("dir", baseDir.getAbsolutePath() + "/brouter/");
intent.putExtra("urlparts", urlparts);
startService(intent);
deleteRawTracks(); // invalidate raw-tracks after data update
}
@Override
@ -99,7 +161,12 @@ public class BInstallerActivity extends Activity {
if (intent.hasExtra("txt")) {
String txt = intent.getStringExtra("txt");
boolean ready = intent.getBooleanExtra("ready", false);
mBInstallerView.setState(txt, ready);
if (!ready) {
mBInstallerView.setVisibility(View.VISIBLE);
mDownloadInfo.setVisibility(View.GONE);
scanExistingFiles();
}
mDownloadInfoText.setText(txt);
}
}
}