From 5ca0a0adf2fd8707a5bdb32b6c48a5956e258902 Mon Sep 17 00:00:00 2001 From: Aris Poloway Date: Fri, 6 Apr 2018 17:19:45 -0400 Subject: [PATCH 1/3] Open downloads after permission granted --- .../java/org/schabi/newpipe/MainActivity.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/app/src/main/java/org/schabi/newpipe/MainActivity.java b/app/src/main/java/org/schabi/newpipe/MainActivity.java index 8285a445e..4b31a1b1f 100644 --- a/app/src/main/java/org/schabi/newpipe/MainActivity.java +++ b/app/src/main/java/org/schabi/newpipe/MainActivity.java @@ -22,11 +22,13 @@ package org.schabi.newpipe; import android.content.Intent; import android.content.SharedPreferences; +import android.content.pm.PackageManager; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.preference.PreferenceManager; +import android.support.annotation.NonNull; import android.support.design.widget.NavigationView; import android.support.v4.app.Fragment; import android.support.v4.view.GravityCompat; @@ -54,6 +56,7 @@ import org.schabi.newpipe.fragments.list.search.SearchFragment; import org.schabi.newpipe.report.ErrorActivity; import org.schabi.newpipe.util.Constants; import org.schabi.newpipe.util.NavigationHelper; +import org.schabi.newpipe.util.PermissionHelper; import org.schabi.newpipe.util.ServiceHelper; import org.schabi.newpipe.util.StateSaver; import org.schabi.newpipe.util.ThemeHelper; @@ -233,6 +236,19 @@ public class MainActivity extends AppCompatActivity { } else super.onBackPressed(); } + @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; + } + } + NavigationHelper.openDownloads(this); + } + } + /** * Implement the following diagram behavior for the up button: *

From 676d64a24a3934cff5cf1464b5f2be49dac34b4f Mon Sep 17 00:00:00 2001
From: Aris Poloway 
Date: Sat, 7 Apr 2018 13:32:02 -0400
Subject: [PATCH 2/3] 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

From 169b6acd24d2a547fa87c087cac3808f00eb7fcc Mon Sep 17 00:00:00 2001
From: Aris Poloway 
Date: Sun, 8 Apr 2018 11:47:11 -0400
Subject: [PATCH 3/3] Clean up download dialog opening

---
 .../schabi/newpipe/fragments/detail/VideoDetailFragment.java | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

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 653ce2082..d5a830461 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,10 +356,9 @@ public class VideoDetailFragment
                 }
                 break;
             case R.id.detail_controls_download:
-                if (!PermissionHelper.checkStoragePermissions(activity, PermissionHelper.DOWNLOAD_DIALOG_REQUEST_CODE)) {
-                    return;
+                if (PermissionHelper.checkStoragePermissions(activity, PermissionHelper.DOWNLOAD_DIALOG_REQUEST_CODE)) {
+                    this.openDownloadDialog();
                 }
-                this.openDownloadDialog();
                 break;
             case R.id.detail_uploader_root_layout:
                 if (TextUtils.isEmpty(currentInfo.getUploaderUrl())) {