Merge remote-tracking branch 'origin/master'
# Conflicts: # app/src/main/java/cy/agorise/crystalwallet/manager/FileBackupManager.java # app/src/main/java/cy/agorise/crystalwallet/requestmanagers/CreateBackupRequest.java # app/src/main/java/cy/agorise/crystalwallet/requestmanagers/FileServiceRequest.java
This commit is contained in:
commit
3efbcbb9f4
4 changed files with 82 additions and 24 deletions
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,9 @@ import android.os.Environment;
|
||||||
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.dao.CrystalDatabase;
|
import cy.agorise.crystalwallet.dao.CrystalDatabase;
|
||||||
|
@ -92,12 +94,15 @@ public class FileBackupManager implements FileServiceRequestsListener {
|
||||||
static void saveBinContentToFile(List<Integer> content, String fileName, CreateBackupRequest request )
|
static void saveBinContentToFile(List<Integer> content, String fileName, 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 + fileName + ".bin";
|
String path = folder + File.separator + fileName + dateHourString +".bin";
|
||||||
|
|
||||||
boolean success = saveBinFile(path,content,request);
|
boolean success = saveBinFile(path,content,request);
|
||||||
if(success) {
|
if(success) {
|
||||||
request.setStatus(CreateBackupRequest.StatusCode.OK);
|
request.setStatus(CreateBackupRequest.StatusCode.SUCCEEDED);
|
||||||
}else{
|
}else{
|
||||||
request.setStatus(CreateBackupRequest.StatusCode.FAILED);
|
request.setStatus(CreateBackupRequest.StatusCode.FAILED);
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,33 +12,43 @@ import cy.agorise.crystalwallet.models.AccountSeed;
|
||||||
public class CreateBackupRequest extends FileServiceRequest {
|
public class CreateBackupRequest extends FileServiceRequest {
|
||||||
|
|
||||||
public enum StatusCode{
|
public enum StatusCode{
|
||||||
PROCESSING(0),
|
NOT_STARTED,
|
||||||
OK(1),
|
SUCCEEDED,
|
||||||
FAILED(2);
|
FAILED
|
||||||
|
|
||||||
protected long code;
|
|
||||||
|
|
||||||
StatusCode(long code) {
|
|
||||||
this.code = code;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getCode() {
|
private String filePath;
|
||||||
return code;
|
private StatusCode status;
|
||||||
}
|
|
||||||
|
public CreateBackupRequest(Context context) {
|
||||||
|
super(context);
|
||||||
|
this.filePath = "";
|
||||||
|
this.status = StatusCode.NOT_STARTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
private StatusCode status = StatusCode.PROCESSING;
|
public void setFilePath(String filePath){
|
||||||
|
this.filePath = filePath;
|
||||||
public CreateBackupRequest(Context context, String password) {
|
this.validate();
|
||||||
super(context,password);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setStatus(StatusCode status) {
|
public void setStatus(StatusCode statusCode){
|
||||||
this.status = status;
|
this.status = statusCode;
|
||||||
this._fireOnCarryOutEvent();
|
this.validate();
|
||||||
}
|
}
|
||||||
|
|
||||||
public StatusCode getStatus(){
|
public StatusCode getStatus(){
|
||||||
return status;
|
return this.status;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFilePath() {
|
||||||
|
return this.filePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void validate(){
|
||||||
|
if (this.status != StatusCode.NOT_STARTED){
|
||||||
|
this._fireOnCarryOutEvent();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -10,10 +10,12 @@ import android.content.Context;
|
||||||
public abstract class FileServiceRequest {
|
public abstract class FileServiceRequest {
|
||||||
|
|
||||||
protected Context context;
|
protected Context context;
|
||||||
|
//protected Activity activity;
|
||||||
protected String password;
|
protected String password;
|
||||||
|
|
||||||
protected FileServiceRequest(Context context, String password) {
|
protected FileServiceRequest(Context context, String password) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
|
//this.activity = activity;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue