From 9c8b91990e8ae6661b09c0fe84f6684db7d56f69 Mon Sep 17 00:00:00 2001 From: Electric1447 Date: Tue, 20 Jul 2021 17:05:45 +0300 Subject: [PATCH] Android 12 support (for real this time) --- OpenPods/app/src/main/AndroidManifest.xml | 8 +-- .../java/com/dosse/airpods/IntroActivity.java | 51 ++++++++++++------- .../com/dosse/airpods/PermissionUtils.java | 12 +++-- OpenPods/app/src/main/res/values/strings.xml | 2 + 4 files changed, 48 insertions(+), 25 deletions(-) diff --git a/OpenPods/app/src/main/AndroidManifest.xml b/OpenPods/app/src/main/AndroidManifest.xml index faa46b7..93ec5e2 100644 --- a/OpenPods/app/src/main/AndroidManifest.xml +++ b/OpenPods/app/src/main/AndroidManifest.xml @@ -2,15 +2,15 @@ + + - - - - + + = Build.VERSION_CODES.S) if (!PermissionUtils.getBluetoothPermissions(this)) - return 1; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) if (!PermissionUtils.getBluetoothScanPermission(this)) + return STEP_PERMISSION_BLUETOOTH_SCAN; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) if (!PermissionUtils.getBluetoothConnectPermission(this)) + return STEP_PERMISSION_BLUETOOTH_CONNECT; if (!PermissionUtils.getBatteryOptimizationsPermission(this)) - return 2; + return STEP_PERMISSION_BATTERY_OPTIMIZATION; if (!PermissionUtils.getFineLocationPermission(this)) - return 3; + return STEP_PERMISSION_LOCATION; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) if (!PermissionUtils.getBackgroundLocationPermission(this)) - return 4; + return STEP_PERMISSION_BACKGROUND_LOCATION; return 0; } @SuppressLint("BatteryLife") private void initScreen () { - int step = getPermissionState() - ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) ? 0 : 1); - int numOfSteps = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) ? ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) ? 4 : 3) : 2; + int currentStep = getPermissionState() - ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) ? 0 : 2); + int numOfSteps = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) ? ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) ? 5 : 3) : 2; - switch (step) { - case 1: - msg.setText(String.format(Locale.getDefault(), "%s %d/%d: %s", getString(R.string.intro_step), step, numOfSteps, getString(R.string.intro_bat_perm))); + switch (getPermissionState()) { + case STEP_PERMISSION_BLUETOOTH_SCAN: + msg.setText(String.format(Locale.getDefault(), "%s %d/%d: %s", getString(R.string.intro_step), currentStep, numOfSteps, getString(R.string.intro_bt1_perm))); + btn.setOnClickListener(view -> { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) requestPermissions(new String[] {Manifest.permission.BLUETOOTH_SCAN}, 101); + }); + break; + case STEP_PERMISSION_BLUETOOTH_CONNECT: + msg.setText(String.format(Locale.getDefault(), "%s %d/%d: %s", getString(R.string.intro_step), currentStep, numOfSteps, getString(R.string.intro_bt2_perm))); + btn.setOnClickListener(view -> { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) requestPermissions(new String[] {Manifest.permission.BLUETOOTH_CONNECT}, 102); + }); + break; + case STEP_PERMISSION_BATTERY_OPTIMIZATION: + msg.setText(String.format(Locale.getDefault(), "%s %d/%d: %s", getString(R.string.intro_step), currentStep, numOfSteps, getString(R.string.intro_bat_perm))); btn.setOnClickListener(view -> { Intent intent = new Intent(); getSystemService(Context.POWER_SERVICE); @@ -86,19 +102,20 @@ public class IntroActivity extends AppCompatActivity { startActivity(intent); }); break; - case 2: - msg.setText(String.format(Locale.getDefault(), "%s %d/%d: %s", getString(R.string.intro_step), step, numOfSteps, getString(R.string.intro_loc1_perm))); - btn.setOnClickListener(view -> requestPermissions(new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, 101)); // Location (for BLE) + case STEP_PERMISSION_LOCATION: + msg.setText(String.format(Locale.getDefault(), "%s %d/%d: %s", getString(R.string.intro_step), currentStep, numOfSteps, getString(R.string.intro_loc1_perm))); + btn.setOnClickListener(view -> requestPermissions(new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, 103)); // Location (for BLE) break; - case 3: - msg.setText(String.format(Locale.getDefault(), "%s %d/%d: %s", getString(R.string.intro_step), step, numOfSteps, getString(R.string.intro_loc2_perm))); + case STEP_PERMISSION_BACKGROUND_LOCATION: + msg.setText(String.format(Locale.getDefault(), "%s %d/%d: %s", getString(R.string.intro_step), currentStep, numOfSteps, getString(R.string.intro_loc2_perm))); btn.setOnClickListener(view -> { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) requestPermissions(new String[] {Manifest.permission.ACCESS_BACKGROUND_LOCATION}, 102); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) + requestPermissions(new String[] {Manifest.permission.ACCESS_BACKGROUND_LOCATION}, 104); }); break; } - runOnUiThread(() -> btn.setText(String.format(Locale.getDefault(), "%s (%d/%d)", getString(R.string.intro_allow), step, numOfSteps))); + runOnUiThread(() -> btn.setText(String.format(Locale.getDefault(), "%s (%d/%d)", getString(R.string.intro_allow), currentStep, numOfSteps))); } } diff --git a/OpenPods/app/src/main/java/com/dosse/airpods/PermissionUtils.java b/OpenPods/app/src/main/java/com/dosse/airpods/PermissionUtils.java index ca693bd..b777f98 100644 --- a/OpenPods/app/src/main/java/com/dosse/airpods/PermissionUtils.java +++ b/OpenPods/app/src/main/java/com/dosse/airpods/PermissionUtils.java @@ -22,9 +22,13 @@ public class PermissionUtils { } @RequiresApi(api = Build.VERSION_CODES.S) - public static boolean getBluetoothPermissions (Context context) { - return ContextCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_SCAN) == PackageManager.PERMISSION_GRANTED - && ContextCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED; + public static boolean getBluetoothScanPermission (Context context) { + return ContextCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_SCAN) == PackageManager.PERMISSION_GRANTED; + } + + @RequiresApi(api = Build.VERSION_CODES.S) + public static boolean getBluetoothConnectPermission (Context context) { + return ContextCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED; } public static boolean getFineLocationPermission (Context context) { @@ -38,7 +42,7 @@ public class PermissionUtils { public static boolean checkAllPermissions (Context context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) - return getBatteryOptimizationsPermission(context) && getFineLocationPermission(context) && getBackgroundLocationPermission(context) && getBluetoothPermissions(context); + return getBatteryOptimizationsPermission(context) && getFineLocationPermission(context) && getBackgroundLocationPermission(context) && getBluetoothScanPermission(context) && getBluetoothConnectPermission(context); else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) return getBatteryOptimizationsPermission(context) && getFineLocationPermission(context) && getBackgroundLocationPermission(context); else diff --git a/OpenPods/app/src/main/res/values/strings.xml b/OpenPods/app/src/main/res/values/strings.xml index 11ef0b7..8baf15a 100644 --- a/OpenPods/app/src/main/res/values/strings.xml +++ b/OpenPods/app/src/main/res/values/strings.xml @@ -10,6 +10,8 @@ To operate, we need permission to use Bluetooth LE (Location) and permission to run in background Allow Step + Bluetooth Scan Permission + Bluetooth Connect Permission Battery Optimization Location Permission Background Location Permission