Skip to content

Commit

Permalink
fix: format android
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed Dec 10, 2022
1 parent de96286 commit 5a21088
Show file tree
Hide file tree
Showing 2 changed files with 271 additions and 257 deletions.
139 changes: 78 additions & 61 deletions android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdater.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ee.forgr.capacitor_updater;


import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
Expand All @@ -11,7 +10,6 @@
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;

import com.android.volley.BuildConfig;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
Expand Down Expand Up @@ -133,6 +131,7 @@ private int calcTotalPercent(
void notifyDownload(final String id, final int percent) {
return;
}

void notifyListeners(final String id, final JSObject res) {
return;
}
Expand Down Expand Up @@ -237,8 +236,10 @@ private void flattenAssets(final File sourceFile, final String dest)
}

public void onResume() {
this.activity.registerReceiver(receiver, new IntentFilter(
DownloadService.NOTIFICATION));
this.activity.registerReceiver(
receiver,
new IntentFilter(DownloadService.NOTIFICATION)
);
}

public void onPause() {
Expand All @@ -261,21 +262,45 @@ public void onReceive(Context context, Intent intent) {
String version = bundle.getString(DownloadService.VERSION);
String sessionKey = bundle.getString(DownloadService.SESSIONKEY);
String checksum = bundle.getString(DownloadService.CHECKSUM);
Log.i(CapacitorUpdater.TAG, "res " + id + " " + dest + " " + version + " " + sessionKey + " " + checksum);
CapacitorUpdater.this.finishBackground(id, dest, version, sessionKey, checksum);
Log.i(
CapacitorUpdater.TAG,
"res " +
id +
" " +
dest +
" " +
version +
" " +
sessionKey +
" " +
checksum
);
CapacitorUpdater.this.finishBackground(
id,
dest,
version,
sessionKey,
checksum
);
} else {
Log.i(TAG, "Unknown action " + action);
}
}
}
};

public void finishBackground(String id, String dest, String version, String sessionKey, String checksumRes) {
public void finishBackground(
String id,
String dest,
String version,
String sessionKey,
String checksumRes
) {
try {
final File downloaded = new File(this.documentsDir, dest);
this.decryptFile(downloaded, sessionKey);
final String checksum;
checksum = this.getChecksum(downloaded);
checksum = this.getChecksum(downloaded);

this.notifyDownload(id, 71);
final File unzipped = this.unzip(id, downloaded, this.randomString(10));
Expand All @@ -286,67 +311,52 @@ public void finishBackground(String id, String dest, String version, String sess
this.notifyDownload(id, 100);
this.saveBundleInfo(id, null);
BundleInfo info = new BundleInfo(
id,
version,
BundleStatus.PENDING,
new Date(System.currentTimeMillis()),
checksum
id,
version,
BundleStatus.PENDING,
new Date(System.currentTimeMillis()),
checksum
);
this.saveBundleInfo(id, info);
if (
!checksumRes.equals("") &&
!checksumRes.equals(checksum)
) {
if (!checksumRes.equals("") && !checksumRes.equals(checksum)) {
Log.e(
CapacitorUpdater.TAG,
"Error checksum " +
info.getChecksum() +
" " +
checksum
);
this.sendStats(
"checksum_fail",
getCurrentBundle().getVersionName()
CapacitorUpdater.TAG,
"Error checksum " + info.getChecksum() + " " + checksum
);
final Boolean res =
this.delete(
info.getId()
);
this.sendStats("checksum_fail", getCurrentBundle().getVersionName());
final Boolean res = this.delete(info.getId());
if (res) {
Log.i(
CapacitorUpdater.TAG,
"Failed bundle deleted: " +
info.getVersionName()
CapacitorUpdater.TAG,
"Failed bundle deleted: " + info.getVersionName()
);
}
return;
}
final JSObject ret = new JSObject();
ret.put("bundle", info.toJSON());
CapacitorUpdater.this.notifyListeners(
"updateAvailable",
ret
);
this.setNextBundle(
info.getId()
);
CapacitorUpdater.this.notifyListeners("updateAvailable", ret);
this.setNextBundle(info.getId());
} catch (IOException e) {
e.printStackTrace();
}
}

private void downloadFileBackground(
final String id,
final String url,
final String version,
final String sessionKey,
final String checksum,
final String dest
final String id,
final String url,
final String version,
final String sessionKey,
final String checksum,
final String dest
) {
Intent intent = new Intent(this.activity, DownloadService.class);
intent.putExtra(DownloadService.URL, url);
intent.putExtra(DownloadService.FILEDEST, dest);
intent.putExtra(DownloadService.DOCDIR, this.documentsDir.getAbsolutePath());
intent.putExtra(
DownloadService.DOCDIR,
this.documentsDir.getAbsolutePath()
);
intent.putExtra(DownloadService.ID, id);
intent.putExtra(DownloadService.VERSION, version);
intent.putExtra(DownloadService.SESSIONKEY, sessionKey);
Expand Down Expand Up @@ -456,25 +466,32 @@ private void decryptFile(final File file, final String ivSessionKey)
}

public void downloadBackground(
final String url,
final String version,
final String sessionKey,
final String checksum
final String url,
final String version,
final String sessionKey,
final String checksum
) {
final String id = this.randomString(10);
this.saveBundleInfo(
id,
new BundleInfo(
id,
version,
BundleStatus.DOWNLOADING,
new Date(System.currentTimeMillis()),
""
)
);
id,
new BundleInfo(
id,
version,
BundleStatus.DOWNLOADING,
new Date(System.currentTimeMillis()),
""
)
);
this.notifyDownload(id, 0);
this.notifyDownload(id, 5);
this.downloadFileBackground(id, url, version, sessionKey, checksum, this.randomString(10));
this.downloadFileBackground(
id,
url,
version,
sessionKey,
checksum,
this.randomString(10)
);
}

public BundleInfo download(
Expand Down

0 comments on commit 5a21088

Please sign in to comment.