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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
| /*
* Copyright (C) 2015 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.yassin.stage.midible;
import android.Manifest;
import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.midi.MidiDevice;
import android.media.midi.MidiDeviceInfo;
import android.media.midi.MidiDeviceStatus;
import android.media.midi.MidiManager;
import android.media.midi.MidiManager.DeviceCallback;
import android.media.midi.MidiOutputPort;
import android.media.midi.MidiReceiver;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.Closeable;
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/*
* App that provides a MIDI echo service.
*/
public class MidiConnection extends Activity implements Serializable {
private MidiDevice gant;
private static final String TAG = "MidiConnection";
private transient Button b;
private MidiManager mMidiManager;
private OpenDeviceListAdapter mOpenDeviceListAdapter;
private static final int REQUEST_BLUETOOTH_SCAN = 1;
public MidiConnection(){
}
/**
* Classe représentant un appareil Bluetooth
* Ses informations seront affichés lors d'un scan
*/
static class BluetoothMidiDeviceTracker implements Serializable{
final public BluetoothDevice bluetoothDevice;
final public MidiDevice midiDevice;
public int inputOpenCount;
public int outputOpenCount;
/** Constructeur
* @param bluetoothDevice
* @param midiDevice
*/
public BluetoothMidiDeviceTracker(BluetoothDevice bluetoothDevice, MidiDevice midiDevice) {
this.bluetoothDevice = bluetoothDevice;
this.midiDevice = midiDevice;
}
@Override
/**
* Code de l'appareil
*/
public int hashCode() {
return midiDevice.getInfo().hashCode();
}
@Override
public boolean equals(Object o) {
if (o instanceof BluetoothMidiDeviceTracker) {
BluetoothMidiDeviceTracker other = (BluetoothMidiDeviceTracker) o;
return midiDevice.getInfo().equals(other.midiDevice.getInfo());
} else {
return false;
}
}
public MidiDevice getMidiDevice(){
return this.midiDevice;
}
}
public MidiDevice getMidiDevice(){
return this.gant;
}
/**
* Classe permettant de stocker tous les éléments de l'UI de l'activité
*/
static class ViewHolder implements Serializable{
TextView deviceName;
TextView deviceStatus;
transient Button closeButton;
}
// Adapter for holding open devices.
/**
* Cette classe permet de contenir toute la liste des appareils
*/
private class OpenDeviceListAdapter extends BaseAdapter implements Serializable {
private ArrayList<BluetoothMidiDeviceTracker> mOpenDevices;
private LayoutInflater mInflator;
private HashMap<MidiDeviceInfo, BluetoothMidiDeviceTracker> mInfoTrackerMap = new HashMap<MidiDeviceInfo, BluetoothMidiDeviceTracker>();
public OpenDeviceListAdapter() {
super();
mOpenDevices = new ArrayList<BluetoothMidiDeviceTracker>();
mInflator = MidiConnection.this.getLayoutInflater();
}
public void addDevice(BluetoothMidiDeviceTracker deviceTracker) {
if (!mOpenDevices.contains(deviceTracker)) {
mOpenDevices.add(deviceTracker);
MidiDeviceInfo info = deviceTracker.midiDevice.getInfo();
mInfoTrackerMap.put(info, deviceTracker);
notifyDataSetChanged();
}
}
/** Retirer l'appareil connecté
* @param info
*/
public void remove(MidiDeviceInfo info) {
BluetoothMidiDeviceTracker deviceTracker = mInfoTrackerMap
.get(info);
if (deviceTracker != null) {
mOpenDevices.remove(deviceTracker);
notifyDataSetChanged();
}
}
public BluetoothMidiDeviceTracker getDevice(int position) {
return mOpenDevices.get(position);
}
public BluetoothMidiDeviceTracker getDevice(MidiDeviceInfo info) {
return mInfoTrackerMap.get(info);
}
public void clear() {
mOpenDevices.clear();
mInfoTrackerMap.clear();
notifyDataSetChanged();
}
@Override
public int getCount() {
return mOpenDevices.size();
}
@Override
public Object getItem(int i) {
return mOpenDevices.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
final BluetoothMidiDeviceTracker deviceTracker;
// General ListView optimization code.
if (view == null) {
view = mInflator.inflate(R.layout.listitem_open_device, null);
viewHolder = new ViewHolder();
viewHolder.deviceStatus = (TextView) view.findViewById(R.id.device_status);
viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name);
viewHolder.closeButton = (Button) view.findViewById(R.id.close_button);
viewHolder.closeButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
BluetoothMidiDeviceTracker deviceTracker = mOpenDevices
.get(i);
doClose(deviceTracker.midiDevice);
((Button) v).setEnabled(false);
}
});
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
deviceTracker = mOpenDevices.get(i);
final String deviceName = deviceTracker.bluetoothDevice.getName();
if (deviceName != null && deviceName.length() > 0) {
viewHolder.deviceName.setText(deviceName);
} else {
viewHolder.deviceName.setText("--");
}
// Show address and number of ports open.
StringBuilder sb = new StringBuilder();
sb.append(" - ");
sb.append(deviceTracker.bluetoothDevice.getAddress());
sb.append(", [" + deviceTracker.inputOpenCount);
sb.append("][" + deviceTracker.outputOpenCount);
sb.append("]");
if ((deviceTracker.inputOpenCount + deviceTracker.outputOpenCount) > 0) {
sb.append(" in use");
}
viewHolder.deviceStatus.setText(sb.toString());
return view;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i(TAG, "app started ========================");
Button bluetoothScanButton = (Button) findViewById(R.id.bluetooth_scan);
bluetoothScanButton.setOnClickListener(mBluetoothScanListener);
// récupération de la vue Button
b = (Button)findViewById(R.id.jouer);
final MidiConnection m = this;
// ajout d'un évènement sur le bouton
b.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// SERIALIZABLE
Intent i2 = new Intent(m, MidiPong.class);
i2.putExtra("com.yassin.stage.midible.MidiConnection", m);
startActivity(i2);
}
});
ListView listView = (ListView) findViewById(R.id.open_device_list);
listView.setEmptyView(findViewById(R.id.empty));
// Initializes list view adapter.
mOpenDeviceListAdapter = new OpenDeviceListAdapter();
listView.setAdapter(mOpenDeviceListAdapter);
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_MIDI)) {
setupMidi();
} else {
Toast.makeText(MidiConnection.this, "MIDI not supported!",
Toast.LENGTH_LONG).show();
bluetoothScanButton.setEnabled(false);
}
}
private void setupMidi() {
// Setup MIDI
mMidiManager = (MidiManager) getSystemService(MIDI_SERVICE);
mMidiManager.registerDeviceCallback(new DeviceCallback() {
@Override
public void onDeviceRemoved(final MidiDeviceInfo info) {
mOpenDeviceListAdapter.remove(info);
}
// Update port open counts so user knows if the device is in use.
@Override
public void onDeviceStatusChanged(final MidiDeviceStatus status) {
MidiDeviceInfo info = status.getDeviceInfo();
BluetoothMidiDeviceTracker tracker = mOpenDeviceListAdapter
.getDevice(info);
if (tracker != null) {
tracker.outputOpenCount = 0;
for (int i = 0; i < info.getOutputPortCount(); i++) {
tracker.outputOpenCount += status
.getOutputPortOpenCount(i);
}
tracker.inputOpenCount = 0;
for (int i = 0; i < info.getInputPortCount(); i++) {
tracker.inputOpenCount += status.isInputPortOpen(i) ? 1
: 0;
}
mOpenDeviceListAdapter.notifyDataSetChanged();
}
}
}, new android.os.Handler(Looper.getMainLooper()));
}
@Override
public void onDestroy() {
mOpenDeviceListAdapter.clear();
super.onDestroy();
}
public MidiDevice onBluetoothDeviceOpen(final BluetoothDevice bluetoothDevice, final MidiDevice midiDevice) {
final MidiConnection mc = this;
runOnUiThread(new Runnable() {
@Override
public void run() {
if (midiDevice != null) {BluetoothMidiDeviceTracker tracker = new BluetoothMidiDeviceTracker(bluetoothDevice, midiDevice);
mOpenDeviceListAdapter.addDevice(tracker); // tracker = appareil séelctionné par l'utilisateur
// control change
//mc.gant = tracker.getMidiDevice();
//MidiOutputPort outputPort = midiDevice.openOutputPort(tracker.outputOpenCount);
//outputPort.connect(new Receiver());
/*boolean b = tracker.equals(mc);
Intent i = new Intent(mc, MidiPong.class);
i.putExtra("gant", mc);*/
//startActivity(i);
} else {
Toast.makeText(MidiConnection.this, "MIDI device open failed!", Toast.LENGTH_LONG).show();
}
}
});
this.gant = mc.gant;
return this.gant; // GANT SPECKTR!!!
}
/**
*
* @param tracker
*/
public void closeBluetoothDevice(BluetoothMidiDeviceTracker tracker) {
if (tracker != null) {
Log.i(TAG, "Closing Bluetooth MIDI device, info = " + tracker.midiDevice.getInfo());
}
doClose(tracker.midiDevice);
}
public void closeAll() {
for (int i = 0; i < mOpenDeviceListAdapter.getCount(); i++) {
BluetoothMidiDeviceTracker tracker = mOpenDeviceListAdapter
.getDevice(i);
if (tracker != null) {
Log.i(TAG, "Closing Bluetooth MIDI device, info = "
+ tracker.midiDevice.getInfo());
}
doClose(tracker.midiDevice);
}
mOpenDeviceListAdapter.clear();
}
public final OnClickListener mBluetoothScanListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (checkSelfPermission(Manifest.permission.BLUETOOTH) == PackageManager.PERMISSION_GRANTED) {
openBluetoothScan();
} else {
requestPermissions(
new String[] { Manifest.permission.BLUETOOTH }, 0);
}
}
};
private void openBluetoothScan() {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_PICK);
intent.setClassName("com.yassin.stage.midible", "com.yassin.stage.midible" + ".DeviceScanActivity");
startActivityForResult(intent, REQUEST_BLUETOOTH_SCAN);
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
openBluetoothScan();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == REQUEST_BLUETOOTH_SCAN && resultCode == RESULT_OK) {
final BluetoothDevice fBluetoothDevice = (BluetoothDevice) data
.getParcelableExtra("device");
if (fBluetoothDevice != null) {
Log.i(TAG, "Bluetooth device name = "
+ fBluetoothDevice.getName()
+ ", address = "
+ fBluetoothDevice.getAddress());
mMidiManager.openBluetoothDevice(fBluetoothDevice,
new MidiManager.OnDeviceOpenedListener() {
@Override
public void onDeviceOpened(MidiDevice device) {
onBluetoothDeviceOpen(fBluetoothDevice, device);
}
}, null);
}
}
}
private void doClose(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException e) {
}
}
}
} |
Partager