From a39dbc378573ce769b4ca4d1af4e34b2c8bff3a5 Mon Sep 17 00:00:00 2001 From: oddlyspaced Date: Thu, 27 Oct 2022 02:02:42 +0530 Subject: [PATCH 1/4] AndroidManifest: add post notification permission to manifest --- app/src/main/AndroidManifest.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 7cc4777..8407911 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,7 @@ + From 92f64ae2dd1726021b83b27fbab95036cab4de78 Mon Sep 17 00:00:00 2001 From: oddlyspaced Date: Thu, 27 Oct 2022 02:18:53 +0530 Subject: [PATCH 2/4] strings: add strings related to notification permission --- app/src/main/res/values/strings.xml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a1be720..5aaff73 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -14,9 +14,11 @@ Battery Optimization Location Permission Background Location Permission + Notification Permission Bluetooth permissions not granted, please enable them in settings. Location permission not granted, please enable it in settings. Background Location permission not granted, please enable it in settings. + Notification Permission not granted, please enable it in settings. The app will show a notification when your AirPods are connected Supported devices: From b15dcfbf9d147f156f21a3a3f30e4872b3bd83eb Mon Sep 17 00:00:00 2001 From: oddlyspaced Date: Thu, 27 Oct 2022 02:19:08 +0530 Subject: [PATCH 3/4] PermissionUtils: setup methods to check for notification perm on >=13 --- .../main/java/com/dosse/airpods/utils/PermissionUtils.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/src/main/java/com/dosse/airpods/utils/PermissionUtils.java b/app/src/main/java/com/dosse/airpods/utils/PermissionUtils.java index 9944a26..389db71 100644 --- a/app/src/main/java/com/dosse/airpods/utils/PermissionUtils.java +++ b/app/src/main/java/com/dosse/airpods/utils/PermissionUtils.java @@ -21,6 +21,11 @@ public class PermissionUtils { } } + @RequiresApi(api = Build.VERSION_CODES.TIRAMISU) + public static boolean getNotificationPermissions (Context context) { + return checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED; + } + @RequiresApi(api = Build.VERSION_CODES.S) public static boolean getBluetoothPermissions (Context context) { return checkSelfPermission(context, Manifest.permission.BLUETOOTH_SCAN) == PackageManager.PERMISSION_GRANTED @@ -44,6 +49,8 @@ public class PermissionUtils { } public static boolean checkAllPermissions (Context context) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) + return getNotificationPermissions(context) && getBatteryOptimizationsPermission(context) && getLocationPermissions(context) && getBluetoothPermissions(context); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) return getBatteryOptimizationsPermission(context) && getLocationPermissions(context) && getBluetoothPermissions(context); else From 4abb9e5d6bc4c5952a6736de5867074c50142bfc Mon Sep 17 00:00:00 2001 From: oddlyspaced Date: Thu, 27 Oct 2022 02:19:44 +0530 Subject: [PATCH 4/4] IntroActivity: ask for notification perm on 13 and above --- .../com/dosse/airpods/ui/IntroActivity.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/dosse/airpods/ui/IntroActivity.java b/app/src/main/java/com/dosse/airpods/ui/IntroActivity.java index 1c3580d..0f10bda 100644 --- a/app/src/main/java/com/dosse/airpods/ui/IntroActivity.java +++ b/app/src/main/java/com/dosse/airpods/ui/IntroActivity.java @@ -28,8 +28,8 @@ public class IntroActivity extends AppCompatActivity { private TextView msg; private Button btn; - private static final int STEP_PERMISSION_BLUETOOTH = 1, STEP_PERMISSION_BATTERY_OPTIMIZATION = 2, STEP_PERMISSION_LOCATION = 3, STEP_PERMISSION_BACKGROUND_LOCATION = 4; - private static final int BLUETOOTH_REQUEST_CODE = 101, LOCATION_REQUEST_CODE = 102, BACKGROUND_LOCATION_REQUEST_CODE = 103; + private static final int STEP_PERMISSION_BLUETOOTH = 1, STEP_PERMISSION_BATTERY_OPTIMIZATION = 2, STEP_PERMISSION_LOCATION = 3, STEP_PERMISSION_BACKGROUND_LOCATION = 4, STEP_PERMISSION_NOTIFICATION = 5; + private static final int BLUETOOTH_REQUEST_CODE = 101, LOCATION_REQUEST_CODE = 102, BACKGROUND_LOCATION_REQUEST_CODE = 103, NOTIFICATION_REQUEST_CODE = 104; @Override protected void onCreate (Bundle savedInstanceState) { @@ -74,14 +74,15 @@ public class IntroActivity extends AppCompatActivity { return STEP_PERMISSION_LOCATION; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) if (!PermissionUtils.getBackgroundLocationPermission(this)) return STEP_PERMISSION_BACKGROUND_LOCATION; - + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) if (!PermissionUtils.getNotificationPermissions(this)) + return STEP_PERMISSION_NOTIFICATION; return 0; } @SuppressLint("BatteryLife") private void initScreen () { int currentStep = getPermissionState() - ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) ? 0 : 1); - int numOfSteps = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) ? ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) ? 4 : 3) : 2; + int numOfSteps = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) ? 5 : (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) ? ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) ? 4 : 3) : 2; runOnUiThread(() -> { switch (getPermissionState()) { @@ -112,6 +113,11 @@ public class IntroActivity extends AppCompatActivity { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) requestPermissions(new String[] {Manifest.permission.ACCESS_BACKGROUND_LOCATION}, BACKGROUND_LOCATION_REQUEST_CODE); }); break; + case STEP_PERMISSION_NOTIFICATION: + msg.setText(String.format(Locale.getDefault(), "%s %d/%d: %s", getString(R.string.intro_step), currentStep, numOfSteps, getString(R.string.intro_notif_perm))); + btn.setOnClickListener(view -> { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) requestPermissions(new String[] {Manifest.permission.POST_NOTIFICATIONS}, NOTIFICATION_REQUEST_CODE); + }); } btn.setText(String.format(Locale.getDefault(), "%s (%d/%d)", getString(R.string.intro_allow), currentStep, numOfSteps)); }); @@ -130,6 +136,9 @@ public class IntroActivity extends AppCompatActivity { case BACKGROUND_LOCATION_REQUEST_CODE: if (!hasAllPermissionsGranted(grantResults)) Toast.makeText(this, getString(R.string.msg_loc2_perm), Toast.LENGTH_LONG).show(); break; + case NOTIFICATION_REQUEST_CODE: + if (!hasAllPermissionsGranted(grantResults)) Toast.makeText(this, getString(R.string.msg_notif_perm), Toast.LENGTH_LONG).show(); + break; } }