From ef18863c7ce9b48b3673d8d2231d4d297e8fb751 Mon Sep 17 00:00:00 2001 From: Javier Varona Date: Fri, 30 Mar 2018 20:59:01 -0400 Subject: [PATCH] - Now Crystal can make file backups --- app/src/main/AndroidManifest.xml | 2 + .../fragments/BackupsSettingsFragment.java | 47 +++++++++++++------ .../manager/FileBackupManager.java | 5 ++ .../service/CrystalWalletService.java | 9 ++++ 4 files changed, 48 insertions(+), 15 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2fc09fc..0de6700 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -4,6 +4,8 @@ + + generalSettingLD = CrystalDatabase.getAppDatabase(getContext()).generalSettingDao().getByName(GeneralSetting.SETTING_PASSWORD); + + generalSettingLD.observe(this, new Observer() { @Override - public void onCarryOut() { - if (backupFileRequest.getStatus() == CreateBackupRequest.StatusCode.SUCCEEDED){ - Toast toast = Toast.makeText( - getContext(), "Backup done! File: "+backupFileRequest.getFilePath(), Toast.LENGTH_LONG); - toast.show(); - } else if (backupFileRequest.getStatus() == CreateBackupRequest.StatusCode.FAILED){ - Toast toast = Toast.makeText( - getContext(), "An error ocurred while making the backup!", Toast.LENGTH_LONG); - toast.show(); + public void onChanged(@Nullable GeneralSetting generalSetting) { + String password = ""; + if (generalSetting != null) { + password = generalSetting.getValue(); } + + final CreateBackupRequest backupFileRequest = new CreateBackupRequest(getContext(), password); + + backupFileRequest.setListener(new FileServiceRequestListener() { + @Override + public void onCarryOut() { + if (backupFileRequest.getStatus() == CreateBackupRequest.StatusCode.SUCCEEDED){ + Toast toast = Toast.makeText( + getContext(), "Backup done! File: "+backupFileRequest.getFilePath(), Toast.LENGTH_LONG); + toast.show(); + } else if (backupFileRequest.getStatus() == CreateBackupRequest.StatusCode.FAILED){ + Toast toast = Toast.makeText( + getContext(), "An error ocurred while making the backup!", Toast.LENGTH_LONG); + toast.show(); + } + } + }); + + FileServiceRequests.getInstance().addRequest(backupFileRequest); } }); - - FileServiceRequests.getInstance().addRequest(backupFileRequest); } } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/manager/FileBackupManager.java b/app/src/main/java/cy/agorise/crystalwallet/manager/FileBackupManager.java index ccd1ec3..b423df3 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/manager/FileBackupManager.java +++ b/app/src/main/java/cy/agorise/crystalwallet/manager/FileBackupManager.java @@ -100,6 +100,11 @@ public class FileBackupManager implements FileServiceRequestsListener { String folder = Environment.getExternalStorageDirectory() + File.separator + "Crystal"; //TODO make constant String path = folder + File.separator + fileName + dateHourString +".bin"; + File folderFile = new File(folder); + if (!folderFile.exists()) { + folderFile.mkdir(); + } + boolean success = saveBinFile(path,content,request); if(success) { request.setFilePath(path); diff --git a/app/src/main/java/cy/agorise/crystalwallet/service/CrystalWalletService.java b/app/src/main/java/cy/agorise/crystalwallet/service/CrystalWalletService.java index 2c2aeca..1fb1442 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/service/CrystalWalletService.java +++ b/app/src/main/java/cy/agorise/crystalwallet/service/CrystalWalletService.java @@ -15,6 +15,7 @@ import android.util.Log; import java.util.ArrayList; import java.util.List; +import cy.agorise.crystalwallet.manager.FileBackupManager; import cy.agorise.crystalwallet.requestmanagers.CryptoNetInfoRequests; import cy.agorise.crystalwallet.dao.CrystalDatabase; import cy.agorise.crystalwallet.enums.CryptoNet; @@ -26,6 +27,8 @@ import cy.agorise.crystalwallet.models.CryptoNetAccount; import cy.agorise.crystalwallet.models.GeneralSetting; import cy.agorise.crystalwallet.models.GrapheneAccount; import cy.agorise.crystalwallet.models.GrapheneAccountInfo; +import cy.agorise.crystalwallet.requestmanagers.FileServiceRequest; +import cy.agorise.crystalwallet.requestmanagers.FileServiceRequests; /** * Created by Henry Varona on 3/10/2017. @@ -42,6 +45,8 @@ public class CrystalWalletService extends LifecycleService { private boolean keepLoadingAccountTransactions; private boolean keepLoadingEquivalences; private CryptoNetInfoRequests cryptoNetInfoRequests; + private FileBackupManager fileBackupManager; + private FileServiceRequests fileServiceRequests; // Handler that receives messages from the thread private final class ServiceHandler extends Handler { @@ -160,11 +165,15 @@ public class CrystalWalletService extends LifecycleService { super.onCreate(); //Creates a instance for the cryptoNetInfoRequest and the managers this.cryptoNetInfoRequests = CryptoNetInfoRequests.getInstance(); + this.fileServiceRequests = FileServiceRequests.getInstance(); this.bitsharesAccountManager = new BitsharesAccountManager(); + this.fileBackupManager = new FileBackupManager(); //Add the managers as listeners of the CryptoNetInfoRequest so //they can carry out the info requests from the ui this.cryptoNetInfoRequests.addListener(this.bitsharesAccountManager); + + this.fileServiceRequests.addListener(this.fileBackupManager); } @Override