Skip to content

Commit

Permalink
Add configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
AozoraDev committed Nov 12, 2023
1 parent 7428e87 commit 2626709
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 14 deletions.
64 changes: 64 additions & 0 deletions lib/src/main/java/com/harubyte/mitobi/Configs.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,72 @@
package com.harubyte.mitobi;

import android.content.Context;
import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import org.json.JSONException;
import org.json.JSONObject;

public class Configs {
public static final String NAME = "Mitobi";
public static final String VERSION = "1.0";
public static final String TITLE = String.format("%s (v%s)", NAME, VERSION);
public static final String ACTION = "com.harubyte.mitobi.action.MAIN";

/*
* Default configuration for Mitobi
*
* @return Default configuration
*/
public static JSONObject defaultConfig() {
JSONObject config = new JSONObject();

try {
config.put("autoBackup", false);
config.put("autoBackup_withCache", false);
config.put("useMaterialTheme", true);
config.put("hideDialog", false);
} catch (JSONException err) {
// Nothing i can do.
}

return config;
}

/*
* Create or update the config file
*
* @param file Output for config file
*/
public static void setupConfig(File file) throws JSONException, IOException {
// Get default config
JSONObject config = defaultConfig();

if (file.exists()) {
// It's exist. We should update it or nah.

// Saved config file
JSONObject savedConfig = new JSONObject(Utils.read(file));

// Update saved config if not same with default config
Iterator<String> keys = config.keys();
boolean isSame = true;
while (keys.hasNext()) {
String key = keys.next();

if (savedConfig.has(key)) continue; // Already exist.
if (isSame) isSame = false; // Once this code area executed, then the config is not same.

savedConfig.put(key, config.get(key)); // Not exist. Add it.
}

// Write it back if not same
if (!isSame) Utils.writeString(file, savedConfig.toString(2));
} else {
// Not exist. Create new config file.
Utils.writeString(file, config.toString(2));
}
}
}
64 changes: 50 additions & 14 deletions lib/src/main/java/com/harubyte/mitobi/Mitobi.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.json.JSONObject;

public class Mitobi {
private ExecutorService executor = Executors.newSingleThreadExecutor();
Expand All @@ -23,27 +24,44 @@ public class Mitobi {
private Handler handler;
private File data, externalData;
private AlertDialog processDialog, mainDialog;
private JSONObject config;

public Mitobi(Context context) {
// Set up some needed stuff
this.context = context;
this.handler = new Handler(context.getMainLooper());
this.data = context.getDataDir();
this.externalData = context.getExternalFilesDir("Mitobi");

try {
// Set up config file
File configFile = new File(externalData.getParent() + File.separator + "MitobiConfig.json");
Configs.setupConfig(configFile);

this.config = new JSONObject(Utils.read(configFile));
} catch (Exception err) {
err.printStackTrace();

// If local config error, use default config
Toast.makeText(context, "Error: Local config is broken, using default config instead.", Toast.LENGTH_LONG).show();
this.config = Configs.defaultConfig();
}

// Determine theme
if (Utils.isNightMode(context)) {
theme = R.style.Theme_Material_Dialog_Alert;
} else {
theme = R.style.Theme_Material_Light_Dialog_Alert;
if (config.optBoolean("useMaterialTheme", true)) {
if (Utils.isNightMode(context)) {
theme = R.style.Theme_Material_Dialog_Alert;
} else {
theme = R.style.Theme_Material_Light_Dialog_Alert;
}
}

// Set up some needed stuff
this.handler = new Handler(context.getMainLooper());
this.data = context.getDataDir();
this.externalData = context.getExternalFilesDir("Mitobi");
// AlertDialog for loading
this.processDialog = new AlertDialog.Builder(context, theme)
.setCancelable(false)
.create();

Log.i(Configs.NAME, Configs.TITLE + " initiated!");

start();
}

Expand All @@ -62,6 +80,21 @@ public void start() {
public void start(Boolean back) {
// If not back, create new dialog
if (!back) {
// Auto Backup
if (config.optBoolean("autoBackup", false)) {
executor.execute(() -> {
Utils.removeAll(externalData);
if (config.optBoolean("autoBackup_withCache", false)) {
backup(data, true, true);
} else {
backup(data, false, true);
}

// Kill the executor if dialog is hidden
if (config.optBoolean("hideDialog", false)) kill();
});
}

Log.i(Configs.NAME, "Started!");

StringBuilder sb = new StringBuilder();
Expand All @@ -82,14 +115,15 @@ public void start(Boolean back) {
// Don't shutdown the executor and handler if its a test app
if (!ai.packageName.equals("com.harubyte.mitobiapp")) {
kill();
Log.i(Configs.NAME, "Handler and Executor terminated!");
}

if (callback != null) callback.onClose();
Log.i(Configs.NAME, "Terminated!");
Log.i(Configs.NAME, "Dialog closed!");
})
.setCancelable(false)
.show();
.create();

if (!config.optBoolean("hideDialog", false)) mainDialog.show();
} else {
// Otherwise, show the already created mainDialog
mainDialog.show();
Expand All @@ -100,6 +134,8 @@ public void start(Boolean back) {
public void kill() {
executor.shutdownNow();
handler = null;

Log.i(Configs.NAME, "Handler and Executor terminated!");
}

/*
Expand Down Expand Up @@ -245,7 +281,7 @@ private void restore(File files, boolean isMainProcess) {

if (isMainProcess) handler.post(() -> {
Toast.makeText(context, "All data restored successfully!\nPlease re-open the app.", Toast.LENGTH_LONG).show();
processDialog.dismiss();
if (processDialog.isShowing()) processDialog.dismiss();

kill();
((Activity) context).finishAffinity();
Expand Down Expand Up @@ -288,7 +324,7 @@ private void backup(File files, boolean withCache, boolean isMainProcess) {

if (isMainProcess) handler.post(() -> {
Toast.makeText(context, "All data backed up successfully!", Toast.LENGTH_LONG).show();
processDialog.dismiss();
if (processDialog.isShowing()) processDialog.dismiss();
});
}

Expand Down
36 changes: 36 additions & 0 deletions lib/src/main/java/com/harubyte/mitobi/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import android.content.Context;
import android.content.res.Configuration;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Utils {
Expand All @@ -26,6 +30,38 @@ public static void write(File file, String path) throws IOException {
}
}

/*
* Reading content inside a file
*
* @param file Input of the file
*/
public static String read(File file) throws IOException {
try (FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr)) {
String line;
StringBuilder sb = new StringBuilder();

while ((line = br.readLine()) != null) {
sb.append(line);
}

return sb.toString();
}
}

/*
* Writing string to file
*
* @param path Output path for the file
* @param content The content to write
*/
public static void writeString(File file, String content) throws IOException {
try (FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw)) {
bw.write(content);
}
}

/*
* Delete all contents inside the directory
*
Expand Down

0 comments on commit 2626709

Please sign in to comment.