Merge Bluetooth permissions

This commit is contained in:
Electric1447
2021-08-14 18:59:34 +03:00
parent 424cc7f4d3
commit e1493ffb83
3 changed files with 16 additions and 28 deletions
@@ -23,7 +23,7 @@ public class IntroActivity extends AppCompatActivity {
private TextView msg;
private Button btn;
private static final int STEP_PERMISSION_BLUETOOTH_SCAN = 1, STEP_PERMISSION_BLUETOOTH_CONNECT = 2, STEP_PERMISSION_BATTERY_OPTIMIZATION = 3, STEP_PERMISSION_LOCATION = 4, STEP_PERMISSION_BACKGROUND_LOCATION = 5;
private static final int STEP_PERMISSION_BLUETOOTH = 1, STEP_PERMISSION_BATTERY_OPTIMIZATION = 2, STEP_PERMISSION_LOCATION = 3, STEP_PERMISSION_BACKGROUND_LOCATION = 4;
@Override
protected void onCreate (Bundle savedInstanceState) {
@@ -60,10 +60,8 @@ public class IntroActivity extends AppCompatActivity {
}
private int getPermissionState () {
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 (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) if (!PermissionUtils.getBluetoothPermissions(this))
return STEP_PERMISSION_BLUETOOTH;
if (!PermissionUtils.getBatteryOptimizationsPermission(this))
return STEP_PERMISSION_BATTERY_OPTIMIZATION;
if (!PermissionUtils.getFineLocationPermission(this))
@@ -76,20 +74,15 @@ public class IntroActivity extends AppCompatActivity {
@SuppressLint("BatteryLife")
private void initScreen () {
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;
int currentStep = 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;
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)));
case STEP_PERMISSION_BLUETOOTH:
msg.setText(String.format(Locale.getDefault(), "%s %d/%d: %s", getString(R.string.intro_step), currentStep, numOfSteps, getString(R.string.intro_bt_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);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
requestPermissions(new String[] {Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_CONNECT}, 101);
});
break;
case STEP_PERMISSION_BATTERY_OPTIMIZATION:
@@ -104,12 +97,12 @@ public class IntroActivity extends AppCompatActivity {
break;
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)
btn.setOnClickListener(view -> requestPermissions(new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, 102)); // Location (for BLE)
break;
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}, 104);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) requestPermissions(new String[] {Manifest.permission.ACCESS_BACKGROUND_LOCATION}, 103);
});
break;
}
@@ -22,13 +22,9 @@ public class PermissionUtils {
}
@RequiresApi(api = Build.VERSION_CODES.S)
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 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 getFineLocationPermission (Context context) {
@@ -42,7 +38,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) && getBluetoothScanPermission(context) && getBluetoothConnectPermission(context);
return getBatteryOptimizationsPermission(context) && getFineLocationPermission(context) && getBackgroundLocationPermission(context) && getBluetoothPermissions(context);
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)
return getBatteryOptimizationsPermission(context) && getFineLocationPermission(context) && getBackgroundLocationPermission(context);
else
+1 -2
View File
@@ -10,8 +10,7 @@
<string name="intro_message">To operate, we need permission to use Bluetooth LE (Location) and permission to run in background</string>
<string name="intro_allow">Allow</string>
<string name="intro_step" tools:ignore="MissingTranslation">Step</string>
<string name="intro_bt1_perm" tools:ignore="MissingTranslation">Bluetooth Scan Permission</string>
<string name="intro_bt2_perm" tools:ignore="MissingTranslation">Bluetooth Connect Permission</string>
<string name="intro_bt_perm" tools:ignore="MissingTranslation">Bluetooth Permissions</string>
<string name="intro_bat_perm" tools:ignore="MissingTranslation">Battery Optimization</string>
<string name="intro_loc1_perm" tools:ignore="MissingTranslation">Location Permission</string>
<string name="intro_loc2_perm" tools:ignore="MissingTranslation">Background Location Permission</string>