Config.java revision 2df89213f063ef9d38f1531253299f80fdb0739d
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.a2dpsink.A2dpSinkService;
31import com.android.bluetooth.avrcpcontroller.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;
40import com.android.bluetooth.pbapclient.PbapClientService;
41
42public class Config {
43    private static final String TAG = "AdapterServiceConfig";
44    /**
45     * List of profile services.
46     */
47    @SuppressWarnings("rawtypes")
48    //Do not inclue OPP and PBAP, because their services
49    //are not managed by AdapterService
50    private static final Class[] PROFILE_SERVICES = {
51        HeadsetService.class,
52        A2dpService.class,
53        A2dpSinkService.class,
54        HidService.class,
55        HealthService.class,
56        PanService.class,
57        GattService.class,
58        BluetoothMapService.class,
59        HeadsetClientService.class,
60        AvrcpControllerService.class,
61        SapService.class,
62        PbapClientService.class
63    };
64    /**
65     * Resource flag to indicate whether profile is supported or not.
66     */
67    private static final int[]  PROFILE_SERVICES_FLAG = {
68        R.bool.profile_supported_hs_hfp,
69        R.bool.profile_supported_a2dp,
70        R.bool.profile_supported_a2dp_sink,
71        R.bool.profile_supported_hid,
72        R.bool.profile_supported_hdp,
73        R.bool.profile_supported_pan,
74        R.bool.profile_supported_gatt,
75        R.bool.profile_supported_map,
76        R.bool.profile_supported_hfpclient,
77        R.bool.profile_supported_avrcp_controller,
78        R.bool.profile_supported_sap,
79        R.bool.profile_supported_pbapclient
80    };
81
82    private static Class[] SUPPORTED_PROFILES = new Class[0];
83
84    static void init(Context ctx) {
85        if (ctx == null) {
86            return;
87        }
88        Resources resources = ctx.getResources();
89        if (resources == null) {
90            return;
91        }
92
93        ArrayList<Class> profiles = new ArrayList<Class>(PROFILE_SERVICES.length);
94        for (int i=0; i < PROFILE_SERVICES_FLAG.length; i++) {
95            boolean supported = resources.getBoolean(PROFILE_SERVICES_FLAG[i]);
96            if (supported && !isProfileDisabled(ctx, PROFILE_SERVICES[i])) {
97                Log.d(TAG, "Adding " + PROFILE_SERVICES[i].getSimpleName());
98                profiles.add(PROFILE_SERVICES[i]);
99            }
100        }
101        int totalProfiles = profiles.size();
102        SUPPORTED_PROFILES = new Class[totalProfiles];
103        profiles.toArray(SUPPORTED_PROFILES);
104    }
105
106    static Class[]  getSupportedProfiles() {
107        return SUPPORTED_PROFILES;
108    }
109
110    private static boolean isProfileDisabled(Context context, Class profile) {
111        int profileIndex = -1;
112
113        if (profile == HeadsetService.class) {
114            profileIndex = BluetoothProfile.HEADSET;
115        } else if (profile == A2dpService.class) {
116            profileIndex = BluetoothProfile.A2DP;
117        } else if (profile == A2dpSinkService.class) {
118            profileIndex = BluetoothProfile.A2DP_SINK;
119        } else if (profile == HidService.class) {
120            profileIndex = BluetoothProfile.INPUT_DEVICE;
121        } else if (profile == HealthService.class) {
122            profileIndex = BluetoothProfile.HEALTH;
123        } else if (profile == PanService.class) {
124            profileIndex = BluetoothProfile.PAN;
125        } else if (profile == GattService.class) {
126            profileIndex = BluetoothProfile.GATT;
127        } else if (profile == BluetoothMapService.class) {
128            profileIndex = BluetoothProfile.MAP;
129        } else if (profile == HeadsetClientService.class) {
130            profileIndex = BluetoothProfile.HEADSET_CLIENT;
131        } else if (profile == AvrcpControllerService.class) {
132            profileIndex = BluetoothProfile.AVRCP_CONTROLLER;
133        } else if (profile == SapService.class) {
134            profileIndex = BluetoothProfile.SAP;
135        } else if (profile == PbapClientService.class) {
136            profileIndex = BluetoothProfile.PBAP_CLIENT;
137        }
138
139        if (profileIndex == -1) {
140            Log.d(TAG, "Could not find profile bit mask");
141            return false;
142        }
143
144        final ContentResolver resolver = context.getContentResolver();
145        final long disabledProfilesBitMask = Settings.Global.getLong(resolver,
146                Settings.Global.BLUETOOTH_DISABLED_PROFILES, 0);
147        long profileBit = 1 << profileIndex;
148
149        return (disabledProfilesBitMask & profileBit) != 0;
150    }
151}
152