Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

android: Pass song information over the JNI bridge #1951

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions android/app/src/main/java/org/musicpd/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import android.content.Context;

import org.musicpd.models.SongInfo;

/**
* Bridge to native code.
*/
Expand All @@ -18,4 +20,7 @@ public interface LogListener {
public static native void run(Context context, LogListener logListener);
public static native void shutdown();
public static native void pause();
public static native void playNext();
public static native void playPrevious();
public static native SongInfo currentSong();
}
9 changes: 9 additions & 0 deletions android/app/src/main/java/org/musicpd/models/SongInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.musicpd.models;

import java.util.HashMap;

public class SongInfo {
String uri;
int durationMilliseconds;
HashMap<String, String> tags;
}
1 change: 1 addition & 0 deletions android/include/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ bridge_header = custom_target(
output: 'org_musicpd_Bridge.h',
input: [
'../app/src/main/java/org/musicpd/Bridge.java',
'../app/src/main/java/org/musicpd/models/SongInfo.java',
],
command: [
javac,
Expand Down
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ else
'src/android/AudioManager.cxx',
'src/android/Environment.cxx',
'src/android/LogListener.cxx',
'src/android/Song.cxx',
]
endif

Expand Down
33 changes: 33 additions & 0 deletions src/Main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
#include "android/Environment.hxx"
#include "android/Context.hxx"
#include "android/LogListener.hxx"
#include "android/Song.hxx"
#include "config/File.hxx"
#include "fs/FileSystem.hxx"
#include "org_musicpd_Bridge.h"
Expand Down Expand Up @@ -614,6 +615,38 @@ Java_org_musicpd_Bridge_pause(JNIEnv *, jclass)
partition.pc.LockSetPause(true);
}

gcc_visibility_default
JNIEXPORT void JNICALL
Java_org_musicpd_Bridge_playNext(JNIEnv *, jclass)
{

}

gcc_visibility_default
JNIEXPORT void JNICALL
Java_org_musicpd_Bridge_playPrevious(JNIEnv *, jclass)
{

}

gcc_visibility_default
JNIEXPORT jobject JNICALL
Java_org_musicpd_Bridge_currentSong(JNIEnv *env, jclass)
{
if (global_instance != nullptr) {
for (auto &partition : global_instance->partitions) {
int current_position = partition.playlist.GetCurrentPosition();
if (current_position < 0)
continue;

std::unique_ptr<DetachedSong> song = std::make_unique<DetachedSong>(partition.playlist.queue.Get(current_position));
return song_to_song_info(env, song);
}
}

return NULL;
}

#else

static inline void
Expand Down
46 changes: 46 additions & 0 deletions src/android/Song.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "Song.hxx"
#include "java/Class.hxx"
#include "tag/Names.hxx"

jobject song_to_song_info(JNIEnv *env, std::unique_ptr<DetachedSong> &song) {
jobject tag_map = song_to_tag_hashmap(env, song);

Java::Class cls(env, "org/musicpd/models/SongInfo");
jmethodID init = env->GetMethodID(cls, "<init>", "()V");
jobject song_info = env->NewObject(cls, init);

jstring uri = env->NewStringUTF(song->GetURI());
jfieldID id_uri = env->GetFieldID(cls, "uri", "Ljava/lang/String;");
env->SetObjectField(song_info, id_uri, uri);
env->DeleteLocalRef(uri);

const auto duration = song->GetDuration();
jfieldID id_duration = env->GetFieldID(cls, "durationMilliseconds", "I");
env->SetIntField(song_info, id_duration, duration.ToMS());

jfieldID id_tags = env->GetFieldID(cls, "tags", "Ljava/util/HashMap;");
env->SetObjectField(song_info, id_tags, tag_map);
env->DeleteLocalRef(tag_map);

return song_info;
}

jobject song_to_tag_hashmap(JNIEnv *env, std::unique_ptr<DetachedSong> &song) {
Java::Class cls(env, "java/util/HashMap");
jmethodID init = env->GetMethodID(cls, "<init>", "()V");
jobject hash_map = env->NewObject(cls, init);
jmethodID put = env->GetMethodID(cls, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");

const Tag &tag = song->GetTag();
for (const auto &i : tag) {
jstring key = env->NewStringUTF(tag_item_names[i.type]);
jstring value = env->NewStringUTF(i.value);

env->CallObjectMethod(hash_map, put, key, value);

env->DeleteLocalRef(key);
env->DeleteLocalRef(value);
}

return hash_map;
}
7 changes: 7 additions & 0 deletions src/android/Song.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "song/DetachedSong.hxx"

#include "java/Object.hxx"

jobject song_to_song_info(JNIEnv *env, std::unique_ptr<DetachedSong> &song);

jobject song_to_tag_hashmap(JNIEnv *env, std::unique_ptr<DetachedSong> &song);
Loading