Request new POST_NOTIFICATIONS permissions

This commit is contained in:
Sergey Solovyev 2023-09-17 13:29:08 +02:00
parent 35408e42b3
commit 308b5504bd
2 changed files with 40 additions and 8 deletions

View File

@ -5,6 +5,7 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="com.android.vending.BILLING" />
<uses-permission android:name="android.permission.VIBRATE" />

View File

@ -22,15 +22,17 @@
package org.solovyev.android.calculator.floating;
import android.Manifest;
import android.annotation.TargetApi;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.FragmentActivity;
import org.solovyev.android.Check;
import org.solovyev.android.calculator.App;
@ -39,6 +41,8 @@ import org.solovyev.android.calculator.R;
public class FloatingCalculatorActivity extends AppCompatActivity {
private static int PERMISSION_REQUEST_CODE = 1;
@TargetApi(Build.VERSION_CODES.M)
public static final class MyFragment extends BaseDialogFragment {
@ -80,17 +84,44 @@ public class FloatingCalculatorActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (FloatingCalculatorView.isOverlayPermissionGranted(this)) {
FloatingCalculatorService.show(this);
finish();
if (!FloatingCalculatorView.isOverlayPermissionGranted(this)) {
Check.isTrue(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M);
if (savedInstanceState == null) {
App.showDialog(new MyFragment(), "no-overlay-permission-dialog",
getSupportFragmentManager());
}
return;
}
tryStartingService(true);
}
Check.isTrue(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M);
if (savedInstanceState == null) {
App.showDialog(new MyFragment(), "no-overlay-permission-dialog",
getSupportFragmentManager());
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSION_REQUEST_CODE) {
tryStartingService(false);
}
}
private void tryStartingService(boolean requirePermission) {
if (requirePermission && !requestNotificationPermission()) return;
FloatingCalculatorService.show(this);
finish();
}
private boolean requestNotificationPermission() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) return true;
final String permission = Manifest.permission.POST_NOTIFICATIONS;
if (ActivityCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED)
return true;
// Notification permission is not essential and if the user hasn't granted it, let's show the floating calculator
// anyway.
if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission))
return true;
requestPermissions(new String[]{permission}, PERMISSION_REQUEST_CODE);
return false;
}
}