Android 13: request notification permissions

This commit is contained in:
M66B 2023-09-16 10:44:36 +02:00
parent b6b831fb5c
commit 4165205ed5
6 changed files with 71 additions and 3 deletions

View File

@ -5,13 +5,13 @@ def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
android {
compileSdkVersion = 31
compileSdkVersion = 34
defaultConfig {
applicationId = "eu.faircode.netguard"
versionName = "2.309"
minSdkVersion 22
targetSdkVersion 31
targetSdkVersion 34
versionCode = 2023082401
archivesBaseName = "NetGuard-v$versionName"

View File

@ -12,6 +12,7 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<!-- http://developer.android.com/guide/topics/security/permissions.html#normal-dangerous -->
<!-- https://developer.android.com/preview/privacy/package-visibility -->

View File

@ -19,6 +19,7 @@ package eu.faircode.netguard;
Copyright 2015-2019 by Marcel Bokhorst (M66B)
*/
import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
@ -63,6 +64,8 @@ import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.SwitchCompat;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.preference.PreferenceManager;
@ -94,6 +97,7 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
private static final int REQUEST_VPN = 1;
private static final int REQUEST_INVITE = 2;
public static final int REQUEST_ROAMING = 3;
private static final int REQUEST_NOTIFICATIONS = 4;
private static final int MIN_SDK = Build.VERSION_CODES.LOLLIPOP_MR1;
@ -306,6 +310,17 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
TextView tvDisabled = findViewById(R.id.tvDisabled);
tvDisabled.setVisibility(enabled ? View.GONE : View.VISIBLE);
// Notification permissions
TextView tvNotifications = findViewById(R.id.tvNotifications);
tvNotifications.setVisibility(View.GONE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
tvNotifications.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
requestPermissions(new String[]{Manifest.permission.POST_NOTIFICATIONS}, REQUEST_NOTIFICATIONS);
}
});
// Application list
RecyclerView rvApplication = findViewById(R.id.rvApplication);
rvApplication.setHasFixedSize(false);
@ -511,6 +526,15 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
IAB.isPurchasedAny(this) || getIntentPro(this).resolveActivity(pm) == null
? View.GONE : View.VISIBLE);
boolean canNotify =
(Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU ||
(ContextCompat.checkSelfPermission(this,
android.Manifest.permission.POST_NOTIFICATIONS) ==
PackageManager.PERMISSION_GRANTED));
TextView tvNotifications = findViewById(R.id.tvNotifications);
if (tvNotifications != null)
tvNotifications.setVisibility(canNotify ? View.GONE : View.VISIBLE);
super.onResume();
}
@ -612,9 +636,23 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == REQUEST_ROAMING)
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_ROAMING) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
ServiceSinkhole.reload("permission granted", this, false);
} else if (requestCode == REQUEST_NOTIFICATIONS) {
if (grantResults[0] != PackageManager.PERMISSION_GRANTED &&
!ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.POST_NOTIFICATIONS))
try {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
} catch (Throwable ex) {
Log.e(TAG, ex + "\n" + ex.getStackTrace());
}
}
}
@Override

View File

@ -0,0 +1,15 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M12,6.5c-2.49,0 -4,2.02 -4,4.5v6h8v-6c0,-2.48 -1.51,-4.5 -4,-4.5z"
android:strokeAlpha="0.3"
android:fillAlpha="0.3"/>
<path
android:fillColor="@android:color/white"
android:pathData="M12,22c1.1,0 2,-0.9 2,-2h-4c0,1.1 0.9,2 2,2zM18,16v-5c0,-3.07 -1.63,-5.64 -4.5,-6.32L13.5,4c0,-0.83 -0.67,-1.5 -1.5,-1.5s-1.5,0.67 -1.5,1.5v0.68C7.64,5.36 6,7.92 6,11v5l-2,2v1h16v-1l-2,-2zM16,17L8,17v-6c0,-2.48 1.51,-4.5 4,-4.5s4,2.02 4,4.5v6z"/>
</vector>

View File

@ -27,6 +27,19 @@
android:textColor="?attr/colorOff"
android:visibility="gone" />
<TextView
android:id="@+id/tvNotifications"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableStart="@drawable/twotone_notifications_24"
android:drawablePadding="8dp"
android:drawableTint="?attr/colorError"
android:padding="8dp"
android:text="@string/msg_notifications"
android:textAppearance="@style/TextSmall"
android:textColor="?attr/colorOff"
android:visibility="visible" />
<TextView
android:id="@+id/tvEol"
android:layout_width="match_parent"

View File

@ -180,6 +180,7 @@
<string name="msg_hosts">%1$d allowed, %2$d blocked, %3$d hosts</string>
<string name="msg_waiting">Waiting for event</string>
<string name="msg_disabled">NetGuard is disabled, use the switch above to enable NetGuard</string>
<string name="msg_notifications">Tap to grant notification permissions (for access attempt notifications, error messages, etc.)</string>
<string name="msg_eol">
Google forces me to state that the app collects and shares email addresses when it does not.
I refuse to make false statements.