A2dpService.java revision 4603dc081506452854023a1c6eacdfca468e0dc4
1/*
2 * Copyright (C) 2012 Google Inc.
3 */
4package com.android.bluetooth.a2dp;
5
6import android.bluetooth.BluetoothDevice;
7import android.bluetooth.BluetoothProfile;
8import android.bluetooth.IBluetoothA2dp;
9import android.content.Context;
10import android.content.Intent;
11import android.provider.Settings;
12import android.util.Log;
13import java.util.ArrayList;
14import java.util.List;
15import java.util.Iterator;
16import java.util.Map;
17import com.android.bluetooth.btservice.ProfileService;
18
19/**
20 * Provides Bluetooth A2DP profile, as a service in the Bluetooth application.
21 * @hide
22 */
23public class A2dpService extends ProfileService {
24    private static final boolean DBG = true;
25    private static final String TAG="A2dpService";
26
27    private A2dpStateMachine mStateMachine;
28
29    protected String getName() {
30        return TAG;
31    }
32
33    protected IProfileServiceBinder initBinder() {
34        return new BluetoothA2dpBinder(this);
35    }
36
37    protected boolean start() {
38        mStateMachine = new A2dpStateMachine(this,this);
39        mStateMachine.start();
40        return true;
41    }
42
43    protected boolean stop() {
44        // TODO(BT) mStateMachine.quit();
45        return true;
46    }
47
48    protected boolean cleanup() {
49        if (mStateMachine!= null) {
50            mStateMachine.cleanup();
51            mStateMachine=null;
52        }
53        return true;
54    }
55
56    //API Methods
57    boolean connect(BluetoothDevice device) {
58        enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
59                                       "Need BLUETOOTH ADMIN permission");
60
61        if (getPriority(device) == BluetoothProfile.PRIORITY_OFF) {
62            return false;
63        }
64
65        int connectionState = mStateMachine.getConnectionState(device);
66        if (connectionState == BluetoothProfile.STATE_CONNECTED ||
67            connectionState == BluetoothProfile.STATE_CONNECTING) {
68            return false;
69        }
70
71        mStateMachine.sendMessage(A2dpStateMachine.CONNECT, device);
72        return true;
73    }
74
75    boolean disconnect(BluetoothDevice device) {
76        enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
77                                       "Need BLUETOOTH ADMIN permission");
78        int connectionState = mStateMachine.getConnectionState(device);
79        if (connectionState != BluetoothProfile.STATE_CONNECTED &&
80            connectionState != BluetoothProfile.STATE_CONNECTING) {
81            return false;
82        }
83
84        mStateMachine.sendMessage(A2dpStateMachine.DISCONNECT, device);
85        return true;
86    }
87
88    List<BluetoothDevice> getConnectedDevices() {
89        enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
90        return mStateMachine.getConnectedDevices();
91    }
92
93    List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
94        enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
95        return mStateMachine.getDevicesMatchingConnectionStates(states);
96    }
97
98    int getConnectionState(BluetoothDevice device) {
99        enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
100        return mStateMachine.getConnectionState(device);
101    }
102
103    boolean setPriority(BluetoothDevice device, int priority) {
104        enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
105                                       "Need BLUETOOTH_ADMIN permission");
106        Settings.Secure.putInt(getContentResolver(),
107            Settings.Secure.getBluetoothA2dpSinkPriorityKey(device.getAddress()),
108            priority);
109        if (DBG) Log.d(TAG,"Saved priority " + device + " = " + priority);
110        return true;
111    }
112
113    int getPriority(BluetoothDevice device) {
114        enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
115                                       "Need BLUETOOTH_ADMIN permission");
116        int priority = Settings.Secure.getInt(getContentResolver(),
117            Settings.Secure.getBluetoothA2dpSinkPriorityKey(device.getAddress()),
118            BluetoothProfile.PRIORITY_UNDEFINED);
119        return priority;
120    }
121
122    synchronized boolean isA2dpPlaying(BluetoothDevice device) {
123        enforceCallingOrSelfPermission(BLUETOOTH_PERM,
124                                       "Need BLUETOOTH permission");
125        if (DBG) Log.d(TAG, "isA2dpPlaying(" + device + ")");
126        return mStateMachine.isPlaying(device);
127    }
128
129    //Binder object: Must be static class or memory leak may occur
130    private static class BluetoothA2dpBinder extends IBluetoothA2dp.Stub
131        implements IProfileServiceBinder {
132        private A2dpService mService;
133
134        private A2dpService getService() {
135            if (mService  != null && mService.isAvailable()) {
136                return mService;
137            }
138            return null;
139        }
140
141        BluetoothA2dpBinder(A2dpService svc) {
142            mService = svc;
143        }
144
145        public boolean cleanup()  {
146            mService = null;
147            return true;
148        }
149
150        public boolean connect(BluetoothDevice device) {
151            A2dpService service = getService();
152            if (service == null) return false;
153            return service.connect(device);
154        }
155
156        public boolean disconnect(BluetoothDevice device) {
157            A2dpService service = getService();
158            if (service == null) return false;
159            return service.disconnect(device);
160        }
161
162        public List<BluetoothDevice> getConnectedDevices() {
163            A2dpService service = getService();
164            if (service == null) return new ArrayList<BluetoothDevice>(0);
165            return service.getConnectedDevices();
166        }
167
168        public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
169            A2dpService service = getService();
170            if (service == null) return new ArrayList<BluetoothDevice>(0);
171            return service.getDevicesMatchingConnectionStates(states);
172        }
173
174        public int getConnectionState(BluetoothDevice device) {
175            A2dpService service = getService();
176            if (service == null) return BluetoothProfile.STATE_DISCONNECTED;
177            return service.getConnectionState(device);
178        }
179
180        public boolean setPriority(BluetoothDevice device, int priority) {
181            A2dpService service = getService();
182            if (service == null) return false;
183            return service.setPriority(device, priority);
184        }
185
186        public int getPriority(BluetoothDevice device) {
187            A2dpService service = getService();
188            if (service == null) return BluetoothProfile.PRIORITY_UNDEFINED;
189            return service.getPriority(device);
190        }
191
192        public boolean isA2dpPlaying(BluetoothDevice device) {
193            A2dpService service = getService();
194            if (service == null) return false;
195            return service.isA2dpPlaying(device);
196        }
197    };
198}
199