WRITE_EXTERNAL_STORAGE at runtime in the backup bin file button

This commit is contained in:
dtvv 2018-09-24 20:38:44 -05:00
parent dae073dc62
commit 0c583fff18
2 changed files with 68 additions and 1 deletions

View file

@ -8,6 +8,7 @@ import android.content.pm.PackageManager;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat; import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
@ -76,6 +77,7 @@ public class BackupsSettingsFragment extends Fragment{
@BindView(R.id.btnBinFile) @BindView(R.id.btnBinFile)
public Button btnBinFile; public Button btnBinFile;
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
@ -103,13 +105,40 @@ public class BackupsSettingsFragment extends Fragment{
public void btnBrainOnClick(){ public void btnBrainOnClick(){
Intent intent = new Intent(getContext(), BackupSeedActivity.class); Intent intent = new Intent(getContext(), BackupSeedActivity.class);
intent.putExtra("SEED_ID",""); intent. putExtra("SEED_ID","");
startActivity(intent); startActivity(intent);
} }
@OnClick(R.id.btnBinFile) @OnClick(R.id.btnBinFile)
public void makeBackupFile(){ 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)) { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
LiveData<GeneralSetting> generalSettingLD = CrystalDatabase.getAppDatabase(getContext()).generalSettingDao().getByName(GeneralSetting.SETTING_PASSWORD); LiveData<GeneralSetting> 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;
}
}
} }

View file

@ -79,6 +79,8 @@
<string name="save">SAVE</string> <string name="save">SAVE</string>
<string name="next">NEXT</string> <string name="next">NEXT</string>
<string name="ASK_PERMISSION">A permission need to be granted before to continue</string>
<string name="please_enter_brainkey">Please enter brainkey</string> <string name="please_enter_brainkey">Please enter brainkey</string>
<string name="please_enter_correct_brainkey">Please enter correct brainkey</string> <string name="please_enter_correct_brainkey">Please enter correct brainkey</string>
<string name="balances">Balances</string> <string name="balances">Balances</string>