First working version

This commit is contained in:
Markus M. May 2018-07-24 22:34:10 +02:00
parent 2090b3c7e1
commit 60721c8395
5 changed files with 51 additions and 19 deletions

View file

@ -127,6 +127,16 @@
</intent-filter>
</receiver>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="info.nightscout.androidaps.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
<!-- Service processing incomming data -->
<service
android:name=".Services.DataService"

View file

@ -4,13 +4,16 @@ import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.PersistableBundle;
import android.os.PowerManager;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.FileProvider;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
@ -39,6 +42,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.util.List;
import info.nightscout.androidaps.data.Profile;
@ -401,10 +405,17 @@ public class MainActivity extends AppCompatActivity {
String logDirectory = LoggerUtils.getLogDirectory();
List<File> 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:

View file

@ -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<File> 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<File> 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<File> files) {
File zip = new File(directory, name);
public static File zipLogs(File zipFile, List<File> 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;
}

View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="exports" path="exports/" />
</paths>

View file

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