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