Skip to content

Commit

Permalink
Start recording after storage permissions are granted (#783)
Browse files Browse the repository at this point in the history
Previously nothing happened after permissions were granted.
  • Loading branch information
werman authored Apr 5, 2020
1 parent b0b28e7 commit 6bc56d9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ public class ActivityMain extends AppCompatActivity implements SearchView.OnQuer

public static final int PERM_REQ_STORAGE_FAV_SAVE = 1;
public static final int PERM_REQ_STORAGE_FAV_LOAD = 2;
public static final int PERM_REQ_STORAGE_RECORD = 3;

private SearchView mSearchView;

Expand Down Expand Up @@ -421,23 +420,9 @@ public void onRequestPermissionsResult(int requestCode,
Log.d(TAG, "on request permissions result:" + requestCode);
}

super.onRequestPermissionsResult(requestCode, permissions, grantResults);

switch (requestCode) {
case PERM_REQ_STORAGE_RECORD: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Fragment currentFragment = mFragmentManager.getFragments().get(mFragmentManager.getFragments().size() - 1);
if (currentFragment instanceof IFragmentRefreshable) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "REFRESH VIEW");
}
((IFragmentRefreshable) currentFragment).Refresh();
}
} else {
Toast toast = Toast.makeText(this, getResources().getString(R.string.error_record_needs_write), Toast.LENGTH_SHORT);
toast.show();
}
return;
}
case PERM_REQ_STORAGE_FAV_LOAD: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
LoadFavourites();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
public class FragmentPlayerFull extends Fragment {
private final String TAG = "FragmentPlayerFull";

private final static int PERM_REQ_STORAGE_RECORD = 1001;

/**
* Fragment may be a part of another view which could be dragged/scrolled
* and certain hacks may require the fragment to request them to stop
Expand Down Expand Up @@ -325,7 +327,7 @@ public void onClick(View view) {
if (PlayerServiceUtil.isRecording()) {
PlayerServiceUtil.stopRecording();
} else {
if (Utils.verifyStoragePermissions(getActivity(), ActivityMain.PERM_REQ_STORAGE_RECORD)) {
if (Utils.verifyStoragePermissions(FragmentPlayerFull.this, PERM_REQ_STORAGE_RECORD)) {
PlayerServiceUtil.startRecording();
}
}
Expand Down Expand Up @@ -844,7 +846,7 @@ protected void run(FragmentPlayerFull fragmentPlayerFull) {
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions, @NonNull int[] grantResults) {
// If request is cancelled, the result arrays are empty.
if (requestCode == ActivityMain.PERM_REQ_STORAGE_RECORD) {
if (requestCode == PERM_REQ_STORAGE_RECORD) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
storagePermissionsDenied = false;
PlayerServiceUtil.startRecording();
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/net/programmierecke/radiodroid2/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,19 @@ public static boolean verifyStoragePermissions(Activity activity, int request_id
return true;
}

public static boolean verifyStoragePermissions(Fragment fragment, int request_id) {
// Check if we have write permission
int permission = ContextCompat.checkSelfPermission(fragment.requireContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE);

if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
fragment.requestPermissions(PERMISSIONS_STORAGE, request_id);
return false;
}

return true;
}

public static String getReadableBytes(double bytes) {
String[] str = new String[]{"B", "KB", "MB", "GB", "TB"};
for (String aStr : str) {
Expand Down

0 comments on commit 6bc56d9

Please sign in to comment.