Skip to content

Commit

Permalink
Use acra-http
Browse files Browse the repository at this point in the history
  • Loading branch information
woesss committed Aug 23, 2024
1 parent 3cc10ae commit 2b3eb27
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 157 deletions.
3 changes: 1 addition & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ dependencies {
//noinspection GradleDependency (next version incompatible with Android 4)
implementation("com.google.oboe:oboe:1.7.0")

implementation("ch.acra:acra-dialog:5.11.3")
implementation("com.android.volley:volley:1.2.1")
implementation("ch.acra:acra-http:5.11.3")
implementation("com.github.nikita36078:mobile-ffmpeg:v4.3.2-compact")
implementation("com.github.nikita36078:pngj:2.2.3")
implementation("com.github.woesss:filepicker:4.4.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.acra.config.CoreConfigurationBuilder;

import ru.playsoftware.j2meloader.crashes.AppCenterCollector;
import ru.playsoftware.j2meloader.crashes.AppCenterSender;
import ru.playsoftware.j2meloader.util.Constants;
import ru.playsoftware.j2meloader.util.FileUtils;

Expand All @@ -53,7 +54,8 @@ protected void attachBaseContext(Context base) {

ACRA.init(this, new CoreConfigurationBuilder()
.withParallel(false)
.withReportContent(AppCenterCollector.REPORT_FIELDS));
.withReportContent(AppCenterCollector.REPORT_FIELDS)
.withPluginConfigurations(AppCenterSender.buildHttpSenderConfiguration(this)));

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
if (!sp.contains(Constants.PREF_TOOLBAR)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,21 @@

import androidx.annotation.NonNull;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.HurlStack;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.acra.ReportField;
import org.acra.config.ConfigUtils;
import org.acra.config.CoreConfiguration;
import org.acra.config.HttpSenderConfiguration;
import org.acra.config.HttpSenderConfigurationBuilder;
import org.acra.data.CrashReportData;
import org.acra.http.DefaultHttpRequest;
import org.acra.security.TLS;
import org.acra.sender.ReportSender;
import org.acra.util.Installation;
import org.json.JSONObject;

import java.io.FileOutputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -48,47 +51,59 @@ public class AppCenterSender implements ReportSender {
private static final String TAG = AppCenterSender.class.getSimpleName();
private static final String BASE_URL = "https://in.appcenter.ms/logs?Api-Version=1.0.0";

private final CoreConfiguration coreConfiguration;
private final HttpSenderConfiguration httpConfig;

public AppCenterSender(CoreConfiguration coreConfiguration) {
this.coreConfiguration = coreConfiguration;
httpConfig = ConfigUtils.getPluginConfiguration(coreConfiguration, HttpSenderConfiguration.class);
}

@NonNull
public static HttpSenderConfiguration buildHttpSenderConfiguration(Context context) {
HttpSenderConfigurationBuilder builder = new HttpSenderConfigurationBuilder();
// Force TLSv1.2 for Android 4.1-4.4
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN
&& Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
builder.withCertificatePath("asset://appcenter.cer");
builder.withTlsProtocols(TLS.V1_2);
}
Map<String, String> httpHeaders = new HashMap<>();
httpHeaders.put("App-Secret", context.getString(R.string.app_center));
httpHeaders.put("Install-ID", Installation.id(context));
return builder.withUri(BASE_URL)
.withHttpHeaders(httpHeaders)
.withCompress(true)
.withEnabled(false)
.build();
}

@Override
public void send(@NonNull Context context, @NonNull final CrashReportData report) {
public void send(@NonNull Context context, @NonNull CrashReportData report) {
final String log = (String) report.get(AppCenterCollector.APPCENTER_LOG);
if (log == null || log.isEmpty()) {
if (log == null || log.isBlank()) {
return;
}
String key = context.getString(R.string.app_center);
if (key.isBlank()) {
saveToFile(context, report);
return;
}

// Force TLSv1.2 for Android 4.1-4.4
boolean forceTls12 = Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN
&& Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP;

HurlStack hurlStack = new HurlStack(null, forceTls12 ? new TLSSocketFactory(context) : null);
RequestQueue queue = Volley.newRequestQueue(context, hurlStack);
StringRequest postRequest = new StringRequest(Request.Method.POST, BASE_URL,
response -> Log.d(TAG, "send success: " + response),
error -> {
Log.e(TAG, "Response error", error);
new Thread(() -> saveToFile(context, report)).start();
}
) {
@Override
public Map<String, String> getHeaders() {
Map<String, String> params = new HashMap<>();
params.put("Content-Type", "application/json");
params.put("App-Secret", key);
params.put("Install-ID", report.getString(ReportField.INSTALLATION_ID));
return params;
}

@Override
public byte[] getBody() {
return log.getBytes();
}
};
postRequest.setShouldCache(false);
queue.add(postRequest);
try {
new DefaultHttpRequest(coreConfiguration,
context,
httpConfig.getHttpMethod(),
coreConfiguration.getReportFormat().getMatchingHttpContentType(),
null,
null,
httpConfig.getConnectionTimeout(),
httpConfig.getSocketTimeout(),
httpConfig.getHttpHeaders()
).send(new URL(httpConfig.getUri()), log);
} catch (Exception e) {
Log.e(TAG, "send: " + e, e);
saveToFile(context, report);
}
}

private static void saveToFile(@NonNull Context context, @NonNull CrashReportData report) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ public class AppCenterSenderFactory implements ReportSenderFactory {
@NonNull
@Override
public ReportSender create(@NonNull Context context, @NonNull CoreConfiguration config) {
return new AppCenterSender();
}

@Override
public boolean enabled(@NonNull CoreConfiguration config) {
return true;
return new AppCenterSender(config);
}
}

This file was deleted.

0 comments on commit 2b3eb27

Please sign in to comment.