add utc time format
This commit is contained in:
parent
ec47fc69d6
commit
9b7f819dfa
5 changed files with 52 additions and 13 deletions
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,11 +38,13 @@
|
|||
<item>2017-02-01 09.08.01.wav</item>
|
||||
<item>1487926249.wav</item>
|
||||
<item>20170528T043902.wav</item>
|
||||
<item>20170528T043902Z.wav</item>
|
||||
</string-array>
|
||||
|
||||
<string-array name="format_values" formatted="false" translatable="false">
|
||||
<item>%s</item>
|
||||
<item>%T</item>
|
||||
<item>%I</item>
|
||||
<item>%U</item>
|
||||
</string-array>
|
||||
</resources>
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@
|
|||
android:summary="@string/pref_mode_summary"
|
||||
android:title="@string/pref_mode_title" />
|
||||
|
||||
<ListPreference
|
||||
<com.github.axet.audiorecorder.widgets.NameFormatPreferenceCompat
|
||||
android:defaultValue="%s"
|
||||
android:entries="@array/format_text"
|
||||
android:entryValues="@array/format_values"
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ buildscript {
|
|||
google()
|
||||
}
|
||||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:3.3.1'
|
||||
classpath 'com.android.tools.build:gradle:3.3.2'
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue