Skip to content

Commit

Permalink
Merge pull request #405 from smartdevicelink/bugfix/issue_376
Browse files Browse the repository at this point in the history
Check for BT Permission in SdlRouterService.onCreate()
  • Loading branch information
joeygrover committed Feb 15, 2017
2 parents 8595a89 + e8c9464 commit 9914b4b
Showing 1 changed file with 36 additions and 9 deletions.
Expand Up @@ -21,6 +21,7 @@
import org.json.JSONException;
import org.json.JSONObject;

import android.Manifest;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActivityManager;
Expand Down Expand Up @@ -72,7 +73,6 @@
import com.smartdevicelink.transport.utl.ByteArrayMessageSpliter;
import com.smartdevicelink.util.AndroidTools;
import com.smartdevicelink.util.BitConverter;

/**
* <b>This class should not be modified by anyone outside of the approved contributors of the SmartDeviceLink project.</b>
* This service is a central point of communication between hardware and the registered clients. It will multiplex a single transport
Expand Down Expand Up @@ -122,7 +122,8 @@ public class SdlRouterService extends Service{
private final static int ALT_TRANSPORT_TIMEOUT_RUNNABLE = 30000;

private boolean wrongProcess = false;

private boolean initPassed = false;

private Intent lastReceivedStartIntent = null;
public static HashMap<Long,RegisteredApp> registeredApps;
private SparseArray<Long> sessionMap;
Expand Down Expand Up @@ -774,23 +775,46 @@ private boolean processCheck(){
return false;

}

private boolean permissionCheck(String permissionToCheck){
if(permissionToCheck == null){
throw new IllegalArgumentException("permission is null");
}
return PackageManager.PERMISSION_GRANTED == getBaseContext().checkPermission(permissionToCheck, android.os.Process.myPid(), android.os.Process.myUid());
}

@Override
public void onCreate() {
super.onCreate();

/**
* Runs several checks to ensure this router service has the correct conditions to run properly
* @return true if this service is set up correctly
*/
private boolean initCheck(){
if(!processCheck()){
Log.e(TAG, "Not using correct process. Shutting down");
wrongProcess = true;
stopSelf();
return;
return false;
}
if(!permissionCheck(Manifest.permission.BLUETOOTH)){
Log.e(TAG, "Bluetooth Permission is not granted. Shutting down");
return false;
}
if(!AndroidTools.isServiceExported(this, new ComponentName(this, this.getClass()))){ //We want to check to see if our service is actually exported
Log.e(TAG, "Service isn't exported. Shutting down");
return false;
}
return true;
}


@Override
public void onCreate() {
super.onCreate();

if(!initCheck()){ // Run checks on process and permissions
stopSelf();
return;
}
else{Log.d(TAG, "We are in the correct process");}
initPassed = true;

synchronized(REGISTERED_APPS_LOCK){
registeredApps = new HashMap<Long,RegisteredApp>();
}
Expand Down Expand Up @@ -853,6 +877,9 @@ public void startUpSequence(){

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(!initPassed) {
return super.onStartCommand(intent, flags, startId);
}
if(registeredApps == null){
synchronized(REGISTERED_APPS_LOCK){
registeredApps = new HashMap<Long,RegisteredApp>();
Expand Down

0 comments on commit 9914b4b

Please sign in to comment.