diff --git a/Dogename/src/main/java/me/lensferno/dogename/data/Data.java b/Dogename/src/main/java/me/lensferno/dogename/data/Data.java index f3770d2..6d91439 100644 --- a/Dogename/src/main/java/me/lensferno/dogename/data/Data.java +++ b/Dogename/src/main/java/me/lensferno/dogename/data/Data.java @@ -13,18 +13,16 @@ public class Data { public static final int IGNORELIST_NAME_ONLY = 0; public static final int IGNORELIST_NUMBER_ONLY = 1; public static final int IGNORELIST_ALL = 2; - File dataFile; - SecureRandom secRandom = new SecureRandom(); - Random random = new Random(); + + private final File dataFile = new File(FilePath.toSpecificPathForm("files/Namelist.data")); + private final SecureRandom secRandom = new SecureRandom(); + private final Random random = new Random(); + private List nameList; private final IgnoreList ignoreList = new IgnoreList(); public Data() { - - dataFile = new File(FilePath.toSpecificPathForm("files/Namelist.data")); - try { - if (!dataFile.exists()) { dataFile.getParentFile().mkdirs(); dataFile.createNewFile(); @@ -48,7 +46,6 @@ public Data() { System.out.println("Failed to load data file."); e.printStackTrace(); } - ignoreList.readIgnoreList(); } @@ -90,9 +87,7 @@ public void importNameList(File path) { System.out.println("error in import namelist:" + e); e.printStackTrace(); } - } - } public void makeMass() { @@ -209,7 +204,7 @@ public int getNumberIgnoreListSize() { return ignoreList.getNumberIgnoreListSize(); } - class IgnoreList { + static class IgnoreList { private final File nameIgnoreFile = new File(FilePath.toSpecificPathForm("files/IgnoredNameList.data")); private final File numbIgnoreFile = new File(FilePath.toSpecificPathForm("files/IgnoredNumberList.data")); @@ -251,7 +246,7 @@ private void writeNumberIgnoreList() { public void readIgnoreList() { readNameIgnoreList(); readNumberIgnoreList(); - System.out.println(String.format("There are %d names and %d numbers ignored", ignoreNameList.size(), ignoreNumberList.size())); + System.out.printf("There are %d names and %d numbers ignored", ignoreNameList.size(), ignoreNumberList.size()); } private void readNameIgnoreList() { diff --git a/Dogename/src/main/java/me/lensferno/dogename/data/History.java b/Dogename/src/main/java/me/lensferno/dogename/data/History.java index 8997b2d..017728f 100644 --- a/Dogename/src/main/java/me/lensferno/dogename/data/History.java +++ b/Dogename/src/main/java/me/lensferno/dogename/data/History.java @@ -16,7 +16,6 @@ public void loadHistory() { File historyFile = new File(HISTORY_FILE); try { - if (!historyFile.exists()) { historyFile.getParentFile().mkdirs(); historyFile.createNewFile(); @@ -57,7 +56,6 @@ private void writeHistory() { File historyFile = new File(HISTORY_FILE); try { - if (!historyFile.exists()) { historyFile.createNewFile(); } diff --git a/Dogename/src/main/java/me/lensferno/dogename/voice/TokenManager.java b/Dogename/src/main/java/me/lensferno/dogename/voice/TokenManager.java index 0bf53b6..ab54d4d 100644 --- a/Dogename/src/main/java/me/lensferno/dogename/voice/TokenManager.java +++ b/Dogename/src/main/java/me/lensferno/dogename/voice/TokenManager.java @@ -32,7 +32,6 @@ private void updateTokenStatus(int statusCode) { if (netAvailable()) { refreshToken(); } - if (checkTokenAvailable() != 0) { tokenStatus = TOKEN_BAD; } @@ -55,19 +54,13 @@ public void init() { if (tokenFile.exists()) { loadToken(); updateTokenStatus(checkTokenAvailable()); - } else { - if (netAvailable()) { - refreshToken(); - updateTokenStatus(checkTokenAvailable()); - } else { tokenStatus = TOKEN_BAD; } - } } @@ -98,17 +91,10 @@ private int checkTokenAvailable() { return 0; } - void fetchToken() { try { token = new Gson().fromJson( - NetworkUtil.getHtml( - TOKEN_API_URL - + "?grant_type=client_credentials&client_id=" + API_KEY - + "&client_secret=" + SEC_KEY, - true) - , Token.class); - + NetworkUtil.getHtml(String.format("%s?grant_type=client_credentials&client_id=%s&client_secret=%s", TOKEN_API_URL, API_KEY, SEC_KEY), true), Token.class); token.setExpTime(); } catch (Exception e) { System.out.println("Error to get Token:" + e); @@ -118,7 +104,6 @@ void fetchToken() { private boolean netAvailable() { try { - URL sourcesURL = new URL("http://www.baidu.com"); HttpURLConnection connection = (HttpURLConnection) sourcesURL.openConnection(); connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36"); @@ -136,9 +121,8 @@ private boolean netAvailable() { } private void loadToken() { - ObjectInputStream ois; try { - ois = new ObjectInputStream(new FileInputStream(tokenFile)); + ObjectInputStream ois = new ObjectInputStream(new FileInputStream(tokenFile)); this.token = (Token) ois.readObject(); ois.close(); } catch (Exception e) { @@ -148,7 +132,6 @@ private void loadToken() { } private void writeToken() { - try { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(tokenFile)); oos.writeObject(token); diff --git a/Dogename/src/main/java/me/lensferno/dogename/voice/VoicePlayer.java b/Dogename/src/main/java/me/lensferno/dogename/voice/VoicePlayer.java index 46e0126..c7ea28b 100644 --- a/Dogename/src/main/java/me/lensferno/dogename/voice/VoicePlayer.java +++ b/Dogename/src/main/java/me/lensferno/dogename/voice/VoicePlayer.java @@ -18,7 +18,7 @@ public class VoicePlayer { Token token; StreamPlayer streamPlayer = new StreamPlayer(); - private VoiceConfig voiceConfig = null; + private final VoiceConfig voiceConfig; public VoicePlayer(Token token, VoiceConfig voiceConfig) { this.token = token;