From f2d5fe32c55338831cc7154e37e3483668093650 Mon Sep 17 00:00:00 2001 From: Ricki Hirner Date: Sat, 22 Oct 2016 02:05:14 +0200 Subject: [PATCH] Debug info: send inline on Android <4.1 and when creating an attachment doesn't work --- .../davdroid/ui/DebugInfoActivity.java | 45 ++++++++++++------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/at/bitfire/davdroid/ui/DebugInfoActivity.java b/app/src/main/java/at/bitfire/davdroid/ui/DebugInfoActivity.java index 39bc4d9b..aab4a0c0 100644 --- a/app/src/main/java/at/bitfire/davdroid/ui/DebugInfoActivity.java +++ b/app/src/main/java/at/bitfire/davdroid/ui/DebugInfoActivity.java @@ -28,7 +28,6 @@ import android.text.TextUtils; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; -import android.widget.Toast; import org.apache.commons.lang3.exception.ExceptionUtils; import org.apache.commons.lang3.text.WordUtils; @@ -86,23 +85,39 @@ public class DebugInfoActivity extends AppCompatActivity implements LoaderManage sendIntent.setType("text/plain"); sendIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.app_name) + " " + BuildConfig.VERSION_NAME + " debug info"); - try { - File debugInfoDir = new File(getCacheDir(), "debug-info"); - debugInfoDir.mkdir(); + // since Android 4.1, FileProvider permissions are handled in a useful way (using ClipData) + boolean asAttachment = Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN; - reportFile = new File(debugInfoDir, "debug.txt"); - App.log.fine("Writing debug info to " + reportFile.getAbsolutePath()); - FileWriter writer = new FileWriter(reportFile); - writer.write(report); - writer.close(); + if (asAttachment) + try { + File debugInfoDir = new File(getCacheDir(), "debug-info"); + debugInfoDir.mkdir(); - sendIntent.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(this, getString(R.string.authority_log_provider), reportFile)); + reportFile = new File(debugInfoDir, "debug.txt"); + App.log.fine("Writing debug info to " + reportFile.getAbsolutePath()); + FileWriter writer = new FileWriter(reportFile); + writer.write(report); + writer.close(); - startActivity(Intent.createChooser(sendIntent, null)); - } catch (IOException e) { - App.log.log(Level.SEVERE, "Couldn't write debug info file", e); - Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_LONG).show(); - } + sendIntent.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(this, getString(R.string.authority_log_provider), reportFile)); + sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + + } catch(IOException e) { + // creating an attachment failed, so send it inline + asAttachment = false; + + StringBuilder builder = new StringBuilder(); + builder .append("Couldn't write debug info file:\n") + .append(ExceptionUtils.getStackTrace(e)) + .append("\n\n") + .append(report); + report = builder.toString(); + } + + if (!asAttachment) + sendIntent.putExtra(Intent.EXTRA_TEXT, report); + + startActivity(Intent.createChooser(sendIntent, null)); } }