public class MoppyMediaService extends MediaBrowserServiceCompat
Control of Moppy is offered through
onCustomAction
,
while playback control is offered through MediaController
. As with any
MediaBrowserServiceCompat
, content is available through
onLoadChildren
,
and loading can be done either through ACTION_LOAD_ITEM
or the MediaControllerCompat
, with the
main advantage of ACTION_LOAD_ITEM
being that the user can supply callbacks for success and failure,
while loading a file through the MediaController
is fire-and-forget.
Modifier and Type | Class and Description |
---|---|
class |
MoppyMediaService.Binder
Used to allow binding to this
MoppyMediaService . |
MediaBrowserServiceCompat.BrowserRoot, MediaBrowserServiceCompat.Result<T>
Modifier and Type | Field and Description |
---|---|
static String |
ACTION_ADD_DEVICE
The custom action for adding a Moppy device.
|
static String |
ACTION_GET_DEVICES
The custom action for refreshing the available USB devices.
|
static String |
ACTION_INIT_LIBRARY
The custom action for triggering media library creation if not done so already.
|
static String |
ACTION_LOAD_ITEM
The custom action for loading a file.
|
static String |
ACTION_REFRESH_DEVICES
The custom action for refreshing the available USB devices.
|
static String |
ACTION_REMOVE_DEVICE
The custom action for removing a Moppy device.
|
static String |
ACTION_SET_MIDI_IN
The custom action for setting the MIDI device to receive input from.
|
static String |
ACTION_SET_MIDI_OUT
The custom action for setting the MIDI device to send output to.
|
static String |
ACTION_SET_MIDI_SPLIT
The custom action for setting the service to intercept MIDI messages from the MIDI input devices
and split up chords to distribute their notes across available MIDI channels.
|
static String |
CHANNEL_ID
The ID for the media service notification channel.
|
static String |
EXTRA_BIND_NORMAL
boolean extra used to allow custom binding without using MediaBrowserServiceCompat . |
static String |
EXTRA_DEVICE_INFOS
ArrayList < UsbDevice > (stored as
ArrayList< Parcelable )> ) extra field containing all available devices. |
static String |
EXTRA_DEVICE_NAMES
|
static String |
EXTRA_DEVICES_CONNECTED
String extra field for the identifiers of connected devices. |
static String |
EXTRA_ERROR_INFORMATIONAL
boolean extra field for if the error is informational and can be ignored. |
static String |
EXTRA_ERROR_REASON
String extra field for the reason an error was raised. |
static String |
EXTRA_EXCEPTION
Exception extra field (stored as Serializable ) for a bundled exception. |
static String |
EXTRA_LIBRARY_CREATED
boolean extra field for if the MidiLibrary was created successfully. |
static String |
EXTRA_MEDIA_ID
String extra field for the media ID of the file to load. |
static String |
EXTRA_MEDIA_MIDI_FILE
MidiLibrary.MidiFile extra field (stored as Parcelable )
for the MIDI file loaded in an ACTION_LOAD_ITEM event. |
static String |
EXTRA_MIDI_IN_DEVICE
MidiPortInfoWrapper extra field (stored as Parcelable ) for the MIDI
device associated with an ACTION_SET_MIDI_IN event. |
static String |
EXTRA_MIDI_OUT_DEVICE
MidiPortInfoWrapper extra field (stored as Parcelable ) for the MIDI
device associated with an ACTION_SET_MIDI_OUT event. |
static String |
EXTRA_MIDI_SPLIT_ENABLE
boolean extra field for whether or not to split incoming MIDI notes. |
static String |
EXTRA_NUM_CONNECTED
int extra field for the number of connected devices. |
static String |
EXTRA_PLAY
boolean extra field for if the sequencer should be started if file loaded successfully. |
static String |
EXTRA_PORT_NAME
String extra field for the port name associated with a ACTION_ADD_DEVICE or ACTION_REMOVE_DEVICE event. |
KEY_MEDIA_ITEM, KEY_SEARCH_RESULTS, RESULT_ERROR, RESULT_OK, RESULT_PROGRESS_UPDATE, SERVICE_INTERFACE
START_CONTINUATION_MASK, START_FLAG_REDELIVERY, START_FLAG_RETRY, START_NOT_STICKY, START_REDELIVER_INTENT, START_STICKY, START_STICKY_COMPATIBILITY, STOP_FOREGROUND_DETACH, STOP_FOREGROUND_REMOVE
ACCESSIBILITY_SERVICE, ACCOUNT_SERVICE, ACTIVITY_SERVICE, ALARM_SERVICE, APP_OPS_SERVICE, APPWIDGET_SERVICE, AUDIO_SERVICE, BATTERY_SERVICE, BIND_ABOVE_CLIENT, BIND_ADJUST_WITH_ACTIVITY, BIND_ALLOW_OOM_MANAGEMENT, BIND_AUTO_CREATE, BIND_DEBUG_UNBIND, BIND_EXTERNAL_SERVICE, BIND_IMPORTANT, BIND_INCLUDE_CAPABILITIES, BIND_NOT_FOREGROUND, BIND_NOT_PERCEPTIBLE, BIND_WAIVE_PRIORITY, BIOMETRIC_SERVICE, BLUETOOTH_SERVICE, CAMERA_SERVICE, CAPTIONING_SERVICE, CARRIER_CONFIG_SERVICE, CLIPBOARD_SERVICE, COMPANION_DEVICE_SERVICE, CONNECTIVITY_SERVICE, CONSUMER_IR_SERVICE, CONTEXT_IGNORE_SECURITY, CONTEXT_INCLUDE_CODE, CONTEXT_RESTRICTED, CROSS_PROFILE_APPS_SERVICE, DEVICE_POLICY_SERVICE, DISPLAY_SERVICE, DOWNLOAD_SERVICE, DROPBOX_SERVICE, EUICC_SERVICE, FINGERPRINT_SERVICE, HARDWARE_PROPERTIES_SERVICE, INPUT_METHOD_SERVICE, INPUT_SERVICE, IPSEC_SERVICE, JOB_SCHEDULER_SERVICE, KEYGUARD_SERVICE, LAUNCHER_APPS_SERVICE, LAYOUT_INFLATER_SERVICE, LOCATION_SERVICE, MEDIA_PROJECTION_SERVICE, MEDIA_ROUTER_SERVICE, MEDIA_SESSION_SERVICE, MIDI_SERVICE, MODE_APPEND, MODE_ENABLE_WRITE_AHEAD_LOGGING, MODE_MULTI_PROCESS, MODE_NO_LOCALIZED_COLLATORS, MODE_PRIVATE, MODE_WORLD_READABLE, MODE_WORLD_WRITEABLE, NETWORK_STATS_SERVICE, NFC_SERVICE, NOTIFICATION_SERVICE, NSD_SERVICE, POWER_SERVICE, PRINT_SERVICE, RECEIVER_VISIBLE_TO_INSTANT_APPS, RESTRICTIONS_SERVICE, ROLE_SERVICE, SEARCH_SERVICE, SENSOR_SERVICE, SHORTCUT_SERVICE, STORAGE_SERVICE, STORAGE_STATS_SERVICE, SYSTEM_HEALTH_SERVICE, TELECOM_SERVICE, TELEPHONY_SERVICE, TELEPHONY_SUBSCRIPTION_SERVICE, TEXT_CLASSIFICATION_SERVICE, TEXT_SERVICES_MANAGER_SERVICE, TV_INPUT_SERVICE, UI_MODE_SERVICE, USAGE_STATS_SERVICE, USB_SERVICE, USER_SERVICE, VIBRATOR_SERVICE, WALLPAPER_SERVICE, WIFI_AWARE_SERVICE, WIFI_P2P_SERVICE, WIFI_RTT_RANGING_SERVICE, WIFI_SERVICE, WINDOW_SERVICE
TRIM_MEMORY_BACKGROUND, TRIM_MEMORY_COMPLETE, TRIM_MEMORY_MODERATE, TRIM_MEMORY_RUNNING_CRITICAL, TRIM_MEMORY_RUNNING_LOW, TRIM_MEMORY_RUNNING_MODERATE, TRIM_MEMORY_UI_HIDDEN
Constructor and Description |
---|
MoppyMediaService() |
Modifier and Type | Method and Description |
---|---|
boolean |
addReceiver(Receiver receiver)
Adds a
Receiver to forward all MIDI messages to, regardless of if they originated on
the MIDI wire input or the MIDI file input. |
Receiver |
getInputReceiver()
Gets the
Receiver that can be used to send MIDI messages to Moppy. |
Receiver |
getReceiver()
Gets the
Receiver that messages are forwarded to. |
boolean |
load(String mediaId,
boolean setToPlaying)
Loads a
MidiLibrary.MidiFile by its media ID into Moppy. |
IBinder |
onBind(Intent intent)
Triggered when this service is bound to with
Context.bindService(Intent, ServiceConnection, int) . |
void |
onCreate()
Triggered when the service is first created, usually by the first
Context.startService(Intent)
call. |
void |
onCustomAction(String action,
Bundle extras,
MediaBrowserServiceCompat.Result<Bundle> result)
Called to request a custom action to this service.
|
void |
onDestroy()
Triggered when the service has completed and is being destroyed by the garbage collector.
|
MediaBrowserServiceCompat.BrowserRoot |
onGetRoot(String clientPackageName,
int clientUid,
Bundle rootHints)
Retrieves the node that allows a connecting client to browse the content library's root folder.
|
void |
onLoadChildren(String parentMediaId,
MediaBrowserServiceCompat.Result<List<MediaBrowserCompat.MediaItem>> result)
Uses the provided media ID to retrieve the node's children, which are either browsable
MediaBrowserCompat.MediaItem s (folders) or playable MediaItem s. |
int |
onStartCommand(Intent intent,
int flags,
int startId)
Triggered when this service is started with a call to
Context.startService(android.content.Intent) . |
void |
onTaskRemoved(Intent rootIntent)
Triggered when the activity is killed (e.g.
|
boolean |
removeReceiver(Receiver receiver)
Removes a
Receiver from the list to forward messages to. |
attachToBaseContext, dump, getBrowserRootHints, getCurrentBrowserInfo, getSessionToken, notifyChildrenChanged, notifyChildrenChanged, notifyChildrenChanged, onLoadChildren, onLoadItem, onSearch, onSubscribe, onUnsubscribe, setSessionToken
getApplication, getForegroundServiceType, onConfigurationChanged, onLowMemory, onRebind, onStart, onTrimMemory, onUnbind, startForeground, startForeground, stopForeground, stopForeground, stopSelf, stopSelf, stopSelfResult
attachBaseContext, bindIsolatedService, bindService, bindService, checkCallingOrSelfPermission, checkCallingOrSelfUriPermission, checkCallingPermission, checkCallingUriPermission, checkPermission, checkSelfPermission, checkUriPermission, checkUriPermission, clearWallpaper, createConfigurationContext, createContextForSplit, createDeviceProtectedStorageContext, createDisplayContext, createPackageContext, databaseList, deleteDatabase, deleteFile, deleteSharedPreferences, enforceCallingOrSelfPermission, enforceCallingOrSelfUriPermission, enforceCallingPermission, enforceCallingUriPermission, enforcePermission, enforceUriPermission, enforceUriPermission, fileList, getApplicationContext, getApplicationInfo, getAssets, getBaseContext, getCacheDir, getClassLoader, getCodeCacheDir, getContentResolver, getDatabasePath, getDataDir, getDir, getExternalCacheDir, getExternalCacheDirs, getExternalFilesDir, getExternalFilesDirs, getExternalMediaDirs, getFilesDir, getFileStreamPath, getMainExecutor, getMainLooper, getNoBackupFilesDir, getObbDir, getObbDirs, getOpPackageName, getPackageCodePath, getPackageManager, getPackageName, getPackageResourcePath, getResources, getSharedPreferences, getSystemService, getSystemServiceName, getTheme, getWallpaper, getWallpaperDesiredMinimumHeight, getWallpaperDesiredMinimumWidth, grantUriPermission, isDeviceProtectedStorage, isRestricted, moveDatabaseFrom, moveSharedPreferencesFrom, openFileInput, openFileOutput, openOrCreateDatabase, openOrCreateDatabase, peekWallpaper, registerReceiver, registerReceiver, registerReceiver, registerReceiver, removeStickyBroadcast, removeStickyBroadcastAsUser, revokeUriPermission, revokeUriPermission, sendBroadcast, sendBroadcast, sendBroadcastAsUser, sendBroadcastAsUser, sendOrderedBroadcast, sendOrderedBroadcast, sendOrderedBroadcastAsUser, sendStickyBroadcast, sendStickyBroadcastAsUser, sendStickyOrderedBroadcast, sendStickyOrderedBroadcastAsUser, setTheme, setWallpaper, setWallpaper, startActivities, startActivities, startActivity, startActivity, startForegroundService, startInstrumentation, startIntentSender, startIntentSender, startService, stopService, unbindService, unregisterReceiver, updateServiceGroup
getColor, getColorStateList, getDrawable, getString, getString, getSystemService, getText, obtainStyledAttributes, obtainStyledAttributes, obtainStyledAttributes, obtainStyledAttributes, registerComponentCallbacks, unregisterComponentCallbacks
public static final String CHANNEL_ID
public static final String ACTION_ADD_DEVICE
Non-standard Bundle
fields required:
KEY | TYPE | VALUE |
---|---|---|
EXTRA_PORT_NAME | String | The provided port name |
(Result only) EXTRA_NUM_CONNECTED |
int |
The number of currently connected devices |
onCustomAction
,
Constant Field Valuespublic static final String ACTION_REMOVE_DEVICE
Non-standard Bundle
fields required:
KEY | TYPE | VALUE |
---|---|---|
EXTRA_PORT_NAME | String | The provided port name |
(Result only) EXTRA_NUM_CONNECTED |
int |
The number of currently connected devices |
EXTRA_ERROR_INFORMATIONAL
may be true
if the device wasn't connected or an
IOException
was raised while trying to disconnect.
onCustomAction
,
Constant Field Valuespublic static final String ACTION_SET_MIDI_IN
Non-standard Bundle
fields required:
KEY | TYPE | VALUE |
---|---|---|
EXTRA_MIDI_IN_DEVICE |
MidiPortInfoWrapper (Parcelable ) |
The MIDI port to connect to |
onCustomAction
,
Constant Field Valuespublic static final String ACTION_SET_MIDI_OUT
Non-standard Bundle
fields required:
KEY | TYPE | VALUE |
---|---|---|
EXTRA_MIDI_OUT_DEVICE |
MidiPortInfoWrapper (Parcelable ) |
The MIDI port to connect to |
onCustomAction
,
Constant Field Valuespublic static final String ACTION_SET_MIDI_SPLIT
Non-standard Bundle
fields required:
KEY | TYPE | VALUE |
---|---|---|
EXTRA_MIDI_SPLIT_ENABLE |
boolean |
Whether or not to enable MIDI note splitting on the MIDI input devices |
onCustomAction
,
Constant Field Valuespublic static final String ACTION_INIT_LIBRARY
Manifest.permission.READ_EXTERNAL_STORAGE
has been granted to the Activity
.
Non-standard Bundle
fields required:
KEY | TYPE | VALUE |
---|---|---|
(Result only) EXTRA_LIBRARY_CREATED |
boolean |
true if the library already exists or was created, or false if creation failed |
onCustomAction
,
Constant Field Valuespublic static final String ACTION_REFRESH_DEVICES
ACTION_GET_DEVICES
upon completion.
Guaranteed to not send an error.
Non-standard Bundle
fields: see ACTION_GET_DEVICES
onCustomAction
,
Constant Field Valuespublic static final String ACTION_GET_DEVICES
Non-standard Bundle
fields required:
KEY | TYPE | VALUE |
---|---|---|
(Result only) EXTRA_DEVICE_NAMES |
ArrayList {@literal <}{@link String}{@literal >} |
The list of available port/device names |
(Result only) EXTRA_DEVICE_INFOS |
ArrayList < UsbDevice > |
The list of device information lists |
(Result only) EXTRA_NUM_CONNECTED | int | The number of currently connected devices |
(Result only) EXTRA_DEVICES_CONNECTED |
ArrayList < String > |
The list of connected device identifiers |
(Result only) EXTRA_MIDI_IN_DEVICE |
MidiPortInfoWrapper (Parcelable ) |
The currently connected MIDI in device, or null if none connected |
(Result only) EXTRA_MIDI_OUT_DEVICE |
MidiPortInfoWrapper (Parcelable ) |
The currently connected MIDI out device, or null if none connected |
(Result only) EXTRA_MIDI_SPLIT_ENABLE |
boolean |
Whether or not MIDI note splitting on the MIDI input devices are enabled |
onCustomAction
,
Constant Field Valuespublic static final String ACTION_LOAD_ITEM
Non-standard Bundle
fields required:
KEY | TYPE | VALUE |
---|---|---|
EXTRA_MEDIA_ID | String | The media ID to load |
(Optional) EXTRA_PLAY | boolean | Start playing after loading finished |
(Result only) EXTRA_MEDIA_MIDI_FILE |
MidiLibrary.MidiFile (Parcelable ) |
Information about the MIDI file that was loaded |
onLoadChildren
,
onCustomAction
,
Constant Field Valuespublic static final String EXTRA_BIND_NORMAL
boolean
extra used to allow custom binding without using MediaBrowserServiceCompat
.public static final String EXTRA_PORT_NAME
String
extra field for the port name associated with a ACTION_ADD_DEVICE
or ACTION_REMOVE_DEVICE
event.public static final String EXTRA_MIDI_IN_DEVICE
MidiPortInfoWrapper
extra field (stored as Parcelable
) for the MIDI
device associated with an ACTION_SET_MIDI_IN
event.public static final String EXTRA_MIDI_OUT_DEVICE
MidiPortInfoWrapper
extra field (stored as Parcelable
) for the MIDI
device associated with an ACTION_SET_MIDI_OUT
event.public static final String EXTRA_MIDI_SPLIT_ENABLE
boolean
extra field for whether or not to split incoming MIDI notes.public static final String EXTRA_LIBRARY_CREATED
boolean
extra field for if the MidiLibrary
was created successfully.public static final String EXTRA_DEVICE_NAMES
public static final String EXTRA_DEVICE_INFOS
ArrayList
<
UsbDevice
>
(stored as
ArrayList<
Parcelable
)>
) extra field containing all available devices.public static final String EXTRA_NUM_CONNECTED
int
extra field for the number of connected devices.public static final String EXTRA_DEVICES_CONNECTED
String
extra field for the identifiers of connected devices.public static final String EXTRA_MEDIA_ID
String
extra field for the media ID of the file to load.public static final String EXTRA_MEDIA_MIDI_FILE
MidiLibrary.MidiFile
extra field (stored as Parcelable
)
for the MIDI file loaded in an ACTION_LOAD_ITEM
event.public static final String EXTRA_PLAY
boolean
extra field for if the sequencer should be started if file loaded successfully.
If an action reads this extra, null
represents false
.load(String, boolean)
,
Constant Field Valuespublic static final String EXTRA_EXCEPTION
Exception
extra field (stored as Serializable
) for a bundled exception.public static final String EXTRA_ERROR_REASON
String
extra field for the reason an error was raised.public static final String EXTRA_ERROR_INFORMATIONAL
boolean
extra field for if the error is informational and can be ignored.public void onCreate()
Context.startService(Intent)
call. Always triggered before onStartCommand(Intent, int, int)
.onCreate
in class MediaBrowserServiceCompat
public int onStartCommand(Intent intent, int flags, int startId)
Context.startService(android.content.Intent)
.onStartCommand
in class Service
intent
- the intent used in the Context.startService call
flags
- either 0, Service.START_FLAG_REDELIVERY
, or Service.START_FLAG_RETRY
startId
- unique identifier for this start instance, used with Service.stopSelfResult(int)
Service.START_CONTINUATION_MASK
flagspublic IBinder onBind(Intent intent)
Context.bindService(Intent, ServiceConnection, int)
.onBind
in class MediaBrowserServiceCompat
intent
- the Intent
send with the bindService
callMoppyMediaService.Binder
if EXTRA_BIND_NORMAL
is specified and true, otherwise the result
of MediaBrowserServiceCompat.onBind(Intent)
public void onTaskRemoved(Intent rootIntent)
onTaskRemoved
in class Service
rootIntent
- the intent used to launch the task being removedpublic void onDestroy()
public MediaBrowserServiceCompat.BrowserRoot onGetRoot(String clientPackageName, int clientUid, Bundle rootHints)
onGetRoot
in class MediaBrowserServiceCompat
clientPackageName
- package name of the connecting clientclientUid
- identifier of the connecting clientrootHints
- hints about what content to return; ignoredMediaBrowserServiceCompat.BrowserRoot.EXTRA_RECENT
,
MediaBrowserServiceCompat.BrowserRoot.EXTRA_OFFLINE
,
MediaBrowserServiceCompat.BrowserRoot.EXTRA_SUGGESTED
public void onLoadChildren(String parentMediaId, MediaBrowserServiceCompat.Result<List<MediaBrowserCompat.MediaItem>> result)
MediaBrowserCompat.MediaItem
s (folders) or playable MediaItem
s.onLoadChildren
in class MediaBrowserServiceCompat
parentMediaId
- the ID of the node to retrieve items fromresult
- where the list of MediaBrowserCompat.MediaItem
s
contained in the parent node is sent toonGetRoot(String, int, Bundle)
public void onCustomAction(@NonNull String action, Bundle extras, @NonNull MediaBrowserServiceCompat.Result<Bundle> result)
MediaBrowserCompat.sendCustomAction(String, Bundle, MediaBrowserCompat.CustomActionCallback)
.
Available actions:
ACTION_ADD_DEVICE
ACTION_GET_DEVICES
ACTION_INIT_LIBRARY
ACTION_LOAD_ITEM
ACTION_REFRESH_DEVICES
ACTION_REMOVE_DEVICE
ACTION_SET_MIDI_IN
ACTION_SET_MIDI_OUT
ACTION_SET_MIDI_SPLIT
Standard Bundle
fields sent upon encountering an error and calling result.sendError
:
KEY | TYPE | VALUE |
---|---|---|
EXTRA_ERROR_REASON | String | The reason for the error |
EXTRA_ERROR_INFORMATIONAL |
boolean |
If the error is informational and can safely be ignored |
(Optional) EXTRA_EXCEPTION |
Exception |
Exception that is associated with how the error occurred |
onCustomAction
in class MediaBrowserServiceCompat
action
- The custom action sent from the media browserextras
- The bundle of arguments sent from the media browserresult
- The MediaBrowserServiceCompat.Result
to send the result of the requested custom actionMediaBrowserCompat.CUSTOM_ACTION_DOWNLOAD
,
MediaBrowserCompat.CUSTOM_ACTION_REMOVE_DOWNLOADED_FILE
public Receiver getInputReceiver()
Receiver
that can be used to send MIDI messages to Moppy.Receiver
to send messages topublic Receiver getReceiver()
Receiver
that messages are forwarded to.Receiver
or null if not setaddReceiver(Receiver)
,
removeReceiver(Receiver)
public boolean addReceiver(Receiver receiver)
Receiver
to forward all MIDI messages to, regardless of if they originated on
the MIDI wire input or the MIDI file input. See ReceiverDispatcher.add(Receiver)
for
more information.receiver
- the Receiver
to addtrue
if receiver
was added, false
if receiver
was null or duplicateremoveReceiver(Receiver)
public boolean removeReceiver(Receiver receiver)
Receiver
from the list to forward messages to.receiver
- the Receiver
to removetrue
if receiver
was removed, false
if it was null
or not addedaddReceiver(Receiver)
public boolean load(String mediaId, boolean setToPlaying)
MidiLibrary.MidiFile
by its media ID into Moppy. If loading is unsuccessful false
will be returned.
Under expected conditions the sequencer state will not be affected unless the load action is valid. The only event that may jeopardize the sequencer state
is if and InvalidMidiDataException
is thrown when calliing Sequencer.setSequence(Sequence)
but is not thrown
when calling MidiFileReader.getSequence(InputStream)
.mediaId
- the media ID of the song to loadsetToPlaying
- true
if the song is to be immediately played, false
if the sequencer is to load the song and pausefalse
if there was an error loading, otherwise true
(unless mediaId is invalid, it's likely a dev issue - check Logcat for details)