Removed onStartCommand().

BT ON/OFF now done via enable() and disable() binder API call.
Also fixed bug with BT OFF if set scan mode times out

Change-Id: Ib88dfc148bb5a24cda1e84a86f044ae85a5a6514
diff --git a/src/com/android/bluetooth/btservice/AdapterService.java b/src/com/android/bluetooth/btservice/AdapterService.java
index 6585e3f..1e56129 100755
--- a/src/com/android/bluetooth/btservice/AdapterService.java
+++ b/src/com/android/bluetooth/btservice/AdapterService.java
@@ -256,62 +256,12 @@
         cleanup();
     }
 
-    public int onStartCommand(Intent intent ,int flags, int startId) {
-        mCurrentRequestId = startId;
-        if (mCleaningUp) {
-            Log.e(TAG,"*************Received new request while service is cleaning up****************************");
-        }
-
-        if (DBG) debugLog("onStartCommand: flags = " + flags + ", startId = " + startId);
-        if (checkCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM)!=PackageManager.PERMISSION_GRANTED) {
-            Log.e(TAG, "Permission denied!");
-            return ADAPTER_SERVICE_TYPE;
-        }
-
-        //Check if we are restarting
-        if (intent == null) {
-            debugLog("Restarting AdapterService");
-            return ADAPTER_SERVICE_TYPE;
-        }
-
-        //Get action and check if valid. If invalid, ignore and return
-        String action  = intent.getStringExtra(EXTRA_ACTION);
-        debugLog("onStartCommand(): action = " + action);
-        if (!ACTION_SERVICE_STATE_CHANGED.equals(action)) {
-            Log.w(TAG,"Unknown action: " + action);
-            return ADAPTER_SERVICE_TYPE;
-        }
-
-        //Check state of request. If invalid, ignore and return
-        int state= intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,BluetoothAdapter.ERROR);
-        debugLog("onStartCommand(): state = " + Utils.debugGetAdapterStateString(state));
-
-        //Cancel any pending shutdown requests
-        synchronized (mHandler) {
-            mHandler.removeMessages(MESSAGE_SHUTDOWN);
-        }
-
-        if (state == BluetoothAdapter.STATE_OFF) {
-            Message m = mAdapterStateMachine.obtainMessage(AdapterState.USER_TURN_OFF);
-            m.arg1= startId;
-            mAdapterStateMachine.sendMessage(m);
-        } else if (state == BluetoothAdapter.STATE_ON) {
-            Message m = mAdapterStateMachine.obtainMessage(AdapterState.USER_TURN_ON);
-            m.arg1= startId;
-            mAdapterStateMachine.sendMessage(m);
-        } else {
-            Log.w(TAG,"Invalid state: " + action);
-            return ADAPTER_SERVICE_TYPE;
-        }
-        return ADAPTER_SERVICE_TYPE;
-    }
-
     void processStart() {
         if (DBG) debugLog("processStart()");
-        Class[] SUPPORTED_PROFILE_SERVICES = Config.getSupportedProfiles();
+        Class[] supportedProfileServices = Config.getSupportedProfiles();
         //Initialize data objects
-        for (int i=0; i < SUPPORTED_PROFILE_SERVICES.length;i++) {
-            mProfileServicesState.put(SUPPORTED_PROFILE_SERVICES[i].getName(),BluetoothAdapter.STATE_OFF);
+        for (int i=0; i < supportedProfileServices.length;i++) {
+            mProfileServicesState.put(supportedProfileServices[i].getName(),BluetoothAdapter.STATE_OFF);
         }
         mRemoteDevices = new RemoteDevices(this);
         mBondStateMachine = new BondStateMachine(this, mAdapterProperties, mRemoteDevices);
@@ -326,9 +276,9 @@
         setAdapterService(this);
 
         //Start profile services
-        if (!mProfilesStarted && SUPPORTED_PROFILE_SERVICES.length >0) {
+        if (!mProfilesStarted && supportedProfileServices.length >0) {
             //Startup all profile services
-            setProfileServiceState(SUPPORTED_PROFILE_SERVICES,BluetoothAdapter.STATE_ON);
+            setProfileServiceState(supportedProfileServices,BluetoothAdapter.STATE_ON);
         }else {
             if (DBG) {debugLog("processStart(): Profile Services alreay started");}
             mAdapterStateMachine.sendMessage(mAdapterStateMachine.obtainMessage(AdapterState.STARTED));
@@ -340,9 +290,9 @@
     }
 
     boolean stopProfileServices() {
-        Class[] SUPPORTED_PROFILE_SERVICES = Config.getSupportedProfiles();
-        if (mProfilesStarted && SUPPORTED_PROFILE_SERVICES.length>0) {
-            setProfileServiceState(SUPPORTED_PROFILE_SERVICES,BluetoothAdapter.STATE_OFF);
+        Class[] supportedProfileServices = Config.getSupportedProfiles();
+        if (mProfilesStarted && supportedProfileServices.length>0) {
+            setProfileServiceState(supportedProfileServices,BluetoothAdapter.STATE_OFF);
             return true;
         } else {
             if (DBG) {debugLog("stopProfileServices(): No profiles services to stop or already stopped.");}
@@ -350,19 +300,6 @@
         }
     }
 
-    void startShutdown(int requestId) {
-        debugLog("startShutdown(): requestId = " + requestId + ", currentRequestId=" + mCurrentRequestId);
-        if (requestId <0) {
-            Log.w(TAG, "Ignoring shutdown request. Invalid requestId");
-            return;
-        }
-        Message m = mHandler.obtainMessage(MESSAGE_SHUTDOWN);
-        synchronized(mHandler) {
-            mHandler.sendMessageDelayed(m, SHUTDOWN_TIMEOUT);
-        }
-        stopSelfResult(requestId);
-    }
-
      void updateAdapterState(int prevState, int newState){
         if (mCallbacks !=null) {
             int n=mCallbacks.beginBroadcast();
@@ -566,10 +503,10 @@
             return false;
         }
 
-        public boolean disable(boolean persist) {
+        public boolean disable() {
             AdapterService service = getService();
             if (service == null) return false;
-            return service.disable(persist);
+            return service.disable();
         }
 
         public String getAddress() {
@@ -786,19 +723,16 @@
         if (DBG) debugLog("enable() called...");
         Message m =
                 mAdapterStateMachine.obtainMessage(AdapterState.USER_TURN_ON);
-        m.arg1 = 1; //persist state
         mAdapterStateMachine.sendMessage(m);
         return true;
     }
 
-     boolean disable(boolean persist) {
+     boolean disable() {
         enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
                 "Need BLUETOOTH ADMIN permission");
         if (DBG) debugLog("disable() called...");
-        int val = (persist ? 1 : 0);
         Message m =
                 mAdapterStateMachine.obtainMessage(AdapterState.USER_TURN_OFF);
-        m.arg1 = val;
         mAdapterStateMachine.sendMessage(m);
         return true;
     }
diff --git a/src/com/android/bluetooth/btservice/AdapterState.java b/src/com/android/bluetooth/btservice/AdapterState.java
index 4c1c117..f6b4943 100755
--- a/src/com/android/bluetooth/btservice/AdapterState.java
+++ b/src/com/android/bluetooth/btservice/AdapterState.java
@@ -42,6 +42,7 @@
     static final int ENABLE_TIMEOUT = 101;
     static final int DISABLE_TIMEOUT = 103;
     static final int STOP_TIMEOUT = 104;
+    static final int SET_SCAN_MODE_TIMEOUT = 105;
 
     static final int USER_TURN_OFF_DELAY_MS=500;
 
@@ -98,11 +99,10 @@
                 Log.d(TAG, "Received quit request...");
                 return false;
                 } */
-            int requestId = msg.arg1;
 
             switch(msg.what) {
                case USER_TURN_ON:
-                   if (DBG) Log.d(TAG,"CURRENT_STATE=OFF, MESSAGE = USER_TURN_ON, requestId= " + msg.arg1);
+                   if (DBG) Log.d(TAG,"CURRENT_STATE=OFF, MESSAGE = USER_TURN_ON");
                    notifyAdapterStateChange(BluetoothAdapter.STATE_TURNING_ON);
                    mPendingCommandState.setTurningOn(true);
                    transitionTo(mPendingCommandState);
@@ -110,9 +110,8 @@
                    mAdapterService.processStart();
                    break;
                case USER_TURN_OFF:
-                   if (DBG) Log.d(TAG,"CURRENT_STATE=OFF, MESSAGE = USER_TURN_OFF, requestId= " + msg.arg1);
-                   //Handle case of service started and stopped without enable
-                   mAdapterService.startShutdown(requestId);
+                   if (DBG) Log.d(TAG,"CURRENT_STATE=OFF, MESSAGE = USER_TURN_OFF");
+                   //TODO: Handle case of service started and stopped without enable
                    break;
                default:
                    if (DBG) Log.d(TAG,"ERROR: UNEXPECTED MESSAGE: CURRENT_STATE=OFF, MESSAGE = " + msg.what );
@@ -132,22 +131,20 @@
         public boolean processMessage(Message msg) {
             switch(msg.what) {
                case USER_TURN_OFF:
-                   if (DBG) Log.d(TAG,"CURRENT_STATE=ON, MESSAGE = USER_TURN_OFF, requestId= " + msg.arg1);
+                   if (DBG) Log.d(TAG,"CURRENT_STATE=ON, MESSAGE = USER_TURN_OFF");
                    notifyAdapterStateChange(BluetoothAdapter.STATE_TURNING_OFF);
                    mPendingCommandState.setTurningOff(true);
-                   mPendingCommandState.setOffRequestId(msg.arg1);
                    transitionTo(mPendingCommandState);
 
                    // Invoke onBluetoothDisable which shall trigger a
                    // setScanMode to SCAN_MODE_NONE
-                   Message m = obtainMessage(BEGIN_DISABLE);
-                   m.arg1 = msg.arg1;
+                   Message m = obtainMessage(SET_SCAN_MODE_TIMEOUT);
                    sendMessageDelayed(m, PROPERTY_OP_DELAY);
                    mAdapterProperties.onBluetoothDisable();
                    break;
 
                case USER_TURN_ON:
-                   if (DBG) Log.d(TAG,"CURRENT_STATE=ON, MESSAGE = USER_TURN_ON, requestId= " + msg.arg1);
+                   if (DBG) Log.d(TAG,"CURRENT_STATE=ON, MESSAGE = USER_TURN_ON");
                    Log.i(TAG,"Bluetooth already ON, ignoring USER_TURN_ON");
                    break;
                default:
@@ -162,8 +159,6 @@
         private boolean mIsTurningOn;
         private boolean mIsTurningOff;
 
-        private int mRequestId;
-
         public void enter() {
             infoLog("Entering PendingCommandState State: isTurningOn()=" + isTurningOn() + ", isTurningOff()=" + isTurningOff());
         }
@@ -184,21 +179,13 @@
             return mIsTurningOff;
         }
 
-        public void setOffRequestId(int requestId) {
-            mRequestId = requestId;
-        }
-
-        public int getOffRequestId() {
-            return mRequestId;
-        }
-
         @Override
         public boolean processMessage(Message msg) {
             boolean isTurningOn= isTurningOn();
             boolean isTurningOff = isTurningOff();
             switch (msg.what) {
                 case USER_TURN_ON:
-                    if (DBG) Log.d(TAG,"CURRENT_STATE=PENDING, MESSAGE = USER_TURN_ON, requestId= " + msg.arg1
+                    if (DBG) Log.d(TAG,"CURRENT_STATE=PENDING, MESSAGE = USER_TURN_ON"
                             + ", isTurningOn=" + isTurningOn + ", isTurningOff=" + isTurningOff);
                     if (isTurningOn) {
                         Log.i(TAG,"CURRENT_STATE=PENDING: Alreadying turning on bluetooth... Ignoring USER_TURN_ON...");
@@ -208,7 +195,7 @@
                     }
                     break;
                 case USER_TURN_OFF:
-                    if (DBG) Log.d(TAG,"CURRENT_STATE=PENDING, MESSAGE = USER_TURN_ON, requestId= " + msg.arg1
+                    if (DBG) Log.d(TAG,"CURRENT_STATE=PENDING, MESSAGE = USER_TURN_ON"
                             + ", isTurningOn=" + isTurningOn + ", isTurningOff=" + isTurningOff);
                     if (isTurningOff) {
                         Log.i(TAG,"CURRENT_STATE=PENDING: Alreadying turning off bluetooth... Ignoring USER_TURN_OFF...");
@@ -243,10 +230,12 @@
                     notifyAdapterStateChange(BluetoothAdapter.STATE_ON);
                     break;
 
+                case SET_SCAN_MODE_TIMEOUT:
+                     Log.w(TAG,"Timeout will setting scan mode..Continuing with disable...");
+                     //Fall through
                 case BEGIN_DISABLE: {
                     if (DBG) Log.d(TAG,"CURRENT_STATE=PENDING, MESSAGE = BEGIN_DISABLE" + isTurningOn + ", isTurningOff=" + isTurningOff);
-                    removeMessages(BEGIN_DISABLE); //Remove extra message we setup in USER_TURN_OFF
-                    //Log.d(TAG,"CURRENT_STATE=ON, MESSAGE = BEGIN_DISABLE_ON, requestId= " + msg.arg1);
+                    removeMessages(SET_SCAN_MODE_TIMEOUT);
                     sendMessageDelayed(DISABLE_TIMEOUT, DISABLE_TIMEOUT_DELAY);
                     boolean ret = mAdapterService.disableNative();
                     if (!ret) {
@@ -254,7 +243,6 @@
                         Log.e(TAG, "Error while turning Bluetooth Off");
                         //FIXME: what about post enable services
                         mPendingCommandState.setTurningOff(false);
-                        mPendingCommandState.setOffRequestId(-1);
                         notifyAdapterStateChange(BluetoothAdapter.STATE_ON);
                     }
                 }
@@ -272,11 +260,8 @@
                     if (DBG) Log.d(TAG,"CURRENT_STATE=PENDING, MESSAGE = STOPPED, isTurningOn=" + isTurningOn + ", isTurningOff=" + isTurningOff);
                     removeMessages(STOP_TIMEOUT);
                     setTurningOff(false);
-                    int requestId= getOffRequestId();
-                    setOffRequestId(-1);
                     transitionTo(mOffState);
                     notifyAdapterStateChange(BluetoothAdapter.STATE_OFF);
-                    mAdapterService.startShutdown(requestId);
                     break;
                 case START_TIMEOUT:
                     if (DBG) Log.d(TAG,"CURRENT_STATE=PENDING, MESSAGE = START_TIMEOUT, isTurningOn=" + isTurningOn + ", isTurningOff=" + isTurningOff);