summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java
blob: b614712b0926e51728a173fd4f00d2076b49c3ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.bluetooth;

import com.android.settings.R;

import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothError;
import android.bluetooth.BluetoothHeadset;
import android.bluetooth.BluetoothClass;
import android.content.Context;
import android.content.SharedPreferences;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * LocalBluetoothProfileManager is an abstract class defining the basic
 * functionality related to a profile.
 */
public abstract class LocalBluetoothProfileManager {

    // TODO: close profiles when we're shutting down
    private static Map<Profile, LocalBluetoothProfileManager> sProfileMap =
            new HashMap<Profile, LocalBluetoothProfileManager>(); 
    
    protected LocalBluetoothManager mLocalManager;
    
    public static LocalBluetoothProfileManager getProfileManager(LocalBluetoothManager localManager,
            Profile profile) {
        
        LocalBluetoothProfileManager profileManager;
        
        synchronized (sProfileMap) {
            profileManager = sProfileMap.get(profile);
            
            if (profileManager == null) {
                switch (profile) {
                case A2DP:
                    profileManager = new A2dpProfileManager(localManager);
                    break;
                    
                case HEADSET:
                    profileManager = new HeadsetProfileManager(localManager);
                    break;
                }
                
                sProfileMap.put(profile, profileManager);    
            }
        }
        
        return profileManager;
    }

    // TODO: remove once the framework has this API
    public static boolean isPreferredProfile(Context context, String address, Profile profile) {
        return getPreferredProfileSharedPreferences(context).getBoolean(
                getPreferredProfileKey(address, profile), true);
    }
    
    public static void setPreferredProfile(Context context, String address, Profile profile,
            boolean preferred) {
        getPreferredProfileSharedPreferences(context).edit().putBoolean(
                getPreferredProfileKey(address, profile), preferred).commit();
    }

    private static SharedPreferences getPreferredProfileSharedPreferences(Context context) {
        return context.getSharedPreferences("bluetooth_preferred_profiles", Context.MODE_PRIVATE);
    }
    
    private static String getPreferredProfileKey(String address, Profile profile) {
        return address + "_" + profile.toString();
    }
    
    /**
     * Temporary method to fill profiles based on a device's class.
     * 
     * @param btClass The class
     * @param profiles The list of profiles to fill
     */
    public static void fill(int btClass, List<Profile> profiles) {
        profiles.clear();

        if (A2dpProfileManager.doesClassMatch(btClass)) {
            profiles.add(Profile.A2DP);
        }
        
        if (HeadsetProfileManager.doesClassMatch(btClass)) {
            profiles.add(Profile.HEADSET);
        }
    }

    protected LocalBluetoothProfileManager(LocalBluetoothManager localManager) {
        mLocalManager = localManager;
    }
    
    public abstract int connect(String address);
    
    public abstract int disconnect(String address);
    
    public abstract int getConnectionStatus(String address);

    public abstract int getSummary(String address);

    public boolean isConnected(String address) {
        return SettingsBtStatus.isConnectionStatusConnected(getConnectionStatus(address));
    }
    
    // TODO: int instead of enum
    public enum Profile {
        HEADSET(R.string.bluetooth_profile_headset),
        A2DP(R.string.bluetooth_profile_a2dp);
        
        public final int localizedString;
        
        private Profile(int localizedString) {
            this.localizedString = localizedString;
        }
    }

    /**
     * A2dpProfileManager is an abstraction for the {@link BluetoothA2dp} service. 
     */
    private static class A2dpProfileManager extends LocalBluetoothProfileManager {
        private BluetoothA2dp mService;
        
        public A2dpProfileManager(LocalBluetoothManager localManager) {
            super(localManager);
            
            mService = new BluetoothA2dp(localManager.getContext());
            // TODO: block until connection?
        }

        @Override
        public int connect(String address) {
            return mService.connectSink(address);
        }

        @Override
        public int disconnect(String address) {
            return mService.disconnectSink(address);
        }
        
        static boolean doesClassMatch(int btClass) {
            if (BluetoothClass.Service.hasService(btClass, BluetoothClass.Service.RENDER)) {
                return true;
            }

            // By the specification A2DP sinks must indicate the RENDER service
            // class, but some do not (Chordette). So match on a few more to be
            // safe
            switch (BluetoothClass.Device.getDevice(btClass)) {
            case BluetoothClass.Device.AUDIO_VIDEO_HIFI_AUDIO:
            case BluetoothClass.Device.AUDIO_VIDEO_HEADPHONES:
            case BluetoothClass.Device.AUDIO_VIDEO_LOUDSPEAKER:
            case BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO:
                return true;
                
            default:
                return false;
            }
        }

        @Override
        public int getConnectionStatus(String address) {
            return convertState(mService.getSinkState(address));
        }
        
        @Override
        public int getSummary(String address) {
            int connectionStatus = getConnectionStatus(address);
            
            if (SettingsBtStatus.isConnectionStatusConnected(connectionStatus)) {
                return R.string.bluetooth_a2dp_profile_summary_connected;
            } else {
                return SettingsBtStatus.getConnectionStatusSummary(connectionStatus);
            }
        }

        private static int convertState(int a2dpState) {
            switch (a2dpState) {
            case BluetoothA2dp.STATE_CONNECTED:
                return SettingsBtStatus.CONNECTION_STATUS_CONNECTED;
            case BluetoothA2dp.STATE_CONNECTING:
                return SettingsBtStatus.CONNECTION_STATUS_CONNECTING;
            case BluetoothA2dp.STATE_DISCONNECTED:
                return SettingsBtStatus.CONNECTION_STATUS_DISCONNECTED;
            case BluetoothA2dp.STATE_DISCONNECTING:
                return SettingsBtStatus.CONNECTION_STATUS_DISCONNECTING;
            case BluetoothA2dp.STATE_PLAYING:
                return SettingsBtStatus.CONNECTION_STATUS_ACTIVE;
            default:
                return SettingsBtStatus.CONNECTION_STATUS_UNKNOWN;
            }
        }
    }
    
    /**
     * HeadsetProfileManager is an abstraction for the {@link BluetoothHeadset} service. 
     */
    private static class HeadsetProfileManager extends LocalBluetoothProfileManager {
        private BluetoothHeadset mService;
        
        public HeadsetProfileManager(LocalBluetoothManager localManager) {
            super(localManager);
            
//            final boolean[] isServiceConnected = new boolean[1];
//            BluetoothHeadset.ServiceListener l = new BluetoothHeadset.ServiceListener() {
//                public void onServiceConnected() {
//                    synchronized (this) {
//                        isServiceConnected[0] = true;
//                        notifyAll();
//                    }
//                }
//                public void onServiceDisconnected() {
//                    mService = null;
//                }
//            };
            
            // TODO: block, but can't on UI thread
            mService = new BluetoothHeadset(localManager.getContext(), null);

//            synchronized (l) {
//                while (!isServiceConnected[0]) {
//                    try {
//                        l.wait(100);
//                    } catch (InterruptedException e) {
//                        throw new IllegalStateException(e);
//                    }
//                }
//            }
        }

        @Override
        public int connect(String address) {
            // Since connectHeadset fails if already connected to a headset, we
            // disconnect from any headset first
            mService.disconnectHeadset();
            return mService.connectHeadset(address, null)
                    ? BluetoothError.SUCCESS : BluetoothError.ERROR;
        }

        @Override
        public int disconnect(String address) {
            if (mService.getHeadsetAddress().equals(address)) {
                return mService.disconnectHeadset() ? BluetoothError.SUCCESS : BluetoothError.ERROR;
            } else {
                return BluetoothError.SUCCESS;
            }
        }
        
        static boolean doesClassMatch(int btClass) {
            switch (BluetoothClass.Device.getDevice(btClass)) {
            case BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE:
            case BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET:
            case BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO:
                return true;
                
            default:
                return false;
            }
        }

        @Override
        public int getConnectionStatus(String address) {
            String headsetAddress = mService.getHeadsetAddress();
            return headsetAddress != null && headsetAddress.equals(address)
                    ? convertState(mService.getState())
                    : SettingsBtStatus.CONNECTION_STATUS_DISCONNECTED;
        }
        
        @Override
        public int getSummary(String address) {
            int connectionStatus = getConnectionStatus(address);
            
            if (SettingsBtStatus.isConnectionStatusConnected(connectionStatus)) {
                return R.string.bluetooth_headset_profile_summary_connected;
            } else {
                return SettingsBtStatus.getConnectionStatusSummary(connectionStatus);
            }
        }

        private static int convertState(int headsetState) {
            switch (headsetState) {
            case BluetoothHeadset.STATE_CONNECTED:
                return SettingsBtStatus.CONNECTION_STATUS_CONNECTED;
            case BluetoothHeadset.STATE_CONNECTING:
                return SettingsBtStatus.CONNECTION_STATUS_CONNECTING;
            case BluetoothHeadset.STATE_DISCONNECTED:
                return SettingsBtStatus.CONNECTION_STATUS_DISCONNECTED;
            default:
                return SettingsBtStatus.CONNECTION_STATUS_UNKNOWN;
            }
        }
    }
    
}