Add support to choose the ISO 8601 date format as the default track name.

Note using the "yyyy-MM-dd HH:mm" format so it is readable
for users. However, the colon, ":", is not a valid
FAT32 filename character. So when saving to the SD card,
the filename becomes "yyyy-MM-dd HH_mm".
This commit is contained in:
Jimmy Shih
2012-01-09 12:50:53 -08:00
parent 88c4e0e499
commit 43cf0659fd
10 changed files with 196 additions and 82 deletions
@@ -18,43 +18,57 @@ package com.google.android.apps.mytracks.services;
import com.google.android.apps.mytracks.Constants;
import com.google.android.apps.mytracks.util.StringUtils;
import com.google.android.maps.mytracks.R;
import com.google.common.annotations.VisibleForTesting;
import android.content.Context;
import android.content.SharedPreferences;
import java.text.SimpleDateFormat;
/**
* Creates a default track name based on the current default track name policy.
* Creates a default track name based on the track name setting.
*
* @author Matthew Simmons
*/
class DefaultTrackNameFactory {
public class DefaultTrackNameFactory {
@VisibleForTesting
static final String ISO_8601_FORMAT = "yyyy-MM-dd HH:mm";
private final Context context;
DefaultTrackNameFactory(Context context) {
public DefaultTrackNameFactory(Context context) {
this.context = context;
}
/**
* Creates a new track name.
* Gets the default track name.
*
* @param trackId The ID for the current track.
* @param startTime The start time, in milliseconds since the epoch, of the
* current track.
* @return The new track name.
* @param trackId the track id
* @param startTime the track start time
*/
String newTrackName(long trackId, long startTime) {
if (useTimestampTrackName()) {
public String getDefaultTrackName(long trackId, long startTime) {
String trackNameSetting = getTrackNameSetting();
if (trackNameSetting.equals(
context.getString(R.string.settings_recording_track_name_date_local_value))) {
return StringUtils.formatDateTime(context, startTime);
} else if (trackNameSetting.equals(
context.getString(R.string.settings_recording_track_name_date_iso_8601_value))) {
SimpleDateFormat dateFormat = new SimpleDateFormat(ISO_8601_FORMAT);
return dateFormat.format(startTime);
} else {
return String.format(context.getString(R.string.track_name_format), trackId);
}
}
/** Determines whether the preferences allow a timestamp-based track name */
protected boolean useTimestampTrackName() {
SharedPreferences prefs = context.getSharedPreferences(
/**
* Gets the track name setting from the shared preferences.
*/
@VisibleForTesting
String getTrackNameSetting() {
SharedPreferences sharedPreferences = context.getSharedPreferences(
Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
return prefs.getBoolean(
context.getString(R.string.timestamp_track_name_key), true);
return sharedPreferences.getString(
context.getString(R.string.settings_recording_track_name_key),
context.getString(R.string.settings_recording_track_name_date_local_value));
}
}
@@ -586,7 +586,7 @@ public class TrackRecordingService extends Service {
Uri trackUri = providerUtils.insertTrack(track);
recordingTrackId = Long.parseLong(trackUri.getLastPathSegment());
track.setId(recordingTrackId);
track.setName(new DefaultTrackNameFactory(this).newTrackName(
track.setName(new DefaultTrackNameFactory(this).getDefaultTrackName(
recordingTrackId, startTime));
track.setCategory(getDefaultActivityType(this));
isRecording = true;
@@ -32,8 +32,8 @@ import java.util.TimeZone;
public class FileUtils {
/**
* The maximum Fat32 path length. See
* http://www.scribd.com/doc/2187273/FAT32-File-System-Specification
* The maximum FAT32 path length. See the FAT32 spec at
* http://msdn.microsoft.com/en-us/windows/hardware/gg463080
*/
@VisibleForTesting
static final int MAX_FAT32_PATH_LENGTH = 260;
@@ -148,7 +148,7 @@ public class FileUtils {
for (int i = 0; i < name.length(); i++) {
int codePoint = name.codePointAt(i);
char character = name.charAt(i);
if (Character.isLetterOrDigit(character) || codePoint > 127 || character == '-') {
if (Character.isLetterOrDigit(character) || codePoint > 127 || isSpecialFat32(character)) {
buffer.appendCodePoint(codePoint);
} else {
buffer.append("_");
@@ -157,6 +157,42 @@ public class FileUtils {
String result = buffer.toString();
return result.replaceAll("_+", "_");
}
/**
* Returns true if it is a special FAT32 character.
*
* @param character the character
*/
private boolean isSpecialFat32(char character) {
switch (character) {
case '$':
case '%':
case '\'':
case '-':
case '_':
case '@':
case '~':
case '`':
case '!':
case '(':
case ')':
case '{':
case '}':
case '^':
case '#':
case '&':
case '+':
case ',':
case ';':
case '=':
case '[':
case ']':
case ' ':
return true;
default:
return false;
}
}
/**
* Truncates the name if necessary so the filename path length (directory +