Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions app/src/org/commcare/google/services/analytics/CCPerfMonitoring.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.commcare.google.services.analytics

import com.google.firebase.perf.FirebasePerformance
import com.google.firebase.perf.metrics.Trace
import org.apache.commons.io.FilenameUtils
import org.commcare.android.logging.ReportingUtils
import org.javarosa.core.services.Logger

Expand All @@ -12,6 +13,7 @@ object CCPerfMonitoring {
const val TRACE_APP_SYNC_DURATION = "app_sync_duration"
const val TRACE_CASE_SEARCH_TIME = "case_search_time"
const val TRACE_FORM_LOADING_TIME = "form_loading_time"
const val TRACE_FILE_ENCRYPTION_TIME = "file_encryption_time"

// Attributes
const val ATTR_NUM_CASES_LOADED = "number_of_cases_loaded"
Expand All @@ -22,6 +24,8 @@ object CCPerfMonitoring {
const val ATTR_SYNC_TYPE = "sync_type"
const val ATTR_FORM_NAME = "form_name"
const val ATTR_FORM_XMLNS = "form_xmlns"
const val ATTR_FILE_SIZE_BYTES = "file_size_bytes"
const val ATTR_FILE_TYPE = "file_type"

fun startTracing(traceName: String): Trace? {
try {
Expand All @@ -46,4 +50,16 @@ object CCPerfMonitoring {
Logger.exception("Error stopping perf trace: ${trace.name}", exception)
}
}

fun stopFileEncryptionTracing(trace: Trace, fileSizeBytes: Long, fileName: String) {
try {
val attrs: MutableMap<String, String> = HashMap()
attrs[ATTR_FILE_SIZE_BYTES] = fileSizeBytes.toString()
attrs[ATTR_FILE_TYPE] = FilenameUtils.getExtension(fileName)
stopTracing(trace, attrs)
} catch (e: java.lang.Exception) {
Logger.exception("Failed to stop tracing: ${trace.name}", e)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import android.content.ContentValues;
import android.database.Cursor;

import com.google.firebase.perf.metrics.Trace;

import org.commcare.CommCareApplication;
import org.commcare.google.services.analytics.CCPerfMonitoring;
import org.commcare.interfaces.AppFilePathBuilder;
import org.commcare.models.encryption.EncryptionIO;
import org.commcare.modern.database.DatabaseHelper;
Expand Down Expand Up @@ -328,6 +331,8 @@ protected byte[] generateKeyAndAdd(ContentValues contentValues) {

private void writeStreamToFile(ByteArrayOutputStream bos, String filename,
byte[] key) throws IOException {
Trace trace = CCPerfMonitoring.INSTANCE.startTracing(CCPerfMonitoring.TRACE_FILE_ENCRYPTION_TIME);

DataOutputStream fileOutputStream = null;
try {
fileOutputStream = getOutputFileStream(filename, key);
Expand All @@ -340,6 +345,7 @@ private void writeStreamToFile(ByteArrayOutputStream bos, String filename,
e.printStackTrace();
}
}
CCPerfMonitoring.INSTANCE.stopFileEncryptionTracing(trace, bos.size(), filename);
}
}

Expand Down
12 changes: 10 additions & 2 deletions app/src/org/commcare/models/encryption/EncryptionIO.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.commcare.models.encryption;

import com.google.firebase.perf.metrics.Trace;

import org.commcare.google.services.analytics.CCPerfMonitoring;
import org.commcare.util.LogTypes;
import org.javarosa.core.io.StreamsUtil;
import org.javarosa.core.services.Logger;
Expand All @@ -10,6 +13,7 @@
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidKeyException;
Expand All @@ -28,13 +32,17 @@
*/
public class EncryptionIO {

public static void encryptFile(String sourceFilePath, String destPath, SecretKeySpec symetricKey) throws FileNotFoundException,
StreamsUtil.InputIOException, StreamsUtil.OutputIOException {
public static void encryptFile(String sourceFilePath, String destPath, SecretKeySpec symetricKey) throws IOException {
Trace trace = CCPerfMonitoring.INSTANCE.startTracing(CCPerfMonitoring.TRACE_FILE_ENCRYPTION_TIME);

OutputStream os;
FileInputStream is;
os = createFileOutputStream(destPath, symetricKey);
is = new FileInputStream(sourceFilePath);
int fileSize = is.available();
StreamsUtil.writeFromInputToOutputNew(is, os);

CCPerfMonitoring.INSTANCE.stopFileEncryptionTracing(trace, fileSize, sourceFilePath);
}

public static OutputStream createFileOutputStream(String filename,
Expand Down
8 changes: 8 additions & 0 deletions app/src/org/commcare/tasks/SaveToDiskTask.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package org.commcare.tasks;

import com.google.firebase.perf.metrics.Trace;

import org.commcare.CommCareApplication;
import org.commcare.activities.FormEntryActivity;
import org.commcare.activities.components.ImageCaptureProcessing;
import org.commcare.android.database.app.models.FormDefRecord;
import org.commcare.android.database.user.models.FormRecord;
import org.commcare.android.logging.ForceCloseLogger;
import org.commcare.google.services.analytics.CCPerfMonitoring;
import org.commcare.interfaces.FormSavedListener;
import org.commcare.logging.XPathErrorLogger;
import org.commcare.models.database.SqlStorage;
Expand Down Expand Up @@ -36,6 +39,8 @@

import javax.crypto.spec.SecretKeySpec;

import static org.commcare.utils.FileUtil.XML_EXTENSION;

/**
* @author Carl Hartung (carlhartung@gmail.com)
* @author Yaw Anokwa (yanokwa@gmail.com)
Expand Down Expand Up @@ -257,11 +262,14 @@ private void exportData(boolean markCompleted)
}

private void writeXmlToStream(ByteArrayPayload payload, OutputStream output) throws IOException {
Trace trace = CCPerfMonitoring.INSTANCE.startTracing(CCPerfMonitoring.TRACE_FILE_ENCRYPTION_TIME);

try {
InputStream is = payload.getPayloadStream();
StreamsUtil.writeFromInputToOutput(is, output);
} finally {
output.close();
CCPerfMonitoring.INSTANCE.stopFileEncryptionTracing(trace, payload.getLength(), XML_EXTENSION);
}
}

Expand Down
1 change: 1 addition & 0 deletions app/src/org/commcare/utils/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public class FileUtil {
private static final int WARNING_SIZE = 3000;

private static final String LOG_TOKEN = "cc-file-util";
public static final String XML_EXTENSION = "xml";

private static final String[] EXIF_TAGS = {
ExifInterface.TAG_GPS_LATITUDE,
Expand Down