Skip to content

Commit

Permalink
Add notification permission when user tries to turn on the VPN
Browse files Browse the repository at this point in the history
  • Loading branch information
deeppandya committed Jan 16, 2025
1 parent 20cf651 commit 39e94d4
Showing 1 changed file with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
/**
* Copyright (c) 2021 The Brave Authors. All rights reserved.
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

package org.chromium.chrome.browser.vpn.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.os.Build;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import org.chromium.chrome.browser.vpn.timer.TimerUtils;
Expand All @@ -24,6 +26,8 @@ public class BraveVpnProfileUtils {
private static volatile BraveVpnProfileUtils sBraveVpnProfileUtils;
private static Object sMutex = new Object();

private static final int NOTIFICATION_PERMISSION_CODE = 123;

private BraveVpnProfileUtils() {}

public static BraveVpnProfileUtils getInstance() {
Expand Down Expand Up @@ -63,6 +67,21 @@ public boolean isVPNRunning(Context context) {
public void startVpn(Context context) {
ContextCompat.startForegroundService(context, new Intent(context, WireguardService.class));
TimerUtils.cancelScheduledVpnAction(context);
// For Android 13 (Tiramisu) and above, we need to explicitly request notification
// permission
if (context != null
&& context instanceof Activity
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU
&& ContextCompat.checkSelfPermission(
context, Manifest.permission.POST_NOTIFICATIONS)
!= PackageManager.PERMISSION_GRANTED) {
// Permission not granted yet, request it from the user via system dialog
// This is required for VPN service notifications to show stats
ActivityCompat.requestPermissions(
(Activity) context,
new String[] {Manifest.permission.POST_NOTIFICATIONS},
NOTIFICATION_PERMISSION_CODE);
}
}

public void stopVpn(Context context) {
Expand Down

0 comments on commit 39e94d4

Please sign in to comment.