Config.java revision eb7b90f5b93db1230a5b64caa3d8d05a642e33a6
1/* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.bluetooth.btservice; 18 19import java.util.ArrayList; 20 21import android.bluetooth.BluetoothProfile; 22import android.content.ContentResolver; 23import android.content.Context; 24import android.content.res.Resources; 25import android.provider.Settings; 26import android.util.Log; 27 28import com.android.bluetooth.R; 29import com.android.bluetooth.a2dp.A2dpService; 30import com.android.bluetooth.a2dp.A2dpSinkService; 31import com.android.bluetooth.avrcp.AvrcpControllerService; 32import com.android.bluetooth.hdp.HealthService; 33import com.android.bluetooth.hfp.HeadsetService; 34import com.android.bluetooth.hfpclient.HeadsetClientService; 35import com.android.bluetooth.hid.HidService; 36import com.android.bluetooth.pan.PanService; 37import com.android.bluetooth.gatt.GattService; 38import com.android.bluetooth.map.BluetoothMapService; 39import com.android.bluetooth.sap.SapService; 40 41public class Config { 42 private static final String TAG = "AdapterServiceConfig"; 43 /** 44 * List of profile services. 45 */ 46 @SuppressWarnings("rawtypes") 47 //Do not inclue OPP and PBAP, because their services 48 //are not managed by AdapterService 49 private static final Class[] PROFILE_SERVICES = { 50 HeadsetService.class, 51 A2dpService.class, 52 A2dpSinkService.class, 53 HidService.class, 54 HealthService.class, 55 PanService.class, 56 GattService.class, 57 BluetoothMapService.class, 58 HeadsetClientService.class, 59 AvrcpControllerService.class, 60 SapService.class 61 }; 62 /** 63 * Resource flag to indicate whether profile is supported or not. 64 */ 65 private static final int[] PROFILE_SERVICES_FLAG = { 66 R.bool.profile_supported_hs_hfp, 67 R.bool.profile_supported_a2dp, 68 R.bool.profile_supported_a2dp_sink, 69 R.bool.profile_supported_hid, 70 R.bool.profile_supported_hdp, 71 R.bool.profile_supported_pan, 72 R.bool.profile_supported_gatt, 73 R.bool.profile_supported_map, 74 R.bool.profile_supported_hfpclient, 75 R.bool.profile_supported_avrcp_controller, 76 R.bool.profile_supported_sap, 77 }; 78 79 private static Class[] SUPPORTED_PROFILES = new Class[0]; 80 81 static void init(Context ctx) { 82 if (ctx == null) { 83 return; 84 } 85 Resources resources = ctx.getResources(); 86 if (resources == null) { 87 return; 88 } 89 90 ArrayList<Class> profiles = new ArrayList<Class>(PROFILE_SERVICES.length); 91 for (int i=0; i < PROFILE_SERVICES_FLAG.length; i++) { 92 boolean supported = resources.getBoolean(PROFILE_SERVICES_FLAG[i]); 93 if (supported && !isProfileDisabled(ctx, PROFILE_SERVICES[i])) { 94 Log.d(TAG, "Adding " + PROFILE_SERVICES[i].getSimpleName()); 95 profiles.add(PROFILE_SERVICES[i]); 96 } 97 } 98 int totalProfiles = profiles.size(); 99 SUPPORTED_PROFILES = new Class[totalProfiles]; 100 profiles.toArray(SUPPORTED_PROFILES); 101 } 102 103 static Class[] getSupportedProfiles() { 104 return SUPPORTED_PROFILES; 105 } 106 107 private static boolean isProfileDisabled(Context context, Class profile) { 108 int profileIndex = -1; 109 110 if (profile == HeadsetService.class) { 111 profileIndex = BluetoothProfile.HEADSET; 112 } else if (profile == A2dpService.class) { 113 profileIndex = BluetoothProfile.A2DP; 114 } else if (profile == A2dpSinkService.class) { 115 profileIndex = BluetoothProfile.A2DP_SINK; 116 } else if (profile == HidService.class) { 117 profileIndex = BluetoothProfile.INPUT_DEVICE; 118 } else if (profile == HealthService.class) { 119 profileIndex = BluetoothProfile.HEALTH; 120 } else if (profile == PanService.class) { 121 profileIndex = BluetoothProfile.PAN; 122 } else if (profile == GattService.class) { 123 profileIndex = BluetoothProfile.GATT; 124 } else if (profile == BluetoothMapService.class) { 125 profileIndex = BluetoothProfile.MAP; 126 } else if (profile == HeadsetClientService.class) { 127 profileIndex = BluetoothProfile.HEADSET_CLIENT; 128 } else if (profile == AvrcpControllerService.class) { 129 profileIndex = BluetoothProfile.AVRCP_CONTROLLER; 130 } else if (profile == SapService.class) { 131 profileIndex = BluetoothProfile.SAP; 132 } 133 134 if (profileIndex == -1) { 135 Log.d(TAG, "Could not find profile bit mask"); 136 return false; 137 } 138 139 final ContentResolver resolver = context.getContentResolver(); 140 final long disabledProfilesBitMask = Settings.Global.getLong(resolver, 141 Settings.Global.BLUETOOTH_DISABLED_PROFILES, 0); 142 long profileBit = 1 << profileIndex; 143 144 return (disabledProfilesBitMask & profileBit) != 0; 145 } 146} 147