diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index bcc1e93abd..74f78532fd 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -127,6 +127,16 @@ + + + + logs = LoggerUtils.getLogfiles(logDirectory, 2); - String zipName = LoggerUtils.constructName(); - File zip = LoggerUtils.zipLogs(zipName, logDirectory, logs); - Intent emailIntent = LoggerUtils.sendMail(zip, recipient, "Log Export"); - startActivity(Intent.createChooser(emailIntent , "Send email...")); + + File zipDir = this.getExternalFilesDir("exports"); + File zipFile = new File(zipDir, LoggerUtils.constructName()); + + log.debug("zipFile: {}", zipFile.getAbsolutePath()); + File zip = LoggerUtils.zipLogs(zipFile, logs); + + Uri attachementUri = FileProvider.getUriForFile(this, "info.nightscout.androidaps.fileprovider", zip); + Intent emailIntent = LoggerUtils.sendMail(attachementUri, recipient, "Log Export"); + log.debug("sending emailIntent"); + startActivity(emailIntent); return true; case R.id.nav_about: diff --git a/app/src/main/java/info/nightscout/utils/LoggerUtils.java b/app/src/main/java/info/nightscout/utils/LoggerUtils.java index ec5f927cb4..8564c3300c 100644 --- a/app/src/main/java/info/nightscout/utils/LoggerUtils.java +++ b/app/src/main/java/info/nightscout/utils/LoggerUtils.java @@ -3,6 +3,7 @@ package info.nightscout.utils; import android.content.Intent; import android.net.Uri; +import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.BufferedInputStream; @@ -26,6 +27,8 @@ import ch.qos.logback.classic.LoggerContext; */ public class LoggerUtils { + private static final Logger LOGGER = LoggerFactory.getLogger(LoggerUtils.class); + private static String SUFFIX = ".log.zip"; /** @@ -49,6 +52,7 @@ public class LoggerUtils { * @return */ public static List getLogfiles(String directory, int amount) { + LOGGER.debug("getting {} logs from directory {}", amount, directory); File logDir = new File(directory); File[] files = logDir.listFiles(new FilenameFilter() { @@ -69,33 +73,32 @@ public class LoggerUtils { List result = Arrays.asList(files); int toIndex = amount++; - if (toIndex > result.size()) { toIndex = result.size(); } + LOGGER.debug("returning sublist 0 to {}", toIndex); return result.subList(0, toIndex); } /** - * Zips the given files in a zipfile which is stored in the given directory using the givven + * Zips the given files in a zipfile which is stored in the given zipDir using the givven * name. * - * @param name - * @param directory + * @param zipFile * @param files * @return */ - public static File zipLogs(String name, String directory, List files) { - File zip = new File(directory, name); + public static File zipLogs(File zipFile, List files) { + LOGGER.debug("creating zip {}", zipFile.getAbsolutePath()); try { - zip(zip, files); + zip(zipFile, files); } catch (IOException e) { - System.out.print("REmomve this one sooner or later...."); + LOGGER.error("Cannot retrieve zip", e); } - return zip; + return zipFile; } /** @@ -150,20 +153,23 @@ public class LoggerUtils { * * startActivity(Intent.createChooser(emailIntent , "Send email...")); * - * @param file + * @param attachementUri * @param recipient * @param subject * @return */ - public static Intent sendMail(File file, String recipient, String subject) { + public static Intent sendMail(Uri attachementUri, String recipient, String subject) { + LOGGER.debug("sending email to {} with subject {}", recipient, subject); Intent emailIntent = new Intent(Intent.ACTION_SEND); - emailIntent .setType("vnd.android.cursor.dir/email"); + emailIntent.setType("text/plain"); emailIntent.putExtra(Intent.EXTRA_EMAIL , new String[]{recipient}); emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject); + emailIntent.putExtra(Intent.EXTRA_TEXT, ""); - Uri path = Uri.fromFile(file); - emailIntent .putExtra(Intent.EXTRA_STREAM, path); + LOGGER.debug("put path {}", attachementUri.toString()); + emailIntent.putExtra(Intent.EXTRA_STREAM, attachementUri); + emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); return emailIntent; } diff --git a/app/src/main/res/xml/filepaths.xml b/app/src/main/res/xml/filepaths.xml new file mode 100644 index 0000000000..7490f459d4 --- /dev/null +++ b/app/src/main/res/xml/filepaths.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/utils/LoggerUtilsTest.java b/app/src/test/java/info/nightscout/utils/LoggerUtilsTest.java index b999c804b7..171dee4373 100644 --- a/app/src/test/java/info/nightscout/utils/LoggerUtilsTest.java +++ b/app/src/test/java/info/nightscout/utils/LoggerUtilsTest.java @@ -31,7 +31,8 @@ public class LoggerUtilsTest { String name = "AndroidAPS.log.zip"; - File zipFile = LoggerUtils.zipLogs(name, "build", logs); + File zipFile = new File("build/" + name); + zipFile = LoggerUtils.zipLogs(zipFile, logs); assertTrue(zipFile.exists()); assertTrue(zipFile.isFile());