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 @@ + 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; } } 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 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: