Skip to content

Commit

Permalink
Review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
gerhardol committed Jul 19, 2020
1 parent 46039f4 commit 72564e7
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 64 deletions.
15 changes: 5 additions & 10 deletions app/src/main/org/runnerup/export/DropboxSynchronizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,26 +225,20 @@ private String getDesc(SQLiteDatabase db, final long mID) {
}

// upload a single file
private Status uploadFile(SQLiteDatabase db, final long mID, Sport sport,
long startTime,
StringWriter writer, String fileExt)
private Status uploadFile(StringWriter writer, final long mID, String fileBase, String fileExt)
throws IOException, JSONException {

Status s;

String date = new SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault()).format(new Date());

// Upload to default directory /Apps/RunnerUp
String file = FileNameHelper.getDropBoxUploadFileName(mID, sport.TapiriikType(), startTime, fileExt);

HttpURLConnection conn = (HttpURLConnection) new URL(UPLOAD_URL).openConnection();
conn.setDoOutput(true);
conn.setRequestMethod(RequestMethod.POST.name());
conn.addRequestProperty("Content-Type", "application/octet-stream");
conn.setRequestProperty("Authorization", "Bearer " + access_token);
JSONObject parameters = new JSONObject();
try {
parameters.put("path", file);
parameters.put("path", fileBase + fileExt);
parameters.put("mode", "add");
parameters.put("autorename", true);
} catch (JSONException e) {
Expand Down Expand Up @@ -321,17 +315,18 @@ public Status upload(SQLiteDatabase db, final long mID) {
}
}

String fileBase = FileNameHelper.getExportFileNameWithModel(start_time, sport.TapiriikType());
if (mFormat.contains(FileFormats.TCX)) {
TCX tcx = new TCX(db, simplifier);
StringWriter writer = new StringWriter();
tcx.export(mID, writer);
s = uploadFile(db, mID, sport, start_time, writer, FileFormats.TCX.getValue());
s = uploadFile(writer, mID, fileBase, FileFormats.TCX.getValue());
}
if (s == Status.OK && mFormat.contains(FileFormats.GPX)) {
GPX gpx = new GPX(db, true, true, simplifier);
StringWriter writer = new StringWriter();
gpx.export(mID, writer);
s = uploadFile(db, mID, sport, start_time, writer, FileFormats.GPX.getValue());
s = uploadFile(writer, mID, fileBase, FileFormats.GPX.getValue());
}

} catch (Exception e) {
Expand Down
5 changes: 1 addition & 4 deletions app/src/main/org/runnerup/export/FileSynchronizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Locale;
import java.text.SimpleDateFormat;
import java.util.Date;


public class FileSynchronizer extends DefaultSynchronizer {
Expand Down Expand Up @@ -178,7 +175,7 @@ public Status upload(SQLiteDatabase db, final long mID) {
}

String fileBase = new File(mPath).getAbsolutePath() + File.separator +
FileNameHelper.getExportFileName(mID, sport.TapiriikType(), startTime);
FileNameHelper.getExportFileName(startTime, sport.TapiriikType());

if (mFormat.contains(FileFormats.TCX)) {
TCX tcx = new TCX(db, simplifier);
Expand Down
17 changes: 6 additions & 11 deletions app/src/main/org/runnerup/export/WebDavSynchronizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,8 @@
import org.runnerup.workout.FileFormats;
import org.runnerup.workout.Sport;

import java.io.FilenameFilter;
import java.io.IOException;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import okhttp3.Authenticator;
import okhttp3.Credentials;
Expand Down Expand Up @@ -229,17 +225,18 @@ public Status upload(SQLiteDatabase db, final long mID){
}
}

String fileBase = FileNameHelper.getExportFileNameWithModel(startTime, sport.TapiriikType());
if (mFormat.contains(FileFormats.TCX)) {
TCX tcx = new TCX(db, simplifier);
StringWriter writer = new StringWriter();
tcx.export(mID, writer);
s = uploadFile(db, mID, sport, startTime, writer, FileFormats.TCX.getValue());
s = uploadFile(writer, fileBase, FileFormats.TCX.getValue());
}
if (s == Status.OK && mFormat.contains(FileFormats.GPX)) {
GPX gpx = new GPX(db, true, true, simplifier);
StringWriter writer = new StringWriter();
gpx.export(mID, writer);
s = uploadFile(db, mID, sport, startTime, writer, FileFormats.GPX.getValue());
s = uploadFile(writer, fileBase, FileFormats.GPX.getValue());
}

} catch (Exception e) {
Expand All @@ -250,14 +247,12 @@ public Status upload(SQLiteDatabase db, final long mID){
return s;
}

private Status uploadFile(SQLiteDatabase db, long mID, Sport sport, long startTime, StringWriter writer, String fileExt) {
String file = FileNameHelper.getDropBoxUploadFileName(mID, sport.TapiriikType(), startTime, fileExt);

private Status uploadFile(StringWriter writer, String fileBase, String fileExt) {
Status s = Status.ERROR;
try{
OkHttpClient client = getAuthClient();
RequestBody body = RequestBody.create(MediaType.parse("application/"+fileExt+"+xml"),writer.toString());
Request request = new Request.Builder().url(url +file).method("PUT", body).build();
RequestBody body = RequestBody.create(MediaType.parse("application/" + fileExt + "+xml"), writer.toString());
Request request = new Request.Builder().url(url + fileBase + fileExt).method("PUT", body).build();

Response response = client.newCall(request).execute();
int responseCode = response.code();
Expand Down
19 changes: 10 additions & 9 deletions app/src/main/org/runnerup/view/DetailActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,9 @@
import org.runnerup.widget.TitleSpinner;
import org.runnerup.widget.WidgetUtil;
import org.runnerup.workout.Intensity;
import org.runnerup.workout.Sport;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
Expand Down Expand Up @@ -120,7 +119,7 @@ public class DetailActivity extends AppCompatActivity implements Constants {
private SyncManager syncManager = null;
private Formatter formatter = null;

private long startTime = 0; // activity start time in unix timestamp
private long mStartTime = 0; // activity start time in unix timestamp

/**
* Called when the activity is first created.
Expand Down Expand Up @@ -481,7 +480,9 @@ private void fillHeaderData() {
// Must include the _id column for the adapter to work
String[] from = new String[]{
DB.ACTIVITY.START_TIME,
DB.ACTIVITY.DISTANCE, DB.ACTIVITY.TIME, DB.ACTIVITY.COMMENT,
DB.ACTIVITY.DISTANCE,
DB.ACTIVITY.TIME,
DB.ACTIVITY.COMMENT,
DB.ACTIVITY.SPORT
};

Expand All @@ -493,7 +494,7 @@ private void fillHeaderData() {

if (tmp.containsKey(DB.ACTIVITY.START_TIME)) {
long st = tmp.getAsLong(DB.ACTIVITY.START_TIME);
startTime = st;
mStartTime = st;
setTitle(formatter.formatDateTime(st));
}
double d = 0;
Expand Down Expand Up @@ -931,10 +932,10 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
}

private void shareActivity() {
final int which[] = {
final int[] which = {
1 //TODO preselect tcx - choice should be remembered
};
final CharSequence items[] = {
final CharSequence[] items = {
"gpx", "tcx" /* "nike+xml" */
};
AlertDialog.Builder builder = new AlertDialog.Builder(this)
Expand All @@ -959,10 +960,10 @@ public void onClick(DialogInterface dialog, int w) {

//Use of content:// (or STREAM?) instead of file:// is not supported in ES and other apps
//Solid Explorer File Manager works though
String actType = Sport.textOf(sport.getValueInt());
Uri uri = Uri.parse("content://" + ActivityProvider.AUTHORITY + "/" + fmt
+ "/" + mID
+ "/" + String.format(Locale.getDefault(), "RunnerUp_%04d_%s.%s", mID,
FileNameHelper.unixTimeToString(startTime), fmt));
+ "/" + FileNameHelper.getExportFileName(mStartTime, actType) + fmt);
intent.putExtra(Intent.EXTRA_STREAM, uri);
context.startActivity(Intent.createChooser(intent, getString(R.string.Share_activity)));
}
Expand Down
46 changes: 16 additions & 30 deletions app/src/play/org/runnerup/util/FileNameHelper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2012 - 2013 [email protected]
* Copyright (C) 2012 - 2020 [email protected]
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -26,55 +26,41 @@
*/
public class FileNameHelper {

private static String DATE_TIME_FORMAT_STRING = "yyyyMMddHHmmss";
private static String DATE_TIME_FORMAT_STRING = "yyyy-MM-dd-HH-mm-ss";

/**
* Generate export file name
* @param activityId activity index
* @param activityType activity type
* @param activityStartTime activity start time with String type
* @return the generated filename
*/
public static String getExportFileName(long activityId, String activityType, String activityStartTime) {
String fileBase =
String.format(Locale.getDefault(), "RunnerUp_%04d_%s_%s.",
activityId, activityType, activityStartTime);

return fileBase;
}

/**
* Generate export file name
* @param activityId activity index
* @param activityType activity type
* @param activityStartTime activity start time in unix timestamp
* @param activityType activity type
* @return the generated filename
*/
public static String getExportFileName(long activityId, String activityType, long activityStartTime) {
return getExportFileName(activityId, activityType, unixTimeToString(activityStartTime));
public static String getExportFileName(long activityStartTime, String activityType) {
return String.format(Locale.getDefault(),
"RunnerUp_%s_%s.",
unixTimeToString(activityStartTime),
activityType);
}

/**
* Generate filename for DropBox/WebDav
* @param activityId activity index
* @param activityType activity type
* Generate filename including the model name, to limit sorting in services like DropBox/WebDav
* @param activityStartTime activity start time in unix timestamp
* @param fileExt file extension (tcx/gpx)
* @param activityType activity type
* @return the generated filename for DropBox/WebDav
*/
public static String getDropBoxUploadFileName(long activityId, String activityType, long activityStartTime, String fileExt) {
return String.format(Locale.getDefault(), "/RunnerUp_%s_%04d_%s_%s.%s",
android.os.Build.MODEL.replaceAll("\\s","_"), activityId, activityType,
public static String getExportFileNameWithModel(long activityStartTime, String activityType) {
return String.format(Locale.getDefault(),
"/RunnerUp_%s_%s_%s.",
android.os.Build.MODEL.replaceAll("\\s","_"),
unixTimeToString(activityStartTime),
fileExt);
activityType);
}

/**
* convert unix timestamp to string
* @param timeStamp unix timestamp in seconds
* @return converted string in 'DATE_TIME_FORMAT_STRING' format
*/
public static String unixTimeToString(long timeStamp) {
private static String unixTimeToString(long timeStamp) {
return new SimpleDateFormat(DATE_TIME_FORMAT_STRING,
Locale.getDefault()).format(new Date(timeStamp * 1000L));
}
Expand Down

0 comments on commit 72564e7

Please sign in to comment.