Config.java revision 013408c9c16e4c1b5b7f285d68bb2f36bca4f654
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    static long getSupportedProfilesBitMask() {
111        long mask = 0;
112        for (final Class profileClass : getSupportedProfiles()) {
113            final int profileIndex = getProfileIndex(profileClass);
114
115            if (profileIndex != -1) {
116                mask |= 1 << getProfileIndex(profileClass);
117            }
118        }
119
120        return mask;
121    }
122
123    private static boolean isProfileDisabled(Context context, Class profile) {
124        final int profileIndex = getProfileIndex(profile);
125
126        if (profileIndex == -1) {
127            Log.w(TAG, "Could not find profile bit mask");
128            return false;
129        }
130
131        final ContentResolver resolver = context.getContentResolver();
132        final long disabledProfilesBitMask = Settings.Global.getLong(resolver,
133                Settings.Global.BLUETOOTH_DISABLED_PROFILES, 0);
134        final long profileBit = 1 << profileIndex;
135
136        return (disabledProfilesBitMask & profileBit) != 0;
137    }
138
139    private static int getProfileIndex(Class profile) {
140        int profileIndex = -1;
141
142        if (profile == HeadsetService.class) {
143            profileIndex = BluetoothProfile.HEADSET;
144        } else if (profile == A2dpService.class) {
145            profileIndex = BluetoothProfile.A2DP;
146        } else if (profile == A2dpSinkService.class) {
147            profileIndex = BluetoothProfile.A2DP_SINK;
148        } else if (profile == HidService.class) {
149            profileIndex = BluetoothProfile.INPUT_DEVICE;
150        } else if (profile == HealthService.class) {
151            profileIndex = BluetoothProfile.HEALTH;
152        } else if (profile == PanService.class) {
153            profileIndex = BluetoothProfile.PAN;
154        } else if (profile == GattService.class) {
155            profileIndex = BluetoothProfile.GATT;
156        } else if (profile == BluetoothMapService.class) {
157            profileIndex = BluetoothProfile.MAP;
158        } else if (profile == HeadsetClientService.class) {
159            profileIndex = BluetoothProfile.HEADSET_CLIENT;
160        } else if (profile == AvrcpControllerService.class) {
161            profileIndex = BluetoothProfile.AVRCP_CONTROLLER;
162        } else if (profile == SapService.class) {
163            profileIndex = BluetoothProfile.SAP;
164        } else if (profile == PbapClientService.class) {
165            profileIndex = BluetoothProfile.PBAP_CLIENT;
166        }
167
168        return profileIndex;
169    }
170}
171