From e5c6ee47df55b51d70d5b63860bf7e11db89b076 Mon Sep 17 00:00:00 2001 From: npes87184 Date: Thu, 28 Jul 2016 18:09:00 +0800 Subject: [PATCH] fully_customizable_filename_format --- .../com/sh1r0/noveldroid/MainActivity.java | 2 +- .../java/com/sh1r0/noveldroid/NovelUtils.java | 42 ++++--- .../sh1r0/noveldroid/SettingsFragment.java | 106 ++++++++++++++++-- .../downloader/AbstractDownloader.java | 2 +- .../downloader/ChuangshiDownloader.java | 2 +- .../downloader/Ck101Downloader.java | 2 +- .../noveldroid/downloader/EynyDownloader.java | 2 +- .../downloader/QidianDownloader.java | 2 +- .../downloader/ZonghengDownloader.java | 2 +- .../noveldroid/downloader/_17kDownloader.java | 2 +- .../main/res/layout/naming_rule_dialog.xml | 25 +++++ app/src/main/res/values-zh-rTW/arrays.xml | 13 --- app/src/main/res/values-zh-rTW/strings.xml | 8 +- app/src/main/res/values/arrays.xml | 13 --- app/src/main/res/values/strings.xml | 6 +- app/src/main/res/xml/settings.xml | 7 +- 16 files changed, 166 insertions(+), 70 deletions(-) create mode 100644 app/src/main/res/layout/naming_rule_dialog.xml diff --git a/app/src/main/java/com/sh1r0/noveldroid/MainActivity.java b/app/src/main/java/com/sh1r0/noveldroid/MainActivity.java index 600956b..e25a757 100644 --- a/app/src/main/java/com/sh1r0/noveldroid/MainActivity.java +++ b/app/src/main/java/com/sh1r0/noveldroid/MainActivity.java @@ -235,7 +235,7 @@ public void run() { downDirPath = prefs.getString("down_dir", NovelUtils.APP_DIR); mHandler.sendEmptyMessage(PREPARING); filename = novelDownloader.process(downDirPath, - Integer.parseInt(prefs.getString("naming_rule", "0")), + prefs.getString("naming_rule", NovelUtils.DEFAULT_NAMING_RULE), prefs.getString("encoding", "UTF-8")); if (filename == null) { throw new Exception(); diff --git a/app/src/main/java/com/sh1r0/noveldroid/NovelUtils.java b/app/src/main/java/com/sh1r0/noveldroid/NovelUtils.java index 7ee1575..234b60a 100644 --- a/app/src/main/java/com/sh1r0/noveldroid/NovelUtils.java +++ b/app/src/main/java/com/sh1r0/noveldroid/NovelUtils.java @@ -7,12 +7,15 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; +import java.text.SimpleDateFormat; +import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public class NovelUtils { + public static final String DEFAULT_NAMING_RULE = "/n"; public static final int MAX_THREAD_NUM = 4; public static final String APP_DIR = Environment.getExternalStorageDirectory().getAbsolutePath() + "/NovelDroid/"; public static final String TEMP_DIR = APP_DIR + "temp/"; @@ -55,22 +58,29 @@ public static NovelUtils getInstance() { return novelUtils; } - public static String genTxtName(String novelName, String authorName, int namingRule) { - String filename = ""; - switch (namingRule) { - case 0: - filename = novelName + ".txt"; - break; - case 1: - filename = novelName + "_" + authorName + ".txt"; - break; - case 2: - filename = authorName + "_" + novelName + ".txt"; - break; - default: - filename = novelName + ".txt"; - break; - } + public static String genTxtName(String novelName, String authorName, String namingRule) { + SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd"); + Date date = new Date(); + String strDate = sdFormat.format(date); + + SimpleDateFormat yyyyFormat = new SimpleDateFormat("yyyy"); + String yyyyDate = yyyyFormat.format(date); + + SimpleDateFormat mmFormat = new SimpleDateFormat("MM"); + String mmDate = mmFormat.format(date); + + SimpleDateFormat ddFormat = new SimpleDateFormat("dd"); + String ddDate = ddFormat.format(date); + + // /n = bookname, /a = author, /t = time, /y = year, /m = month and /d = day + String filename = namingRule.replace("/n", novelName); + filename = filename.replace("/a", authorName); + filename = filename.replace("/t", strDate); + filename = filename.replace("/y", yyyyDate); + filename = filename.replace("/m", mmDate); + filename = filename.replace("/d", ddDate); + + filename = filename + ".txt"; return filename; } diff --git a/app/src/main/java/com/sh1r0/noveldroid/SettingsFragment.java b/app/src/main/java/com/sh1r0/noveldroid/SettingsFragment.java index c778d8d..02fe4eb 100644 --- a/app/src/main/java/com/sh1r0/noveldroid/SettingsFragment.java +++ b/app/src/main/java/com/sh1r0/noveldroid/SettingsFragment.java @@ -22,6 +22,13 @@ import android.support.v7.preference.Preference; import android.support.v7.preference.Preference.OnPreferenceClickListener; import android.support.v7.preference.PreferenceFragmentCompat; +import android.support.v7.view.ContextThemeWrapper; +import android.text.Editable; +import android.text.TextWatcher; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.EditText; +import android.widget.TextView; import android.widget.Toast; import com.nononsenseapps.filepicker.FilePickerActivity; @@ -41,23 +48,26 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class SettingsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener { private static final String KEY_ENCODING = "encoding"; private static final String KEY_NAMING_RULE = "naming_rule"; + private static final String KEY_NAMING_RULE_PREVIEW = "naming_rule_preivew"; private static final String KEY_DOWN_DIR = "down_dir"; private static final String KEY_CHECK_UPDATE = "check_update"; private static final String KEY_ABOUT = "about"; private static final int FILE_CODE = 0; - private String[] namingRuleList; private Preference encoding; private Preference namingRule; private Preference downDir; private Preference checkUpdate; private Preference about; - private int namingRulePref; private SharedPreferences prefs; private String versionName; private String aboutMessage; @@ -81,10 +91,56 @@ public void onCreate(Bundle paramBundle) { } aboutMessage = getString(R.string.version_tag) + versionName + "\n" + getString(R.string.author_tag) + getString(R.string.author_name); - namingRuleList = this.getResources().getStringArray(R.array.naming_rule); - encoding = findPreference(KEY_ENCODING); namingRule = findPreference(KEY_NAMING_RULE); + namingRule.setOnPreferenceClickListener(new OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + LayoutInflater factory = LayoutInflater.from(getActivity()); + final View dialog_view = factory.inflate(R.layout.naming_rule_dialog,null); + final EditText editText; + final TextView previewTextView; + previewTextView = (TextView) dialog_view.findViewById(R.id.tv_preivew_string); + editText = (EditText) dialog_view.findViewById(R.id.et_naming_rule); + editText.addTextChangedListener(new TextWatcher() { + @Override + public void afterTextChanged(Editable s) { + } + + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + String userDefinedName = s.toString(); + String previewString = toPreviewFileNameString(userDefinedName); + previewTextView.setText(previewString); + } + }); + editText.setText(prefs.getString(KEY_NAMING_RULE, NovelUtils.DEFAULT_NAMING_RULE)); + AlertDialog.Builder editDialog = new AlertDialog.Builder(getActivity()); + editDialog.setTitle(getResources().getString(R.string.filename_rule_dialog_title)); + editDialog.setView(dialog_view); + editDialog.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() { + // do something when the button is clicked + public void onClick(DialogInterface arg0, int arg1) { + String userDefinedName = editText.getText().toString(); + String previewString = toPreviewFileNameString(userDefinedName); + if(isFilenameValid(previewString) && !previewString.equals("")) { + prefs.edit().putString(KEY_NAMING_RULE_PREVIEW, previewString).commit(); + namingRule.getSharedPreferences().edit().putString(KEY_NAMING_RULE, userDefinedName).commit(); + } else { + // illegal file name + Toast.makeText(getActivity(), getString(R.string.illegalName), Toast.LENGTH_LONG).show(); + } + } + }); + editDialog.show(); + return true; + } + }); + downDir = findPreference(KEY_DOWN_DIR); downDir.setOnPreferenceClickListener(new OnPreferenceClickListener() { @@ -147,6 +203,42 @@ public void onReceive(Context context, Intent intent) { }; } + private boolean isFilenameValid(String fileName) { + File f = new File(fileName); + try { + return f.getCanonicalFile().getName().equals(fileName); + } catch (IOException e) { + return false; + } + } + + private String toPreviewFileNameString(String format) { + String novelName = getString(R.string.bookname); + String authorName = getString(R.string.author); + SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd"); + Date date = new Date(); + String strDate = sdFormat.format(date); + + SimpleDateFormat yyyyFormat = new SimpleDateFormat("yyyy"); + String yyyyDate = yyyyFormat.format(date); + + SimpleDateFormat mmFormat = new SimpleDateFormat("MM"); + String mmDate = mmFormat.format(date); + + SimpleDateFormat ddFormat = new SimpleDateFormat("dd"); + String ddDate = ddFormat.format(date); + + // /n = bookname, /a = author, /t = time, /y = year, /m = month and /d = day + String filename = format.replace("/n", novelName); + filename = filename.replace("/a", authorName); + filename = filename.replace("/t", strDate); + filename = filename.replace("/y", yyyyDate); + filename = filename.replace("/m", mmDate); + filename = filename.replace("/d", ddDate); + + return filename; + } + @Override public void onCreatePreferences(Bundle bundle, String s) { @@ -157,8 +249,7 @@ public void onResume() { super.onResume(); getActivity().setTitle(R.string.settings); encoding.setSummary(prefs.getString(KEY_ENCODING, "UTF-8")); - namingRulePref = Integer.parseInt(prefs.getString(KEY_NAMING_RULE, "0")); - namingRule.setSummary(namingRuleList[namingRulePref]); + namingRule.setSummary(prefs.getString(KEY_NAMING_RULE_PREVIEW, toPreviewFileNameString(NovelUtils.DEFAULT_NAMING_RULE))); downDir.setSummary(prefs.getString(KEY_DOWN_DIR, NovelUtils.APP_DIR)); getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); getActivity().registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); @@ -186,8 +277,7 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin encoding.setSummary(sharedPreferences.getString(KEY_ENCODING, "UTF-8")); break; case KEY_NAMING_RULE: - namingRulePref = Integer.parseInt(sharedPreferences.getString(KEY_NAMING_RULE, "0")); - namingRule.setSummary(namingRuleList[namingRulePref]); + namingRule.setSummary(prefs.getString(KEY_NAMING_RULE_PREVIEW, toPreviewFileNameString(NovelUtils.DEFAULT_NAMING_RULE))); break; case KEY_DOWN_DIR: downDir.setSummary(sharedPreferences.getString(KEY_DOWN_DIR, NovelUtils.APP_DIR)); diff --git a/app/src/main/java/com/sh1r0/noveldroid/downloader/AbstractDownloader.java b/app/src/main/java/com/sh1r0/noveldroid/downloader/AbstractDownloader.java index e0a8808..ac9908b 100644 --- a/app/src/main/java/com/sh1r0/noveldroid/downloader/AbstractDownloader.java +++ b/app/src/main/java/com/sh1r0/noveldroid/downloader/AbstractDownloader.java @@ -20,5 +20,5 @@ abstract public class AbstractDownloader { public abstract void download(Handler progressHandler) throws Exception; - public abstract String process(String downDirPath, int namingRule, String encoding); + public abstract String process(String downDirPath, String namingRule, String encoding); } diff --git a/app/src/main/java/com/sh1r0/noveldroid/downloader/ChuangshiDownloader.java b/app/src/main/java/com/sh1r0/noveldroid/downloader/ChuangshiDownloader.java index 719dd68..9986777 100644 --- a/app/src/main/java/com/sh1r0/noveldroid/downloader/ChuangshiDownloader.java +++ b/app/src/main/java/com/sh1r0/noveldroid/downloader/ChuangshiDownloader.java @@ -62,7 +62,7 @@ public void download(Handler progressHandler) throws Exception { } @Override - public String process(String downDirPath, int namingRule, String encoding) { + public String process(String downDirPath, String namingRule, String encoding) { File downDir = new File(downDirPath); if (!downDir.exists()) { downDir.mkdirs(); diff --git a/app/src/main/java/com/sh1r0/noveldroid/downloader/Ck101Downloader.java b/app/src/main/java/com/sh1r0/noveldroid/downloader/Ck101Downloader.java index a5a9d7d..b98bbf8 100644 --- a/app/src/main/java/com/sh1r0/noveldroid/downloader/Ck101Downloader.java +++ b/app/src/main/java/com/sh1r0/noveldroid/downloader/Ck101Downloader.java @@ -120,7 +120,7 @@ public void download(Handler progressHandler) throws Exception { } @TargetApi(Build.VERSION_CODES.HONEYCOMB) - public String process(String downDirPath, int namingRule, String encoding) { + public String process(String downDirPath, String namingRule, String encoding) { File downDir = new File(downDirPath); if (!downDir.exists()) { downDir.mkdirs(); diff --git a/app/src/main/java/com/sh1r0/noveldroid/downloader/EynyDownloader.java b/app/src/main/java/com/sh1r0/noveldroid/downloader/EynyDownloader.java index 875bc94..9a8758b 100644 --- a/app/src/main/java/com/sh1r0/noveldroid/downloader/EynyDownloader.java +++ b/app/src/main/java/com/sh1r0/noveldroid/downloader/EynyDownloader.java @@ -118,7 +118,7 @@ public void download(Handler progressHandler) throws Exception { } @TargetApi(Build.VERSION_CODES.HONEYCOMB) - public String process(String downDirPath, int namingRule, String encoding) { + public String process(String downDirPath, String namingRule, String encoding) { File downDir = new File(downDirPath); if (!downDir.exists()) { downDir.mkdirs(); diff --git a/app/src/main/java/com/sh1r0/noveldroid/downloader/QidianDownloader.java b/app/src/main/java/com/sh1r0/noveldroid/downloader/QidianDownloader.java index 8889ae9..f45a8c4 100644 --- a/app/src/main/java/com/sh1r0/noveldroid/downloader/QidianDownloader.java +++ b/app/src/main/java/com/sh1r0/noveldroid/downloader/QidianDownloader.java @@ -66,7 +66,7 @@ public void download(Handler progressHandler) throws Exception { } @Override - public String process(String downDirPath, int namingRule, String encoding) { + public String process(String downDirPath, String namingRule, String encoding) { File downDir = new File(downDirPath); if (!downDir.exists()) { downDir.mkdirs(); diff --git a/app/src/main/java/com/sh1r0/noveldroid/downloader/ZonghengDownloader.java b/app/src/main/java/com/sh1r0/noveldroid/downloader/ZonghengDownloader.java index c3a256e..3dba778 100644 --- a/app/src/main/java/com/sh1r0/noveldroid/downloader/ZonghengDownloader.java +++ b/app/src/main/java/com/sh1r0/noveldroid/downloader/ZonghengDownloader.java @@ -65,7 +65,7 @@ public void download(Handler progressHandler) throws Exception { } @Override - public String process(String downDirPath, int namingRule, String encoding) { + public String process(String downDirPath, String namingRule, String encoding) { File downDir = new File(downDirPath); if (!downDir.exists()) { downDir.mkdirs(); diff --git a/app/src/main/java/com/sh1r0/noveldroid/downloader/_17kDownloader.java b/app/src/main/java/com/sh1r0/noveldroid/downloader/_17kDownloader.java index faf000d..360a282 100644 --- a/app/src/main/java/com/sh1r0/noveldroid/downloader/_17kDownloader.java +++ b/app/src/main/java/com/sh1r0/noveldroid/downloader/_17kDownloader.java @@ -65,7 +65,7 @@ public void download(Handler progressHandler) throws Exception { } @Override - public String process(String downDirPath, int namingRule, String encoding) { + public String process(String downDirPath, String namingRule, String encoding) { File downDir = new File(downDirPath); if (!downDir.exists()) { downDir.mkdirs(); diff --git a/app/src/main/res/layout/naming_rule_dialog.xml b/app/src/main/res/layout/naming_rule_dialog.xml new file mode 100644 index 0000000..1a145cb --- /dev/null +++ b/app/src/main/res/layout/naming_rule_dialog.xml @@ -0,0 +1,25 @@ + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values-zh-rTW/arrays.xml b/app/src/main/res/values-zh-rTW/arrays.xml index ac4bfe0..94e7501 100644 --- a/app/src/main/res/values-zh-rTW/arrays.xml +++ b/app/src/main/res/values-zh-rTW/arrays.xml @@ -13,17 +13,4 @@ UTF-8 UTF-16LE - - - @string/book_name - @string/book_name_author - @string/author_book_name - - - - 0 - 1 - 2 - - \ No newline at end of file diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index 4689acd..62aa220 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -13,6 +13,7 @@ 小說 ID 小說名稱 作者 + 書名 起始頁數 分析 分析中\u2026 @@ -40,14 +41,12 @@ 更新 編碼 選擇編碼 - 選擇命名規則 + 命名規則 + /n=書名, /a=作者, /t=年-月-日, /y=年, /m=月, /d=日 下載資料夾 新資料夾 新資料夾名稱 選擇性 - 作者_書名 - 書名 - 書名_作者 取消 檢查更新 無新版本。 @@ -56,5 +55,6 @@ 檔名規則 已完成 終止頁數 + 檔名不合適 diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index ac4bfe0..94e7501 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -13,17 +13,4 @@ UTF-8 UTF-16LE - - - @string/book_name - @string/book_name_author - @string/author_book_name - - - - 0 - 1 - 2 - - \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ad4e03d..aa8f81f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -13,6 +13,7 @@ Quit Novel ID Novel Name + Bookname Author Optional From Page @@ -48,13 +49,12 @@ Select Encoding Filename Rule Select Naming Rule + /n=bookname, /a=author /t=yyyy-MM-dd, /y=year, /m=month and /d=day Download Folder New Folder New Folder Name Edit Delete - BookName - BookName_Author - Author_BookName + Illegal file name diff --git a/app/src/main/res/xml/settings.xml b/app/src/main/res/xml/settings.xml index 528035b..efad528 100644 --- a/app/src/main/res/xml/settings.xml +++ b/app/src/main/res/xml/settings.xml @@ -11,14 +11,11 @@ android:dialogTitle="@string/encoding_dialog_title" /> -