- Making Backup to work
This commit is contained in:
parent
6c5fd9de37
commit
9e5a4a2fcc
4 changed files with 85 additions and 17 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<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 path = folder + File.separator + _accountName + ".bin";
|
||||
String path = folder + File.separator + _accountName + dateHourString +".bin";
|
||||
|
||||
boolean success = saveBinFile(path,content,request);
|
||||
//TODO handle sucess
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in a new issue