2 * Copyright (C) 2017 Christian Browet
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
9 #include "JNIXBMCMediaSession.h"
11 #include "AndroidKey.h"
12 #include "CompileInfo.h"
13 #include "ServiceBroker.h"
15 #include "application/Application.h"
16 #include "application/ApplicationComponents.h"
17 #include "application/ApplicationPlayer.h"
18 #include "input/actions/Action.h"
19 #include "input/actions/ActionIDs.h"
20 #include "messaging/ApplicationMessenger.h"
22 #include <androidjni/Context.h>
23 #include <androidjni/KeyEvent.h>
24 #include <androidjni/jutils-details.hpp>
28 static std::string s_className
= std::string(CCompileInfo::GetClass()) + "/XBMCMediaSession";
30 CJNIXBMCMediaSession::CJNIXBMCMediaSession()
31 : CJNIBase(s_className
)
33 m_object
= new_object(CJNIContext::getClassLoader().loadClass(GetDotClassName(s_className
)));
36 add_instance(m_object
, this);
39 CJNIXBMCMediaSession::CJNIXBMCMediaSession(const CJNIXBMCMediaSession
& other
)
42 add_instance(m_object
, this);
45 CJNIXBMCMediaSession::~CJNIXBMCMediaSession()
47 remove_instance(this);
50 void CJNIXBMCMediaSession::RegisterNatives(JNIEnv
* env
)
52 jclass cClass
= env
->FindClass(s_className
.c_str());
55 JNINativeMethod methods
[] =
57 {"_onPlayRequested", "()V", (void*)&CJNIXBMCMediaSession::_onPlayRequested
},
58 {"_onPauseRequested", "()V", (void*)&CJNIXBMCMediaSession::_onPauseRequested
},
59 {"_onNextRequested", "()V", (void*)&CJNIXBMCMediaSession::_onNextRequested
},
60 {"_onPreviousRequested", "()V", (void*)&CJNIXBMCMediaSession::_onPreviousRequested
},
61 {"_onForwardRequested", "()V", (void*)&CJNIXBMCMediaSession::_onForwardRequested
},
62 {"_onRewindRequested", "()V", (void*)&CJNIXBMCMediaSession::_onRewindRequested
},
63 {"_onStopRequested", "()V", (void*)&CJNIXBMCMediaSession::_onStopRequested
},
64 {"_onSeekRequested", "(J)V", (void*)&CJNIXBMCMediaSession::_onSeekRequested
},
65 {"_onMediaButtonEvent", "(Landroid/content/Intent;)Z", (void*)&CJNIXBMCMediaSession::_onMediaButtonEvent
},
68 env
->RegisterNatives(cClass
, methods
, sizeof(methods
)/sizeof(methods
[0]));
72 void CJNIXBMCMediaSession::activate(bool state
)
74 if (state
== m_isActive
)
77 call_method
<void>(m_object
,
83 void CJNIXBMCMediaSession::updatePlaybackState(const CJNIPlaybackState
& state
)
85 call_method
<void>(m_object
,
86 "updatePlaybackState", "(Landroid/media/session/PlaybackState;)V",
90 void CJNIXBMCMediaSession::updateMetadata(const CJNIMediaMetadata
& myData
)
92 call_method
<void>(m_object
,
93 "updateMetadata", "(Landroid/media/MediaMetadata;)V",
97 void CJNIXBMCMediaSession::updateIntent(const CJNIIntent
& intent
)
99 call_method
<void>(m_object
,
100 "updateIntent", "(Landroid/content/Intent;)V",
104 void CJNIXBMCMediaSession::OnPlayRequested()
106 const auto& components
= CServiceBroker::GetAppComponents();
107 const auto appPlayer
= components
.GetComponent
<CApplicationPlayer
>();
108 if (appPlayer
->IsPlaying())
110 if (appPlayer
->IsPaused())
111 CServiceBroker::GetAppMessenger()->PostMsg(TMSG_GUI_ACTION
, WINDOW_INVALID
, -1,
112 static_cast<void*>(new CAction(ACTION_PAUSE
)));
116 void CJNIXBMCMediaSession::OnPauseRequested()
118 const auto& components
= CServiceBroker::GetAppComponents();
119 const auto appPlayer
= components
.GetComponent
<CApplicationPlayer
>();
120 if (appPlayer
->IsPlaying())
122 if (!appPlayer
->IsPaused())
123 CServiceBroker::GetAppMessenger()->PostMsg(TMSG_GUI_ACTION
, WINDOW_INVALID
, -1,
124 static_cast<void*>(new CAction(ACTION_PAUSE
)));
128 void CJNIXBMCMediaSession::OnNextRequested()
130 const auto& components
= CServiceBroker::GetAppComponents();
131 const auto appPlayer
= components
.GetComponent
<CApplicationPlayer
>();
132 if (appPlayer
->IsPlaying())
133 CServiceBroker::GetAppMessenger()->PostMsg(TMSG_GUI_ACTION
, WINDOW_INVALID
, -1,
134 static_cast<void*>(new CAction(ACTION_NEXT_ITEM
)));
137 void CJNIXBMCMediaSession::OnPreviousRequested()
139 const auto& components
= CServiceBroker::GetAppComponents();
140 const auto appPlayer
= components
.GetComponent
<CApplicationPlayer
>();
141 if (appPlayer
->IsPlaying())
142 CServiceBroker::GetAppMessenger()->PostMsg(TMSG_GUI_ACTION
, WINDOW_INVALID
, -1,
143 static_cast<void*>(new CAction(ACTION_PREV_ITEM
)));
146 void CJNIXBMCMediaSession::OnForwardRequested()
148 const auto& components
= CServiceBroker::GetAppComponents();
149 const auto appPlayer
= components
.GetComponent
<CApplicationPlayer
>();
150 if (appPlayer
->IsPlaying())
152 if (!appPlayer
->IsPaused())
153 CServiceBroker::GetAppMessenger()->PostMsg(
154 TMSG_GUI_ACTION
, WINDOW_INVALID
, -1,
155 static_cast<void*>(new CAction(ACTION_PLAYER_FORWARD
)));
159 void CJNIXBMCMediaSession::OnRewindRequested()
161 const auto& components
= CServiceBroker::GetAppComponents();
162 const auto appPlayer
= components
.GetComponent
<CApplicationPlayer
>();
163 if (appPlayer
->IsPlaying())
165 if (!appPlayer
->IsPaused())
166 CServiceBroker::GetAppMessenger()->PostMsg(
167 TMSG_GUI_ACTION
, WINDOW_INVALID
, -1,
168 static_cast<void*>(new CAction(ACTION_PLAYER_REWIND
)));
172 void CJNIXBMCMediaSession::OnStopRequested()
174 const auto& components
= CServiceBroker::GetAppComponents();
175 const auto appPlayer
= components
.GetComponent
<CApplicationPlayer
>();
176 if (appPlayer
->IsPlaying())
177 CServiceBroker::GetAppMessenger()->PostMsg(TMSG_GUI_ACTION
, WINDOW_INVALID
, -1,
178 static_cast<void*>(new CAction(ACTION_STOP
)));
181 void CJNIXBMCMediaSession::OnSeekRequested(int64_t pos
)
183 g_application
.SeekTime(pos
/ 1000.0);
186 bool CJNIXBMCMediaSession::OnMediaButtonEvent(const CJNIIntent
& intent
)
188 if (CXBMCApp::Get().HasFocus())
190 CXBMCApp::Get().onReceive(intent
);
196 bool CJNIXBMCMediaSession::isActive() const
201 /**********************/
203 void CJNIXBMCMediaSession::_onPlayRequested(JNIEnv
* env
, jobject thiz
)
207 CJNIXBMCMediaSession
*inst
= find_instance(thiz
);
209 inst
->OnPlayRequested();
212 void CJNIXBMCMediaSession::_onPauseRequested(JNIEnv
* env
, jobject thiz
)
216 CJNIXBMCMediaSession
*inst
= find_instance(thiz
);
218 inst
->OnPauseRequested();
221 void CJNIXBMCMediaSession::_onNextRequested(JNIEnv
* env
, jobject thiz
)
225 CJNIXBMCMediaSession
*inst
= find_instance(thiz
);
227 inst
->OnNextRequested();
230 void CJNIXBMCMediaSession::_onPreviousRequested(JNIEnv
* env
, jobject thiz
)
234 CJNIXBMCMediaSession
*inst
= find_instance(thiz
);
236 inst
->OnPreviousRequested();
239 void CJNIXBMCMediaSession::_onForwardRequested(JNIEnv
* env
, jobject thiz
)
243 CJNIXBMCMediaSession
*inst
= find_instance(thiz
);
245 inst
->OnForwardRequested();
248 void CJNIXBMCMediaSession::_onRewindRequested(JNIEnv
* env
, jobject thiz
)
252 CJNIXBMCMediaSession
*inst
= find_instance(thiz
);
254 inst
->OnRewindRequested();
257 void CJNIXBMCMediaSession::_onStopRequested(JNIEnv
* env
, jobject thiz
)
261 CJNIXBMCMediaSession
*inst
= find_instance(thiz
);
263 inst
->OnStopRequested();
266 void CJNIXBMCMediaSession::_onSeekRequested(JNIEnv
* env
, jobject thiz
, jlong pos
)
270 CJNIXBMCMediaSession
*inst
= find_instance(thiz
);
272 inst
->OnSeekRequested(pos
);
275 bool CJNIXBMCMediaSession::_onMediaButtonEvent(JNIEnv
* env
, jobject thiz
, jobject intent
)
279 CJNIXBMCMediaSession
*inst
= find_instance(thiz
);
281 return inst
->OnMediaButtonEvent(CJNIIntent(jhobject::fromJNI(intent
)));