From 0c583fff18f55fded0bf0776fcf4788b6e82401d Mon Sep 17 00:00:00 2001 From: dtvv Date: Mon, 24 Sep 2018 20:38:44 -0500 Subject: [PATCH] WRITE_EXTERNAL_STORAGE at runtime in the backup bin file button --- .../fragments/BackupsSettingsFragment.java | 67 ++++++++++++++++++- app/src/main/res/values/strings.xml | 2 + 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/cy/agorise/crystalwallet/fragments/BackupsSettingsFragment.java b/app/src/main/java/cy/agorise/crystalwallet/fragments/BackupsSettingsFragment.java index c4b9e50..0b57370 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/fragments/BackupsSettingsFragment.java +++ b/app/src/main/java/cy/agorise/crystalwallet/fragments/BackupsSettingsFragment.java @@ -8,6 +8,7 @@ import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; import android.os.Environment; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.ActivityCompat; import android.support.v4.app.Fragment; @@ -76,6 +77,7 @@ public class BackupsSettingsFragment extends Fragment{ @BindView(R.id.btnBinFile) public Button btnBinFile; + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -103,13 +105,40 @@ public class BackupsSettingsFragment extends Fragment{ public void btnBrainOnClick(){ Intent intent = new Intent(getContext(), BackupSeedActivity.class); - intent.putExtra("SEED_ID",""); + intent. putExtra("SEED_ID",""); startActivity(intent); } + @OnClick(R.id.btnBinFile) public void makeBackupFile(){ + /* + * Check for WRITE_EXTERNAL_STORAGE permission + * */ + if (Build.VERSION.SDK_INT >= 23) { + if (checkPermission()) { + // Code for above or equal 23 API Oriented Device + // Your Permission granted already .Do next code + + makeBackupfileAfterPermission(); + + } else { + requestPermission(); // Code for permission + } + } + else { + + // Code for Below 23 API Oriented Device + // Do next code + + makeBackupfileAfterPermission(); + } + } + + + private void makeBackupfileAfterPermission(){ + if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { LiveData generalSettingLD = CrystalDatabase.getAppDatabase(getContext()).generalSettingDao().getByName(GeneralSetting.SETTING_PASSWORD); @@ -144,4 +173,40 @@ public class BackupsSettingsFragment extends Fragment{ }); } } + + private boolean checkPermission() { + int result = ContextCompat.checkSelfPermission(getActivity(), android.Manifest.permission.WRITE_EXTERNAL_STORAGE); + if (result == PackageManager.PERMISSION_GRANTED) { + return true; + } else { + return false; + } + } + + private void requestPermission() { + + if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) { + Toast.makeText(getActivity(), "Write External Storage permission allows us to do store images. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show(); + } else { + ActivityCompat.requestPermissions(getActivity(), new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE); + } + } + + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + + switch (requestCode) { + case PERMISSION_REQUEST_CODE: + if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + Log.e("value", "Permission Granted, Now you can use local drive ."); + + } else { + Log.e("value", "Permission Denied, You cannot use local drive ."); + + makeBackupfileAfterPermission(); + } + break; + } + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f83ddac..89ba537 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -79,6 +79,8 @@ SAVE NEXT + A permission need to be granted before to continue + Please enter brainkey Please enter correct brainkey Balances