From 9e5a4a2fccff0fef1c5ae0c88fe06a27a480de74 Mon Sep 17 00:00:00 2001 From: Javier Varona Date: Wed, 28 Mar 2018 19:09:15 -0400 Subject: [PATCH] - Making Backup to work --- .../fragments/BackupsSettingsFragment.java | 41 +++++++++++++++++ .../manager/FileBackupManager.java | 7 ++- .../requestmanagers/CreateBackupRequest.java | 44 ++++++++++++++----- .../requestmanagers/FileServiceRequest.java | 10 ++--- 4 files changed, 85 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/cy/agorise/crystalwallet/fragments/BackupsSettingsFragment.java b/app/src/main/java/cy/agorise/crystalwallet/fragments/BackupsSettingsFragment.java index 7d8349e..e7b18c2 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/fragments/BackupsSettingsFragment.java +++ b/app/src/main/java/cy/agorise/crystalwallet/fragments/BackupsSettingsFragment.java @@ -1,17 +1,30 @@ package cy.agorise.crystalwallet.fragments; import android.os.Bundle; +import android.os.Environment; import android.support.v4.app.Fragment; import android.text.Spannable; import android.text.SpannableStringBuilder; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.Button; import android.widget.TextView; +import android.widget.Toast; + +import java.io.File; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; import butterknife.BindView; import butterknife.ButterKnife; +import butterknife.OnClick; import cy.agorise.crystalwallet.R; +import cy.agorise.crystalwallet.requestmanagers.CreateBackupRequest; +import cy.agorise.crystalwallet.requestmanagers.FileServiceRequest; +import cy.agorise.crystalwallet.requestmanagers.FileServiceRequestListener; +import cy.agorise.crystalwallet.requestmanagers.FileServiceRequests; /** * Created by xd on 1/11/18. @@ -38,6 +51,9 @@ public class BackupsSettingsFragment extends Fragment{ @BindView(R.id.tvWIFKey) public TextView tvWIFKey; + @BindView(R.id.btnBinFile) + public Button btnBinFile; + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -59,4 +75,29 @@ public class BackupsSettingsFragment extends Fragment{ return ssb; } + + @OnClick(R.id.btnBinFile) + public void makeBackupFile(){ + if (Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED) { + + final CreateBackupRequest backupFileRequest = new CreateBackupRequest(getContext()); + + 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); + } + } } 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 67ff6d3..44af56a 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/manager/FileBackupManager.java +++ b/app/src/main/java/cy/agorise/crystalwallet/manager/FileBackupManager.java @@ -7,7 +7,9 @@ import android.util.Log; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Date; import java.util.List; import cy.agorise.crystalwallet.application.CrystalApplication; @@ -85,8 +87,11 @@ public class FileBackupManager implements FileServiceRequestsListener { static void saveBinContentToFile(List content, String _accountName, CreateBackupRequest request ) { + SimpleDateFormat df = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss"); + String dateHourString = df.format(new Date()); + String folder = Environment.getExternalStorageDirectory() + File.separator + "Crystal"; //TODO make constant - String path = folder + File.separator + _accountName + ".bin"; + String path = folder + File.separator + _accountName + dateHourString +".bin"; boolean success = saveBinFile(path,content,request); //TODO handle sucess diff --git a/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/CreateBackupRequest.java b/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/CreateBackupRequest.java index 28942b4..e105e97 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/CreateBackupRequest.java +++ b/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/CreateBackupRequest.java @@ -11,21 +11,43 @@ import cy.agorise.crystalwallet.models.AccountSeed; public class CreateBackupRequest extends FileServiceRequest { - private AccountSeed seed; - - enum StatusCode{ - - - + public enum StatusCode{ + NOT_STARTED, + SUCCEEDED, + FAILED } - public CreateBackupRequest(Context context, Activity activity, AccountSeed seed) { - super(context, activity); - this.seed = seed; + private String filePath; + private StatusCode status; + + public CreateBackupRequest(Context context) { + super(context); + this.filePath = ""; + this.status = StatusCode.NOT_STARTED; } - public AccountSeed getSeed() { - return seed; + public void setFilePath(String filePath){ + this.filePath = filePath; + this.validate(); + } + + public void setStatus(StatusCode statusCode){ + this.status = statusCode; + this.validate(); + } + + public StatusCode getStatus(){ + return this.status; + } + + public String getFilePath() { + return this.filePath; + } + + public void validate(){ + if (this.status != StatusCode.NOT_STARTED){ + this._fireOnCarryOutEvent(); + } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/FileServiceRequest.java b/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/FileServiceRequest.java index b6c0af3..4e54582 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/FileServiceRequest.java +++ b/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/FileServiceRequest.java @@ -10,11 +10,11 @@ import android.content.Context; public abstract class FileServiceRequest { protected Context context; - protected Activity activity; + //protected Activity activity; - protected FileServiceRequest(Context context, Activity activity) { + protected FileServiceRequest(Context context/*, Activity activity*/) { this.context = context; - this.activity = activity; + //this.activity = activity; } protected FileServiceRequestListener listener; @@ -27,9 +27,9 @@ public abstract class FileServiceRequest { return context; } - public Activity getActivity() { + /*public Activity getActivity() { return activity; - } + }*/ protected void _fireOnCarryOutEvent(){ listener.onCarryOut();