Skip to content

Commit

Permalink
fix: android direct download
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed Aug 21, 2023
1 parent 4fea5cb commit 1784c75
Showing 1 changed file with 103 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -80,21 +79,34 @@ public class CapacitorUpdaterPlugin

private volatile Thread appReadyCheck;

private static final CountDownLatch semaphoreReady = new CountDownLatch(1);
private static final CountDownLatch semaphoreReady = new CountDownLatch(0);

public void startNewThread(final Runnable function) {
new Thread(() -> {
try {
function.run();
} catch (Exception e) {
e.printStackTrace();
}
})
.start();
}

@Override
public void load() {
super.load();
new Thread(() -> {
startNewThread(() -> {
Log.i(CapacitorUpdater.TAG, "semaphoreReady load");
try {
Log.i(CapacitorUpdater.TAG, "semaphoreReady load");
CapacitorUpdaterPlugin.this.semaphoreReady.await(0, TimeUnit.SECONDS);
Log.i(CapacitorUpdater.TAG, "semaphoreReady load done");
CapacitorUpdaterPlugin.this.semaphoreReady.await(
CapacitorUpdaterPlugin.this.appReadyTimeout,
TimeUnit.SECONDS
);
} catch (InterruptedException e) {
e.printStackTrace();
}
})
.start();
Log.i(CapacitorUpdater.TAG, "semaphoreReady load done");
});
this.prefs =
this.getContext()
.getSharedPreferences(
Expand Down Expand Up @@ -194,20 +206,19 @@ private void directUpdateFinish(final BundleInfo latest) {
ret.put("status", "update installed");
CapacitorUpdaterPlugin.this.implementation.set(latest);
CapacitorUpdaterPlugin.this._reload();
new Thread(() -> {
startNewThread(() -> {
Log.i(CapacitorUpdater.TAG, "semaphoreReady directUpdateFinish");
try {
Log.i(CapacitorUpdater.TAG, "semaphoreReady directUpdateFinish");
CapacitorUpdaterPlugin.this.semaphoreReady.await(
CapacitorUpdaterPlugin.this.appReadyTimeout,
TimeUnit.SECONDS
);
Log.i(CapacitorUpdater.TAG, "semaphoreReady directUpdateFinish done");
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.i(CapacitorUpdater.TAG, "semaphoreReady directUpdateFinish done");
CapacitorUpdaterPlugin.this.notifyListeners("appReady", ret);
})
.start();
});
}

private void cleanupObsoleteVersions() {
Expand Down Expand Up @@ -349,24 +360,18 @@ public void setChannel(final PluginCall call) {
}
try {
Log.i(CapacitorUpdater.TAG, "setChannel " + channel);
new Thread(
new Runnable() {
@Override
public void run() {
CapacitorUpdaterPlugin.this.implementation.setChannel(
channel,
res -> {
if (res.has("error")) {
call.reject(res.getString("error"));
} else {
call.resolve(res);
}
}
);
}
}
)
.start();
startNewThread(() -> {
CapacitorUpdaterPlugin.this.implementation.setChannel(
channel,
res -> {
if (res.has("error")) {
call.reject(res.getString("error"));
} else {
call.resolve(res);
}
}
);
});
} catch (final Exception e) {
Log.e(CapacitorUpdater.TAG, "Failed to setChannel: " + channel, e);
call.reject("Failed to setChannel: " + channel, e);
Expand All @@ -377,21 +382,15 @@ public void run() {
public void getChannel(final PluginCall call) {
try {
Log.i(CapacitorUpdater.TAG, "getChannel");
new Thread(
new Runnable() {
@Override
public void run() {
CapacitorUpdaterPlugin.this.implementation.getChannel(res -> {
if (res.has("error")) {
call.reject(res.getString("error"));
} else {
call.resolve(res);
}
});
}
}
)
.start();
startNewThread(() -> {
CapacitorUpdaterPlugin.this.implementation.getChannel(res -> {
if (res.has("error")) {
call.reject(res.getString("error"));
} else {
call.resolve(res);
}
});
});
} catch (final Exception e) {
Log.e(CapacitorUpdater.TAG, "Failed to getChannel", e);
call.reject("Failed to getChannel", e);
Expand All @@ -416,40 +415,31 @@ public void download(final PluginCall call) {
}
try {
Log.i(CapacitorUpdater.TAG, "Downloading " + url);
new Thread(
new Runnable() {
@Override
public void run() {
try {
final BundleInfo downloaded =
CapacitorUpdaterPlugin.this.implementation.download(
url,
version,
sessionKey,
checksum
);
startNewThread(() -> {
try {
final BundleInfo downloaded =
CapacitorUpdaterPlugin.this.implementation.download(
url,
version,
sessionKey,
checksum
);

call.resolve(downloaded.toJSON());
} catch (final IOException e) {
Log.e(CapacitorUpdater.TAG, "Failed to download from: " + url, e);
call.reject("Failed to download from: " + url, e);
final JSObject ret = new JSObject();
ret.put("version", version);
CapacitorUpdaterPlugin.this.notifyListeners(
"downloadFailed",
ret
);
final BundleInfo current =
CapacitorUpdaterPlugin.this.implementation.getCurrentBundle();
CapacitorUpdaterPlugin.this.implementation.sendStats(
"download_fail",
current.getVersionName()
);
}
}
call.resolve(downloaded.toJSON());
} catch (final IOException e) {
Log.e(CapacitorUpdater.TAG, "Failed to download from: " + url, e);
call.reject("Failed to download from: " + url, e);
final JSObject ret = new JSObject();
ret.put("version", version);
CapacitorUpdaterPlugin.this.notifyListeners("downloadFailed", ret);
final BundleInfo current =
CapacitorUpdaterPlugin.this.implementation.getCurrentBundle();
CapacitorUpdaterPlugin.this.implementation.sendStats(
"download_fail",
current.getVersionName()
);
}
)
.start();
});
} catch (final Exception e) {
Log.e(CapacitorUpdater.TAG, "Failed to download from: " + url, e);
call.reject("Failed to download from: " + url, e);
Expand All @@ -467,16 +457,18 @@ public void run() {

private boolean _reload() {
final String path = this.implementation.getCurrentBundlePath();
new Thread(() -> {
startNewThread(() -> {
Log.i(CapacitorUpdater.TAG, "semaphoreReady _reload");
try {
Log.i(CapacitorUpdater.TAG, "semaphoreReady _reload");
CapacitorUpdaterPlugin.this.semaphoreReady.await(0, TimeUnit.SECONDS);
Log.i(CapacitorUpdater.TAG, "semaphoreReady _reload done");
CapacitorUpdaterPlugin.this.semaphoreReady.await(
CapacitorUpdaterPlugin.this.appReadyTimeout,
TimeUnit.SECONDS
);
} catch (InterruptedException e) {
e.printStackTrace();
}
})
.start();
Log.i(CapacitorUpdater.TAG, "semaphoreReady _reload done");
});
Log.i(CapacitorUpdater.TAG, "Reloading: " + path);
if (this.implementation.isUsingBuiltin()) {
this.bridge.setServerAssetPath(path);
Expand Down Expand Up @@ -596,46 +588,35 @@ public void list(final PluginCall call) {

@PluginMethod
public void getLatest(final PluginCall call) {
try {
new Thread(
new Runnable() {
@Override
public void run() {
CapacitorUpdaterPlugin.this.implementation.getLatest(
CapacitorUpdaterPlugin.this.updateUrl,
res -> {
if (res.has("error")) {
call.reject(res.getString("error"));
return;
} else if (res.has("message")) {
call.reject(res.getString("message"));
return;
} else {
call.resolve(res);
}
final JSObject ret = new JSObject();
Iterator<String> keys = res.keys();
while (keys.hasNext()) {
String key = keys.next();
if (res.has(key)) {
try {
ret.put(key, res.get(key));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
call.resolve(ret);
startNewThread(() -> {
CapacitorUpdaterPlugin.this.implementation.getLatest(
CapacitorUpdaterPlugin.this.updateUrl,
res -> {
if (res.has("error")) {
call.reject(res.getString("error"));
return;
} else if (res.has("message")) {
call.reject(res.getString("message"));
return;
} else {
call.resolve(res);
}
final JSObject ret = new JSObject();
Iterator<String> keys = res.keys();
while (keys.hasNext()) {
String key = keys.next();
if (res.has(key)) {
try {
ret.put(key, res.get(key));
} catch (JSONException e) {
e.printStackTrace();
}
);
}
}
call.resolve(ret);
}
}
)
.start();
} catch (final Exception e) {
Log.e(CapacitorUpdater.TAG, "Failed to getLatest", e);
call.reject("Failed to getLatest", e);
}
);
});
}

private boolean _reset(final Boolean toLastSuccessful) {
Expand Down

0 comments on commit 1784c75

Please sign in to comment.