Home | History | Annotate | Download | only in btservice
      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 
     17 package com.android.bluetooth.btservice;
     18 
     19 import android.bluetooth.BluetoothProfile;
     20 import android.content.ContentResolver;
     21 import android.content.Context;
     22 import android.content.res.Resources;
     23 import android.provider.Settings;
     24 import android.util.Log;
     25 
     26 import com.android.bluetooth.R;
     27 import com.android.bluetooth.a2dp.A2dpService;
     28 import com.android.bluetooth.a2dpsink.A2dpSinkService;
     29 import com.android.bluetooth.avrcp.AvrcpTargetService;
     30 import com.android.bluetooth.avrcpcontroller.AvrcpControllerService;
     31 import com.android.bluetooth.gatt.GattService;
     32 import com.android.bluetooth.hdp.HealthService;
     33 import com.android.bluetooth.hearingaid.HearingAidService;
     34 import com.android.bluetooth.hfp.HeadsetService;
     35 import com.android.bluetooth.hfpclient.HeadsetClientService;
     36 import com.android.bluetooth.hid.HidDeviceService;
     37 import com.android.bluetooth.hid.HidHostService;
     38 import com.android.bluetooth.map.BluetoothMapService;
     39 import com.android.bluetooth.mapclient.MapClientService;
     40 import com.android.bluetooth.opp.BluetoothOppService;
     41 import com.android.bluetooth.pan.PanService;
     42 import com.android.bluetooth.pbap.BluetoothPbapService;
     43 import com.android.bluetooth.pbapclient.PbapClientService;
     44 import com.android.bluetooth.sap.SapService;
     45 
     46 import java.util.ArrayList;
     47 
     48 public class Config {
     49     private static final String TAG = "AdapterServiceConfig";
     50 
     51     private static class ProfileConfig {
     52         Class mClass;
     53         int mSupported;
     54         long mMask;
     55 
     56         ProfileConfig(Class theClass, int supportedFlag, long mask) {
     57             mClass = theClass;
     58             mSupported = supportedFlag;
     59             mMask = mask;
     60         }
     61     }
     62 
     63     /**
     64      * List of profile services with the profile-supported resource flag and bit mask.
     65      */
     66     private static final ProfileConfig[] PROFILE_SERVICES_AND_FLAGS = {
     67             new ProfileConfig(HeadsetService.class, R.bool.profile_supported_hs_hfp,
     68                     (1 << BluetoothProfile.HEADSET)),
     69             new ProfileConfig(A2dpService.class, R.bool.profile_supported_a2dp,
     70                     (1 << BluetoothProfile.A2DP)),
     71             new ProfileConfig(A2dpSinkService.class, R.bool.profile_supported_a2dp_sink,
     72                     (1 << BluetoothProfile.A2DP_SINK)),
     73             new ProfileConfig(HidHostService.class, R.bool.profile_supported_hid_host,
     74                     (1 << BluetoothProfile.HID_HOST)),
     75             new ProfileConfig(HealthService.class, R.bool.profile_supported_hdp,
     76                     (1 << BluetoothProfile.HEALTH)),
     77             new ProfileConfig(PanService.class, R.bool.profile_supported_pan,
     78                     (1 << BluetoothProfile.PAN)),
     79             new ProfileConfig(GattService.class, R.bool.profile_supported_gatt,
     80                     (1 << BluetoothProfile.GATT)),
     81             new ProfileConfig(BluetoothMapService.class, R.bool.profile_supported_map,
     82                     (1 << BluetoothProfile.MAP)),
     83             new ProfileConfig(HeadsetClientService.class, R.bool.profile_supported_hfpclient,
     84                     (1 << BluetoothProfile.HEADSET_CLIENT)),
     85             new ProfileConfig(AvrcpTargetService.class, R.bool.profile_supported_avrcp_target,
     86                     (1 << BluetoothProfile.AVRCP)),
     87             new ProfileConfig(AvrcpControllerService.class,
     88                     R.bool.profile_supported_avrcp_controller,
     89                     (1 << BluetoothProfile.AVRCP_CONTROLLER)),
     90             new ProfileConfig(SapService.class, R.bool.profile_supported_sap,
     91                     (1 << BluetoothProfile.SAP)),
     92             new ProfileConfig(PbapClientService.class, R.bool.profile_supported_pbapclient,
     93                     (1 << BluetoothProfile.PBAP_CLIENT)),
     94             new ProfileConfig(MapClientService.class, R.bool.profile_supported_mapmce,
     95                     (1 << BluetoothProfile.MAP_CLIENT)),
     96             new ProfileConfig(HidDeviceService.class, R.bool.profile_supported_hid_device,
     97                     (1 << BluetoothProfile.HID_DEVICE)),
     98             new ProfileConfig(BluetoothOppService.class, R.bool.profile_supported_opp,
     99                     (1 << BluetoothProfile.OPP)),
    100             new ProfileConfig(BluetoothPbapService.class, R.bool.profile_supported_pbap,
    101                     (1 << BluetoothProfile.PBAP)),
    102             new ProfileConfig(HearingAidService.class, R.bool.profile_supported_hearing_aid,
    103                     (1 << BluetoothProfile.HEARING_AID))
    104     };
    105 
    106     private static Class[] sSupportedProfiles = new Class[0];
    107 
    108     static void init(Context ctx) {
    109         if (ctx == null) {
    110             return;
    111         }
    112         Resources resources = ctx.getResources();
    113         if (resources == null) {
    114             return;
    115         }
    116 
    117         ArrayList<Class> profiles = new ArrayList<>(PROFILE_SERVICES_AND_FLAGS.length);
    118         for (ProfileConfig config : PROFILE_SERVICES_AND_FLAGS) {
    119             boolean supported = resources.getBoolean(config.mSupported);
    120             if (supported && !isProfileDisabled(ctx, config.mMask)) {
    121                 Log.v(TAG, "Adding " + config.mClass.getSimpleName());
    122                 profiles.add(config.mClass);
    123             }
    124             sSupportedProfiles = profiles.toArray(new Class[profiles.size()]);
    125         }
    126     }
    127 
    128     static Class[] getSupportedProfiles() {
    129         return sSupportedProfiles;
    130     }
    131 
    132     private static long getProfileMask(Class profile) {
    133         for (ProfileConfig config : PROFILE_SERVICES_AND_FLAGS) {
    134             if (config.mClass == profile) {
    135                 return config.mMask;
    136             }
    137         }
    138         Log.w(TAG, "Could not find profile bit mask for " + profile.getSimpleName());
    139         return 0;
    140     }
    141 
    142     static long getSupportedProfilesBitMask() {
    143         long mask = 0;
    144         for (final Class profileClass : getSupportedProfiles()) {
    145             mask |= getProfileMask(profileClass);
    146         }
    147         return mask;
    148     }
    149 
    150     private static boolean isProfileDisabled(Context context, long profileMask) {
    151         final ContentResolver resolver = context.getContentResolver();
    152         final long disabledProfilesBitMask =
    153                 Settings.Global.getLong(resolver, Settings.Global.BLUETOOTH_DISABLED_PROFILES, 0);
    154 
    155         return (disabledProfilesBitMask & profileMask) != 0;
    156     }
    157 }
    158