2 * Copyright (C) 2012-2018 Team Kodi
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.
11 #include "IActivityHandler.h"
12 #include "IInputHandler.h"
13 #include "JNIMainActivity.h"
14 #include "JNIXBMCAudioManagerOnAudioFocusChangeListener.h"
15 #include "JNIXBMCDisplayManagerDisplayListener.h"
16 #include "JNIXBMCMainView.h"
17 #include "JNIXBMCMediaSession.h"
18 #include "interfaces/IAnnouncer.h"
19 #include "platform/xbmc.h"
20 #include "threads/Event.h"
21 #include "utils/Geometry.h"
30 #include <android/native_activity.h>
31 #include <androidjni/Activity.h>
32 #include <androidjni/AudioManager.h>
33 #include <androidjni/BroadcastReceiver.h>
34 #include <androidjni/SurfaceHolder.h>
35 #include <androidjni/View.h>
38 class CAESinkAUDIOTRACK
;
40 class IInputDeviceCallbacks
;
41 class IInputDeviceEventHandler
;
42 class CVideoSyncAndroid
;
44 typedef struct _JNIEnv JNIEnv
;
55 std::string packageName
;
56 std::string packageLabel
;
62 friend class CWinSystemAndroidGLESContext
; // meh
65 static std::shared_ptr
<CNativeWindow
> CreateFromSurface(CJNISurfaceHolder holder
);
68 bool SetBuffersGeometry(int width
, int height
, int format
);
69 int32_t GetWidth() const;
70 int32_t GetHeight() const;
73 explicit CNativeWindow(ANativeWindow
* window
);
75 CNativeWindow() = delete;
76 CNativeWindow(const CNativeWindow
&) = delete;
77 CNativeWindow
& operator=(const CNativeWindow
&) = delete;
79 ANativeWindow
* m_window
{nullptr};
82 class CXBMCApp
: public IActivityHandler
,
83 public jni::CJNIMainActivity
,
84 public CJNIBroadcastReceiver
,
85 public ANNOUNCEMENT::IAnnouncer
,
86 public CJNISurfaceHolderCallback
89 static CXBMCApp
& Create(ANativeActivity
* nativeActivity
, IInputHandler
& inputhandler
)
91 m_appinstance
.reset(new CXBMCApp(nativeActivity
, inputhandler
));
92 return *m_appinstance
;
94 static CXBMCApp
& Get() { return *m_appinstance
; }
95 static void Destroy() { m_appinstance
.reset(); }
101 void Announce(ANNOUNCEMENT::AnnouncementFlag flag
,
102 const std::string
& sender
,
103 const std::string
& message
,
104 const CVariant
& data
) override
;
106 void onReceive(CJNIIntent intent
) override
;
107 void onNewIntent(CJNIIntent intent
) override
;
108 void onActivityResult(int requestCode
, int resultCode
, CJNIIntent resultData
) override
;
109 void onVolumeChanged(int volume
) override
;
110 virtual void onAudioFocusChange(int focusChange
);
111 void doFrame(int64_t frameTimeNanos
) override
;
112 void onVisibleBehindCanceled() override
;
114 // implementation of CJNIInputManagerInputDeviceListener
115 void onInputDeviceAdded(int deviceId
) override
;
116 void onInputDeviceChanged(int deviceId
) override
;
117 void onInputDeviceRemoved(int deviceId
) override
;
119 // implementation of DisplayManager::DisplayListener
120 void onDisplayAdded(int displayId
) override
;
121 void onDisplayChanged(int displayId
) override
;
122 void onDisplayRemoved(int displayId
) override
;
123 jni::jhobject
getDisplayListener() { return m_displayListener
.get_raw(); }
125 bool isValid() { return m_activity
!= NULL
; }
127 void onStart() override
;
128 void onResume() override
;
129 void onPause() override
;
130 void onStop() override
;
131 void onDestroy() override
;
133 void onSaveState(void **data
, size_t *size
) override
;
134 void onConfigurationChanged() override
;
135 void onLowMemory() override
;
137 void onCreateWindow(ANativeWindow
* window
) override
;
138 void onResizeWindow() override
;
139 void onDestroyWindow() override
;
140 void onGainFocus() override
;
141 void onLostFocus() override
;
146 bool Stop(int exitCode
);
149 std::shared_ptr
<CNativeWindow
> GetNativeWindow(int timeout
) const;
151 bool SetBuffersGeometry(int width
, int height
, int format
);
152 static int android_printf(const char *format
, ...);
154 int GetBatteryLevel() const;
155 void KeepScreenOn(bool on
);
156 bool HasFocus() const { return m_hasFocus
; }
158 static bool StartActivity(const std::string
& package
,
159 const std::string
& intent
= std::string(),
160 const std::string
& dataType
= std::string(),
161 const std::string
& dataURI
= std::string(),
162 const std::string
& flags
= std::string(),
163 const std::string
& extras
= std::string(),
164 const std::string
& action
= std::string(),
165 const std::string
& category
= std::string(),
166 const std::string
& className
= std::string());
167 std::vector
<androidPackage
> GetApplications() const;
169 static int GetMaxSystemVolume();
170 static float GetSystemVolume();
171 static void SetSystemVolume(float percent
);
173 void SetDisplayMode(int mode
, float rate
);
175 void SetVideoLayoutBackgroundColor(const int color
);
177 CRect
MapRenderToDroid(const CRect
& srcRect
);
179 // Playback callbacks
180 void OnPlayBackStarted();
181 void OnPlayBackPaused();
182 void OnPlayBackStopped();
185 void UpdateSessionMetadata();
186 void UpdateSessionState();
188 // input device methods
189 void RegisterInputDeviceCallbacks(IInputDeviceCallbacks
* handler
);
190 void UnregisterInputDeviceCallbacks();
191 static const CJNIViewInputDevice
GetInputDevice(int deviceId
);
192 static std::vector
<int> GetInputDeviceIds();
194 void RegisterInputDeviceEventHandler(IInputDeviceEventHandler
* handler
);
195 void UnregisterInputDeviceEventHandler();
196 bool onInputDeviceEvent(const AInputEvent
* event
);
198 void InitFrameCallback(CVideoSyncAndroid
* syncImpl
);
199 void DeinitFrameCallback();
201 // Application slow ping
204 bool WaitVSync(unsigned int milliSeconds
);
205 int64_t GetNextFrameTime() const;
206 float GetFrameLatencyMs() const;
208 bool getVideosurfaceInUse();
209 void setVideosurfaceInUse(bool videosurfaceInUse
);
212 // limit who can access Volume
213 friend class CAESinkAUDIOTRACK
;
215 static int GetMaxSystemVolume(JNIEnv
*env
);
216 bool AcquireAudioFocus();
217 bool ReleaseAudioFocus();
218 void RequestVisibleBehind(bool requested
);
221 static std::unique_ptr
<CXBMCApp
> m_appinstance
;
223 CXBMCApp(ANativeActivity
* nativeActivity
, IInputHandler
& inputhandler
);
225 jni::CJNIXBMCAudioManagerOnAudioFocusChangeListener m_audioFocusListener
;
226 jni::CJNIXBMCDisplayManagerDisplayListener m_displayListener
;
227 std::unique_ptr
<jni::CJNIXBMCMainView
> m_mainView
;
228 std::unique_ptr
<jni::CJNIXBMCMediaSession
> m_mediaSession
;
229 std::string
GetFilenameFromIntent(const CJNIIntent
&intent
);
234 static void SetDisplayModeCallback(void* modeVariant
);
235 static void KeepScreenOnCallback(void* onVariant
);
236 static void SetViewBackgroundColorCallback(void* mapVariant
);
238 static void RegisterDisplayListenerCallback(void*);
239 void UnregisterDisplayListener();
241 ANativeActivity
* m_activity
{nullptr};
242 IInputHandler
& m_inputHandler
;
243 int m_batteryLevel
{0};
244 bool m_hasFocus
{false};
245 bool m_headsetPlugged
{false};
246 bool m_hdmiSource
{false};
247 bool m_wakeUp
{false};
248 bool m_aeReset
{false};
249 bool m_hdmiPlugged
{true};
250 bool m_mediaSessionUpdated
{false};
251 IInputDeviceCallbacks
* m_inputDeviceCallbacks
{nullptr};
252 IInputDeviceEventHandler
* m_inputDeviceEventHandler
{nullptr};
253 bool m_hasReqVisible
{false};
254 bool m_firstrun
{true};
255 std::atomic
<bool> m_exiting
{false};
257 bool m_bResumePlayback
{false};
258 std::thread m_thread
;
259 mutable CCriticalSection m_applicationsMutex
;
260 mutable std::vector
<androidPackage
> m_applications
;
262 std::shared_ptr
<CNativeWindow
> m_window
;
264 CVideoSyncAndroid
* m_syncImpl
{nullptr};
266 CEvent m_displayChangeEvent
;
268 bool XBMC_DestroyDisplay();
269 bool XBMC_SetupDisplay();
274 uint32_t m_playback_state
{0};
275 int64_t m_frameTimeNanos
{0};
276 float m_refreshRate
{0.0f
};
279 // CJNISurfaceHolderCallback interface
280 void surfaceChanged(CJNISurfaceHolder holder
, int format
, int width
, int height
) override
;
281 void surfaceCreated(CJNISurfaceHolder holder
) override
;
282 void surfaceDestroyed(CJNISurfaceHolder holder
) override
;