Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions app/src/main/java/org/schabi/newpipe/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,17 @@ protected void onCreate(final Bundle savedInstanceState) {
}
openMiniPlayerUponPlayerStarted();

if (PermissionHelper.checkPostNotificationsPermission(this,
PermissionHelper.POST_NOTIFICATIONS_REQUEST_CODE)) {
// Schedule worker for checking for new streams and creating corresponding notifications
// if this is enabled by the user.
NotificationWorker.initialize(this);
}
PermissionHelper.checkPostNotificationsPermissionOnStartup(
this,
notificationAllowed -> {
// Schedule worker for checking for new streams and creating corresponding
// notifications if this is enabled by the user.
if (Boolean.TRUE.equals(notificationAllowed)) {
NotificationWorker.initialize(this);
}
}
);

if (!UpdateSettingsFragment.wasUserAskedForConsent(this)
&& !App.getApp().isFirstRun()
&& ReleaseVersionUtil.INSTANCE.isReleaseApk()) {
Expand Down
71 changes: 61 additions & 10 deletions app/src/main/java/org/schabi/newpipe/util/PermissionHelper.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
package org.schabi.newpipe.util;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.widget.Toast;

import androidx.activity.ComponentActivity;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.RequiresApi;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.preference.PreferenceManager;

import org.schabi.newpipe.R;
import org.schabi.newpipe.settings.NewPipeSettings;

import java.util.function.Consumer;

public final class PermissionHelper {
public static final int POST_NOTIFICATIONS_REQUEST_CODE = 779;
public static final int DOWNLOAD_DIALOG_REQUEST_CODE = 778;
Expand Down Expand Up @@ -81,17 +90,59 @@ public static boolean checkWriteStoragePermissions(final Activity activity,
return true;
}

public static boolean checkPostNotificationsPermission(final Activity activity,
final int requestCode) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU
&& ContextCompat.checkSelfPermission(activity,
Manifest.permission.POST_NOTIFICATIONS)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity,
new String[] {Manifest.permission.POST_NOTIFICATIONS}, requestCode);
return false;

/**
* Check that we have the notification permission or ask the user for permission.
*
* @param activity main activity
* @param userChoice a callback that gets called with the user choice
*/
public static void checkPostNotificationsPermissionOnStartup(
final ComponentActivity activity,
final Consumer<Boolean> userChoice) {

// On Android before TIRAMISU, notifications are always allowed to be sent
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
userChoice.accept(true);
return;
}
return true;

// if we have the permission already, continue
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.POST_NOTIFICATIONS)
== PackageManager.PERMISSION_GRANTED) {
userChoice.accept(true);
return;
}

// if we already asked the user, don’t ask again
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
final String wasAskedKey = activity.getString(
R.string.user_was_asked_notification_permission_on_startup_key);
if (prefs.getBoolean(wasAskedKey, false)) {
userChoice.accept(false);
return;
}

// else let’s ask the user for permission
@SuppressLint("ApplySharedPref")
final ActivityResultCallback<Boolean> cb = isGranted -> {
// first make sure that we only ever ask the user once on startup
final SharedPreferences prefs2 =
PreferenceManager.getDefaultSharedPreferences(activity);
// commit setting before doing anything else
prefs2.edit().putBoolean(wasAskedKey, true).commit();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, this means that once it has been asked once, it will never ever be asked again. I just tested and if you dismiss the request by clicking outside of the permission dialog, NewPipe will never again ask. I think what we want is just to avoid asking twice in the same execution of the app. This can be achieved by setting wasAskedKey to false in App.onCreate(). Or probably directly using a boolean field in App is even better, without having to go through settings, since in theory the App instance is not recreated on orientation change.


// forward the user choice
userChoice.accept(isGranted);
};

final ActivityResultLauncher<String> notificationRequestReference =
activity.registerForActivityResult(
new ActivityResultContracts.RequestPermission(),
cb
);

notificationRequestReference.launch(Manifest.permission.POST_NOTIFICATIONS);
}

/**
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/settings_keys.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,7 @@

<string name="recaptcha_cookies_key">recaptcha_cookies_key</string>

<string name="user_was_asked_notification_permission_on_startup_key">user_was_asked_notification_permission_on_startup</string>
<string name="enable_streams_notifications">enable_streams_notifications</string>
<string name="streams_notifications_interval_key">streams_notifications_interval</string>
<string name="streams_notifications_interval_default">14400</string>
Expand Down