1/*
2 * Copyright (C) 2015 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.bluetoothmidiservice;
18
19import android.app.Service;
20import android.bluetooth.BluetoothDevice;
21import android.content.Intent;
22import android.media.midi.IBluetoothMidiService;
23import android.media.midi.MidiManager;
24import android.os.IBinder;
25import android.util.Log;
26
27import java.util.HashMap;
28
29public class BluetoothMidiService extends Service {
30    private static final String TAG = "BluetoothMidiService";
31
32    // BluetoothMidiDevices keyed by BluetoothDevice
33    private final HashMap<BluetoothDevice,BluetoothMidiDevice> mDeviceServerMap
34            = new HashMap<BluetoothDevice,BluetoothMidiDevice>();
35
36    @Override
37    public IBinder onBind(Intent intent) {
38        // Return the interface
39        return mBinder;
40    }
41
42
43    private final IBluetoothMidiService.Stub mBinder = new IBluetoothMidiService.Stub() {
44
45        public IBinder addBluetoothDevice(BluetoothDevice bluetoothDevice) {
46            BluetoothMidiDevice device;
47            if (bluetoothDevice == null) {
48                Log.e(TAG, "no BluetoothDevice in addBluetoothDevice()");
49                return null;
50            }
51            synchronized (mDeviceServerMap) {
52                device = mDeviceServerMap.get(bluetoothDevice);
53                if (device == null) {
54                    device = new BluetoothMidiDevice(BluetoothMidiService.this,
55                            bluetoothDevice, BluetoothMidiService.this);
56                    mDeviceServerMap.put(bluetoothDevice, device);
57                }
58            }
59            return device.getBinder();
60        }
61
62    };
63
64    void deviceClosed(BluetoothDevice device) {
65        synchronized (mDeviceServerMap) {
66            mDeviceServerMap.remove(device);
67        }
68    }
69}
70