From e32d36cdc0070acf4539042981652b8ce61a7058 Mon Sep 17 00:00:00 2001 From: Aman Nishad <1000017313@dit.edu.in> Date: Tue, 25 Jul 2023 21:42:21 +0530 Subject: [PATCH] Added Permission Checks --- .../src/org/envirocar/obd/OBDSimulator.java | 43 +++++++++++++------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/android-obd-simulator/src/org/envirocar/obd/OBDSimulator.java b/android-obd-simulator/src/org/envirocar/obd/OBDSimulator.java index 9dcdf9292..91699f6cb 100644 --- a/android-obd-simulator/src/org/envirocar/obd/OBDSimulator.java +++ b/android-obd-simulator/src/org/envirocar/obd/OBDSimulator.java @@ -18,9 +18,11 @@ */ package org.envirocar.obd; +import android.Manifest; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.content.Intent; +import android.content.pm.PackageManager; import android.os.Bundle; import android.os.Handler; import android.os.Message; @@ -74,7 +76,7 @@ public class OBDSimulator extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - if(D) Log.e(TAG, "+++ ON CREATE +++"); + if (D) Log.e(TAG, "+++ ON CREATE +++"); // Set up the window layout setContentView(R.layout.main); @@ -93,14 +95,18 @@ public void onCreate(Bundle savedInstanceState) { @Override public void onStart() { super.onStart(); - if(D) Log.e(TAG, "++ ON START ++"); + if (D) Log.e(TAG, "++ ON START ++"); // If BT is not on, request that it be enabled. // setupChat() will then be called during onActivityResult if (!mBluetoothAdapter.isEnabled()) { Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); + if (checkSelfPermission(Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) { + + return; + } startActivityForResult(enableIntent, REQUEST_ENABLE_BT); - // Otherwise, setup the chat session + // Otherwise, setup the chat session } else { if (mChatService == null) setupChat(); } @@ -109,7 +115,7 @@ public void onStart() { @Override public synchronized void onResume() { super.onResume(); - if(D) Log.e(TAG, "+ ON RESUME +"); + if (D) Log.e(TAG, "+ ON RESUME +"); // Performing this check in onResume() covers the case in which BT was // not enabled during onStart(), so we were paused to enable it... @@ -117,8 +123,8 @@ public synchronized void onResume() { if (mChatService != null) { // Only if the state is STATE_NONE, do we know that we haven't started already if (mChatService.getState() == SimulatorService.STATE_NONE) { - // Start the Bluetooth chat services - mChatService.start(); + // Start the Bluetooth chat services + mChatService.start(); } } } @@ -137,7 +143,7 @@ private void setupChat() { mSendButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { // Send a message using content of the edit text widget - //TODO disable enable + //TODO disable enable } }); @@ -148,13 +154,13 @@ public void onClick(View v) { @Override public synchronized void onPause() { super.onPause(); - if(D) Log.e(TAG, "- ON PAUSE -"); + if (D) Log.e(TAG, "- ON PAUSE -"); } @Override public void onStop() { super.onStop(); - if(D) Log.e(TAG, "-- ON STOP --"); + if (D) Log.e(TAG, "-- ON STOP --"); } @Override @@ -162,15 +168,28 @@ public void onDestroy() { super.onDestroy(); // Stop the Bluetooth chat services if (mChatService != null) mChatService.stop(); - if(D) Log.e(TAG, "--- ON DESTROY ---"); + if (D) Log.e(TAG, "--- ON DESTROY ---"); } private void ensureDiscoverable() { - if(D) Log.d(TAG, "ensure discoverable"); + if (D) Log.d(TAG, "ensure discoverable"); + if (checkSelfPermission(Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) { + // TODO: Consider calling + // Activity#requestPermissions + // here to request the missing permissions, and then overriding + // public void onRequestPermissionsResult(int requestCode, String[] permissions, + // int[] grantResults) + // to handle the case where the user grants the permission. See the documentation + // for Activity#requestPermissions for more details. + return; + } if (mBluetoothAdapter.getScanMode() != - BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) { + BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) { Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); + if (checkSelfPermission(Manifest.permission.BLUETOOTH_ADVERTISE) != PackageManager.PERMISSION_GRANTED) { + return; + } startActivity(discoverableIntent); } }