- Making Backup to work

This commit is contained in:
Javier Varona 2018-03-28 19:09:15 -04:00
parent 6c5fd9de37
commit 9e5a4a2fcc
4 changed files with 85 additions and 17 deletions

View file

@ -1,17 +1,30 @@
package cy.agorise.crystalwallet.fragments; package cy.agorise.crystalwallet.fragments;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.text.Spannable; import android.text.Spannable;
import android.text.SpannableStringBuilder; import android.text.SpannableStringBuilder;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView; 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.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import butterknife.OnClick;
import cy.agorise.crystalwallet.R; 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. * Created by xd on 1/11/18.
@ -38,6 +51,9 @@ public class BackupsSettingsFragment extends Fragment{
@BindView(R.id.tvWIFKey) @BindView(R.id.tvWIFKey)
public TextView tvWIFKey; public TextView tvWIFKey;
@BindView(R.id.btnBinFile)
public Button btnBinFile;
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
@ -59,4 +75,29 @@ public class BackupsSettingsFragment extends Fragment{
return ssb; 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);
}
}
} }

View file

@ -7,7 +7,9 @@ import android.util.Log;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.List; import java.util.List;
import cy.agorise.crystalwallet.application.CrystalApplication; import cy.agorise.crystalwallet.application.CrystalApplication;
@ -85,8 +87,11 @@ public class FileBackupManager implements FileServiceRequestsListener {
static void saveBinContentToFile(List<Integer> content, String _accountName, CreateBackupRequest request ) static void saveBinContentToFile(List<Integer> 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 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); boolean success = saveBinFile(path,content,request);
//TODO handle sucess //TODO handle sucess

View file

@ -11,21 +11,43 @@ import cy.agorise.crystalwallet.models.AccountSeed;
public class CreateBackupRequest extends FileServiceRequest { public class CreateBackupRequest extends FileServiceRequest {
private AccountSeed seed; public enum StatusCode{
NOT_STARTED,
enum StatusCode{ SUCCEEDED,
FAILED
} }
public CreateBackupRequest(Context context, Activity activity, AccountSeed seed) { private String filePath;
super(context, activity); private StatusCode status;
this.seed = seed;
public CreateBackupRequest(Context context) {
super(context);
this.filePath = "";
this.status = StatusCode.NOT_STARTED;
} }
public AccountSeed getSeed() { public void setFilePath(String filePath){
return seed; 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();
}
} }

View file

@ -10,11 +10,11 @@ import android.content.Context;
public abstract class FileServiceRequest { public abstract class FileServiceRequest {
protected Context context; 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.context = context;
this.activity = activity; //this.activity = activity;
} }
protected FileServiceRequestListener listener; protected FileServiceRequestListener listener;
@ -27,9 +27,9 @@ public abstract class FileServiceRequest {
return context; return context;
} }
public Activity getActivity() { /*public Activity getActivity() {
return activity; return activity;
} }*/
protected void _fireOnCarryOutEvent(){ protected void _fireOnCarryOutEvent(){
listener.onCarryOut(); listener.onCarryOut();