Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1 from npes87184/fully_customizable_filename_format
Browse files Browse the repository at this point in the history
Support more customizable format for filename
  • Loading branch information
sh1r0 authored Aug 14, 2016
2 parents c062d8b + e5c6ee4 commit 6a9c5e2
Show file tree
Hide file tree
Showing 16 changed files with 166 additions and 70 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/sh1r0/noveldroid/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
42 changes: 26 additions & 16 deletions app/src/main/java/com/sh1r0/noveldroid/NovelUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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/";
Expand Down Expand Up @@ -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;
}
Expand Down
106 changes: 98 additions & 8 deletions app/src/main/java/com/sh1r0/noveldroid/SettingsFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -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) {

Expand All @@ -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));
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
25 changes: 25 additions & 0 deletions app/src/main/res/layout/naming_rule_dialog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name_rule"
android:id="@+id/tv_naming_rule"
android:layout_gravity="center_horizontal" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_preivew_string"
android:textSize="30sp"
android:layout_gravity="center_horizontal" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_naming_rule" />

</LinearLayout>
13 changes: 0 additions & 13 deletions app/src/main/res/values-zh-rTW/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,4 @@
<item>UTF-8</item>
<item>UTF-16LE</item>
</string-array>

<string-array name="naming_rule">
<item>@string/book_name</item>
<item>@string/book_name_author</item>
<item>@string/author_book_name</item>
</string-array>

<string-array name="naming_rule_value">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>

</resources>
8 changes: 4 additions & 4 deletions app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<string name="id_hint">小說 ID</string>
<string name="novel_name">小說名稱</string>
<string name="author">作者</string>
<string name="bookname">書名</string>
<string name="from_page">起始頁數</string>
<string name="analyze">分析</string>
<string name="analyzing">分析中\u2026</string>
Expand Down Expand Up @@ -40,14 +41,12 @@
<string name="update">更新</string>
<string name="encoding">編碼</string>
<string name="encoding_dialog_title">選擇編碼</string>
<string name="filename_rule_dialog_title">選擇命名規則</string>
<string name="filename_rule_dialog_title">命名規則</string>
<string name="name_rule">/n=書名, /a=作者, /t=年-月-日, /y=年, /m=月, /d=日</string>
<string name="download_folder">下載資料夾</string>
<string name="new_folder">新資料夾</string>
<string name="new_folder_name">新資料夾名稱</string>
<string name="author_hint">選擇性</string>
<string name="author_book_name">作者_書名</string>
<string name="book_name">書名</string>
<string name="book_name_author">書名_作者</string>
<string name="cancel">取消</string>
<string name="check_update">檢查更新</string>
<string name="current_is_latest">無新版本。</string>
Expand All @@ -56,5 +55,6 @@
<string name="filename_rule">檔名規則</string>
<string name="novel_saved_tooltip">已完成</string>
<string name="to_page">終止頁數</string>
<string name="illegalName">檔名不合適</string>

</resources>
13 changes: 0 additions & 13 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,4 @@
<item>UTF-8</item>
<item>UTF-16LE</item>
</string-array>

<string-array name="naming_rule">
<item>@string/book_name</item>
<item>@string/book_name_author</item>
<item>@string/author_book_name</item>
</string-array>

<string-array name="naming_rule_value">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>

</resources>
Loading

0 comments on commit 6a9c5e2

Please sign in to comment.