|
| 1 | +/** |
| 2 | + * |
| 3 | + * For information on usage and redistribution, and for a DISCLAIMER OF ALL |
| 4 | + * WARRANTIES, see the file, "LICENSE.txt," in this distribution. |
| 5 | + * |
| 6 | + */ |
| 7 | + |
| 8 | +package org.puredata.android.io; |
| 9 | +import org.puredata.android.service.R; |
| 10 | + |
| 11 | +import android.content.Context; |
| 12 | +import android.app.Activity; |
| 13 | +import android.media.AudioManager; |
| 14 | +import android.media.AudioDeviceCallback; |
| 15 | +import android.media.AudioDeviceInfo; |
| 16 | +import android.util.Log; |
| 17 | +import android.content.res.Resources; |
| 18 | +import android.preference.ListPreference; |
| 19 | +import android.preference.PreferenceFragment; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +public class AudioDevices { |
| 25 | + |
| 26 | + private static final String TAG = "AudioDevices"; |
| 27 | + private AudioManager mAudioManager; |
| 28 | + |
| 29 | + private class AudioDeviceList { |
| 30 | + private ListPreference mPref; |
| 31 | + private List<String> mEntries = new ArrayList<>(); |
| 32 | + private List<String> mValues = new ArrayList<>(); |
| 33 | + |
| 34 | + private CharSequence[] listToArray(List<String> l) { |
| 35 | + String[] array = new String[l.size()]; |
| 36 | + l.toArray(array); |
| 37 | + return array; |
| 38 | + } |
| 39 | + AudioDeviceList(PreferenceFragment prefFragment, String key) { |
| 40 | + mPref = (ListPreference) prefFragment.findPreference(key); |
| 41 | + mEntries.add("Default"); |
| 42 | + mValues.add("-1"); |
| 43 | + } |
| 44 | + public void add(AudioDeviceInfo device) { |
| 45 | + String name = device.getProductName().toString() + " " + typeToString(device.getType()); |
| 46 | + mEntries.add(name); |
| 47 | + mValues.add(Integer.toString(device.getId())); |
| 48 | + mPref.setEntries(listToArray(mEntries)); |
| 49 | + mPref.setEntryValues(listToArray(mValues)); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private AudioDeviceList mInputDevices = null; |
| 54 | + private AudioDeviceList mOutputDevices = null; |
| 55 | + |
| 56 | + /** |
| 57 | + * @param context activity or service that calls this method |
| 58 | + */ |
| 59 | + public AudioDevices(Activity context) { |
| 60 | + mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); |
| 61 | + PreferenceFragment prefFragment = (PreferenceFragment) context.getFragmentManager().findFragmentByTag("prefFragment"); |
| 62 | + Resources res = context.getResources(); |
| 63 | + mInputDevices = new AudioDeviceList(prefFragment, res.getString(R.string.pref_key_indevice)); |
| 64 | + mOutputDevices = new AudioDeviceList(prefFragment, res.getString(R.string.pref_key_outdevice)); |
| 65 | + setupAudioDeviceCallback(); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Converts the value from {@link AudioDeviceInfo#getType()} into a human |
| 70 | + * readable string |
| 71 | + * @param type One of the {@link AudioDeviceInfo}.TYPE_* values |
| 72 | + * e.g. AudioDeviceInfo.TYPE_BUILT_IN_SPEAKER |
| 73 | + * @return string which describes the type of audio device |
| 74 | + */ |
| 75 | + static String typeToString(int type){ |
| 76 | + switch (type) { |
| 77 | + case AudioDeviceInfo.TYPE_AUX_LINE: |
| 78 | + return "auxiliary line-level connectors"; |
| 79 | + case AudioDeviceInfo.TYPE_BLUETOOTH_A2DP: |
| 80 | + return "Bluetooth device supporting the A2DP profile"; |
| 81 | + case AudioDeviceInfo.TYPE_BLUETOOTH_SCO: |
| 82 | + return "Bluetooth device typically used for telephony"; |
| 83 | + case AudioDeviceInfo.TYPE_BUILTIN_EARPIECE: |
| 84 | + return "built-in earphone speaker"; |
| 85 | + case AudioDeviceInfo.TYPE_BUILTIN_MIC: |
| 86 | + return "built-in microphone"; |
| 87 | + case AudioDeviceInfo.TYPE_BUILTIN_SPEAKER: |
| 88 | + return "built-in speaker"; |
| 89 | + case AudioDeviceInfo.TYPE_BUS: |
| 90 | + return "BUS"; |
| 91 | + case AudioDeviceInfo.TYPE_DOCK: |
| 92 | + return "DOCK"; |
| 93 | + case AudioDeviceInfo.TYPE_FM: |
| 94 | + return "FM"; |
| 95 | + case AudioDeviceInfo.TYPE_FM_TUNER: |
| 96 | + return "FM tuner"; |
| 97 | + case AudioDeviceInfo.TYPE_HDMI: |
| 98 | + return "HDMI"; |
| 99 | + case AudioDeviceInfo.TYPE_HDMI_ARC: |
| 100 | + return "HDMI audio return channel"; |
| 101 | + case AudioDeviceInfo.TYPE_IP: |
| 102 | + return "IP"; |
| 103 | + case AudioDeviceInfo.TYPE_LINE_ANALOG: |
| 104 | + return "line analog"; |
| 105 | + case AudioDeviceInfo.TYPE_LINE_DIGITAL: |
| 106 | + return "line digital"; |
| 107 | + case AudioDeviceInfo.TYPE_TELEPHONY: |
| 108 | + return "telephony"; |
| 109 | + case AudioDeviceInfo.TYPE_TV_TUNER: |
| 110 | + return "TV tuner"; |
| 111 | + case AudioDeviceInfo.TYPE_USB_ACCESSORY: |
| 112 | + return "USB accessory"; |
| 113 | + case AudioDeviceInfo.TYPE_USB_DEVICE: |
| 114 | + return "USB device"; |
| 115 | + case AudioDeviceInfo.TYPE_WIRED_HEADPHONES: |
| 116 | + return "wired headphones"; |
| 117 | + case AudioDeviceInfo.TYPE_WIRED_HEADSET: |
| 118 | + return "wired headset"; |
| 119 | + default: |
| 120 | + case AudioDeviceInfo.TYPE_UNKNOWN: |
| 121 | + return "unknown"; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private void setupAudioDeviceCallback(){ |
| 126 | + // Note that we will immediately receive a call to onDevicesAdded with the list of |
| 127 | + // devices which are currently connected. |
| 128 | + mAudioManager.registerAudioDeviceCallback(new AudioDeviceCallback() { |
| 129 | + @Override |
| 130 | + public void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) { |
| 131 | + for (AudioDeviceInfo device : addedDevices){ |
| 132 | + if (device.isSource()) mInputDevices.add(device); |
| 133 | + else if (device.isSink()) mOutputDevices.add(device); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + public void onAudioDevicesRemoved(AudioDeviceInfo[] removedDevices) { |
| 138 | + } |
| 139 | + }, null); |
| 140 | + } |
| 141 | +} |
0 commit comments