Refactoring

This commit is contained in:
M66B 2021-01-04 15:35:57 +01:00
parent 0d8535815a
commit 1b69db2b93
1 changed files with 72 additions and 68 deletions

View File

@ -278,9 +278,58 @@ public class MessageClassifier {
static synchronized void save(Context context) throws JSONException, IOException { static synchronized void save(Context context) throws JSONException, IOException {
if (!dirty) if (!dirty)
return; return;
File file = getFile(context);
Helper.writeText(file, toJson().toString(2));
dirty = false;
Log.i("Classifier data saved");
}
private static synchronized void load(Context context) throws IOException, JSONException {
if (loaded)
return;
if (!isEnabled(context)) if (!isEnabled(context))
return; return;
classMessages.clear();
wordClassFrequency.clear();
File file = getFile(context);
if (file.exists()) {
String json = Helper.readText(file);
fromJson(new JSONObject(json));
}
loaded = true;
Log.i("Classifier data loaded");
}
static synchronized void clear(Context context) {
Log.i("Classifier clear");
classMessages.clear();
wordClassFrequency.clear();
dirty = true;
}
static boolean isEnabled(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
return prefs.getBoolean("classification", false);
}
static boolean canClassify(String folderType) {
return EntityFolder.INBOX.equals(folderType) ||
EntityFolder.JUNK.equals(folderType) ||
EntityFolder.USER.equals(folderType);
}
static File getFile(Context context) {
return new File(context.getFilesDir(), "classifier.json");
}
static JSONObject toJson() throws JSONException {
JSONArray jmessages = new JSONArray(); JSONArray jmessages = new JSONArray();
for (Long account : classMessages.keySet()) for (Long account : classMessages.keySet())
for (String clazz : classMessages.get(account).keySet()) { for (String clazz : classMessages.get(account).keySet()) {
@ -309,29 +358,10 @@ public class MessageClassifier {
jroot.put("messages", jmessages); jroot.put("messages", jmessages);
jroot.put("words", jwords); jroot.put("words", jwords);
File file = getFile(context); return jroot;
Helper.writeText(file, jroot.toString(2));
dirty = false;
Log.i("Classifier saved");
} }
private static synchronized void load(Context context) throws IOException, JSONException { static void fromJson(JSONObject jroot) throws JSONException {
if (loaded)
return;
if (!isEnabled(context))
return;
classMessages.clear();
wordClassFrequency.clear();
File file = getFile(context);
if (file.exists()) {
String json = Helper.readText(file);
JSONObject jroot = new JSONObject(json);
JSONArray jmessages = jroot.getJSONArray("messages"); JSONArray jmessages = jroot.getJSONArray("messages");
for (int m = 0; m < jmessages.length(); m++) { for (int m = 0; m < jmessages.length(); m++) {
JSONObject jmessage = (JSONObject) jmessages.get(m); JSONObject jmessage = (JSONObject) jmessages.get(m);
@ -357,32 +387,6 @@ public class MessageClassifier {
} }
} }
loaded = true;
Log.i("Classifier loaded");
}
static synchronized void clear(Context context) {
Log.i("Classifier clear");
classMessages.clear();
wordClassFrequency.clear();
dirty = true;
}
static boolean isEnabled(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
return prefs.getBoolean("classification", false);
}
static boolean canClassify(String folderType) {
return EntityFolder.INBOX.equals(folderType) ||
EntityFolder.JUNK.equals(folderType) ||
EntityFolder.USER.equals(folderType);
}
static File getFile(Context context) {
return new File(context.getFilesDir(), "classifier.json");
}
private static class Stat { private static class Stat {
int matchedWords = 0; int matchedWords = 0;
int totalFrequency = 0; int totalFrequency = 0;