Fix log file not being attached (#2316)

* Fix log file not being attached

* Add filter for email clients
This commit is contained in:
Vivek Maskara 2019-01-24 13:51:07 +05:30 committed by Josephine Lim
parent 53e186df9a
commit e389a95592
6 changed files with 59 additions and 17 deletions

View file

@ -134,7 +134,7 @@ public class CommonsApplication extends Application {
private void initTimber() {
boolean isBeta = ConfigUtils.isBetaFlavour();
String logFileName = isBeta ? "CommonsBetaAppLogs" : "CommonsAppLogs";
String logDirectory = LogUtils.getLogDirectory(isBeta);
String logDirectory = LogUtils.getLogDirectory();
FileLoggingTree tree = new FileLoggingTree(
Log.DEBUG,
logFileName,

View file

@ -1,8 +1,12 @@
package fr.free.nrw.commons.logging;
import android.content.Context;
import android.os.Environment;
import java.io.File;
import fr.free.nrw.commons.upload.FileUtils;
import fr.free.nrw.commons.utils.ConfigUtils;
/**
* Returns the log directory
*/
@ -12,14 +16,35 @@ public final class LogUtils {
/**
* Returns the directory for saving logs on the device
* @param isBeta
*
* @return
*/
public static String getLogDirectory(boolean isBeta) {
if (isBeta) {
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/logs/beta";
public static String getLogDirectory() {
String dirPath;
if (ConfigUtils.isBetaFlavour()) {
dirPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/logs/beta";
} else {
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/logs/prod";
dirPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/logs/prod";
}
FileUtils.recursivelyCreateDirs(dirPath);
return dirPath;
}
/**
* Returns the directory for saving logs on the device
*
* @return
*/
public static String getLogZipDirectory() {
String dirPath;
if (ConfigUtils.isBetaFlavour()) {
dirPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/logs/beta/zip";
} else {
dirPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/logs/prod/zip";
}
FileUtils.recursivelyCreateDirs(dirPath);
return dirPath;
}
}

View file

@ -5,10 +5,12 @@ import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.FileProvider;
import org.acra.collector.CrashReportData;
import org.acra.sender.ReportSender;
import org.apache.commons.codec.Charsets;
import org.apache.http.protocol.HTTP;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
@ -19,6 +21,7 @@ import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.auth.SessionManager;
import fr.free.nrw.commons.utils.ConfigUtils;
import timber.log.Timber;
@ -81,19 +84,20 @@ public abstract class LogsSender implements ReportSender {
String subject = emailSubject;
String body = emailBody;
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.fromParts("mailto", mailTo, null));
emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("message/rfc822");
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{mailTo});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, body);
emailIntent.putExtra(Intent.EXTRA_STREAM, logFileUri);
context.startActivity(emailIntent);
emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(Intent.createChooser(emailIntent, context.getString(R.string.share_logs_using)));
}
/**
* Returns the URI for the zipped log file
*
* @param context
* @param report
* @return
*/
@ -106,9 +110,11 @@ public abstract class LogsSender implements ReportSender {
attachUserInfo(builder);
attachExtraInfo(builder);
byte[] metaData = builder.toString().getBytes(Charsets.UTF_8);
File zipFile = new File(context.getExternalFilesDir(null), logFileName);
File zipFile = new File(LogUtils.getLogZipDirectory(), logFileName);
writeLogToZipFile(metaData, zipFile);
return Uri.fromFile(zipFile);
return FileProvider
.getUriForFile(context,
context.getApplicationContext().getPackageName() + ".provider", zipFile);
} catch (IOException e) {
Timber.w(e, "Error in generating log file");
}
@ -159,8 +165,7 @@ public abstract class LogsSender implements ReportSender {
FileOutputStream fos = new FileOutputStream(zipFile);
BufferedOutputStream bos = new BufferedOutputStream(fos);
ZipOutputStream zos = new ZipOutputStream(bos);
boolean isBeta = ConfigUtils.isBetaFlavour();
File logDir = new File(LogUtils.getLogDirectory(isBeta));
File logDir = new File(LogUtils.getLogDirectory());
if (!logDir.exists() || logDir.listFiles().length == 0) {
return;
@ -168,6 +173,9 @@ public abstract class LogsSender implements ReportSender {
byte[] buffer = new byte[1024];
for (File file : logDir.listFiles()) {
if (file.isDirectory()) {
continue;
}
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
zos.putNextEntry(new ZipEntry(file.getName()));

View file

@ -152,4 +152,12 @@ public class FileUtils {
public static FileInputStream getFileInputStream(String filePath) throws FileNotFoundException {
return new FileInputStream(filePath);
}
public static boolean recursivelyCreateDirs(String dirPath) {
File fileDir = new File(dirPath);
if (!fileDir.exists()) {
return fileDir.mkdirs();
}
return true;
}
}

View file

@ -460,4 +460,5 @@ Upload your first media by touching the camera or gallery icon above.</string>
<string name="no_image">No images used</string>
<string name="no_image_reverted">No images reverted</string>
<string name="no_image_uploaded">No images uploaded</string>
<string name="share_logs_using">Share logs using</string>
</resources>

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<paths>
<cache-path name="images" path="images/" />
<external-path name="logs" path="CommonsApp/"/>
<external-path name="Download" path="./"/>
</paths>