From a55cae70165206bd2a04306291fb3442968b69c3 Mon Sep 17 00:00:00 2001 From: Assam Boudjelthia Date: Wed, 4 Mar 2020 14:42:13 +0200 Subject: Fix notification example crashing The notification example needed some fixing because it was crashing, now passing context from C++ side. There are some changes to the Android Notification API, so some changes has to be made to make sure the example works for different old and new Android API levels. + Updating the icons. Task-number: QTBUG-80717 Change-Id: I1560e31b73233895053b1a9a6a523a7b163c5d89 Reviewed-by: BogDan Vatra --- .../android-sources/AndroidManifest.xml | 40 --------- .../android-sources/res/drawable/icon.png | Bin 3264 -> 0 bytes .../example/notification/NotificationClient.java | 80 ----------------- .../notification/android/AndroidManifest.xml | 87 +++++++++++++++++++ .../android/res/drawable-hdpi/icon.png | Bin 0 -> 3654 bytes .../android/res/drawable-ldpi/icon.png | Bin 0 -> 1432 bytes .../android/res/drawable-mdpi/icon.png | Bin 0 -> 1998 bytes .../android/res/drawable-xhdpi/icon.png | Bin 0 -> 4549 bytes .../android/res/drawable-xxhdpi/icon.png | Bin 0 -> 9275 bytes .../android/res/drawable-xxxhdpi/icon.png | Bin 0 -> 12866 bytes .../example/notification/NotificationClient.java | 95 +++++++++++++++++++++ .../src/qtandroidextras-example-notification.qdoc | 13 ++- .../androidextras/notification/notification.pro | 12 ++- .../notification/notificationclient.cpp | 12 +-- 14 files changed, 204 insertions(+), 135 deletions(-) delete mode 100644 examples/androidextras/notification/android-sources/AndroidManifest.xml delete mode 100644 examples/androidextras/notification/android-sources/res/drawable/icon.png delete mode 100644 examples/androidextras/notification/android-sources/src/org/qtproject/example/notification/NotificationClient.java create mode 100644 examples/androidextras/notification/android/AndroidManifest.xml create mode 100644 examples/androidextras/notification/android/res/drawable-hdpi/icon.png create mode 100644 examples/androidextras/notification/android/res/drawable-ldpi/icon.png create mode 100644 examples/androidextras/notification/android/res/drawable-mdpi/icon.png create mode 100644 examples/androidextras/notification/android/res/drawable-xhdpi/icon.png create mode 100644 examples/androidextras/notification/android/res/drawable-xxhdpi/icon.png create mode 100644 examples/androidextras/notification/android/res/drawable-xxxhdpi/icon.png create mode 100644 examples/androidextras/notification/android/src/org/qtproject/example/notification/NotificationClient.java (limited to 'examples') diff --git a/examples/androidextras/notification/android-sources/AndroidManifest.xml b/examples/androidextras/notification/android-sources/AndroidManifest.xml deleted file mode 100644 index 2434bbb..0000000 --- a/examples/androidextras/notification/android-sources/AndroidManifest.xml +++ /dev/null @@ -1,40 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/examples/androidextras/notification/android-sources/res/drawable/icon.png b/examples/androidextras/notification/android-sources/res/drawable/icon.png deleted file mode 100644 index 035c600..0000000 Binary files a/examples/androidextras/notification/android-sources/res/drawable/icon.png and /dev/null differ diff --git a/examples/androidextras/notification/android-sources/src/org/qtproject/example/notification/NotificationClient.java b/examples/androidextras/notification/android-sources/src/org/qtproject/example/notification/NotificationClient.java deleted file mode 100644 index fd0ad5a..0000000 --- a/examples/androidextras/notification/android-sources/src/org/qtproject/example/notification/NotificationClient.java +++ /dev/null @@ -1,80 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtAndroidExtras module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** 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 The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -package org.qtproject.example.notification; - -import android.app.Notification; -import android.app.NotificationManager; -import android.content.Context; - -public class NotificationClient extends org.qtproject.qt5.android.bindings.QtActivity -{ - private static NotificationManager m_notificationManager; - private static Notification.Builder m_builder; - private static NotificationClient m_instance; - - public NotificationClient() - { - m_instance = this; - } - - public static void notify(String s) - { - if (m_notificationManager == null) { - m_notificationManager = (NotificationManager)m_instance.getSystemService(Context.NOTIFICATION_SERVICE); - m_builder = new Notification.Builder(m_instance); - m_builder.setSmallIcon(R.drawable.icon); - m_builder.setContentTitle("A message from Qt!"); - } - - m_builder.setContentText(s); - m_notificationManager.notify(1, m_builder.build()); - } -} diff --git a/examples/androidextras/notification/android/AndroidManifest.xml b/examples/androidextras/notification/android/AndroidManifest.xml new file mode 100644 index 0000000..903db5b --- /dev/null +++ b/examples/androidextras/notification/android/AndroidManifest.xml @@ -0,0 +1,87 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/androidextras/notification/android/res/drawable-hdpi/icon.png b/examples/androidextras/notification/android/res/drawable-hdpi/icon.png new file mode 100644 index 0000000..d3ccebe Binary files /dev/null and b/examples/androidextras/notification/android/res/drawable-hdpi/icon.png differ diff --git a/examples/androidextras/notification/android/res/drawable-ldpi/icon.png b/examples/androidextras/notification/android/res/drawable-ldpi/icon.png new file mode 100644 index 0000000..2194be1 Binary files /dev/null and b/examples/androidextras/notification/android/res/drawable-ldpi/icon.png differ diff --git a/examples/androidextras/notification/android/res/drawable-mdpi/icon.png b/examples/androidextras/notification/android/res/drawable-mdpi/icon.png new file mode 100644 index 0000000..31812cc Binary files /dev/null and b/examples/androidextras/notification/android/res/drawable-mdpi/icon.png differ diff --git a/examples/androidextras/notification/android/res/drawable-xhdpi/icon.png b/examples/androidextras/notification/android/res/drawable-xhdpi/icon.png new file mode 100644 index 0000000..3aeae64 Binary files /dev/null and b/examples/androidextras/notification/android/res/drawable-xhdpi/icon.png differ diff --git a/examples/androidextras/notification/android/res/drawable-xxhdpi/icon.png b/examples/androidextras/notification/android/res/drawable-xxhdpi/icon.png new file mode 100644 index 0000000..f754fd1 Binary files /dev/null and b/examples/androidextras/notification/android/res/drawable-xxhdpi/icon.png differ diff --git a/examples/androidextras/notification/android/res/drawable-xxxhdpi/icon.png b/examples/androidextras/notification/android/res/drawable-xxxhdpi/icon.png new file mode 100644 index 0000000..d0d043b Binary files /dev/null and b/examples/androidextras/notification/android/res/drawable-xxxhdpi/icon.png differ diff --git a/examples/androidextras/notification/android/src/org/qtproject/example/notification/NotificationClient.java b/examples/androidextras/notification/android/src/org/qtproject/example/notification/NotificationClient.java new file mode 100644 index 0000000..84adcf0 --- /dev/null +++ b/examples/androidextras/notification/android/src/org/qtproject/example/notification/NotificationClient.java @@ -0,0 +1,95 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtAndroidExtras module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** 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 The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +package org.qtproject.example.notification; + +import android.app.Notification; +import android.app.NotificationManager; +import android.content.Context; +import android.content.Intent; +import android.app.PendingIntent; +import android.graphics.Color; +import android.graphics.BitmapFactory; +import android.app.NotificationChannel; + +public class NotificationClient +{ + private static NotificationManager m_notificationManager; + private static Notification.Builder m_builder; + + public NotificationClient() {} + + public static void notify(Context context, String message) { + try { + m_notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { + int importance = NotificationManager.IMPORTANCE_DEFAULT; + NotificationChannel notificationChannel = new NotificationChannel("Qt", "Qt Notifier", importance); + m_notificationManager.createNotificationChannel(notificationChannel); + m_builder = new Notification.Builder(context, notificationChannel.getId()); + } else { + m_builder = new Notification.Builder(context); + } + + m_builder.setSmallIcon(R.drawable.ic_launcher) + .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher)) + .setContentTitle("A message from Qt!") + .setContentText(message) + .setDefaults(Notification.DEFAULT_SOUND) + .setColor(Color.GREEN) + .setAutoCancel(true); + + m_notificationManager.notify(0, m_builder.build()); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/examples/androidextras/notification/doc/src/qtandroidextras-example-notification.qdoc b/examples/androidextras/notification/doc/src/qtandroidextras-example-notification.qdoc index 0bc08c1..a042af5 100644 --- a/examples/androidextras/notification/doc/src/qtandroidextras-example-notification.qdoc +++ b/examples/androidextras/notification/doc/src/qtandroidextras-example-notification.qdoc @@ -47,7 +47,7 @@ We define a custom Java class called \c NotificationClient in the NotificationClient.java file: - \quotefromfile notification/android-sources/src/org/qtproject/example/notification/NotificationClient.java + \quotefromfile notification/android/src/org/qtproject/example/notification/NotificationClient.java \skipto org.qtproject.example.notification \printuntil /^\}/ @@ -71,12 +71,19 @@ \printuntil notificationClient); In the NotificationClient C++ class source file, notificationclient.cpp, we - import the QtAndroidJniObject class to be able to use its functions: + import the QtAndroid class: \quotefromfile notification/notificationclient.cpp - \skipto QAndroidJniObject + \skipto + This will allow us to use \c QAndroidJniObject for JNI calls, and also allow + us to pass our application's context to the Java methods as in: + + \code + QtAndroid::androidContext().object() + \endcode + We connect the \c notificationChanged() signal to the \c updateAndroidNotification() slot to update the notification text when the \c notification property changes: diff --git a/examples/androidextras/notification/notification.pro b/examples/androidextras/notification/notification.pro index a3ce911..b2fdec8 100644 --- a/examples/androidextras/notification/notification.pro +++ b/examples/androidextras/notification/notification.pro @@ -1,16 +1,9 @@ QT += quick androidextras -ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android-sources - SOURCES += \ main.cpp \ notificationclient.cpp -OTHER_FILES += \ - qml/main.qml \ - android-sources/src/org/qtproject/example/notification/NotificationClient.java \ - android-sources/AndroidManifest.xml - RESOURCES += \ main.qrc @@ -19,3 +12,8 @@ HEADERS += \ target.path = $$[QT_INSTALL_EXAMPLES]/androidextras/notification INSTALLS += target + +ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android +OTHER_FILES += \ + android/src/org/qtproject/example/notification/NotificationClient.java \ + android/AndroidManifest.xml diff --git a/examples/androidextras/notification/notificationclient.cpp b/examples/androidextras/notification/notificationclient.cpp index 77b942c..871d889 100644 --- a/examples/androidextras/notification/notificationclient.cpp +++ b/examples/androidextras/notification/notificationclient.cpp @@ -50,7 +50,7 @@ #include "notificationclient.h" -#include +#include NotificationClient::NotificationClient(QObject *parent) : QObject(parent) @@ -75,8 +75,10 @@ QString NotificationClient::notification() const void NotificationClient::updateAndroidNotification() { QAndroidJniObject javaNotification = QAndroidJniObject::fromString(m_notification); - QAndroidJniObject::callStaticMethod("org/qtproject/example/notification/NotificationClient", - "notify", - "(Ljava/lang/String;)V", - javaNotification.object()); + QAndroidJniObject::callStaticMethod( + "org/qtproject/example/notification/NotificationClient", + "notify", + "(Landroid/content/Context;Ljava/lang/String;)V", + QtAndroid::androidContext().object(), + javaNotification.object()); } -- cgit v1.2.3