Added support for enabling/disabling profiles

Change-Id: I819ca7920d9d866bd3aca88d4365a18e2369ee81
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 698c35b..e9022e2 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -67,11 +67,13 @@
         <service
             android:process="@string/process"
             android:name=".opp.BluetoothOppService"
-            android:permission="android.permission.ACCESS_BLUETOOTH_SHARE" />
+            android:permission="android.permission.ACCESS_BLUETOOTH_SHARE"
+            android:enabled="@bool/profile_supported_opp"/>
         <receiver
             android:process="@string/process"
             android:exported="true"
-            android:name=".opp.BluetoothOppReceiver">
+            android:name=".opp.BluetoothOppReceiver"
+            android:enabled="@bool/profile_supported_opp">
             <intent-filter>
                 <action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
                 <!--action android:name="android.intent.action.BOOT_COMPLETED" /-->
@@ -99,7 +101,9 @@
         </receiver>
         <activity android:name=".opp.BluetoothOppLauncherActivity"
             android:process="@string/process"
-            android:theme="@android:style/Theme.Holo.Dialog" android:label="@string/bt_share_picker_label">
+            android:theme="@android:style/Theme.Holo.Dialog"
+            android:label="@string/bt_share_picker_label"
+            android:enabled="@bool/profile_supported_opp">
             <intent-filter>
                 <action android:name="android.intent.action.SEND" />
                 <category android:name="android.intent.category.DEFAULT" />
@@ -132,7 +136,8 @@
         <activity android:name=".opp.BluetoothOppBtEnableActivity"
                   android:process="@string/process"
                   android:excludeFromRecents="true"
-                  android:theme="@*android:style/Theme.Holo.Dialog.Alert">
+                  android:theme="@*android:style/Theme.Holo.Dialog.Alert"
+                  android:enabled="@bool/profile_supported_opp">
         </activity>
         <activity android:name=".opp.BluetoothOppBtErrorActivity"
                   android:process="@string/process"
@@ -142,22 +147,26 @@
         <activity android:name=".opp.BluetoothOppBtEnablingActivity"
                   android:process="@string/process"
                   android:excludeFromRecents="true"
-                  android:theme="@*android:style/Theme.Holo.Dialog.Alert">
+                  android:theme="@*android:style/Theme.Holo.Dialog.Alert"
+                  android:enabled="@bool/profile_supported_opp">
         </activity>
         <activity android:name=".opp.BluetoothOppIncomingFileConfirmActivity"
                   android:process="@string/process"
                   android:excludeFromRecents="true"
-                  android:theme="@*android:style/Theme.Holo.Dialog.Alert">
+                  android:theme="@*android:style/Theme.Holo.Dialog.Alert"
+                  android:enabled="@bool/profile_supported_opp">
         </activity>
         <activity android:name=".opp.BluetoothOppTransferActivity"
                   android:process="@string/process"
                   android:excludeFromRecents="true"
-                  android:theme="@*android:style/Theme.Holo.Dialog.Alert">
+                  android:theme="@*android:style/Theme.Holo.Dialog.Alert"
+                  android:enabled="@bool/profile_supported_opp">
         </activity>
         <activity android:name=".opp.BluetoothOppLiveFolder"
             android:process="@string/process"
             android:icon="@drawable/ic_launcher_folder_bluetooth"
-            android:label="@string/btopp_live_folder">
+            android:label="@string/btopp_live_folder"
+            android:enabled="@bool/profile_supported_opp">
             <intent-filter>
                 <action android:name="android.intent.action.CREATE_LIVE_FOLDER" />
                 <category android:name="android.intent.category.DEFAULT" />
@@ -167,19 +176,22 @@
                   android:process="@string/process"
                   android:label=""
                   android:excludeFromRecents="true"
-                  android:configChanges="orientation|keyboardHidden">
+                  android:configChanges="orientation|keyboardHidden"
+                  android:enabled="@bool/profile_supported_opp">
         </activity>
         <activity android:name=".pbap.BluetoothPbapActivity"
             android:process="@string/process"
             android:excludeFromRecents="true"
-            android:theme="@*android:style/Theme.Holo.Dialog.Alert">
+            android:theme="@*android:style/Theme.Holo.Dialog.Alert"
+            android:enabled="@bool/profile_supported_pbap">
             <intent-filter>
                 <category android:name="android.intent.category.DEFAULT" />
             </intent-filter>
         </activity>
         <service
             android:process="@string/process"
-            android:name=".pbap.BluetoothPbapService" >
+            android:name=".pbap.BluetoothPbapService"
+            android:enabled="@bool/profile_supported_pbap" >
             <intent-filter>
                 <action android:name="android.bluetooth.IBluetoothPbap" />
             </intent-filter>
@@ -187,7 +199,8 @@
         <receiver
             android:process="@string/process"
             android:exported="true"
-            android:name=".pbap.BluetoothPbapReceiver">
+            android:name=".pbap.BluetoothPbapReceiver"
+            android:enabled="@bool/profile_supported_pbap">
             <intent-filter>
                 <action android:name="android.bluetooth.adapter.action.STATE_CHANGED"/>
                 <action android:name="android.bluetooth.device.action.CONNECTION_ACCESS_REPLY" />
@@ -196,35 +209,40 @@
         </receiver>
         <service
             android:process="@string/process"
-            android:name = ".hfp.HeadsetService">
+            android:name = ".hfp.HeadsetService"
+            android:enabled="@bool/profile_supported_hs_hfp">
             <intent-filter>
                 <action android:name="android.bluetooth.IBluetoothHeadset" />
             </intent-filter>
         </service>
         <service
             android:process="@string/process"
-            android:name = ".a2dp.A2dpService">
+            android:name = ".a2dp.A2dpService"
+            android:enabled="@bool/profile_supported_a2dp">
             <intent-filter>
                 <action android:name="android.bluetooth.IBluetoothA2dp" />
             </intent-filter>
         </service>
         <service
             android:process="@string/process"
-            android:name = ".hid.HidService">
+            android:name = ".hid.HidService"
+            android:enabled="@bool/profile_supported_hid">
             <intent-filter>
                 <action android:name="android.bluetooth.IBluetoothInputDevice" />
             </intent-filter>
         </service>
         <service
             android:process="@string/process"
-            android:name = ".hdp.HealthService">
+            android:name = ".hdp.HealthService"
+            android:enabled="@bool/profile_supported_hdp">
             <intent-filter>
                 <action android:name="android.bluetooth.IBluetoothHealth" />
             </intent-filter>
         </service>
         <service
             android:process="@string/process"
-            android:name = ".pan.PanService">
+            android:name = ".pan.PanService"
+            android:enabled="@bool/profile_supported_pan">
             <intent-filter>
                 <action android:name="android.bluetooth.IBluetoothPan" />
             </intent-filter>
diff --git a/res/values/config.xml b/res/values/config.xml
index 538325e..700be24 100644
--- a/res/values/config.xml
+++ b/res/values/config.xml
@@ -13,6 +13,13 @@
    limitations under the License.
 -->
 <resources>
+    <bool name="profile_supported_a2dp">true</bool>
+    <bool name="profile_supported_hdp">true</bool>
+    <bool name="profile_supported_hs_hfp">true</bool>
+    <bool name="profile_supported_hid">true</bool>
+    <bool name="profile_supported_opp">true</bool>
+    <bool name="profile_supported_pan">true</bool>
+    <bool name="profile_supported_pbap">true</bool>
     <bool name="pbap_include_photos_in_vcard">true</bool>
     <bool name="pbap_use_profile_for_owner_vcard">true</bool>
 </resources>
\ No newline at end of file
diff --git a/src/com/android/bluetooth/btservice/AdapterApp.java b/src/com/android/bluetooth/btservice/AdapterApp.java
index be5d769..f2d989e 100644
--- a/src/com/android/bluetooth/btservice/AdapterApp.java
+++ b/src/com/android/bluetooth/btservice/AdapterApp.java
@@ -36,6 +36,7 @@
     public void onCreate() {
         super.onCreate();
         if (DBG) Log.d(TAG, "onCreate");
+        Config.init(this);
     }
 
     @Override
diff --git a/src/com/android/bluetooth/btservice/AdapterService.java b/src/com/android/bluetooth/btservice/AdapterService.java
index 8b81e27..2ea073a 100755
--- a/src/com/android/bluetooth/btservice/AdapterService.java
+++ b/src/com/android/bluetooth/btservice/AdapterService.java
@@ -39,10 +39,12 @@
 import com.android.bluetooth.hfp.HeadsetService;
 import com.android.bluetooth.hdp.HealthService;
 import com.android.bluetooth.pan.PanService;
+import com.android.bluetooth.R;
 import com.android.bluetooth.Utils;
 import com.android.bluetooth.btservice.RemoteDevices.DeviceProperties;
 import java.io.FileDescriptor;
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Set;
 import java.util.Map;
@@ -58,19 +60,6 @@
     //For Debugging only
     private static int sRefCount=0;
 
-    /**
-     * List of profile services to support.Comment out to disable a profile
-     * Profiles started in order of appearance
-     */
-    @SuppressWarnings("rawtypes")
-    private static final Class[] SUPPORTED_PROFILE_SERVICES = {
-        HeadsetService.class,
-        A2dpService.class,
-        HidService.class,
-        HealthService.class,
-        PanService.class
-    };
-
     public static final String ACTION_LOAD_ADAPTER_PROPERTIES="com.android.bluetooth.btservice.action.LOAD_ADAPTER_PROPERTIES";
     public static final String ACTION_SERVICE_STATE_CHANGED="com.android.bluetooth.btservice.action.STATE_CHANGED";
     public static final String EXTRA_ACTION="action";
@@ -319,7 +308,7 @@
 
     void processStart() {
         if (DBG) debugLog("processStart()");
-
+        Class[] SUPPORTED_PROFILE_SERVICES = 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);
@@ -351,6 +340,7 @@
     }
 
     boolean stopProfileServices() {
+        Class[] SUPPORTED_PROFILE_SERVICES = Config.getSupportedProfiles();
         if (mProfilesStarted && SUPPORTED_PROFILE_SERVICES.length>0) {
             setProfileServiceState(SUPPORTED_PROFILE_SERVICES,BluetoothAdapter.STATE_OFF);
             return true;
diff --git a/src/com/android/bluetooth/btservice/Config.java b/src/com/android/bluetooth/btservice/Config.java
new file mode 100644
index 0000000..057ae63
--- /dev/null
+++ b/src/com/android/bluetooth/btservice/Config.java
@@ -0,0 +1,68 @@
+package com.android.bluetooth.btservice;
+
+import java.util.ArrayList;
+
+import android.content.Context;
+import android.content.res.Resources;
+import android.util.Log;
+
+import com.android.bluetooth.R;
+import com.android.bluetooth.a2dp.A2dpService;
+import com.android.bluetooth.hdp.HealthService;
+import com.android.bluetooth.hfp.HeadsetService;
+import com.android.bluetooth.hid.HidService;
+import com.android.bluetooth.pan.PanService;
+
+public class Config {
+    private static final String TAG = "AdapterServiceConfig";
+    /**
+     * List of profile services.
+     */
+    @SuppressWarnings("rawtypes")
+    //Do not inclue OPP and PBAP, because their services
+    //are not managed by AdapterService
+    private static final Class[] PROFILE_SERVICES = {
+        HeadsetService.class,
+        A2dpService.class,
+        HidService.class,
+        HealthService.class,
+        PanService.class
+    };
+    /**
+     * Resource flag to indicate whether profile is supported or not.
+     */
+    private static final int[]  PROFILE_SERVICES_FLAG = {
+        R.bool.profile_supported_hs_hfp,
+        R.bool.profile_supported_a2dp,
+        R.bool.profile_supported_hid,
+        R.bool.profile_supported_hdp,
+        R.bool.profile_supported_pan
+    };
+
+    private static Class[] SUPPORTED_PROFILES = new Class[0];
+
+    static void init(Context ctx) {
+        if (ctx == null) {
+            return;
+        }
+        Resources resources = ctx.getResources();
+        if (resources == null) {
+            return;
+        }
+        ArrayList<Class> profiles = new ArrayList<Class>(PROFILE_SERVICES.length);
+        for (int i=0; i < PROFILE_SERVICES_FLAG.length; i++) {
+            boolean supported = resources.getBoolean(PROFILE_SERVICES_FLAG[i]);
+            if (supported) {
+                Log.d(TAG, "Adding " + PROFILE_SERVICES[i].getSimpleName());
+                profiles.add(PROFILE_SERVICES[i]);
+            }
+        }
+        int totalProfiles = profiles.size();
+        SUPPORTED_PROFILES = new Class[totalProfiles];
+        profiles.toArray(SUPPORTED_PROFILES);
+    }
+
+    static Class[]  getSupportedProfiles() {
+        return SUPPORTED_PROFILES;
+    }
+}
diff --git a/src/com/android/bluetooth/pan/PanService.java b/src/com/android/bluetooth/pan/PanService.java
index 97fe37b..6116ee8 100755
--- a/src/com/android/bluetooth/pan/PanService.java
+++ b/src/com/android/bluetooth/pan/PanService.java
@@ -89,7 +89,6 @@
     }
 
     protected boolean stop() {
-        if (DBG) log("stop");
         mHandler.removeCallbacksAndMessages(null);
         return true;
     }