Make app importer path lookup more dynamic

Also fixes a crash in SteamAppImporter that occurred with empty dirs
This commit is contained in:
Alexander Bakker 2019-04-17 01:32:20 +02:00
parent f5cbec21f4
commit ebb9d0be3f
3 changed files with 31 additions and 15 deletions

View file

@ -1,6 +1,9 @@
package com.beemdevelopment.aegis.importers;
import android.content.Context;
import android.content.pm.PackageManager;
import com.topjohnwu.superuser.io.SuFile;
import java.lang.reflect.InvocationTargetException;
import java.util.Collections;
@ -17,10 +20,22 @@ public abstract class DatabaseAppImporter implements DatabaseImporter {
_importers = Collections.unmodifiableMap(importers);
}
private SuFile _path;
private Context _context;
protected DatabaseAppImporter(Context context) {
protected DatabaseAppImporter(Context context, String pkgName, String subPath) throws DatabaseImporterException {
_context = context;
try {
PackageManager man = context.getPackageManager();
_path = new SuFile(man.getApplicationInfo(pkgName, 0).dataDir, subPath);
} catch (PackageManager.NameNotFoundException e) {
throw new DatabaseImporterException(e);
}
}
protected SuFile getPath() {
return _path;
}
public abstract void parse() throws DatabaseImporterException;

View file

@ -1,6 +1,5 @@
package com.beemdevelopment.aegis.importers;
import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
@ -14,7 +13,6 @@ import com.beemdevelopment.aegis.otp.OtpInfo;
import com.beemdevelopment.aegis.otp.OtpInfoException;
import com.beemdevelopment.aegis.otp.TotpInfo;
import com.topjohnwu.superuser.ShellUtils;
import com.topjohnwu.superuser.io.SuFile;
import com.topjohnwu.superuser.io.SuFileInputStream;
import java.io.File;
@ -29,13 +27,13 @@ public class GoogleAuthAppImporter extends DatabaseAppImporter {
private static final int TYPE_TOTP = 0;
private static final int TYPE_HOTP = 1;
@SuppressLint("SdCardPath")
private static final String _filename = "/data/data/com.google.android.apps.authenticator2/databases/databases";
private static final String _subPath = "databases/databases";
private static final String _pkgName = "com.google.android.apps.authenticator2";
private List<Entry> _entries = new ArrayList<>();
public GoogleAuthAppImporter(Context context) {
super(context);
public GoogleAuthAppImporter(Context context) throws DatabaseImporterException {
super(context, _pkgName, _subPath);
}
@Override
@ -45,7 +43,7 @@ public class GoogleAuthAppImporter extends DatabaseAppImporter {
try {
// create a temporary copy of the database so that SQLiteDatabase can open it
file = File.createTempFile("google-import-", "", getContext().getCacheDir());
try (SuFileInputStream in = new SuFileInputStream(new SuFile(_filename))) {
try (SuFileInputStream in = new SuFileInputStream(getPath())) {
try (FileOutputStream out = new FileOutputStream(file)) {
ShellUtils.pump(in, out);
}

View file

@ -1,6 +1,5 @@
package com.beemdevelopment.aegis.importers;
import android.annotation.SuppressLint;
import android.content.Context;
import com.beemdevelopment.aegis.db.DatabaseEntry;
@ -21,19 +20,23 @@ import java.util.ArrayList;
import java.util.List;
public class SteamAppImporter extends DatabaseAppImporter {
@SuppressLint("SdCardPath")
private static final String _path = "/data/data/com.valvesoftware.android.steam.community/files";
private static final String _subDir = "files";
private static final String _pkgName = "com.valvesoftware.android.steam.community";
private List<JSONObject> _objs = new ArrayList<>();
public SteamAppImporter(Context context) {
super(context);
public SteamAppImporter(Context context) throws DatabaseImporterException {
super(context, _pkgName, _subDir);
}
@Override
public void parse() throws DatabaseImporterException {
SuFile dir = new SuFile(_path);
for (SuFile file : dir.listFiles((d, name) -> name.startsWith("Steamguard-"))) {
SuFile[] files = getPath().listFiles((d, name) -> name.startsWith("Steamguard-"));
if (files == null || files.length == 0) {
throw new DatabaseImporterException(String.format("Empty directory: %s", getPath().getAbsolutePath()));
}
for (SuFile file : files) {
try (SuFileInputStream in = new SuFileInputStream(file)) {
try (ByteInputStream stream = ByteInputStream.create(in)) {
JSONObject obj = new JSONObject(new String(stream.getBytes(), StandardCharsets.UTF_8));