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.MidiManager;
23import android.os.IBinder;
24import android.util.Log;
25
26import java.util.HashMap;
27
28public class BluetoothMidiService extends Service {
29    private static final String TAG = "BluetoothMidiService";
30
31    // BluetoothMidiDevices keyed by BluetoothDevice
32    private final HashMap<BluetoothDevice,BluetoothMidiDevice> mDeviceServerMap
33            = new HashMap<BluetoothDevice,BluetoothMidiDevice>();
34
35    @Override
36    public IBinder onBind(Intent intent) {
37        if (MidiManager.BLUETOOTH_MIDI_SERVICE_INTENT.equals(intent.getAction())) {
38            BluetoothDevice bluetoothDevice = (BluetoothDevice)intent.getParcelableExtra("device");
39            if (bluetoothDevice == null) {
40                Log.e(TAG, "no BluetoothDevice in onBind intent");
41                return null;
42            }
43
44            BluetoothMidiDevice device;
45            synchronized (mDeviceServerMap) {
46                device = mDeviceServerMap.get(bluetoothDevice);
47                if (device == null) {
48                    device = new BluetoothMidiDevice(this, bluetoothDevice, this);
49                }
50            }
51            return device.getBinder();
52        }
53        return null;
54    }
55
56    void deviceClosed(BluetoothDevice device) {
57        synchronized (mDeviceServerMap) {
58            mDeviceServerMap.remove(device);
59        }
60    }
61}
62