[app] Fix crash when repo request failed (#173)

This commit is contained in:
tehcneko 2021-02-23 10:28:40 +08:00 committed by GitHub
parent 6314bae5db
commit 4e934858f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 43 additions and 32 deletions

View File

@ -20,12 +20,12 @@
package io.github.lsposed.manager.repo; package io.github.lsposed.manager.repo;
import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -79,6 +79,7 @@ public class RepoLoader {
.build()).enqueue(new Callback() { .build()).enqueue(new Callback() {
@Override @Override
public void onFailure(@NonNull Call call, @NonNull IOException e) { public void onFailure(@NonNull Call call, @NonNull IOException e) {
Log.e(App.TAG, Log.getStackTraceString(e));
App.getInstance().runOnUiThread(() -> Toast.makeText(App.getInstance(), e.getMessage(), Toast.LENGTH_LONG).show()); App.getInstance().runOnUiThread(() -> Toast.makeText(App.getInstance(), e.getMessage(), Toast.LENGTH_LONG).show());
synchronized (this) { synchronized (this) {
isLoading = false; isLoading = false;
@ -86,30 +87,32 @@ public class RepoLoader {
} }
@Override @Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException { public void onResponse(@NonNull Call call, @NonNull Response response) {
try { if (response.isSuccessful()) {
ResponseBody body = response.body(); ResponseBody body = response.body();
if (body != null) { if (body != null) {
String bodyString = body.string(); try {
Gson gson = new Gson(); String bodyString = body.string();
Map<String, OnlineModule> modules = new HashMap<>(); Gson gson = new Gson();
OnlineModule[] repoModules = gson.fromJson(bodyString, OnlineModule[].class); Map<String, OnlineModule> modules = new HashMap<>();
Arrays.stream(repoModules).forEach(onlineModule -> modules.put(onlineModule.getName(), onlineModule)); OnlineModule[] repoModules = gson.fromJson(bodyString, OnlineModule[].class);
onlineModules = modules; Arrays.stream(repoModules).forEach(onlineModule -> modules.put(onlineModule.getName(), onlineModule));
Files.write(repoFile, bodyString.getBytes(StandardCharsets.UTF_8)); onlineModules = modules;
Files.write(repoFile, bodyString.getBytes(StandardCharsets.UTF_8));
for (Listener listener : listeners) {
listener.repoLoaded();
}
synchronized (this) {
repoLoaded = true;
}
} catch (Throwable e) {
Log.e(App.TAG, Log.getStackTraceString(e));
App.getInstance().runOnUiThread(() -> Toast.makeText(App.getInstance(), e.getMessage(), Toast.LENGTH_LONG).show());
}
} }
for (Listener listener : listeners) { }
listener.repoLoaded(); synchronized (this) {
} isLoading = false;
synchronized (this) {
isLoading = false;
repoLoaded = true;
}
} catch (Throwable e) {
if (e instanceof IOException)
throw e;
else
throw new IOException(e.getMessage(), e.getCause());
} }
} }
}); });
@ -121,20 +124,28 @@ public class RepoLoader {
.build()).enqueue(new Callback() { .build()).enqueue(new Callback() {
@Override @Override
public void onFailure(@NonNull Call call, @NonNull IOException e) { public void onFailure(@NonNull Call call, @NonNull IOException e) {
Log.e(App.TAG, Log.getStackTraceString(e));
App.getInstance().runOnUiThread(() -> Toast.makeText(App.getInstance(), e.getMessage(), Toast.LENGTH_LONG).show()); App.getInstance().runOnUiThread(() -> Toast.makeText(App.getInstance(), e.getMessage(), Toast.LENGTH_LONG).show());
} }
@Override @Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException { public void onResponse(@NonNull Call call, @NonNull Response response) {
ResponseBody body = response.body(); if (response.isSuccessful()) {
if (body != null) { ResponseBody body = response.body();
String bodyString = body.string(); if (body != null) {
Gson gson = new Gson(); try {
OnlineModule module = gson.fromJson(bodyString, OnlineModule.class); String bodyString = body.string();
module.releasesLoaded = true; Gson gson = new Gson();
onlineModules.replace(packageName, module); OnlineModule module = gson.fromJson(bodyString, OnlineModule.class);
for (Listener listener : listeners) { module.releasesLoaded = true;
listener.moduleReleasesLoaded(module); onlineModules.replace(packageName, module);
for (Listener listener : listeners) {
listener.moduleReleasesLoaded(module);
}
} catch (Throwable e) {
Log.e(App.TAG, Log.getStackTraceString(e));
App.getInstance().runOnUiThread(() -> Toast.makeText(App.getInstance(), e.getMessage(), Toast.LENGTH_LONG).show());
}
} }
} }
} }