This commit is contained in:
1 parent
19ee837986
commit
c2127c3f31
14 files changed
+3621
-5
No files matched your search
@@ -7,4 +7,9 @@
|
||||
<custom_options><option>-B</option><option>-CX-</option><option>-XX-</option><option>-k--no-undefined</option><option>-k--no-gc-sections</option></custom_options>
|
||||
<library_paths />
|
||||
</compiler_options>
|
||||
<android>
|
||||
<services>
|
||||
<service name="diagnostic_report_export" />
|
||||
</services>
|
||||
</android>
|
||||
</project>
|
||||
+1
@@ -0,0 +1 @@
|
||||
services.add(new ServiceDiagnosticReportExport(this));
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
package io.castleengine;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class ServiceDiagnosticReportExport extends ServiceAbstract
|
||||
{
|
||||
private static final int REQUEST_CREATE_DOCUMENT = 40127;
|
||||
private static String pendingJson;
|
||||
|
||||
public ServiceDiagnosticReportExport(MainActivity activity)
|
||||
{
|
||||
super(activity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName()
|
||||
{
|
||||
return "diagnostic_report_export";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean messageReceived(String[] parts)
|
||||
{
|
||||
if (parts.length != 2 || !parts[0].equals("diagnostic-report-export")) {
|
||||
return false;
|
||||
}
|
||||
if (pendingJson != null) {
|
||||
sendError("Export already pending");
|
||||
return true;
|
||||
}
|
||||
|
||||
pendingJson = parts[1];
|
||||
try {
|
||||
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
|
||||
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
||||
intent.setType("application/json");
|
||||
intent.putExtra(Intent.EXTRA_TITLE, "kam-mobile-diagnostics.json");
|
||||
getActivity().startActivityForResult(intent, REQUEST_CREATE_DOCUMENT);
|
||||
} catch (Exception error) {
|
||||
finishError(error);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent intent)
|
||||
{
|
||||
if (requestCode != REQUEST_CREATE_DOCUMENT) {
|
||||
return;
|
||||
}
|
||||
if (resultCode == Activity.RESULT_CANCELED) {
|
||||
pendingJson = null;
|
||||
messageSend(new String[] { "diagnostic-report-export-result", "cancel" });
|
||||
return;
|
||||
}
|
||||
if (resultCode != Activity.RESULT_OK || intent == null || intent.getData() == null || pendingJson == null) {
|
||||
finishError("Invalid export result");
|
||||
return;
|
||||
}
|
||||
|
||||
Uri uri = intent.getData();
|
||||
String json = pendingJson;
|
||||
try (OutputStream stream = getActivity().getContentResolver().openOutputStream(uri)) {
|
||||
if (stream == null) {
|
||||
throw new IllegalStateException("Document provider returned no output stream");
|
||||
}
|
||||
stream.write(json.getBytes(StandardCharsets.UTF_8));
|
||||
stream.flush();
|
||||
} catch (Exception error) {
|
||||
finishError(error);
|
||||
return;
|
||||
}
|
||||
pendingJson = null;
|
||||
messageSend(new String[] { "diagnostic-report-export-result", "success" });
|
||||
}
|
||||
|
||||
private void finishError(Exception error)
|
||||
{
|
||||
String message = error.getClass().getSimpleName();
|
||||
if (error.getMessage() != null) {
|
||||
message += ": " + error.getMessage();
|
||||
}
|
||||
finishError(message);
|
||||
}
|
||||
|
||||
private void finishError(String message)
|
||||
{
|
||||
pendingJson = null;
|
||||
sendError(message);
|
||||
}
|
||||
|
||||
private void sendError(String message)
|
||||
{
|
||||
messageSend(new String[] { "diagnostic-report-export-result", "error", message });
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user