From 2c9ca420bf3f8b32977a88e7923f18cc7557c613 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Str=C3=B8mme?= <christian.stromme@digia.com> Date: Thu, 30 Jan 2014 13:16:03 +0100 Subject: [PATCH] Add new method to get the main Android activity. There were no public APIs the user could use to get access to their applications main activity, and thus far the only solution have been to access undocumented and private APIs. [ChangeLog][QtAndroidExtras] Added functionality to get the Android main activity. Change-Id: Ifd4121e8bc643360ed7a7038ffae9e872b59db71 Reviewed-by: Paul Olav Tvete <paul.tvete@digia.com> --- src/androidextras/android/android.pri | 4 ++ .../android/qandroidfunctions.cpp | 69 +++++++++++++++++++ src/androidextras/android/qandroidfunctions.h | 61 ++++++++++++++++ src/androidextras/androidextras.pro | 2 + .../doc/qtandroidextras.qdocconf | 8 ++- .../doc/src/qtandroidextras-index.qdoc | 2 +- tests/auto/auto.pro | 3 +- .../qandroidfunctions/qandroidfunctions.pro | 4 ++ .../tst_qandroidfunctions.cpp | 61 ++++++++++++++++ 9 files changed, 209 insertions(+), 5 deletions(-) create mode 100644 src/androidextras/android/android.pri create mode 100644 src/androidextras/android/qandroidfunctions.cpp create mode 100644 src/androidextras/android/qandroidfunctions.h create mode 100644 tests/auto/qandroidfunctions/qandroidfunctions.pro create mode 100644 tests/auto/qandroidfunctions/tst_qandroidfunctions.cpp diff --git a/src/androidextras/android/android.pri b/src/androidextras/android/android.pri new file mode 100644 index 0000000..5869312 --- /dev/null +++ b/src/androidextras/android/android.pri @@ -0,0 +1,4 @@ +SOURCES += \ + $$PWD/qandroidfunctions.cpp +HEADERS += \ + $$PWD/qandroidfunctions.h \ No newline at end of file diff --git a/src/androidextras/android/qandroidfunctions.cpp b/src/androidextras/android/qandroidfunctions.cpp new file mode 100644 index 0000000..55bc50a --- /dev/null +++ b/src/androidextras/android/qandroidfunctions.cpp @@ -0,0 +1,69 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qandroidfunctions.h" + +#include <QtCore/private/qjnihelpers_p.h> + +QT_BEGIN_NAMESPACE + +/*! + \namespace QtAndroid + \inmodule QtAndroidExtras + \since 5.3 + \brief The QtAndroid namespace provides miscellaneous functions to aid Android development. + \inheaderfile QtAndroid +*/ + +/*! + \since 5.3 + \fn QAndroidJniObject QtAndroid::androidActivity() + + Returns a handle to this applications main Activity + + \sa QAndroidJniObject +*/ +QAndroidJniObject QtAndroid::androidActivity() +{ + return QtAndroidPrivate::activity(); +} + +QT_END_NAMESPACE diff --git a/src/androidextras/android/qandroidfunctions.h b/src/androidextras/android/qandroidfunctions.h new file mode 100644 index 0000000..ffd7c3b --- /dev/null +++ b/src/androidextras/android/qandroidfunctions.h @@ -0,0 +1,61 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QANDROIDFUNCTIONS_H +#define QANDROIDFUNCTIONS_H + +#if 0 +#pragma qt_class(QtAndroid) +#endif + +#include <QtAndroidExtras/qandroidextrasglobal.h> +#include <QtAndroidExtras/qandroidjniobject.h> + +QT_BEGIN_NAMESPACE + +namespace QtAndroid +{ + Q_ANDROIDEXTRAS_EXPORT QAndroidJniObject androidActivity(); +} + +QT_END_NAMESPACE + +#endif // QANDROIDFUNCTIONS_H diff --git a/src/androidextras/androidextras.pro b/src/androidextras/androidextras.pro index 24d4371..78bd8eb 100644 --- a/src/androidextras/androidextras.pro +++ b/src/androidextras/androidextras.pro @@ -4,5 +4,7 @@ QMAKE_DOCS = \ $$PWD/doc/qtandroidextras.qdocconf QT -= gui QT += core-private + load(qt_module) include(jni/jni.pri) +include(android/android.pri) diff --git a/src/androidextras/doc/qtandroidextras.qdocconf b/src/androidextras/doc/qtandroidextras.qdocconf index c1f7806..ab6b5d0 100644 --- a/src/androidextras/doc/qtandroidextras.qdocconf +++ b/src/androidextras/doc/qtandroidextras.qdocconf @@ -1,5 +1,7 @@ include($QT_INSTALL_DOCS/global/qt-module-defaults.qdocconf) +Cpp.ignoretokens += Q_ANDROIDEXTRAS_EXPORT + project = QtAndroidExtras description = Qt Android Extras Reference Documentation url = http://qt-project.org/doc/qt-$QT_VER @@ -19,10 +21,10 @@ qhp.QtAndroidExtras.filterAttributes = qtandroidextras $QT_VERSION qtrefdoc qhp.QtAndroidExtras.customFilters.Qt.name = QtAndroidExtras $QT_VERSION qhp.QtAndroidExtras.customFilters.Qt.filterAttributes = qtandroidextras $QT_VERSION -qhp.QtAndroidExtras.subprojects = classes -qhp.QtAndroidExtras.subprojects.classes.title = C++ Classes +qhp.QtAndroidExtras.subprojects = classes examples +qhp.QtAndroidExtras.subprojects.classes.title = C++ Classes and Namespaces qhp.QtAndroidExtras.subprojects.classes.indexTitle = Qt Android Extras C++ Classes -qhp.QtAndroidExtras.subprojects.classes.selectors = class fake:headerfile +qhp.QtAndroidExtras.subprojects.classes.selectors = class function namespace fake:headerfile qhp.QtAndroidExtras.subprojects.classes.sortPages = true qhp.QtAndroidExtras.subprojects.examples.title = Examples qhp.QtAndroidExtras.subprojects.examples.indexTitle = Qt Android Extras Examples diff --git a/src/androidextras/doc/src/qtandroidextras-index.qdoc b/src/androidextras/doc/src/qtandroidextras-index.qdoc index 74ca249..c9eaadb 100644 --- a/src/androidextras/doc/src/qtandroidextras-index.qdoc +++ b/src/androidextras/doc/src/qtandroidextras-index.qdoc @@ -50,7 +50,7 @@ Links to the API reference materials: \list - \li \l{Qt Android Extras C++ Classes}{C++ Classes} + \li \l{Qt Android Extras C++ Classes}{C++ Classes and Namespaces} \endlist \section1 Examples diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro index 94b6dd8..bf54603 100644 --- a/tests/auto/auto.pro +++ b/tests/auto/auto.pro @@ -2,4 +2,5 @@ TEMPLATE = subdirs SUBDIRS += \ cmake \ qandroidjnienvironment \ - qandroidjniobject + qandroidjniobject \ + qandroidfunctions diff --git a/tests/auto/qandroidfunctions/qandroidfunctions.pro b/tests/auto/qandroidfunctions/qandroidfunctions.pro new file mode 100644 index 0000000..813b214 --- /dev/null +++ b/tests/auto/qandroidfunctions/qandroidfunctions.pro @@ -0,0 +1,4 @@ +CONFIG += testcase +TARGET = tst_qandroidfunctions +QT += testlib androidextras +SOURCES += tst_qandroidfunctions.cpp diff --git a/tests/auto/qandroidfunctions/tst_qandroidfunctions.cpp b/tests/auto/qandroidfunctions/tst_qandroidfunctions.cpp new file mode 100644 index 0000000..9f6bfd9 --- /dev/null +++ b/tests/auto/qandroidfunctions/tst_qandroidfunctions.cpp @@ -0,0 +1,61 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtTest/QtTest> +#include <QtAndroidExtras/QtAndroid> + +class tst_QAndroidFunctions : public QObject +{ + Q_OBJECT +private slots: + void testAndroidActivity(); +}; + +void tst_QAndroidFunctions::testAndroidActivity() +{ + QAndroidJniObject activity = QtAndroid::androidActivity(); + QVERIFY(activity.isValid()); + QVERIFY(activity.callMethod<jboolean>("isTaskRoot")); +} + +QTEST_APPLESS_MAIN(tst_QAndroidFunctions) + +#include "tst_qandroidfunctions.moc" -- GitLab