diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 3fc57077..20b537d0 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -5,6 +5,7 @@ + diff --git a/app/src/main/java/org/solovyev/android/calculator/floating/FloatingCalculatorActivity.java b/app/src/main/java/org/solovyev/android/calculator/floating/FloatingCalculatorActivity.java index 730b8d52..135af6db 100644 --- a/app/src/main/java/org/solovyev/android/calculator/floating/FloatingCalculatorActivity.java +++ b/app/src/main/java/org/solovyev/android/calculator/floating/FloatingCalculatorActivity.java @@ -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; + } }