mirror of
https://github.com/beemdevelopment/Aegis.git
synced 2025-05-14 14:02:49 +00:00
Rework the import code a bit
This commit is contained in:
parent
df5a815e57
commit
41846f9114
6 changed files with 101 additions and 44 deletions
|
@ -25,17 +25,20 @@ import android.view.View;
|
|||
import android.widget.LinearLayout;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.UndeclaredThrowableException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import me.impy.aegis.crypto.MasterKey;
|
||||
import me.impy.aegis.db.DatabaseEntry;
|
||||
import me.impy.aegis.db.DatabaseManager;
|
||||
import me.impy.aegis.ext.FreeOTPImporter;
|
||||
import me.impy.aegis.ext.DatabaseImporter;
|
||||
import me.impy.aegis.helpers.SimpleItemTouchHelperCallback;
|
||||
import me.impy.aegis.util.ByteInputStream;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
private static final int CODE_GET_KEYINFO = 0;
|
||||
|
@ -49,7 +52,6 @@ public class MainActivity extends AppCompatActivity {
|
|||
private DatabaseManager _db;
|
||||
|
||||
private boolean _nightMode = false;
|
||||
|
||||
private Menu _menu;
|
||||
|
||||
@Override
|
||||
|
@ -145,28 +147,51 @@ public class MainActivity extends AppCompatActivity {
|
|||
return;
|
||||
}
|
||||
|
||||
InputStream stream = null;
|
||||
InputStream fileStream = null;
|
||||
try {
|
||||
try {
|
||||
stream = getContentResolver().openInputStream(data.getData());
|
||||
fileStream = getContentResolver().openInputStream(data.getData());
|
||||
} catch (Exception e) {
|
||||
Toast.makeText(this, "An error occurred while trying to open the file", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
FreeOTPImporter importer = new FreeOTPImporter(stream);
|
||||
ByteInputStream stream;
|
||||
try {
|
||||
for (DatabaseEntry profile : importer.convert()) {
|
||||
addKey(new KeyProfile(profile));
|
||||
int read;
|
||||
byte[] buf = new byte[4096];
|
||||
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
|
||||
while ((read = fileStream.read(buf, 0, buf.length)) != -1) {
|
||||
outStream.write(buf, 0, read);
|
||||
}
|
||||
stream = new ByteInputStream(outStream.toByteArray());
|
||||
} catch (Exception e) {
|
||||
Toast.makeText(this, "An error occurred while trying to read the file", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
List<DatabaseEntry> entries = null;
|
||||
for (DatabaseImporter converter : DatabaseImporter.create(stream)) {
|
||||
try {
|
||||
entries = converter.convert();
|
||||
break;
|
||||
} catch (Exception e) {
|
||||
stream.reset();
|
||||
}
|
||||
}
|
||||
|
||||
if (entries == null) {
|
||||
Toast.makeText(this, "An error occurred while trying to parse the file", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
for (DatabaseEntry entry : entries) {
|
||||
addKey(new KeyProfile(entry));
|
||||
}
|
||||
} finally {
|
||||
if (stream != null) {
|
||||
if (fileStream != null) {
|
||||
try {
|
||||
stream.close();
|
||||
fileStream.close();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,30 +1,27 @@
|
|||
package me.impy.aegis.ext;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
||||
import me.impy.aegis.db.Database;
|
||||
import me.impy.aegis.db.DatabaseEntry;
|
||||
import me.impy.aegis.util.ByteInputStream;
|
||||
|
||||
public class AegisImporter extends KeyConverter {
|
||||
public class AegisImporter extends DatabaseImporter {
|
||||
|
||||
public AegisImporter(InputStream stream) {
|
||||
public AegisImporter(ByteInputStream stream) {
|
||||
super(stream);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<DatabaseEntry> convert() throws Exception {
|
||||
int read;
|
||||
byte[] buffer = new byte[4096];
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
while ((read = _stream.read(buffer, 0, buffer.length)) != -1) {
|
||||
stream.write(buffer, 0, read);
|
||||
}
|
||||
|
||||
byte[] bytes = stream.toByteArray();
|
||||
byte[] bytes = _stream.getBytes();
|
||||
Database db = new Database();
|
||||
db.deserialize(bytes);
|
||||
return db.getKeys();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Aegis";
|
||||
}
|
||||
}
|
||||
|
|
41
app/src/main/java/me/impy/aegis/ext/DatabaseImporter.java
Normal file
41
app/src/main/java/me/impy/aegis/ext/DatabaseImporter.java
Normal file
|
@ -0,0 +1,41 @@
|
|||
package me.impy.aegis.ext;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import me.impy.aegis.db.DatabaseEntry;
|
||||
import me.impy.aegis.util.ByteInputStream;
|
||||
|
||||
public abstract class DatabaseImporter {
|
||||
private static List<Class<? extends DatabaseImporter>> _converters = Collections.unmodifiableList(
|
||||
new ArrayList<>(Arrays.asList(AegisImporter.class, FreeOTPImporter.class))
|
||||
);
|
||||
|
||||
protected ByteInputStream _stream;
|
||||
|
||||
protected DatabaseImporter(ByteInputStream stream) {
|
||||
_stream = stream;
|
||||
}
|
||||
|
||||
public abstract List<DatabaseEntry> convert() throws Exception;
|
||||
|
||||
public abstract String getName();
|
||||
|
||||
public static DatabaseImporter create(ByteInputStream stream, Class<? extends DatabaseImporter> type) {
|
||||
try {
|
||||
return type.getConstructor(ByteInputStream.class).newInstance(stream);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static List<DatabaseImporter> create(ByteInputStream stream) {
|
||||
List<DatabaseImporter> list = new ArrayList<>();
|
||||
for (Class<? extends DatabaseImporter> type : _converters) {
|
||||
list.add(create(stream, type));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
|
@ -9,15 +9,15 @@ import org.xmlpull.v1.XmlPullParser;
|
|||
import org.xmlpull.v1.XmlPullParserException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import me.impy.aegis.crypto.KeyInfo;
|
||||
import me.impy.aegis.db.DatabaseEntry;
|
||||
import me.impy.aegis.util.ByteInputStream;
|
||||
|
||||
public class FreeOTPImporter extends KeyConverter {
|
||||
public FreeOTPImporter(InputStream stream) {
|
||||
public class FreeOTPImporter extends DatabaseImporter {
|
||||
public FreeOTPImporter(ByteInputStream stream) {
|
||||
super(stream);
|
||||
}
|
||||
|
||||
|
@ -35,6 +35,11 @@ public class FreeOTPImporter extends KeyConverter {
|
|||
return parse(parser);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "FreeOTP";
|
||||
}
|
||||
|
||||
private static List<DatabaseEntry> parse(XmlPullParser parser) throws IOException, XmlPullParserException, JSONException {
|
||||
List<Entry> entries = new ArrayList<>();
|
||||
|
||||
|
|
|
@ -1,16 +0,0 @@
|
|||
package me.impy.aegis.ext;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
||||
import me.impy.aegis.db.DatabaseEntry;
|
||||
|
||||
public abstract class KeyConverter {
|
||||
protected InputStream _stream;
|
||||
|
||||
public KeyConverter(InputStream stream) {
|
||||
_stream = stream;
|
||||
}
|
||||
|
||||
public abstract List<DatabaseEntry> convert() throws Exception;
|
||||
}
|
|
@ -1,8 +1,13 @@
|
|||
package me.impy.aegis.util;
|
||||
|
||||
/**
|
||||
* Created by alex on 12/3/17.
|
||||
*/
|
||||
import java.io.ByteArrayInputStream;
|
||||
|
||||
public class ByteInputStream {
|
||||
public class ByteInputStream extends ByteArrayInputStream {
|
||||
public ByteInputStream(byte[] buf) {
|
||||
super(buf);
|
||||
}
|
||||
|
||||
public byte[] getBytes() {
|
||||
return this.buf;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue