Skip to content

Commit b6ca303

Browse files
committed
use android ALooper as rxcpp scheduler on android platform
1 parent 96f47ee commit b6ca303

File tree

4 files changed

+51
-19
lines changed

4 files changed

+51
-19
lines changed

include/Application/RunLoop.hpp

+14
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
#include <rxcpp/operators/rx-observe_on.hpp>
2222
#include <rxcpp/schedulers/rx-runloop.hpp>
2323

24+
#if defined(__ANDROID__)
25+
#include <android/looper.h>
26+
#endif
27+
28+
#include "Utility/Log.hpp"
29+
2430
namespace VGG
2531
{
2632

@@ -33,7 +39,15 @@ class RunLoop
3339

3440
rxcpp::observe_on_one_worker thread()
3541
{
42+
#if defined(__ANDROID__)
43+
ALooper* looper = ALooper_forThread();
44+
if (looper == nullptr) {
45+
looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
46+
}
47+
return rxcpp::observe_on_new_thread();
48+
#else
3649
return rxcpp::observe_on_run_loop(m_runLoop);
50+
#endif
3751
}
3852

3953
bool empty() const

include/Utility/VggTimer.hpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,15 @@ class Timer
2727
using TCallback = std::function<void()>;
2828

2929
private:
30+
double m_interval;
3031
TCallback m_callback;
32+
bool m_repeats;
3133
rxcpp::composite_subscription m_timer;
3234

3335
public:
3436
Timer(double interval, TCallback callback, bool repeats = false);
37+
bool setup();
3538
void invalidate();
3639
};
3740

38-
} // namespace VGG
41+
} // namespace VGG

src/Application/UIScrollView.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -316,14 +316,19 @@ void UIScrollView::setScrollAnimation(std::shared_ptr<UIScrollViewAnimation> ani
316316

317317
if (!m_scrollTimer)
318318
{
319-
m_scrollTimer.reset(new Timer(
319+
auto t = new Timer(
320320
1. / 60,
321321
[this]()
322322
{
323323
VERBOSE("UIScrollView: call updateScrollAnimation by timer");
324324
this->updateScrollAnimation();
325325
},
326-
true));
326+
true);
327+
m_scrollTimer.reset(t);
328+
if (!m_scrollTimer->setup())
329+
{
330+
WARN("Failed to setup scroll timer");
331+
}
327332
}
328333
}
329334

src/Utility/VggTimer.cpp

+26-16
Original file line numberDiff line numberDiff line change
@@ -37,35 +37,45 @@ namespace VGG
3737
{
3838

3939
Timer::Timer(double interval, TCallback callback, bool repeats)
40-
: m_callback(callback)
40+
: m_interval(interval)
41+
, m_callback(callback)
42+
, m_repeats(repeats)
4143
{
42-
auto period = std::chrono::milliseconds(static_cast<int64_t>(interval * 1000));
44+
}
45+
46+
bool Timer::setup()
47+
{
48+
auto period = std::chrono::milliseconds(static_cast<int64_t>(m_interval * 1000));
4349

44-
if (repeats)
50+
if (m_repeats)
4551
{
4652
auto values = rxcpp::observable<>::interval(period);
47-
m_timer = values.observe_on(RunLoop::sharedInstance()->thread())
48-
.subscribe([this](int v) { this->m_callback(); }, []() {});
53+
auto observeTarget = RunLoop::sharedInstance()->thread();
54+
auto s = values
55+
#if defined(__ANDROID__)
56+
.subscribe_on(rxcpp::observe_on_new_thread())
57+
#endif
58+
.observe_on(observeTarget);
59+
m_timer = s.subscribe([this](int v) { if (this->m_callback) { this->m_callback(); } }, []() {});
4960
}
5061
else
5162
{
5263
auto values = rxcpp::observable<>::timer(period);
53-
m_timer = values.observe_on(RunLoop::sharedInstance()->thread())
54-
.subscribe(
55-
[this](int v)
56-
{
57-
if (this->m_callback)
58-
{
59-
this->m_callback();
60-
}
61-
},
62-
[]() {});
64+
auto observeTarget = RunLoop::sharedInstance()->thread();
65+
auto s = values
66+
#if defined(__ANDROID__)
67+
.subscribe_on(rxcpp::observe_on_new_thread())
68+
#endif
69+
.observe_on(observeTarget);
70+
m_timer = s.subscribe([this](int v) { if (this->m_callback) { this->m_callback(); } }, []() {});
6371
}
72+
73+
return true;
6474
}
6575

6676
void Timer::invalidate()
6777
{
6878
m_timer.unsubscribe();
6979
}
7080

71-
} // namespace VGG
81+
} // namespace VGG

0 commit comments

Comments
 (0)