Skip to content

Commit

Permalink
actions: refactor download model to kotlin (fixes #2741) (#2745)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
Okuro3499 and dogi authored Nov 29, 2023
1 parent 4153434 commit 9b74062
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 147 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
applicationId "org.ole.planet.myplanet"
minSdkVersion 21
targetSdkVersion 34
versionCode 1162
versionName "0.11.62"
versionCode 1163
versionName "0.11.63"
ndkVersion '21.3.6528147'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ public void onReceive(Context context, Intent intent) {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(MESSAGE_PROGRESS) && prgDialog != null) {
Download download = intent.getParcelableExtra("download");
if (!download.isFailed()) {
if (!download.failed) {
setProgress(download);
} else {
DialogUtils.showError(prgDialog, download.getMessage());
DialogUtils.showError(prgDialog, download.message);
}
}
}
Expand Down Expand Up @@ -171,11 +171,11 @@ public void notAvailable() {
}

public void setProgress(Download download) {
prgDialog.setProgress(download.getProgress());
if (!TextUtils.isEmpty(download.getFileName())) {
prgDialog.setTitle(download.getFileName());
prgDialog.setProgress(download.progress);
if (!TextUtils.isEmpty(download.fileName)) {
prgDialog.setTitle(download.fileName);
}
if (download.isCompleteAll()) {
if (download.completeAll) {
DialogUtils.showError(prgDialog, getString(R.string.all_files_downloaded_successfully));
onDownloadComplete();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(MESSAGE_PROGRESS)) {
Download download = intent.getParcelableExtra("download");
if (onProgressChange != null) {
onProgressChange.onProgressChange(String.format("Downloading file %d/%d KB\n%d%% Completed.", download.getCurrentFileSize(), download.getTotalFileSize(), download.getProgress()));
onProgressChange.onProgressChange(String.format("Downloading file %d/%d KB\n%d%% Completed.", download.currentFileSize, download.totalFileSize, download.progress));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ private void downloadFiled(String message) {
notificationManager.notify(0, notificationBuilder.build());
Download d = new Download();
completeAll = false;
d.setFailed(true);
d.setMessage(message);
d.failed = true;
d.message = message;
sendIntent(d);
stopSelf();
}
Expand All @@ -119,11 +119,11 @@ private void downloadFile(ResponseBody body) throws IOException {
int progress = (int) ((total * 100) / fileSize);
long currentTime = System.currentTimeMillis() - startTime;
Download download = new Download();
download.setFileName(FileUtils.getFileNameFromUrl(url));
download.setTotalFileSize(totalFileSize);
download.fileName = FileUtils.getFileNameFromUrl(url);
download.totalFileSize = totalFileSize;
if (currentTime > 1000 * timeCount) {
download.setCurrentFileSize((int) current);
download.setProgress(progress);
download.currentFileSize= (int) current;
download.progress = progress;
sendNotification(download);
timeCount++;
}
Expand Down Expand Up @@ -151,10 +151,10 @@ private void closeStreams(OutputStream output, InputStream bis) throws IOExcepti
}

private void sendNotification(Download download) {
download.setFileName("Downloading : " + FileUtils.getFileNameFromUrl(url));
download.fileName = "Downloading : " + FileUtils.getFileNameFromUrl(url);
sendIntent(download);
notificationBuilder.setProgress(100, download.getProgress(), false);
notificationBuilder.setContentText("Downloading file " + download.getCurrentFileSize() + "/" + totalFileSize + " KB");
notificationBuilder.setProgress(100, download.progress, false);
notificationBuilder.setContentText("Downloading file " + download.currentFileSize + "/" + totalFileSize + " KB");
notificationManager.notify(0, notificationBuilder.build());
}

Expand All @@ -167,12 +167,12 @@ private void sendIntent(Download download) {
private void onDownloadComplete() {
if (outputFile.length() > 0) changeOfflineStatus();
Download download = new Download();
download.setFileName(FileUtils.getFileNameFromUrl(url));
download.setFileUrl(url);
download.setProgress(100);
download.fileName = FileUtils.getFileNameFromUrl(url);
download.fileUrl = url;
download.progress = 100;
if (currentIndex == urls.size() - 1) {
completeAll = true;
download.setCompleteAll(true);
download.completeAll = true;
}
sendIntent(download);
notificationManager.cancel(0);
Expand Down
118 changes: 0 additions & 118 deletions app/src/main/java/org/ole/planet/myplanet/model/Download.java

This file was deleted.

59 changes: 59 additions & 0 deletions app/src/main/java/org/ole/planet/myplanet/model/Download.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.ole.planet.myplanet.model

import android.os.Parcel
import android.os.Parcelable

class Download() : Parcelable {
@JvmField
var fileName: String? = null
@JvmField
var progress: Int = 0
@JvmField
var currentFileSize: Int = 0
@JvmField
var totalFileSize: Int = 0
@JvmField
var completeAll: Boolean = false
@JvmField
var failed: Boolean = false
@JvmField
var message: String? = null
@JvmField
var fileUrl: String? = null

constructor(parcel: Parcel) : this() {
fileName = parcel.readString()
progress = parcel.readInt()
currentFileSize = parcel.readInt()
totalFileSize = parcel.readInt()
completeAll = parcel.readByte() != 0.toByte()
failed = parcel.readByte() != 0.toByte()
message = parcel.readString()
fileUrl = parcel.readString()
}

override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeString(fileName)
parcel.writeInt(progress)
parcel.writeInt(currentFileSize)
parcel.writeInt(totalFileSize)
parcel.writeByte(if (completeAll) 1 else 0)
parcel.writeByte(if (failed) 1 else 0)
parcel.writeString(message)
parcel.writeString(fileUrl)
}

override fun describeContents(): Int {
return 0
}

companion object CREATOR : Parcelable.Creator<Download> {
override fun createFromParcel(parcel: Parcel): Download {
return Download(parcel)
}

override fun newArray(size: Int): Array<Download?> {
return arrayOfNulls(size)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ private boolean isMyServiceRunning(Class<?> serviceClass) {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(MESSAGE_PROGRESS)) {
Download download = intent.getParcelableExtra("download");
if (!download.isFailed() && download.isCompleteAll()) {
FileUtils.installApk(context, download.getFileUrl());
if (!download.failed && download.completeAll) {
FileUtils.installApk(context, download.fileUrl);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ public boolean validateEditText(EditText textField, TextInputLayout textLayout,
}

public void checkDownloadResult(Download download, ProgressDialog progressDialog) {
if (!download.isFailed()) {
progressDialog.setMessage(getString(R.string.downloading) + download.getProgress() + "% " + getString(R.string.complete));
if (download.isCompleteAll()) {
if (!download.failed) {
progressDialog.setMessage(getString(R.string.downloading) + download.progress + "% " + getString(R.string.complete));
if (download.completeAll) {
progressDialog.dismiss();
FileUtils.installApk(this, download.getFileUrl());
FileUtils.installApk(this, download.fileUrl);
}
} else {
progressDialog.dismiss();
DialogUtils.showError(progressDialog, download.getMessage());
DialogUtils.showError(progressDialog, download.message);
}
}

Expand Down

0 comments on commit 9b74062

Please sign in to comment.