Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Permission Checks #993

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 31 additions & 12 deletions android-obd-simulator/src/org/envirocar/obd/OBDSimulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Comment on lines +104 to +107
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, what else then, if permissions are not granted? Shouldn't they be requested? Just doing nothing, will make the simulator unusable.

startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
// Otherwise, setup the chat session
// Otherwise, setup the chat session
} else {
if (mChatService == null) setupChat();
}
Expand All @@ -109,16 +115,16 @@ 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...
// onResume() will be called when ACTION_REQUEST_ENABLE activity returns.
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();
}
}
}
Expand All @@ -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
}
});

Expand All @@ -148,29 +154,42 @@ 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
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;
Comment on lines +176 to +184
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. The recent implementation requests permissions and closes the app, if permissions are declined. But your implementation just do nothing then. Your IDE even suggests you, what to do: call requestPermissions and overwrite onRequestPermissionsResult to handle permission grant or decline. And this is the way to go.

}
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);
}
}
Expand Down