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 17 package com.example.android.midisynth; 18 19 import android.app.ActionBar; 20 import android.app.Activity; 21 import android.content.pm.PackageManager; 22 import android.media.midi.MidiDevice.MidiConnection; 23 import android.media.midi.MidiDeviceInfo; 24 import android.media.midi.MidiManager; 25 import android.os.Bundle; 26 import android.view.Menu; 27 import android.view.MenuItem; 28 import android.view.WindowManager; 29 import android.widget.Toast; 30 import android.widget.Toolbar; 31 32 import com.example.android.common.midi.MidiOutputPortConnectionSelector; 33 import com.example.android.common.midi.MidiPortConnector; 34 import com.example.android.common.midi.MidiTools; 35 36 /** 37 * Simple synthesizer as a MIDI Device. 38 */ 39 public class MainActivity extends Activity { 40 static final String TAG = "MidiSynthExample"; 41 42 private MidiManager mMidiManager; 43 private MidiOutputPortConnectionSelector mPortSelector; 44 45 @Override onCreate(Bundle savedInstanceState)46 public void onCreate(Bundle savedInstanceState) { 47 super.onCreate(savedInstanceState); 48 setContentView(R.layout.main); 49 setActionBar((Toolbar) findViewById(R.id.toolbar)); 50 ActionBar actionBar = getActionBar(); 51 if (actionBar != null) { 52 actionBar.setDisplayShowTitleEnabled(false); 53 } 54 55 if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_MIDI)) { 56 setupMidi(); 57 } 58 } 59 60 @Override onCreateOptionsMenu(Menu menu)61 public boolean onCreateOptionsMenu(Menu menu) { 62 getMenuInflater().inflate(R.menu.main, menu); 63 setKeepScreenOn(menu.findItem(R.id.action_keep_screen_on).isChecked()); 64 return true; 65 } 66 67 @Override onOptionsItemSelected(MenuItem item)68 public boolean onOptionsItemSelected(MenuItem item) { 69 switch (item.getItemId()) { 70 case R.id.action_keep_screen_on: 71 boolean checked = !item.isChecked(); 72 setKeepScreenOn(checked); 73 item.setChecked(checked); 74 break; 75 } 76 return super.onOptionsItemSelected(item); 77 } 78 setKeepScreenOn(boolean keepScreenOn)79 private void setKeepScreenOn(boolean keepScreenOn) { 80 if (keepScreenOn) { 81 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 82 } else { 83 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 84 } 85 } 86 setupMidi()87 private void setupMidi() { 88 // Setup MIDI 89 mMidiManager = (MidiManager) getSystemService(MIDI_SERVICE); 90 91 MidiDeviceInfo synthInfo = MidiTools.findDevice(mMidiManager, "AndroidTest", 92 "SynthExample"); 93 int portIndex = 0; 94 mPortSelector = new MidiOutputPortConnectionSelector(mMidiManager, this, 95 R.id.spinner_synth_sender, synthInfo, portIndex); 96 mPortSelector.setConnectedListener(new MyPortsConnectedListener()); 97 } 98 closeSynthResources()99 private void closeSynthResources() { 100 if (mPortSelector != null) { 101 mPortSelector.close(); 102 } 103 } 104 105 // TODO A better way would be to listen to the synth server 106 // for open/close events and then disable/enable the spinner. 107 private class MyPortsConnectedListener 108 implements MidiPortConnector.OnPortsConnectedListener { 109 @Override onPortsConnected(final MidiConnection connection)110 public void onPortsConnected(final MidiConnection connection) { 111 runOnUiThread(new Runnable() { 112 @Override 113 public void run() { 114 if (connection == null) { 115 Toast.makeText(MainActivity.this, 116 R.string.error_port_busy, Toast.LENGTH_SHORT) 117 .show(); 118 mPortSelector.clearSelection(); 119 } else { 120 Toast.makeText(MainActivity.this, 121 R.string.port_open_ok, Toast.LENGTH_SHORT) 122 .show(); 123 } 124 } 125 }); 126 } 127 } 128 129 @Override onDestroy()130 public void onDestroy() { 131 closeSynthResources(); 132 super.onDestroy(); 133 } 134 135 } 136