Refactor SettingsHelper and move read/write functions to a new class
This commit is contained in:
parent
159c28dd0d
commit
58ef2bc2ff
2 changed files with 87 additions and 69 deletions
|
@ -0,0 +1,50 @@
|
||||||
|
package org.shadowice.flocke.andotp;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.net.Uri;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.io.OutputStreamWriter;
|
||||||
|
|
||||||
|
public class FileHelper {
|
||||||
|
public static String readFileToString(Context context, Uri file) {
|
||||||
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
|
|
||||||
|
try {
|
||||||
|
InputStream inputStream = context.getContentResolver().openInputStream(file);
|
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
||||||
|
String line;
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
stringBuilder.append(line);
|
||||||
|
}
|
||||||
|
reader.close();
|
||||||
|
inputStream.close();
|
||||||
|
} catch (Exception error) {
|
||||||
|
error.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return(stringBuilder.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean writeStringToFile(Context context, Uri file, String content) {
|
||||||
|
boolean success = true;
|
||||||
|
|
||||||
|
try {
|
||||||
|
OutputStream outputStream = context.getContentResolver().openOutputStream(file);
|
||||||
|
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));
|
||||||
|
writer.write(content.toString());
|
||||||
|
writer.close();
|
||||||
|
outputStream.close();
|
||||||
|
} catch (Exception error) {
|
||||||
|
success = false;
|
||||||
|
error.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -46,9 +46,19 @@ public class SettingsHelper {
|
||||||
public static final String KEY_FILE = "otp.key";
|
public static final String KEY_FILE = "otp.key";
|
||||||
public static final String SETTINGS_FILE = "secrets.dat";
|
public static final String SETTINGS_FILE = "secrets.dat";
|
||||||
|
|
||||||
public static void store(Context context, JSONArray json) {
|
public static void store(Context context, ArrayList<Entry> entries) {
|
||||||
|
JSONArray a = new JSONArray();
|
||||||
|
|
||||||
|
for(Entry e: entries){
|
||||||
|
try {
|
||||||
|
a.put(e.toJSON());
|
||||||
|
} catch (Exception error) {
|
||||||
|
error.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
byte[] data = json.toString().getBytes();
|
byte[] data = a.toString().getBytes();
|
||||||
|
|
||||||
SecretKey key = EncryptionHelper.loadOrGenerateKeys(context, new File(context.getFilesDir() + "/" + KEY_FILE));
|
SecretKey key = EncryptionHelper.loadOrGenerateKeys(context, new File(context.getFilesDir() + "/" + KEY_FILE));
|
||||||
data = EncryptionHelper.encrypt(key,data);
|
data = EncryptionHelper.encrypt(key,data);
|
||||||
|
@ -60,28 +70,19 @@ public class SettingsHelper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void store(Context context, ArrayList<Entry> entries){
|
|
||||||
JSONArray a = new JSONArray();
|
|
||||||
|
|
||||||
for(Entry e: entries){
|
|
||||||
try {
|
|
||||||
a.put(e.toJSON());
|
|
||||||
} catch (Exception error) {
|
|
||||||
error.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
store(context, a);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static ArrayList<Entry> load(Context context){
|
public static ArrayList<Entry> load(Context context){
|
||||||
ArrayList<Entry> entries = new ArrayList<>();
|
ArrayList<Entry> entries = new ArrayList<>();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
JSONArray a = readJSON(context);
|
byte[] data = readFully(new File(context.getFilesDir() + "/" + SETTINGS_FILE));
|
||||||
|
|
||||||
for (int i = 0; i < a.length(); i++) {
|
SecretKey key = EncryptionHelper.loadOrGenerateKeys(context, new File(context.getFilesDir() + "/" + KEY_FILE));
|
||||||
entries.add(new Entry(a.getJSONObject(i)));
|
data = EncryptionHelper.decrypt(key, data);
|
||||||
|
|
||||||
|
JSONArray json = new JSONArray(new String(data));
|
||||||
|
|
||||||
|
for (int i = 0; i < json.length(); i++) {
|
||||||
|
entries.add(new Entry(json.getJSONObject(i)));
|
||||||
}
|
}
|
||||||
} catch (Exception error) {
|
} catch (Exception error) {
|
||||||
error.printStackTrace();
|
error.printStackTrace();
|
||||||
|
@ -90,73 +91,40 @@ public class SettingsHelper {
|
||||||
return entries;
|
return entries;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JSONArray readJSON(Context context) {
|
public static boolean exportAsJSON(Context context, Uri file) {
|
||||||
|
ArrayList<Entry> entries = load(context);
|
||||||
|
|
||||||
JSONArray json = new JSONArray();
|
JSONArray json = new JSONArray();
|
||||||
|
|
||||||
try {
|
for(Entry e: entries){
|
||||||
byte[] data = readFully(new File(context.getFilesDir() + "/" + SETTINGS_FILE));
|
try {
|
||||||
|
json.put(e.toJSON());
|
||||||
SecretKey key = EncryptionHelper.loadOrGenerateKeys(context, new File(context.getFilesDir() + "/" + KEY_FILE));
|
} catch (Exception error) {
|
||||||
data = EncryptionHelper.decrypt(key, data);
|
error.printStackTrace();
|
||||||
|
}
|
||||||
json = new JSONArray(new String(data));
|
|
||||||
}
|
|
||||||
catch (Exception error) {
|
|
||||||
error.printStackTrace();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return json;
|
return FileHelper.writeStringToFile(context, file, json.toString());
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean exportAsJSON(Context context, Uri file) {
|
|
||||||
JSONArray data = readJSON(context);
|
|
||||||
|
|
||||||
boolean success = true;
|
|
||||||
|
|
||||||
try {
|
|
||||||
OutputStream outputStream = context.getContentResolver().openOutputStream(file);
|
|
||||||
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));
|
|
||||||
writer.write(data.toString());
|
|
||||||
writer.close();
|
|
||||||
outputStream.close();
|
|
||||||
} catch (Exception error) {
|
|
||||||
success = false;
|
|
||||||
error.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return success;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean importFromJSON(Context context, Uri file) {
|
public static boolean importFromJSON(Context context, Uri file) {
|
||||||
boolean success = true;
|
boolean success = true;
|
||||||
|
|
||||||
StringBuilder stringBuilder = new StringBuilder();
|
String content = FileHelper.readFileToString(context, file);
|
||||||
|
ArrayList<Entry> entries = new ArrayList<>();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
InputStream inputStream = context.getContentResolver().openInputStream(file);
|
JSONArray json = new JSONArray(content);
|
||||||
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
|
||||||
String line;
|
for (int i = 0; i < json.length(); i++) {
|
||||||
while ((line = reader.readLine()) != null) {
|
entries.add(new Entry(json.getJSONObject(i)));
|
||||||
stringBuilder.append(line);
|
|
||||||
}
|
}
|
||||||
reader.close();
|
|
||||||
inputStream.close();
|
|
||||||
} catch (Exception error) {
|
|
||||||
success = false;
|
|
||||||
error.printStackTrace();
|
|
||||||
}
|
|
||||||
String content = stringBuilder.toString();
|
|
||||||
|
|
||||||
JSONArray json = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
json = new JSONArray(content);
|
|
||||||
} catch (Exception error) {
|
} catch (Exception error) {
|
||||||
success = false;
|
success = false;
|
||||||
error.printStackTrace();
|
error.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
store(context, json);
|
store(context, entries);
|
||||||
|
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue