From 5db6ce2afc7a5592940d8faa247560159e17ca3b Mon Sep 17 00:00:00 2001 From: Javier Varona Date: Tue, 3 Apr 2018 20:50:44 -0400 Subject: [PATCH] - Importing Backup is now working --- app/src/main/AndroidManifest.xml | 2 +- .../ImportAccountOptionsFragment.java | 60 +++++++++++++- .../manager/FileBackupManager.java | 2 + .../requestmanagers/ImportBackupRequest.java | 4 + .../crystalwallet/util/UriTranslator.java | 81 +++++++++++++++++++ 5 files changed, 147 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/cy/agorise/crystalwallet/util/UriTranslator.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 0de6700..3b5bc5e 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -17,7 +17,7 @@ android:configChanges="locale" android:theme="@style/AppTheme"> diff --git a/app/src/main/java/cy/agorise/crystalwallet/fragments/ImportAccountOptionsFragment.java b/app/src/main/java/cy/agorise/crystalwallet/fragments/ImportAccountOptionsFragment.java index d676d4c..17db27e 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/fragments/ImportAccountOptionsFragment.java +++ b/app/src/main/java/cy/agorise/crystalwallet/fragments/ImportAccountOptionsFragment.java @@ -1,21 +1,29 @@ package cy.agorise.crystalwallet.fragments; import android.app.Dialog; +import android.content.Intent; +import android.net.Uri; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.app.DialogFragment; -import android.support.v4.app.Fragment; import android.support.v7.app.AlertDialog; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.Button; +import android.widget.Toast; + +import java.net.URISyntaxException; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; import cy.agorise.crystalwallet.R; +import cy.agorise.crystalwallet.requestmanagers.FileServiceRequestListener; +import cy.agorise.crystalwallet.requestmanagers.FileServiceRequests; +import cy.agorise.crystalwallet.requestmanagers.ImportBackupRequest; +import cy.agorise.crystalwallet.util.UriTranslator; /** * Created by xd on 1/25/18. @@ -24,8 +32,12 @@ import cy.agorise.crystalwallet.R; public class ImportAccountOptionsFragment extends DialogFragment { + public static final int FILE_CONTENT_REQUEST_CODE = 0; + @BindView(R.id.btnCancel) Button btnClose; + @BindView(R.id.btnImportBackup) + Button btnImportBackup; public ImportAccountOptionsFragment() { // Required empty public constructor @@ -69,4 +81,50 @@ public class ImportAccountOptionsFragment extends DialogFragment { public void cancel() { dismiss(); } + + @OnClick (R.id.btnImportBackup) + public void importBackup(){ + Intent fileIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT); + fileIntent.setType("*/*"); + fileIntent.addCategory(Intent.CATEGORY_OPENABLE); + startActivityForResult(fileIntent, FILE_CONTENT_REQUEST_CODE); + + + } + + @Override + public void onActivityResult(int requestCode, int resultCode, Intent data) { + super.onActivityResult(requestCode, resultCode, data); + + if (requestCode == FILE_CONTENT_REQUEST_CODE){ + Uri fileUri = data.getData(); + + String filePath = null; + try { + filePath = UriTranslator.getFilePath(getContext(), fileUri); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + + final ImportBackupRequest importBackupRequest = new ImportBackupRequest(getContext(), "", filePath); + + importBackupRequest.setListener(new FileServiceRequestListener() { + @Override + public void onCarryOut() { + if (importBackupRequest.getStatus() == ImportBackupRequest.StatusCode.SUCCEEDED){ + Toast toast = Toast.makeText( + getContext(), "Backup restored!", Toast.LENGTH_LONG); + toast.show(); + } else if (importBackupRequest.getStatus() == ImportBackupRequest.StatusCode.FAILED){ + Toast toast = Toast.makeText( + getContext(), "An error ocurred while restoring the backup!", Toast.LENGTH_LONG); + toast.show(); + } + } + }); + + FileServiceRequests.getInstance().addRequest(importBackupRequest); + + } + } } 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 de7f6d1..298e177 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/manager/FileBackupManager.java +++ b/app/src/main/java/cy/agorise/crystalwallet/manager/FileBackupManager.java @@ -42,6 +42,8 @@ public class FileBackupManager implements FileServiceRequestsListener { public void onNewRequest(FileServiceRequest request) { if(request instanceof CreateBackupRequest){ createBackupBinFile((CreateBackupRequest) request); + } else if (request instanceof ImportBackupRequest){ + readBinFile((ImportBackupRequest) request); } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/ImportBackupRequest.java b/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/ImportBackupRequest.java index 83fe791..195fbd7 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/ImportBackupRequest.java +++ b/app/src/main/java/cy/agorise/crystalwallet/requestmanagers/ImportBackupRequest.java @@ -33,6 +33,10 @@ public class ImportBackupRequest extends FileServiceRequest { this.validate(); } + public StatusCode getStatus(){ + return this.status; + } + public void validate(){ if (this.status != StatusCode.NOT_STARTED){ this._fireOnCarryOutEvent(); diff --git a/app/src/main/java/cy/agorise/crystalwallet/util/UriTranslator.java b/app/src/main/java/cy/agorise/crystalwallet/util/UriTranslator.java new file mode 100644 index 0000000..8c10a67 --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/util/UriTranslator.java @@ -0,0 +1,81 @@ +package cy.agorise.crystalwallet.util; + +import android.content.ContentUris; +import android.content.Context; +import android.database.Cursor; +import android.net.Uri; +import android.os.Build; +import android.os.Environment; +import android.provider.DocumentsContract; +import android.provider.MediaStore; + +import java.net.URISyntaxException; + +/** + * Created by Henry Varona on 3/4/2018. + */ + +public class UriTranslator { + + public static String getFilePath(Context context, Uri uri) throws URISyntaxException { + String selection = null; + String[] selectionArgs = null; + // Uri is different in versions after KITKAT (Android 4.4), we need to + if (Build.VERSION.SDK_INT >= 19 && DocumentsContract.isDocumentUri(context.getApplicationContext(), uri)) { + if (isExternalStorageDocument(uri)) { + final String docId = DocumentsContract.getDocumentId(uri); + final String[] split = docId.split(":"); + return Environment.getExternalStorageDirectory() + "/" + split[1]; + } else if (isDownloadsDocument(uri)) { + final String id = DocumentsContract.getDocumentId(uri); + uri = ContentUris.withAppendedId( + Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); + } else if (isMediaDocument(uri)) { + final String docId = DocumentsContract.getDocumentId(uri); + final String[] split = docId.split(":"); + final String type = split[0]; + if ("image".equals(type)) { + uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; + } else if ("video".equals(type)) { + uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; + } else if ("audio".equals(type)) { + uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; + } + selection = "_id=?"; + selectionArgs = new String[]{ + split[1] + }; + } + } + if ("content".equalsIgnoreCase(uri.getScheme())) { + String[] projection = { + MediaStore.Images.Media.DATA + }; + Cursor cursor = null; + try { + cursor = context.getContentResolver() + .query(uri, projection, selection, selectionArgs, null); + int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); + if (cursor.moveToFirst()) { + return cursor.getString(column_index); + } + } catch (Exception e) { + } + } else if ("file".equalsIgnoreCase(uri.getScheme())) { + return uri.getPath(); + } + return null; + } + + public static boolean isExternalStorageDocument(Uri uri) { + return "com.android.externalstorage.documents".equals(uri.getAuthority()); + } + + public static boolean isDownloadsDocument(Uri uri) { + return "com.android.providers.downloads.documents".equals(uri.getAuthority()); + } + + public static boolean isMediaDocument(Uri uri) { + return "com.android.providers.media.documents".equals(uri.getAuthority()); + } +}