diff --git a/app/src/main/java/com/github/axet/audiorecorder/app/Storage.java b/app/src/main/java/com/github/axet/audiorecorder/app/Storage.java index f4912fc..285bb6a 100644 --- a/app/src/main/java/com/github/axet/audiorecorder/app/Storage.java +++ b/app/src/main/java/com/github/axet/audiorecorder/app/Storage.java @@ -8,14 +8,22 @@ import android.os.Build; import android.preference.PreferenceManager; import java.io.File; +import java.text.SimpleDateFormat; import java.util.Date; +import java.util.Locale; +import java.util.TimeZone; public class Storage extends com.github.axet.audiolibrary.app.Storage { + public static final SimpleDateFormat ISO8601Z = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'", Locale.US) {{ + setTimeZone(TimeZone.getTimeZone("UTC")); + }}; + public static String getFormatted(String format, Date date) { format = format.replaceAll("%s", SIMPLE.format(date)); format = format.replaceAll("%I", ISO8601.format(date)); format = format.replaceAll("%T", "" + System.currentTimeMillis() / 1000); + format = format.replaceAll("%U", ISO8601Z.format(date)); return format; } @@ -41,7 +49,7 @@ public class Storage extends com.github.axet.audiolibrary.app.Storage { return getNextFile(context, path, format, ext); } else if (s.equals(ContentResolver.SCHEME_FILE)) { File f = getFile(path); - if (!f.exists() && !f.mkdirs()) + if (!f.exists() && !f.mkdirs() && !f.exists()) throw new RuntimeException("Unable to create: " + path); return Uri.fromFile(getNextFile(f, format, ext)); } else { @@ -58,7 +66,7 @@ public class Storage extends com.github.axet.audiolibrary.app.Storage { format = getFormatted(format, new Date()); - if (!f.exists() && !f.mkdirs()) + if (!f.exists() && !f.mkdirs() && !f.exists()) throw new RuntimeException("Unable to create: " + f); return getNextFile(f, format, ext); } diff --git a/app/src/main/java/com/github/axet/audiorecorder/widgets/NameFormatPreferenceCompat.java b/app/src/main/java/com/github/axet/audiorecorder/widgets/NameFormatPreferenceCompat.java index d981e82..4b8c0c9 100644 --- a/app/src/main/java/com/github/axet/audiorecorder/widgets/NameFormatPreferenceCompat.java +++ b/app/src/main/java/com/github/axet/audiorecorder/widgets/NameFormatPreferenceCompat.java @@ -1,13 +1,20 @@ package com.github.axet.audiorecorder.widgets; import android.content.Context; +import android.content.SharedPreferences; +import android.content.res.TypedArray; +import android.preference.PreferenceManager; +import android.support.v7.preference.ListPreference; import android.util.AttributeSet; +import com.github.axet.audiorecorder.app.AudioApplication; import com.github.axet.audiorecorder.app.Storage; import java.util.Date; -public class NameFormatPreferenceCompat extends com.github.axet.androidlibrary.widgets.NameFormatPreferenceCompat { +public class NameFormatPreferenceCompat extends ListPreference { + public long now = System.currentTimeMillis(); + public NameFormatPreferenceCompat(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @@ -25,15 +32,37 @@ public class NameFormatPreferenceCompat extends com.github.axet.androidlibrary.w } @Override - public String getFormatted(String str) { + public boolean callChangeListener(Object newValue) { + update(newValue); + return super.callChangeListener(newValue); + } + + @Override + protected Object onGetDefaultValue(TypedArray a, int index) { + Object def = super.onGetDefaultValue(a, index); + update(def); + return def; + } + + @Override + protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { + super.onSetInitialValue(restoreValue, defaultValue); CharSequence[] text = getEntries(); CharSequence[] values = getEntryValues(); - for (int i = 0; i < text.length; i++) { - String t = text[i].toString(); - String v = values[i].toString(); - if (v.equals(str)) - return t; - } - return Storage.getFormatted(str, new Date(1487926249000l)); + for (int i = 0; i < values.length; i++) + text[i] = getFormatted((String) values[i]); + setEntries(text); + update(getValue()); // defaultValue null after defaults set + } + + public void update(Object value) { + String v = (String) value; + setSummary(getFormatted(v)); + } + + public String getFormatted(String str) { + SharedPreferences shared = PreferenceManager.getDefaultSharedPreferences(getContext()); + String ext = shared.getString(AudioApplication.PREFERENCE_ENCODING, ""); + return Storage.getFormatted(str, new Date(now)) + "." + ext; } } diff --git a/app/src/main/res/values/attrs.xml b/app/src/main/res/values/attrs.xml index 71a8b1f..003ca07 100644 --- a/app/src/main/res/values/attrs.xml +++ b/app/src/main/res/values/attrs.xml @@ -38,11 +38,13 @@ 2017-02-01 09.08.01.wav 1487926249.wav 20170528T043902.wav + 20170528T043902Z.wav %s %T %I + %U diff --git a/app/src/main/res/xml/pref_general.xml b/app/src/main/res/xml/pref_general.xml index 6ce525a..c2482e5 100644 --- a/app/src/main/res/xml/pref_general.xml +++ b/app/src/main/res/xml/pref_general.xml @@ -47,7 +47,7 @@ android:summary="@string/pref_mode_summary" android:title="@string/pref_mode_title" /> -