|
1 | 1 | package org.schabi.newpipe.util;
|
2 | 2 |
|
3 | 3 | import android.Manifest;
|
| 4 | +import android.annotation.SuppressLint; |
4 | 5 | import android.app.Activity;
|
5 | 6 | import android.content.ActivityNotFoundException;
|
6 | 7 | import android.content.Context;
|
7 | 8 | import android.content.Intent;
|
| 9 | +import android.content.SharedPreferences; |
8 | 10 | import android.content.pm.PackageManager;
|
9 | 11 | import android.net.Uri;
|
10 | 12 | import android.os.Build;
|
11 | 13 | import android.provider.Settings;
|
12 | 14 | import android.widget.Toast;
|
13 | 15 |
|
| 16 | +import androidx.activity.ComponentActivity; |
| 17 | +import androidx.activity.result.ActivityResultCallback; |
| 18 | +import androidx.activity.result.ActivityResultLauncher; |
| 19 | +import androidx.activity.result.contract.ActivityResultContracts; |
14 | 20 | import androidx.annotation.RequiresApi;
|
15 | 21 | import androidx.core.app.ActivityCompat;
|
16 | 22 | import androidx.core.content.ContextCompat;
|
| 23 | +import androidx.preference.PreferenceManager; |
17 | 24 |
|
18 | 25 | import org.schabi.newpipe.R;
|
19 | 26 | import org.schabi.newpipe.settings.NewPipeSettings;
|
20 | 27 |
|
| 28 | +import java.util.function.Consumer; |
| 29 | + |
21 | 30 | public final class PermissionHelper {
|
22 | 31 | public static final int POST_NOTIFICATIONS_REQUEST_CODE = 779;
|
23 | 32 | public static final int DOWNLOAD_DIALOG_REQUEST_CODE = 778;
|
@@ -81,17 +90,59 @@ public static boolean checkWriteStoragePermissions(final Activity activity,
|
81 | 90 | return true;
|
82 | 91 | }
|
83 | 92 |
|
84 |
| - public static boolean checkPostNotificationsPermission(final Activity activity, |
85 |
| - final int requestCode) { |
86 |
| - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU |
87 |
| - && ContextCompat.checkSelfPermission(activity, |
88 |
| - Manifest.permission.POST_NOTIFICATIONS) |
89 |
| - != PackageManager.PERMISSION_GRANTED) { |
90 |
| - ActivityCompat.requestPermissions(activity, |
91 |
| - new String[] {Manifest.permission.POST_NOTIFICATIONS}, requestCode); |
92 |
| - return false; |
| 93 | + |
| 94 | + /** |
| 95 | + * Check that we have the notification permission or ask the user for permission. |
| 96 | + * |
| 97 | + * @param activity main activity |
| 98 | + * @param userChoice a callback that gets called with the user choice |
| 99 | + */ |
| 100 | + public static void checkPostNotificationsPermissionOnStartup( |
| 101 | + final ComponentActivity activity, |
| 102 | + final Consumer<Boolean> userChoice) { |
| 103 | + |
| 104 | + // On Android before TIRAMISU, notifications are always allowed to be sent |
| 105 | + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { |
| 106 | + userChoice.accept(true); |
| 107 | + return; |
93 | 108 | }
|
94 |
| - return true; |
| 109 | + |
| 110 | + // if we have the permission already, continue |
| 111 | + if (ContextCompat.checkSelfPermission(activity, Manifest.permission.POST_NOTIFICATIONS) |
| 112 | + == PackageManager.PERMISSION_GRANTED) { |
| 113 | + userChoice.accept(true); |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + // if we already asked the user, don’t ask again |
| 118 | + final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity); |
| 119 | + final String wasAskedKey = activity.getString( |
| 120 | + R.string.user_was_asked_notification_permission_on_startup_key); |
| 121 | + if (prefs.getBoolean(wasAskedKey, false)) { |
| 122 | + userChoice.accept(false); |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + // else let’s ask the user for permission |
| 127 | + @SuppressLint("ApplySharedPref") |
| 128 | + final ActivityResultCallback<Boolean> cb = isGranted -> { |
| 129 | + // first make sure that we only ever ask the user once on startup |
| 130 | + final SharedPreferences prefs2 = |
| 131 | + PreferenceManager.getDefaultSharedPreferences(activity); |
| 132 | + // commit setting before doing anything else |
| 133 | + prefs2.edit().putBoolean(wasAskedKey, true).commit(); |
| 134 | + |
| 135 | + // forward the user choice |
| 136 | + userChoice.accept(isGranted); |
| 137 | + }; |
| 138 | + |
| 139 | + final ActivityResultLauncher<String> notificationRequestReference = |
| 140 | + activity.registerForActivityResult( |
| 141 | + new ActivityResultContracts.RequestPermission(), |
| 142 | + cb |
| 143 | + ); |
| 144 | + |
| 145 | + notificationRequestReference.launch(Manifest.permission.POST_NOTIFICATIONS); |
95 | 146 | }
|
96 | 147 |
|
97 | 148 | /**
|
|
0 commit comments