Stop using dependency injection in AegisBackupAgent

Because the app launches in a restricted mode when restoring a backup,
dependency injection with Dagger Hilt doesn't work inside BackupAgent. This
would cause backup restore operations to fail. This issue got introduced by the
recent switch to Dagger Hilt, which has not been included in a release of Aegis
yet.
This commit is contained in:
Alexander Bakker 2022-03-16 14:43:13 +01:00
parent 71eb6b133c
commit 903948d57c
3 changed files with 11 additions and 26 deletions

View file

@ -9,7 +9,6 @@ import android.os.ParcelFileDescriptor;
import android.util.Log;
import com.beemdevelopment.aegis.util.IOUtils;
import com.beemdevelopment.aegis.vault.VaultManager;
import com.beemdevelopment.aegis.vault.VaultRepository;
import java.io.File;
@ -17,24 +16,17 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import dagger.hilt.InstallIn;
import dagger.hilt.android.EarlyEntryPoint;
import dagger.hilt.android.EarlyEntryPoints;
import dagger.hilt.components.SingletonComponent;
public class AegisBackupAgent extends BackupAgent {
private static final String TAG = AegisBackupAgent.class.getSimpleName();
private VaultManager _vaultManager;
private Preferences _prefs;
@Override
public void onCreate() {
super.onCreate();
EntryPoint entryPoint = EarlyEntryPoints.get(this, EntryPoint.class);
_vaultManager = entryPoint.getVaultManager();
_prefs = entryPoint.getPreferences();
// cannot use injection with Dagger Hilt here, because the app is launched in a restricted mode on restore
_prefs = new Preferences(this);
}
@Override
@ -57,7 +49,7 @@ public class AegisBackupAgent extends BackupAgent {
createBackupDir();
File vaultBackupFile = getVaultBackupFile();
try {
_vaultManager.getVault().backupTo(vaultBackupFile);
VaultRepository.copyFileTo(this, vaultBackupFile);
} catch (IOException e) {
Log.e(TAG, String.format("onFullBackup() failed: %s", e));
deleteBackupDir();
@ -128,11 +120,4 @@ public class AegisBackupAgent extends BackupAgent {
private File getVaultBackupFile() {
return new File(new File(getFilesDir(), "backup"), VaultRepository.FILENAME);
}
@EarlyEntryPoint
@InstallIn(SingletonComponent.class)
interface EntryPoint {
Preferences getPreferences();
VaultManager getVaultManager();
}
}

View file

@ -189,7 +189,7 @@ public class VaultManager {
}
File tempFile = File.createTempFile(VaultBackupManager.FILENAME_PREFIX, ".json", dir);
getVault().backupTo(tempFile);
VaultRepository.copyFileTo(_context, tempFile);
_backups.scheduleBackup(tempFile, _prefs.getBackupsLocation(), _prefs.getBackupsVersionCount());
} catch (IOException e) {
throw new VaultRepositoryException(e);

View file

@ -107,6 +107,13 @@ public class VaultRepository {
return new VaultRepository(context, vault, creds);
}
public static void copyFileTo(Context context, File destFile) throws IOException {
try (InputStream inStream = VaultRepository.getAtomicFile(context).openRead();
OutputStream outStream = new FileOutputStream(destFile)) {
IOUtils.copy(inStream, outStream);
}
}
void save() throws VaultRepositoryException {
try {
JSONObject obj = _vault.toJson();
@ -172,13 +179,6 @@ public class VaultRepository {
}
}
public void backupTo(File destFile) throws IOException {
try (InputStream inStream = getAtomicFile(_context).openRead();
OutputStream outStream = new FileOutputStream(destFile)) {
IOUtils.copy(inStream, outStream);
}
}
public void addEntry(VaultEntry entry) {
_vault.getEntries().add(entry);
}