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;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,9 @@ import android.os.Environment;
|
|||
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.dao.CrystalDatabase;
|
||||
|
@ -92,12 +94,15 @@ public class FileBackupManager implements FileServiceRequestsListener {
|
|||
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 path = folder + File.separator + fileName + ".bin";
|
||||
String path = folder + File.separator + fileName + dateHourString +".bin";
|
||||
|
||||
boolean success = saveBinFile(path,content,request);
|
||||
if(success) {
|
||||
request.setStatus(CreateBackupRequest.StatusCode.OK);
|
||||
request.setStatus(CreateBackupRequest.StatusCode.SUCCEEDED);
|
||||
}else{
|
||||
request.setStatus(CreateBackupRequest.StatusCode.FAILED);
|
||||
}
|
||||
|
|
|
@ -12,33 +12,43 @@ import cy.agorise.crystalwallet.models.AccountSeed;
|
|||
public class CreateBackupRequest extends FileServiceRequest {
|
||||
|
||||
public enum StatusCode{
|
||||
PROCESSING(0),
|
||||
OK(1),
|
||||
FAILED(2);
|
||||
NOT_STARTED,
|
||||
SUCCEEDED,
|
||||
FAILED
|
||||
}
|
||||
|
||||
protected long code;
|
||||
private String filePath;
|
||||
private StatusCode status;
|
||||
|
||||
StatusCode(long code) {
|
||||
this.code = code;
|
||||
}
|
||||
public CreateBackupRequest(Context context) {
|
||||
super(context);
|
||||
this.filePath = "";
|
||||
this.status = StatusCode.NOT_STARTED;
|
||||
}
|
||||
|
||||
public long getCode() {
|
||||
return code;
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
private StatusCode status = StatusCode.PROCESSING;
|
||||
|
||||
public CreateBackupRequest(Context context, String password) {
|
||||
super(context,password);
|
||||
}
|
||||
|
||||
public void setStatus(StatusCode status) {
|
||||
this.status = status;
|
||||
this._fireOnCarryOutEvent();
|
||||
}
|
||||
|
||||
public StatusCode getStatus() {
|
||||
return status;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,10 +10,12 @@ import android.content.Context;
|
|||
public abstract class FileServiceRequest {
|
||||
|
||||
protected Context context;
|
||||
//protected Activity activity;
|
||||
protected String password;
|
||||
|
||||
protected FileServiceRequest(Context context, String password) {
|
||||
this.context = context;
|
||||
//this.activity = activity;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue