1
0
mirror of https://github.com/etesync/android synced 2024-11-15 12:28:57 +00:00

Debug info: send inline on Android <4.1 and when creating an attachment doesn't work

This commit is contained in:
Ricki Hirner 2016-10-22 02:05:14 +02:00
parent 08639b2e89
commit f2d5fe32c5

View File

@ -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));
}
}