Commit 0940f327 authored by Paweł Wegner's avatar Paweł Wegner

cloudbrowser: don't use QtAndroid methods.

parent 55eb5737
......@@ -26,6 +26,8 @@ public class CloudBrowser extends QtActivity {
private RelativeLayout m_ad_view;
private boolean m_ad_view_visible = false;
private native void onRequestResult(int request, int result, Intent data);
private void updateAdView() {
if (m_ad_view != null && m_view_group != null)
m_view_group.removeView(m_ad_view);
......@@ -88,6 +90,12 @@ public class CloudBrowser extends QtActivity {
updateAdView();
}
@Override
public void onActivityResult(int request, int result, Intent data) {
super.onActivityResult(request, result, data);
onRequestResult(request, result, data);
}
@Override
public void onDestroy() {
m_notification_helper.release();
......
......@@ -7,7 +7,7 @@
#include <QDebug>
#include <QIcon>
#include <QUrl>
#include <QtAndroid>
#include <set>
#include "CloudContext.h"
#include "Request/GetThumbnail.h"
......@@ -17,6 +17,10 @@ const int RECEIVER_CODE = 43;
namespace {
AndroidUtility *android = nullptr;
QAndroidJniObject activity;
std::mutex mutex;
std::unordered_map<int, std::unordered_set<AndroidUtility::IResultListener *>>
result_listener;
} // namespace
extern "C" {
......@@ -28,6 +32,25 @@ Java_org_videolan_cloudbrowser_NotificationHelper_callback(JNIEnv *env,
emit android->notify(str);
env->ReleaseStringUTFChars(action, str);
}
JNIEXPORT void JNICALL
Java_org_videolan_cloudbrowser_CloudBrowser_onRequestResult(JNIEnv *, jobject *,
jint request,
jint result,
jobject data) {
std::unique_lock<std::mutex> lock(mutex);
auto cb = result_listener.find(request);
if (cb != result_listener.end()) {
while (!cb->second.empty()) {
auto c = *cb->second.begin();
cb->second.erase(cb->second.begin());
lock.unlock();
c->done(request, result, QAndroidJniObject(data));
lock.lock();
}
result_listener.erase(cb);
}
}
}
AndroidUtility::AndroidUtility() { android = this; }
......@@ -40,24 +63,24 @@ QString AndroidUtility::name() const { return "android"; }
bool AndroidUtility::openWebPage(QString url) {
if (intent_.isValid()) closeWebPage();
intent_ = QtAndroid::androidActivity().callObjectMethod(
intent_ = activity().callObjectMethod(
"openWebPage", "(Ljava/lang/String;)Landroid/content/Intent;",
QAndroidJniObject::fromString(url).object());
QtAndroid::startActivity(intent_, RECEIVER_CODE, &receiver_);
startActivity(intent_, RECEIVER_CODE);
return true;
}
void AndroidUtility::closeWebPage() {
QtAndroid::androidActivity().callMethod<void>(
"closeWebPage", "(Landroid/content/Intent;)V", intent_.object());
activity().callMethod<void>("closeWebPage", "(Landroid/content/Intent;)V",
intent_.object());
}
void AndroidUtility::landscapeOrientation() {
QtAndroid::androidActivity().callMethod<void>("setLandScapeOrientation");
activity().callMethod<void>("setLandScapeOrientation");
}
void AndroidUtility::defaultOrientation() {
QtAndroid::androidActivity().callMethod<void>("setDefaultOrientation");
activity().callMethod<void>("setDefaultOrientation");
}
void AndroidUtility::showPlayerNotification(bool playing, QString filename,
......@@ -66,7 +89,7 @@ void AndroidUtility::showPlayerNotification(bool playing, QString filename,
GetThumbnailRequest::thumbnail_path(filename));
auto arg1 = QAndroidJniObject::fromString(filename);
auto arg2 = QAndroidJniObject::fromString(title);
auto notification = QtAndroid::androidContext().callObjectMethod(
auto notification = activity().callObjectMethod(
"notification", "()Lorg/videolan/cloudbrowser/NotificationHelper;");
notification.callMethod<void>(
"showPlayerNotification",
......@@ -75,32 +98,59 @@ void AndroidUtility::showPlayerNotification(bool playing, QString filename,
}
void AndroidUtility::hidePlayerNotification() {
auto notification = QtAndroid::androidContext().callObjectMethod(
auto notification = activity().callObjectMethod(
"notification", "()Lorg/videolan/cloudbrowser/NotificationHelper;");
notification.callMethod<void>("hidePlayerNotification");
}
void AndroidUtility::enableKeepScreenOn() {
QtAndroid::androidActivity().callMethod<void>("enableKeepScreenOn");
activity().callMethod<void>("enableKeepScreenOn");
}
void AndroidUtility::disableKeepScreenOn() {
QtAndroid::androidActivity().callMethod<void>("disableKeepScreenOn");
activity().callMethod<void>("disableKeepScreenOn");
}
void AndroidUtility::showAd() {
QtAndroid::androidActivity().callMethod<void>("showAd");
}
void AndroidUtility::showAd() { activity().callMethod<void>("showAd"); }
void AndroidUtility::hideAd() {
QtAndroid::androidActivity().callMethod<void>("hideAd");
void AndroidUtility::hideAd() { activity().callMethod<void>("hideAd"); }
void AndroidUtility::setActivity(const QAndroidJniObject &activity) {
::activity = activity;
}
void AndroidUtility::ResultReceiver::handleActivityResult(
int, int, const QAndroidJniObject &) {}
QAndroidJniObject AndroidUtility::activity() { return ::activity; }
void AndroidUtility::startActivity(const QAndroidJniObject &intent,
int request_code,
const IResultListener *listener) {
if (listener) {
listener->code_ = request_code;
std::unique_lock<std::mutex> lock(mutex);
result_listener[request_code].insert(
const_cast<IResultListener *>(listener));
lock.unlock();
activity().callMethod<void>("startActivityForResult",
"(Landroid/content/Intent;I)V", intent.object(),
request_code);
} else {
activity().callMethod<void>("startActivity", "(Landroid/content/Intent;)V",
intent.object());
}
}
IPlatformUtility::Pointer IPlatformUtility::create() {
return cloudstorage::util::make_unique<AndroidUtility>();
}
AndroidUtility::IResultListener::~IResultListener() {
std::unique_lock<std::mutex> lock(mutex);
auto it = result_listener.find(code_);
if (it != result_listener.end()) {
auto e = it->second.find(this);
if (e != it->second.end()) it->second.erase(e);
if (it->second.empty()) result_listener.erase(it);
}
}
#endif // __ANDROID__
......@@ -3,13 +3,24 @@
#ifdef __ANDROID__
#include <QAndroidActivityResultReceiver>
#include <QAndroidJniObject>
#include <QObject>
#include <QtAndroid>
#include "IPlatformUtility.h"
#include "IRequest.h"
class AndroidUtility : public IPlatformUtility {
public:
class IResultListener
: public cloudstorage::IGenericCallback<int, int,
const QAndroidJniObject&> {
public:
~IResultListener() override;
private:
friend class AndroidUtility;
mutable int code_;
};
AndroidUtility();
~AndroidUtility();
......@@ -27,13 +38,14 @@ class AndroidUtility : public IPlatformUtility {
void showAd() override;
void hideAd() override;
static void setActivity(const QAndroidJniObject&);
static QAndroidJniObject activity();
static void startActivity(const QAndroidJniObject& intent, int request_code,
const IResultListener* = nullptr);
private:
QAndroidJniObject intent_;
class ResultReceiver : public QAndroidActivityResultReceiver {
private:
void handleActivityResult(int receiverRequestCode, int resultCode,
const QAndroidJniObject &data) override;
} receiver_;
};
#endif // __ANDROID__
......
......@@ -35,6 +35,7 @@
#include <QQmlEngine>
#include <QtQml>
#include "AndroidUtility.h"
#include "CloudContext.h"
#include "FileDialog.h"
#include "IPlatformUtility.h"
......@@ -106,6 +107,10 @@ int exec_cloudbrowser(int argc, char** argv) {
QtWebView::initialize();
#endif
#ifdef __ANDROID__
AndroidUtility::setActivity(QtAndroid::androidActivity());
#endif
app.setOrganizationName("VideoLAN");
app.setApplicationName("cloudbrowser");
app.setWindowIcon(QPixmap(":/resources/cloud.png"));
......
......@@ -5,7 +5,7 @@
#ifdef __ANDROID__
#include <QAndroidJniEnvironment>
#include <QtAndroid>
#include "AndroidUtility.h"
AndroidFile::AndroidFile(QString uri) : uri_(uri) {}
......@@ -14,7 +14,7 @@ bool AndroidFile::open(QIODevice::OpenMode mode) {
if (mode == QIODevice::WriteOnly) {
file_ = QAndroidJniObject("org/videolan/cloudbrowser/FileOutput",
"(Landroid/content/Context;Ljava/lang/String;)V",
QtAndroid::androidActivity().object(),
AndroidUtility::activity().object(),
QAndroidJniObject::fromString(uri_).object());
if (env->ExceptionCheck()) {
env->ExceptionDescribe();
......@@ -26,7 +26,7 @@ bool AndroidFile::open(QIODevice::OpenMode mode) {
} else if (mode == QIODevice::ReadOnly) {
file_ = QAndroidJniObject("org/videolan/cloudbrowser/FileInput",
"(Landroid/content/Context;Ljava/lang/String;)V",
QtAndroid::androidActivity().object(),
AndroidUtility::activity().object(),
QAndroidJniObject::fromString(uri_).object());
if (env->ExceptionCheck()) {
env->ExceptionDescribe();
......
......@@ -25,7 +25,7 @@ void IFileDialog::setFilename(QString e) {
#include <QAndroidActivityResultReceiver>
#include <QAndroidJniObject>
#include <QtAndroid>
#include "AndroidUtility.h"
const int REQUEST_CODE = 42;
......@@ -43,17 +43,18 @@ void FileDialog::open() {
"org/videolan/cloudbrowser/Utility", "openFileDialog",
"()Landroid/content/Intent;");
}
QtAndroid::startActivity(intent, REQUEST_CODE, &result_receiver_);
AndroidUtility::startActivity(intent.object(), REQUEST_CODE,
&result_receiver_);
}
FileDialog::ActivityReceiver::ActivityReceiver(FileDialog *dialog)
: file_dialog_(dialog) {}
void FileDialog::ActivityReceiver::handleActivityResult(
int request_code, int result_code, const QAndroidJniObject &data) {
void FileDialog::ActivityReceiver::done(int request_code, int result_code,
const QAndroidJniObject &data) {
if (request_code != REQUEST_CODE || result_code != -1) return;
auto uri = data.callObjectMethod("getData", "()Landroid/net/Uri;");
auto filename = QtAndroid::androidActivity().callObjectMethod(
auto filename = AndroidUtility::activity().callObjectMethod(
"filename", "(Landroid/net/Uri;)Ljava/lang/String;", uri.object());
file_dialog_->setFilename(filename.toString());
file_dialog_->setUrl(uri.toString());
......
......@@ -43,9 +43,8 @@ class IFileDialog : public QObject {
#ifdef __ANDROID__
#include <QAndroidActivityResultReceiver>
#include <QAndroidJniObject>
#include <QtAndroid>
#include "AndroidUtility.h"
class FileDialog : public IFileDialog {
public:
......@@ -54,10 +53,10 @@ class FileDialog : public IFileDialog {
void open() override;
private:
class ActivityReceiver : public QAndroidActivityResultReceiver {
class ActivityReceiver : public AndroidUtility::IResultListener {
public:
ActivityReceiver(FileDialog*);
void handleActivityResult(int request_code, int result_code,
void done(int request_code, int result_code,
const QAndroidJniObject& data) override;
private:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment