Skip to content

Commit

Permalink
fixed issue #162 for Android 13 and 14
Browse files Browse the repository at this point in the history
  • Loading branch information
hpsaturn committed Mar 5, 2024
1 parent 5769188 commit 046334a
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/hpsaturn/pollutionreporter/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public class Config {
public static final int GEOHASHACCU = 7; // max GeoHash precision

public enum PermissionRequestType {
LOCATION, LOCATION_BACKGROUND, STORAGE
LOCATION, LOCATION_BACKGROUND, STORAGE, BLUETOOTH, BLUETOOTH_SCAN
}
}
41 changes: 37 additions & 4 deletions app/src/main/java/hpsaturn/pollutionreporter/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ private void startDataBase(){
};

public void showDisclosureFragment(int title, int desc, int img) {
Logger.i(TAG,"showDisclosureFragment..");
DisclosureFragment dialog = DisclosureFragment.newInstance(title,desc,img);
showDialogFragment(dialog,DisclosureFragment.TAG);
}
Expand Down Expand Up @@ -507,6 +508,24 @@ public void requestLocationPermission() {
Config.PermissionRequestType.LOCATION.ordinal());
}

/**
* Permission check and request functions
*/
public void requestBluetoothScanPermission() {
ActivityCompat.requestPermissions(this,
PermissionUtil.getBluetoothScanPermission(),
Config.PermissionRequestType.BLUETOOTH_SCAN.ordinal());
}

/**
* Permission check and request functions
*/
public void requestBluetoothPermission() {
ActivityCompat.requestPermissions(this,
PermissionUtil.getBluetoothPermission(),
Config.PermissionRequestType.BLUETOOTH.ordinal());
}

public void requestBackgroundPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ActivityCompat.requestPermissions(this,
Expand All @@ -526,16 +545,14 @@ private void requestStoragePermission() {
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (Config.PermissionRequestType.values()[requestCode]) {
case LOCATION:
if (grantResults.length == 0 || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "[PERM] User denied foreground location permission");
break;
}
Log.i(TAG, "[PERM] User granted foreground location permission");
prefBuilder.addBoolean(Keys.PERMISSION_BLE, true).save();
break;
case LOCATION_BACKGROUND:
if (grantResults.length == 0 || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Expand All @@ -555,13 +572,29 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
prefBuilder.addBoolean(Keys.PERMISSION_STORAGE, true).save();
}
break;
case BLUETOOTH:
if (grantResults.length == 0 || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "[PERM] User denied bluetooth connect");
break;
}
Log.i(TAG, "[PERM] User granted bluetooth connect permission");
requestBluetoothScanPermission();

case BLUETOOTH_SCAN:
if (grantResults.length == 0 || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "[PERM] User denied scan nearby devices");
break;
}
Log.i(TAG, "[PERM] User granted scan nearby devices permission");
prefBuilder.addBoolean(Keys.PERMISSION_BLE, true).save();
requestLocationPermission();

default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}

public void performBLEScan() {
Logger.i(TAG, "[PERM] BLEPermissionsGranted..");
if(!isBLEGranted())prefBuilder.addBoolean(Keys.PERMISSION_BLE, true).save();
if(scanFragment!=null)scanFragment.executeScan();
}
Expand Down
20 changes: 19 additions & 1 deletion app/src/main/java/hpsaturn/pollutionreporter/PermissionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ public static String[] getBackgroundPermissions() {

public static String[] getBluetoothPermission() {
return new String[]{
Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_CONNECT,
};
}

public static String[] getBluetoothScanPermission() {
return new String[]{
Manifest.permission.BLUETOOTH_SCAN,
};
}

Expand Down Expand Up @@ -68,6 +74,16 @@ public static boolean hasBluetoothPermission(@NonNull Context context) {
return true;
}


public static boolean hasBluetoothScanPermission(@NonNull Context context) {
for (String perm : PermissionUtil.getBluetoothPermission()) {
if (ContextCompat.checkSelfPermission(context, perm) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}

public static boolean hasBackgroundPermission(@NonNull Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
for (String perm : PermissionUtil.getBackgroundPermissions()) {
Expand All @@ -78,4 +94,6 @@ public static boolean hasBackgroundPermission(@NonNull Context context) {
}
return true;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
private final View.OnClickListener onClickContinueListener = view -> {
if (resource_desc == R.string.msg_gps_desc) {
getMain().requestBackgroundPermission();
} else if (resource_desc == R.string.msg_ble_desc) {
getMain().requestLocationPermission();
}
else if (resource_desc == R.string.msg_file_desc) {
if (resource_desc == R.string.msg_ble_desc) {
getMain().requestBluetoothPermission();
}
if (resource_desc == R.string.msg_file_desc) {
getMain().requestAllFilesAccessPermission();
}
getDialog().dismiss();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,16 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
}

private void actionRequestBLEPermission(){
Logger.i(TAG, "[PERM] request BLE permission" );
if(!getMain().isBLEGranted()) getMain().showDisclosureFragment(R.string.msg_ble_title,R.string.msg_ble_desc,R.drawable.ic_cpu);
else {
if (!PermissionUtil.hasLocationPermission(getContext())) getMain().requestLocationPermission();
if (!PermissionUtil.hasLocationPermission(getContext())) getMain().requestBluetoothPermission();
else getMain().performBLEScan();
}
}

public void executeScan(){
Logger.i(TAG, "[BLE] perform device scanning.." );
getActivity().runOnUiThread(this::actionScan);
}

Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ org.gradle.jvmargs=-Xmx3096m -Dfile.encoding=UTF-8 \
mCompileSdkVersion=33
mMinSdkVersion=23
mTargetSdkVersion=33
mVersionCode=618
mVersionName=0.8.9
mVersionCode=619
mVersionName=0.9.0
android.useAndroidX=true
android.enableJetifier=true

Expand Down

0 comments on commit 046334a

Please sign in to comment.