From 676d64a24a3934cff5cf1464b5f2be49dac34b4f Mon Sep 17 00:00:00 2001 From: Aris Poloway Date: Sat, 7 Apr 2018 13:32:02 -0400 Subject: [PATCH] Open download dialog if video download is clicked instead --- .../java/org/schabi/newpipe/MainActivity.java | 21 ++++++++---- .../newpipe/download/DownloadDialog.java | 2 +- .../fragments/detail/VideoDetailFragment.java | 32 +++++++++++-------- .../schabi/newpipe/util/NavigationHelper.java | 2 +- .../schabi/newpipe/util/PermissionHelper.java | 18 +++++------ 5 files changed, 43 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/MainActivity.java b/app/src/main/java/org/schabi/newpipe/MainActivity.java index 4b31a1b1f..1fdb67cf3 100644 --- a/app/src/main/java/org/schabi/newpipe/MainActivity.java +++ b/app/src/main/java/org/schabi/newpipe/MainActivity.java @@ -238,14 +238,21 @@ public class MainActivity extends AppCompatActivity { @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { - if (requestCode == PermissionHelper.PERMISSION_READ_STORAGE || - requestCode == PermissionHelper.PERMISSION_WRITE_STORAGE){ - for (int i: grantResults){ - if (i == PackageManager.PERMISSION_DENIED){ - return; - } + for (int i: grantResults){ + if (i == PackageManager.PERMISSION_DENIED){ + return; } - NavigationHelper.openDownloads(this); + } + switch (requestCode) { + case PermissionHelper.DOWNLOADS_REQUEST_CODE: + NavigationHelper.openDownloads(this); + break; + case PermissionHelper.DOWNLOAD_DIALOG_REQUEST_CODE: + Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_holder); + if (fragment instanceof VideoDetailFragment) { + ((VideoDetailFragment) fragment).openDownloadDialog(); + } + break; } } diff --git a/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java b/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java index 9bcd0bcb7..2934284c5 100644 --- a/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java +++ b/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java @@ -79,7 +79,7 @@ public class DownloadDialog extends DialogFragment implements RadioGroup.OnCheck public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (DEBUG) Log.d(TAG, "onCreate() called with: savedInstanceState = [" + savedInstanceState + "]"); - if (!PermissionHelper.checkStoragePermissions(getActivity())) { + if (!PermissionHelper.checkStoragePermissions(getActivity(), PermissionHelper.DOWNLOAD_DIALOG_REQUEST_CODE)) { getDialog().dismiss(); return; } diff --git a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java index 74e561f99..653ce2082 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java @@ -356,22 +356,10 @@ public class VideoDetailFragment } break; case R.id.detail_controls_download: - if (!PermissionHelper.checkStoragePermissions(activity)) { + if (!PermissionHelper.checkStoragePermissions(activity, PermissionHelper.DOWNLOAD_DIALOG_REQUEST_CODE)) { return; } - - try { - DownloadDialog downloadDialog = - DownloadDialog.newInstance(currentInfo, - sortedStreamVideosList, - selectedVideoStream); - downloadDialog.show(activity.getSupportFragmentManager(), "downloadDialog"); - } catch (Exception e) { - Toast.makeText(activity, - R.string.could_not_setup_download_menu, - Toast.LENGTH_LONG).show(); - e.printStackTrace(); - } + this.openDownloadDialog(); break; case R.id.detail_uploader_root_layout: if (TextUtils.isEmpty(currentInfo.getUploaderUrl())) { @@ -1226,6 +1214,22 @@ public class VideoDetailFragment } } + + public void openDownloadDialog() { + try { + DownloadDialog downloadDialog = + DownloadDialog.newInstance(currentInfo, + sortedStreamVideosList, + selectedVideoStream); + downloadDialog.show(activity.getSupportFragmentManager(), "downloadDialog"); + } catch (Exception e) { + Toast.makeText(activity, + R.string.could_not_setup_download_menu, + Toast.LENGTH_LONG).show(); + e.printStackTrace(); + } + } + /*////////////////////////////////////////////////////////////////////////// // Stream Results //////////////////////////////////////////////////////////////////////////*/ diff --git a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java index 26088a64c..288191677 100644 --- a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java @@ -428,7 +428,7 @@ public class NavigationHelper { } public static boolean openDownloads(Activity activity) { - if (!PermissionHelper.checkStoragePermissions(activity)) { + if (!PermissionHelper.checkStoragePermissions(activity, PermissionHelper.DOWNLOADS_REQUEST_CODE)) { return false; } Intent intent = new Intent(activity, DownloadActivity.class); diff --git a/app/src/main/java/org/schabi/newpipe/util/PermissionHelper.java b/app/src/main/java/org/schabi/newpipe/util/PermissionHelper.java index a33348934..7574a9304 100644 --- a/app/src/main/java/org/schabi/newpipe/util/PermissionHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/PermissionHelper.java @@ -18,26 +18,26 @@ import android.widget.Toast; import org.schabi.newpipe.R; public class PermissionHelper { - public static final int PERMISSION_WRITE_STORAGE = 778; - public static final int PERMISSION_READ_STORAGE = 777; + public static final int DOWNLOAD_DIALOG_REQUEST_CODE = 778; + public static final int DOWNLOADS_REQUEST_CODE = 777; - public static boolean checkStoragePermissions(Activity activity) { + public static boolean checkStoragePermissions(Activity activity, int requestCode) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { - if(!checkReadStoragePermissions(activity)) return false; + if(!checkReadStoragePermissions(activity, requestCode)) return false; } - return checkWriteStoragePermissions(activity); + return checkWriteStoragePermissions(activity, requestCode); } @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) - public static boolean checkReadStoragePermissions(Activity activity) { + public static boolean checkReadStoragePermissions(Activity activity, int requestCode) { if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(activity, new String[]{ Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, - PERMISSION_READ_STORAGE); + requestCode); return false; } @@ -45,7 +45,7 @@ public class PermissionHelper { } - public static boolean checkWriteStoragePermissions(Activity activity) { + public static boolean checkWriteStoragePermissions(Activity activity, int requestCode) { // Here, thisActivity is the current activity if (ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) @@ -63,7 +63,7 @@ public class PermissionHelper { // No explanation needed, we can request the permission. ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, - PERMISSION_WRITE_STORAGE); + requestCode); // PERMISSION_WRITE_STORAGE is an // app-defined int constant. The callback method gets the