more checks for online problems

This commit is contained in:
afischerdev 2023-04-17 10:03:32 +02:00
parent 3675a2c9dd
commit b4ad0c4b38
2 changed files with 38 additions and 27 deletions

View file

@ -33,7 +33,6 @@ import androidx.work.NetworkType;
import androidx.work.OneTimeWorkRequest; import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkInfo; import androidx.work.WorkInfo;
import androidx.work.WorkManager; import androidx.work.WorkManager;
import androidx.work.WorkRequest;
import com.google.android.material.progressindicator.LinearProgressIndicator; import com.google.android.material.progressindicator.LinearProgressIndicator;
import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListenableFuture;
@ -92,8 +91,7 @@ public class BInstallerActivity extends AppCompatActivity {
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.POST_NOTIFICATIONS) if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.POST_NOTIFICATIONS)
== PackageManager.PERMISSION_GRANTED) { == PackageManager.PERMISSION_GRANTED) {
// nothing to do // nothing to do
} } else if (shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) {
if (shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) {
// //
} else { } else {
// You can directly ask for the permission. // You can directly ask for the permission.
@ -222,14 +220,14 @@ public class BInstallerActivity extends AppCompatActivity {
.setRequiredNetworkType(NetworkType.CONNECTED) .setRequiredNetworkType(NetworkType.CONNECTED)
.build(); .build();
WorkRequest downloadWorkRequest = OneTimeWorkRequest downloadWorkRequest =
new OneTimeWorkRequest.Builder(DownloadWorker.class) new OneTimeWorkRequest.Builder(DownloadWorker.class)
.setInputData(inputData) .setInputData(inputData)
.setConstraints(constraints) .setConstraints(constraints)
.build(); .build();
WorkManager workManager = WorkManager.getInstance(getApplicationContext()); WorkManager workManager = WorkManager.getInstance(getApplicationContext());
workManager.enqueueUniqueWork(DownloadWorker.WORKER_NAME, ExistingWorkPolicy.KEEP, (OneTimeWorkRequest) downloadWorkRequest); workManager.enqueueUniqueWork(DownloadWorker.WORKER_NAME, ExistingWorkPolicy.KEEP, downloadWorkRequest);
try { try {
WorkInfo wi = WorkManager.getInstance(getApplicationContext()).getWorkInfoById(downloadWorkRequest.getId()).get(); WorkInfo wi = WorkManager.getInstance(getApplicationContext()).getWorkInfoById(downloadWorkRequest.getId()).get();
@ -256,7 +254,6 @@ public class BInstallerActivity extends AppCompatActivity {
private void startObserver(WorkInfo workInfo) { private void startObserver(WorkInfo workInfo) {
if (workInfo != null) { if (workInfo != null) {
if (workInfo.getState() == WorkInfo.State.ENQUEUED || workInfo.getState() == WorkInfo.State.BLOCKED) { if (workInfo.getState() == WorkInfo.State.ENQUEUED || workInfo.getState() == WorkInfo.State.BLOCKED) {
Log.d("worker", "cancel " + workInfo.getState());
//WorkManager.getInstance(getApplicationContext()).cancelWorkById(downloadWorkRequest.getId()); //WorkManager.getInstance(getApplicationContext()).cancelWorkById(downloadWorkRequest.getId());
} }
@ -265,6 +262,9 @@ public class BInstallerActivity extends AppCompatActivity {
mProgressIndicator.hide(); mProgressIndicator.hide();
mProgressIndicator.setIndeterminate(true); mProgressIndicator.setIndeterminate(true);
mProgressIndicator.show(); mProgressIndicator.show();
mButtonDownload.setText(getString(R.string.action_cancel));
mButtonDownload.setEnabled(true);
} }
if (workInfo.getState() == WorkInfo.State.RUNNING) { if (workInfo.getState() == WorkInfo.State.RUNNING) {
@ -310,12 +310,17 @@ public class BInstallerActivity extends AppCompatActivity {
} }
} }
if (error != null && error.startsWith("error new app")) { if (error != null && error.startsWith("Version new app")) {
showAppUpdate(); showAppUpdate();
} else if (error != null && error.startsWith("Version error")) { } else if (error != null && error.startsWith("Version error")) {
showConfirmNextSteps(); showConfirmNextSteps();
} else if (error != null && error.startsWith("Version diffs")) { } else if (error != null && error.startsWith("Version diffs")) {
showConfirmGetDiffs(); showConfirmGetDiffs();
} else if (error != null) {
stopDownload();
mBInstallerView.setOnSelectListener(onSelectListener);
mBInstallerView.clearAllTilesStatus(MASK_SELECTED_RD5);
scanExistingFiles();
} else { } else {
mBInstallerView.setOnSelectListener(onSelectListener); mBInstallerView.setOnSelectListener(onSelectListener);
mBInstallerView.clearAllTilesStatus(MASK_SELECTED_RD5); mBInstallerView.clearAllTilesStatus(MASK_SELECTED_RD5);

View file

@ -266,7 +266,7 @@ public class DownloadWorker extends Worker {
} }
if (newappversion != -1 && newappversion > appversion) { if (newappversion != -1 && newappversion > appversion) {
if (DEBUG) Log.d(LOG_TAG, "app version old " + appversion + " new " + newappversion); if (DEBUG) Log.d(LOG_TAG, "app version old " + appversion + " new " + newappversion);
errorCode = "error new app"; errorCode = "Version new app";
return false; return false;
} }
if (changed && downloadAll == VALUE_SEGMENT_PARTS) { if (changed && downloadAll == VALUE_SEGMENT_PARTS) {
@ -375,36 +375,42 @@ public class DownloadWorker extends Worker {
connection.setConnectTimeout(5000); connection.setConnectTimeout(5000);
connection.setRequestMethod("HEAD"); connection.setRequestMethod("HEAD");
connection.setDoInput(false); connection.setDoInput(false);
connection.connect(); try {
connection.connect();
return connection.getResponseCode() == HttpURLConnection.HTTP_OK;
} finally {
connection.disconnect();
}
return connection.getResponseCode() == HttpURLConnection.HTTP_OK;
} }
private boolean downloadFile(URL downloadUrl, File outputFile, int fileSize, boolean limitDownloadSpeed, DownloadType type) throws IOException, InterruptedException { private boolean downloadFile(URL downloadUrl, File outputFile, int fileSize, boolean limitDownloadSpeed, DownloadType type) throws IOException, InterruptedException {
if (DEBUG) Log.d(LOG_TAG, "download " + outputFile.getAbsolutePath()); if (DEBUG) Log.d(LOG_TAG, "download " + outputFile.getAbsolutePath());
HttpURLConnection connection = (HttpURLConnection) downloadUrl.openConnection(); HttpURLConnection connection = (HttpURLConnection) downloadUrl.openConnection();
connection.setConnectTimeout(5000); connection.setConnectTimeout(5000);
connection.connect(); connection.setDefaultUseCaches(false);
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new IOException("HTTP Request failed: " + downloadUrl + " returned " + connection.getResponseCode());
}
int dataLength = connection.getContentLength();
// no need of download when size equal
// file size not the best coice but easy to handle, date is not available
switch (type) {
case LOOKUP:
if (fileSize == dataLength) return false;
break;
case PROFILE:
if (fileSize == dataLength) return false;
break;
default:
break;
}
InputStream input = null; InputStream input = null;
OutputStream output = null; OutputStream output = null;
try { try {
connection.connect();
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new IOException("HTTP Request failed: " + downloadUrl + " returned " + connection.getResponseCode());
}
int dataLength = connection.getContentLength();
// no need of download when size equal
// file size not the best coice but easy to handle, date is not available
switch (type) {
case LOOKUP:
if (fileSize == dataLength) return false;
break;
case PROFILE:
if (fileSize == dataLength) return false;
break;
default:
break;
}
input = connection.getInputStream(); input = connection.getInputStream();
output = new FileOutputStream(outputFile); output = new FileOutputStream(outputFile);