2017-01-10 10:41:24 +00:00
|
|
|
package org.schabi.newpipe.util;
|
|
|
|
|
|
|
|
import android.Manifest;
|
|
|
|
import android.app.Activity;
|
2017-03-09 07:42:40 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2017-01-10 10:41:24 +00:00
|
|
|
import android.content.pm.PackageManager;
|
2017-03-09 07:42:40 +00:00
|
|
|
import android.net.Uri;
|
2017-01-10 10:41:24 +00:00
|
|
|
import android.os.Build;
|
2017-03-09 07:42:40 +00:00
|
|
|
import android.provider.Settings;
|
2019-10-04 12:59:08 +00:00
|
|
|
import androidx.annotation.RequiresApi;
|
|
|
|
import androidx.core.app.ActivityCompat;
|
|
|
|
import androidx.core.content.ContextCompat;
|
2018-01-04 06:53:31 +00:00
|
|
|
import android.view.Gravity;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import org.schabi.newpipe.R;
|
2017-01-10 10:41:24 +00:00
|
|
|
|
|
|
|
public class PermissionHelper {
|
2019-08-15 02:00:11 +00:00
|
|
|
public static final int DOWNLOAD_DIALOG_REQUEST_CODE = 778;
|
|
|
|
public static final int DOWNLOADS_REQUEST_CODE = 777;
|
2017-01-10 10:41:24 +00:00
|
|
|
|
2018-04-07 17:32:02 +00:00
|
|
|
public static boolean checkStoragePermissions(Activity activity, int requestCode) {
|
2017-01-10 10:41:24 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
|
2019-08-15 02:00:11 +00:00
|
|
|
if (!checkReadStoragePermissions(activity, requestCode)) return false;
|
2017-01-10 10:41:24 +00:00
|
|
|
}
|
2018-04-07 17:32:02 +00:00
|
|
|
return checkWriteStoragePermissions(activity, requestCode);
|
2017-01-10 10:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
|
2018-04-07 17:32:02 +00:00
|
|
|
public static boolean checkReadStoragePermissions(Activity activity, int requestCode) {
|
2017-01-10 10:41:24 +00:00
|
|
|
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},
|
2018-04-07 17:32:02 +00:00
|
|
|
requestCode);
|
2017-01-10 10:41:24 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-07 17:32:02 +00:00
|
|
|
public static boolean checkWriteStoragePermissions(Activity activity, int requestCode) {
|
2017-01-10 10:41:24 +00:00
|
|
|
// Here, thisActivity is the current activity
|
|
|
|
if (ContextCompat.checkSelfPermission(activity,
|
|
|
|
Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
|
|
|
!= PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
|
|
|
// Should we show an explanation?
|
|
|
|
/*if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
|
|
|
|
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
|
|
|
|
|
|
|
|
// Show an explanation to the user *asynchronously* -- don't block
|
|
|
|
// this thread waiting for the user's response! After the user
|
|
|
|
// sees the explanation, try again to request the permission.
|
|
|
|
} else {*/
|
|
|
|
|
|
|
|
// No explanation needed, we can request the permission.
|
|
|
|
ActivityCompat.requestPermissions(activity,
|
|
|
|
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
|
2018-04-07 17:32:02 +00:00
|
|
|
requestCode);
|
2017-01-10 10:41:24 +00:00
|
|
|
|
|
|
|
// PERMISSION_WRITE_STORAGE is an
|
|
|
|
// app-defined int constant. The callback method gets the
|
|
|
|
// result of the request.
|
|
|
|
/*}*/
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2017-03-09 07:42:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* In order to be able to draw over other apps, the permission android.permission.SYSTEM_ALERT_WINDOW have to be granted.
|
|
|
|
* <p>
|
|
|
|
* On < API 23 (MarshMallow) the permission was granted when the user installed the application (via AndroidManifest),
|
2018-01-23 00:40:00 +00:00
|
|
|
* on > 23, however, it have to start a activity asking the user if he agrees.
|
2017-03-09 07:42:40 +00:00
|
|
|
* <p>
|
2018-01-23 00:40:00 +00:00
|
|
|
* This method just return if the app has permission to draw over other apps, and if it doesn't, it will try to get the permission.
|
2017-03-09 07:42:40 +00:00
|
|
|
*
|
|
|
|
* @return returns {@link Settings#canDrawOverlays(Context)}
|
|
|
|
**/
|
|
|
|
@RequiresApi(api = Build.VERSION_CODES.M)
|
2018-01-23 00:40:00 +00:00
|
|
|
public static boolean checkSystemAlertWindowPermission(Context context) {
|
|
|
|
if (!Settings.canDrawOverlays(context)) {
|
|
|
|
Intent i = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + context.getPackageName()));
|
|
|
|
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
context.startActivity(i);
|
2017-03-09 07:42:40 +00:00
|
|
|
return false;
|
2019-08-15 02:00:11 +00:00
|
|
|
} else return true;
|
2017-03-09 07:42:40 +00:00
|
|
|
}
|
2018-01-04 06:53:31 +00:00
|
|
|
|
2018-01-23 00:40:00 +00:00
|
|
|
public static boolean isPopupEnabled(Context context) {
|
2018-01-04 06:53:31 +00:00
|
|
|
return Build.VERSION.SDK_INT < Build.VERSION_CODES.M ||
|
2018-01-23 00:40:00 +00:00
|
|
|
PermissionHelper.checkSystemAlertWindowPermission(context);
|
2018-01-04 06:53:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void showPopupEnablementToast(Context context) {
|
|
|
|
Toast toast = Toast.makeText(context, R.string.msg_popup_permission, Toast.LENGTH_LONG);
|
|
|
|
TextView messageView = toast.getView().findViewById(android.R.id.message);
|
|
|
|
if (messageView != null) messageView.setGravity(Gravity.CENTER);
|
|
|
|
toast.show();
|
|
|
|
}
|
2017-01-10 10:41:24 +00:00
|
|
|
}
|