Merge branch 'download-permissions-screen' of https://github.com/arispoloway/NewPipe into perm

This commit is contained in:
Christian Schabesberger 2018-04-08 18:21:27 +02:00
commit f1691050cd
5 changed files with 52 additions and 26 deletions

View file

@ -22,11 +22,13 @@ package org.schabi.newpipe;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView; import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat; 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.report.ErrorActivity;
import org.schabi.newpipe.util.Constants; import org.schabi.newpipe.util.Constants;
import org.schabi.newpipe.util.NavigationHelper; import org.schabi.newpipe.util.NavigationHelper;
import org.schabi.newpipe.util.PermissionHelper;
import org.schabi.newpipe.util.ServiceHelper; import org.schabi.newpipe.util.ServiceHelper;
import org.schabi.newpipe.util.StateSaver; import org.schabi.newpipe.util.StateSaver;
import org.schabi.newpipe.util.ThemeHelper; import org.schabi.newpipe.util.ThemeHelper;
@ -233,6 +236,26 @@ public class MainActivity extends AppCompatActivity {
} else super.onBackPressed(); } else super.onBackPressed();
} }
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
for (int i: grantResults){
if (i == PackageManager.PERMISSION_DENIED){
return;
}
}
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;
}
}
/** /**
* Implement the following diagram behavior for the up button: * Implement the following diagram behavior for the up button:
* <pre> * <pre>

View file

@ -79,7 +79,7 @@ public class DownloadDialog extends DialogFragment implements RadioGroup.OnCheck
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
if (DEBUG) Log.d(TAG, "onCreate() called with: savedInstanceState = [" + 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(); getDialog().dismiss();
return; return;
} }

View file

@ -355,21 +355,8 @@ public class VideoDetailFragment
} }
break; break;
case R.id.detail_controls_download: case R.id.detail_controls_download:
if (!PermissionHelper.checkStoragePermissions(activity)) { if (PermissionHelper.checkStoragePermissions(activity, PermissionHelper.DOWNLOAD_DIALOG_REQUEST_CODE)) {
return; this.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();
} }
break; break;
case R.id.detail_uploader_root_layout: case R.id.detail_uploader_root_layout:
@ -1227,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 // Stream Results
//////////////////////////////////////////////////////////////////////////*/ //////////////////////////////////////////////////////////////////////////*/

View file

@ -428,7 +428,7 @@ public class NavigationHelper {
} }
public static boolean openDownloads(Activity activity) { public static boolean openDownloads(Activity activity) {
if (!PermissionHelper.checkStoragePermissions(activity)) { if (!PermissionHelper.checkStoragePermissions(activity, PermissionHelper.DOWNLOADS_REQUEST_CODE)) {
return false; return false;
} }
Intent intent = new Intent(activity, DownloadActivity.class); Intent intent = new Intent(activity, DownloadActivity.class);

View file

@ -18,26 +18,26 @@ import android.widget.Toast;
import org.schabi.newpipe.R; import org.schabi.newpipe.R;
public class PermissionHelper { public class PermissionHelper {
public static final int PERMISSION_WRITE_STORAGE = 778; public static final int DOWNLOAD_DIALOG_REQUEST_CODE = 778;
public static final int PERMISSION_READ_STORAGE = 777; 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 (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) @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) if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) { != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, ActivityCompat.requestPermissions(activity,
new String[]{ new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE}, Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSION_READ_STORAGE); requestCode);
return false; 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 // Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(activity, if (ContextCompat.checkSelfPermission(activity,
Manifest.permission.WRITE_EXTERNAL_STORAGE) Manifest.permission.WRITE_EXTERNAL_STORAGE)
@ -63,7 +63,7 @@ public class PermissionHelper {
// No explanation needed, we can request the permission. // No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(activity, ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSION_WRITE_STORAGE); requestCode);
// PERMISSION_WRITE_STORAGE is an // PERMISSION_WRITE_STORAGE is an
// app-defined int constant. The callback method gets the // app-defined int constant. The callback method gets the