From 51545510a1000362725c86bfe83ef7281f2ff743 Mon Sep 17 00:00:00 2001 From: lensferno Date: Sat, 18 Sep 2021 16:50:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=8F=E4=BF=AE=E5=B0=8F=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/me/lensferno/dogename/data/Data.java | 19 +++++++---------- .../me/lensferno/dogename/data/History.java | 2 -- .../dogename/voice/TokenManager.java | 21 ++----------------- .../lensferno/dogename/voice/VoicePlayer.java | 2 +- 4 files changed, 10 insertions(+), 34 deletions(-) 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 class Data { System.out.println("Failed to load data file."); e.printStackTrace(); } - ignoreList.readIgnoreList(); } @@ -90,9 +87,7 @@ public class Data { System.out.println("error in import namelist:" + e); e.printStackTrace(); } - } - } public void makeMass() { @@ -209,7 +204,7 @@ public class Data { 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 @@ public class Data { 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 class History { File historyFile = new File(HISTORY_FILE); try { - if (!historyFile.exists()) { historyFile.getParentFile().mkdirs(); historyFile.createNewFile(); @@ -57,7 +56,6 @@ public class History { 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 @@ public class TokenManager { if (netAvailable()) { refreshToken(); } - if (checkTokenAvailable() != 0) { tokenStatus = TOKEN_BAD; } @@ -55,19 +54,13 @@ public class TokenManager { if (tokenFile.exists()) { loadToken(); updateTokenStatus(checkTokenAvailable()); - } else { - if (netAvailable()) { - refreshToken(); - updateTokenStatus(checkTokenAvailable()); - } else { tokenStatus = TOKEN_BAD; } - } } @@ -98,17 +91,10 @@ public class TokenManager { 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 @@ public class TokenManager { 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 @@ public class TokenManager { } 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 @@ public class TokenManager { } 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;