From 98f8b080a94a850c7f31a7ea1ef35f0ea83e7c37 Mon Sep 17 00:00:00 2001 From: cdhiraj40 Date: Tue, 15 Mar 2022 01:31:34 +0530 Subject: [PATCH 1/2] resolved crash while opening the OBDSelectionFragment in android 12 --- .../obdselection/OBDSelectionActivity.java | 77 ++++++++++++++++++- .../obdselection/OBDSelectionFragment.java | 70 +---------------- 2 files changed, 75 insertions(+), 72 deletions(-) diff --git a/org.envirocar.app/src/org/envirocar/app/views/obdselection/OBDSelectionActivity.java b/org.envirocar.app/src/org/envirocar/app/views/obdselection/OBDSelectionActivity.java index a8e693248..5db3c82d9 100644 --- a/org.envirocar.app/src/org/envirocar/app/views/obdselection/OBDSelectionActivity.java +++ b/org.envirocar.app/src/org/envirocar/app/views/obdselection/OBDSelectionActivity.java @@ -18,8 +18,11 @@ */ package org.envirocar.app.views.obdselection; +import android.Manifest; import android.os.Bundle; import com.google.android.material.snackbar.Snackbar; + +import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; import androidx.appcompat.widget.Toolbar; import android.view.MenuItem; @@ -34,20 +37,24 @@ import org.envirocar.app.injection.BaseInjectorActivity; import org.envirocar.core.events.bluetooth.BluetoothStateChangedEvent; import org.envirocar.core.logging.Logger; +import org.jetbrains.annotations.NotNull; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.List; import javax.inject.Inject; import butterknife.BindView; import butterknife.ButterKnife; +import pub.devrel.easypermissions.EasyPermissions; +import pub.devrel.easypermissions.PermissionRequest; /** * @dewall */ public class OBDSelectionActivity extends BaseInjectorActivity implements - OBDSelectionFragment.ShowSnackbarListener { + OBDSelectionFragment.ShowSnackbarListener,EasyPermissions.PermissionCallbacks { private static final Logger LOGGER = Logger.getLogger(OBDSelectionActivity.class); @Inject @@ -88,14 +95,18 @@ protected void onCreate(Bundle savedInstanceState) { if (mOBDSelectionFragment == null) mOBDSelectionFragment = new OBDSelectionFragment(); + + checkAndRequestPermissions(); + // Setup the bluetooth toolbar + setupBluetoothSwitch(); + } + + private void navigateToOBDSelection(){ // And set the fragment in the layout container. getSupportFragmentManager() .beginTransaction() .replace(R.id.activity_obd_selection_layout_container, mOBDSelectionFragment) .commit(); - - // Setup the bluetooth toolbar - setupBluetoothSwitch(); } private void setupBluetoothSwitch() { @@ -163,4 +174,62 @@ private void setSwitchState(float value) { } } + private final int BLUETOOTH_PERMISSIONS = 1; + + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + + EasyPermissions.onRequestPermissionsResult(requestCode,permissions,grantResults,this); + } + + public void checkAndRequestPermissions() { + String[] perms; + if (android.os.Build.VERSION.SDK_INT >= 31) { + perms = new String[]{ + Manifest.permission.ACCESS_FINE_LOCATION, + Manifest.permission.ACCESS_COARSE_LOCATION, + Manifest.permission.BLUETOOTH_CONNECT, + Manifest.permission.BLUETOOTH_SCAN + }; + } + else{ + perms = new String[]{ + Manifest.permission.ACCESS_FINE_LOCATION, + Manifest.permission.ACCESS_COARSE_LOCATION + }; + } + + if (EasyPermissions.hasPermissions(this, perms)){ + // if all permissions are granted, start bluetooth discovery. + navigateToOBDSelection(); + } + else{ + // Dialog requesting the user for location permission. + EasyPermissions.requestPermissions( + new PermissionRequest.Builder(this, BLUETOOTH_PERMISSIONS, perms) + .setRationale(R.string.location_permission_to_discover_newdevices) + .setPositiveButtonText(R.string.grant_permissions) + .setNegativeButtonText(R.string.cancel) + .setTheme(R.style.MaterialDialog) + .build()); + } + } + + @Override + public void onPermissionsGranted(int requestCode, @NonNull @NotNull List perms) { + // if location permissions are granted, start Bluetooth discovery. + if (requestCode == BLUETOOTH_PERMISSIONS) { + navigateToOBDSelection(); + showSnackbar(getString(R.string.location_permission_granted)); + } + } + + @Override + public void onPermissionsDenied(int requestCode, @NonNull @NotNull List perms) { + // if permissions are not granted, show toast. + if (requestCode == BLUETOOTH_PERMISSIONS) { + showSnackbar(getString(R.string.location_permission_denied)); + } + } } diff --git a/org.envirocar.app/src/org/envirocar/app/views/obdselection/OBDSelectionFragment.java b/org.envirocar.app/src/org/envirocar/app/views/obdselection/OBDSelectionFragment.java index 4d7b439da..31a277846 100644 --- a/org.envirocar.app/src/org/envirocar/app/views/obdselection/OBDSelectionFragment.java +++ b/org.envirocar.app/src/org/envirocar/app/views/obdselection/OBDSelectionFragment.java @@ -18,7 +18,6 @@ */ package org.envirocar.app.views.obdselection; -import android.Manifest; import android.bluetooth.BluetoothDevice; import android.os.Bundle; import android.os.CountDownTimer; @@ -47,7 +46,6 @@ import org.envirocar.core.logging.Logger; import org.jetbrains.annotations.NotNull; -import java.util.List; import java.util.Set; import javax.inject.Inject; @@ -59,16 +57,13 @@ import io.reactivex.disposables.Disposable; import io.reactivex.observers.DisposableObserver; import io.reactivex.schedulers.Schedulers; -import pub.devrel.easypermissions.EasyPermissions; -import pub.devrel.easypermissions.PermissionRequest; - /** * TODO JavaDoc * * @author dewall */ -public class OBDSelectionFragment extends BaseInjectorFragment implements EasyPermissions.PermissionCallbacks { +public class OBDSelectionFragment extends BaseInjectorFragment { private static final Logger LOGGER = Logger.getLogger(OBDSelectionFragment.class); @Override @@ -136,14 +131,13 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle // // TODO: very ugly... Instead a dynamic LinearLayout should be used. // setDynamicListHeight(mNewDevicesListView); // setDynamicListHeight(mPairedDevicesListView); - return contentView; } @Override public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); - checkAndRequestPermissions(); + startBluetoothDiscovery(); } @Override @@ -166,7 +160,6 @@ public void onBluetoothStateChangedEvent(BluetoothStateChangedEvent event) { @OnClick(R.id.activity_obd_selection_layout_rescan_bluetooth) protected void rediscover() { mBluetoothHandler.stopBluetoothDeviceDiscovery(); - checkAndRequestPermissions(); } /** @@ -188,65 +181,6 @@ private void updateContentView() { } } - private final int BLUETOOTH_PERMISSIONS = 1; - - @Override - public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { - super.onRequestPermissionsResult(requestCode, permissions, grantResults); - - EasyPermissions.onRequestPermissionsResult(requestCode,permissions,grantResults,this); - } - - public void checkAndRequestPermissions() { - String[] perms; - if (android.os.Build.VERSION.SDK_INT >= 31) { - perms = new String[]{ - Manifest.permission.ACCESS_FINE_LOCATION, - Manifest.permission.ACCESS_COARSE_LOCATION, - Manifest.permission.BLUETOOTH_CONNECT, - Manifest.permission.BLUETOOTH_SCAN - }; - } - else{ - perms = new String[]{ - Manifest.permission.ACCESS_FINE_LOCATION, - Manifest.permission.ACCESS_COARSE_LOCATION - }; - } - - if (EasyPermissions.hasPermissions(getContext(), perms)){ - // if all permissions are granted, start bluetooth discovery. - startBluetoothDiscovery(); - } - else{ - // Dialog requesting the user for location permission. - EasyPermissions.requestPermissions( - new PermissionRequest.Builder(this, BLUETOOTH_PERMISSIONS, perms) - .setRationale(R.string.location_permission_to_discover_newdevices) - .setPositiveButtonText(R.string.grant_permissions) - .setNegativeButtonText(R.string.cancel) - .setTheme(R.style.MaterialDialog) - .build()); - } - } - - @Override - public void onPermissionsGranted(int requestCode, @NonNull @NotNull List perms) { - // if location permissions are granted, start Bluetooth discovery. - if (requestCode == BLUETOOTH_PERMISSIONS) { - startBluetoothDiscovery(); - showSnackbar(getString(R.string.location_permission_granted)); - } - } - - @Override - public void onPermissionsDenied(int requestCode, @NonNull @NotNull List perms) { - // if permissions are not granted, show toast. - if (requestCode == BLUETOOTH_PERMISSIONS) { - showSnackbar(getString(R.string.location_permission_denied)); - } - } - @Override public void onResume() { super.onResume(); From fb8fe1c2a1a17f56a38c5874a35a5a86c81d225a Mon Sep 17 00:00:00 2001 From: cdhiraj40 Date: Tue, 15 Mar 2022 01:36:44 +0530 Subject: [PATCH 2/2] added proper comments --- .../app/views/obdselection/OBDSelectionActivity.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/org.envirocar.app/src/org/envirocar/app/views/obdselection/OBDSelectionActivity.java b/org.envirocar.app/src/org/envirocar/app/views/obdselection/OBDSelectionActivity.java index 5db3c82d9..34d9c7ffe 100644 --- a/org.envirocar.app/src/org/envirocar/app/views/obdselection/OBDSelectionActivity.java +++ b/org.envirocar.app/src/org/envirocar/app/views/obdselection/OBDSelectionActivity.java @@ -95,7 +95,6 @@ protected void onCreate(Bundle savedInstanceState) { if (mOBDSelectionFragment == null) mOBDSelectionFragment = new OBDSelectionFragment(); - checkAndRequestPermissions(); // Setup the bluetooth toolbar setupBluetoothSwitch(); @@ -201,7 +200,7 @@ public void checkAndRequestPermissions() { } if (EasyPermissions.hasPermissions(this, perms)){ - // if all permissions are granted, start bluetooth discovery. + // if all permissions are granted, navigate to OBDSelectionFragment. navigateToOBDSelection(); } else{ @@ -218,7 +217,7 @@ public void checkAndRequestPermissions() { @Override public void onPermissionsGranted(int requestCode, @NonNull @NotNull List perms) { - // if location permissions are granted, start Bluetooth discovery. + // if location permissions are granted, navigate to OBDSelectionFragment. if (requestCode == BLUETOOTH_PERMISSIONS) { navigateToOBDSelection(); showSnackbar(getString(R.string.location_permission_granted));