mirror of
https://github.com/beemdevelopment/Aegis.git
synced 2025-05-16 23:12:51 +00:00
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:
parent
71eb6b133c
commit
903948d57c
3 changed files with 11 additions and 26 deletions
|
@ -9,7 +9,6 @@ import android.os.ParcelFileDescriptor;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import com.beemdevelopment.aegis.util.IOUtils;
|
import com.beemdevelopment.aegis.util.IOUtils;
|
||||||
import com.beemdevelopment.aegis.vault.VaultManager;
|
|
||||||
import com.beemdevelopment.aegis.vault.VaultRepository;
|
import com.beemdevelopment.aegis.vault.VaultRepository;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
@ -17,24 +16,17 @@ import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
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 {
|
public class AegisBackupAgent extends BackupAgent {
|
||||||
private static final String TAG = AegisBackupAgent.class.getSimpleName();
|
private static final String TAG = AegisBackupAgent.class.getSimpleName();
|
||||||
|
|
||||||
private VaultManager _vaultManager;
|
|
||||||
private Preferences _prefs;
|
private Preferences _prefs;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
|
|
||||||
EntryPoint entryPoint = EarlyEntryPoints.get(this, EntryPoint.class);
|
// cannot use injection with Dagger Hilt here, because the app is launched in a restricted mode on restore
|
||||||
_vaultManager = entryPoint.getVaultManager();
|
_prefs = new Preferences(this);
|
||||||
_prefs = entryPoint.getPreferences();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -57,7 +49,7 @@ public class AegisBackupAgent extends BackupAgent {
|
||||||
createBackupDir();
|
createBackupDir();
|
||||||
File vaultBackupFile = getVaultBackupFile();
|
File vaultBackupFile = getVaultBackupFile();
|
||||||
try {
|
try {
|
||||||
_vaultManager.getVault().backupTo(vaultBackupFile);
|
VaultRepository.copyFileTo(this, vaultBackupFile);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.e(TAG, String.format("onFullBackup() failed: %s", e));
|
Log.e(TAG, String.format("onFullBackup() failed: %s", e));
|
||||||
deleteBackupDir();
|
deleteBackupDir();
|
||||||
|
@ -128,11 +120,4 @@ public class AegisBackupAgent extends BackupAgent {
|
||||||
private File getVaultBackupFile() {
|
private File getVaultBackupFile() {
|
||||||
return new File(new File(getFilesDir(), "backup"), VaultRepository.FILENAME);
|
return new File(new File(getFilesDir(), "backup"), VaultRepository.FILENAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
@EarlyEntryPoint
|
|
||||||
@InstallIn(SingletonComponent.class)
|
|
||||||
interface EntryPoint {
|
|
||||||
Preferences getPreferences();
|
|
||||||
VaultManager getVaultManager();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -189,7 +189,7 @@ public class VaultManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
File tempFile = File.createTempFile(VaultBackupManager.FILENAME_PREFIX, ".json", dir);
|
File tempFile = File.createTempFile(VaultBackupManager.FILENAME_PREFIX, ".json", dir);
|
||||||
getVault().backupTo(tempFile);
|
VaultRepository.copyFileTo(_context, tempFile);
|
||||||
_backups.scheduleBackup(tempFile, _prefs.getBackupsLocation(), _prefs.getBackupsVersionCount());
|
_backups.scheduleBackup(tempFile, _prefs.getBackupsLocation(), _prefs.getBackupsVersionCount());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new VaultRepositoryException(e);
|
throw new VaultRepositoryException(e);
|
||||||
|
|
|
@ -107,6 +107,13 @@ public class VaultRepository {
|
||||||
return new VaultRepository(context, vault, creds);
|
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 {
|
void save() throws VaultRepositoryException {
|
||||||
try {
|
try {
|
||||||
JSONObject obj = _vault.toJson();
|
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) {
|
public void addEntry(VaultEntry entry) {
|
||||||
_vault.getEntries().add(entry);
|
_vault.getEntries().add(entry);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue