Android: Light/dark modes detection

After commit: 2248487c6ca9d5459c70a16868d5aeee07d96157 light/dark mode
detection is supported by Windows and macOS. This commit add similar
implementation on the Android side.

Task-number: QTBUG-83185
Change-Id: Id1ece98e91a31759b58d651ef62b3715ea25d85f
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
(cherry picked from commit 76abdaafb540be90e87c6689cc64040e95147777)
This commit is contained in:
Bartlomiej Moskal 2022-08-02 12:09:36 +02:00
parent 85e0298e9c
commit 7d372f6e7a
7 changed files with 43 additions and 0 deletions

View File

@ -849,6 +849,8 @@ public class QtActivityDelegate
QtNative.handleOrientationChanged(rotation, m_nativeOrientation);
m_currentRotation = rotation;
handleUiModeChange(m_activity.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK);
float refreshRate = (Build.VERSION.SDK_INT < Build.VERSION_CODES.R)
? m_activity.getWindowManager().getDefaultDisplay().getRefreshRate()
: m_activity.getDisplay().getRefreshRate();
@ -969,6 +971,18 @@ public class QtActivityDelegate
updateFullScreen();
}
private void handleUiModeChange(int uiMode)
{
switch (uiMode) {
case Configuration.UI_MODE_NIGHT_NO:
QtNative.handleUiDarkModeChanged(0);
break;
case Configuration.UI_MODE_NIGHT_YES:
QtNative.handleUiDarkModeChanged(1);
break;
}
}
public void onConfigurationChanged(Configuration configuration)
{
try {
@ -976,6 +990,7 @@ public class QtActivityDelegate
} catch (Exception e) {
e.printStackTrace();
}
handleUiModeChange(configuration.uiMode & Configuration.UI_MODE_NIGHT_MASK);
}
public void onDestroy()

View File

@ -1404,6 +1404,7 @@ public class QtNative
public static native void handleOrientationChanged(int newRotation, int nativeOrientation);
public static native void handleRefreshRateChanged(float refreshRate);
// screen methods
public static native void handleUiDarkModeChanged(int newUiMode);
// pointer methods
public static native void mouseDown(int winId, int x, int y);

View File

@ -755,6 +755,12 @@ static void handleRefreshRateChanged(JNIEnv */*env*/, jclass /*cls*/, jfloat ref
m_androidPlatformIntegration->setRefreshRate(refreshRate);
}
static void handleUiDarkModeChanged(JNIEnv */*env*/, jobject /*thiz*/, jint newUiMode)
{
QAndroidPlatformIntegration::setAppearance(
(newUiMode == 1 ) ? QPlatformTheme::Appearance::Dark : QPlatformTheme::Appearance::Light);
}
static void onActivityResult(JNIEnv */*env*/, jclass /*cls*/,
jint requestCode,
jint resultCode,
@ -784,6 +790,7 @@ static JNINativeMethod methods[] = {
{ "setSurface", "(ILjava/lang/Object;II)V", (void *)setSurface },
{ "updateWindow", "()V", (void *)updateWindow },
{ "updateApplicationState", "(I)V", (void *)updateApplicationState },
{ "handleUiDarkModeChanged", "(I)V", (void *)handleUiDarkModeChanged },
{ "handleOrientationChanged", "(II)V", (void *)handleOrientationChanged },
{ "onActivityResult", "(IILandroid/content/Intent;)V", (void *)onActivityResult },
{ "onNewIntent", "(Landroid/content/Intent;)V", (void *)onNewIntent },

View File

@ -488,6 +488,15 @@ void QAndroidPlatformIntegration::setScreenSize(int width, int height)
QMetaObject::invokeMethod(m_primaryScreen, "setSize", Qt::AutoConnection, Q_ARG(QSize, QSize(width, height)));
}
QPlatformTheme::Appearance QAndroidPlatformIntegration::m_appearance = QPlatformTheme::Appearance::Light;
void QAndroidPlatformIntegration::setAppearance(QPlatformTheme::Appearance newAppearance)
{
if (m_appearance == newAppearance)
return;
m_appearance = newAppearance;
}
void QAndroidPlatformIntegration::setScreenSizeParameters(const QSize &physicalSize,
const QSize &screenSize,
const QRect &availableGeometry)

View File

@ -13,6 +13,7 @@
#include <qpa/qplatformnativeinterface.h>
#include <qpa/qplatformopenglcontext.h>
#include <qpa/qplatformoffscreensurface.h>
#include <qpa/qplatformtheme.h>
#include <EGL/egl.h>
#include <memory>
@ -103,6 +104,8 @@ public:
void flushPendingUpdates();
static void setAppearance(QPlatformTheme::Appearance newAppearance);
static QPlatformTheme::Appearance appearance() { return m_appearance; }
#if QT_CONFIG(vulkan)
QPlatformVulkanInstance *createPlatformVulkanInstance(QVulkanInstance *instance) const override;
#endif
@ -115,6 +118,8 @@ private:
QThread *m_mainThread;
static QPlatformTheme::Appearance m_appearance;
static QRect m_defaultAvailableGeometry;
static QSize m_defaultPhysicalSize;
static QSize m_defaultScreenSize;

View File

@ -358,6 +358,11 @@ void QAndroidPlatformTheme::showPlatformMenuBar()
QtAndroidMenu::openOptionsMenu();
}
QPlatformTheme::Appearance QAndroidPlatformTheme::appearance() const
{
return QAndroidPlatformIntegration::appearance();
}
static inline int paletteType(QPlatformTheme::Palette type)
{
switch (type) {

View File

@ -35,6 +35,7 @@ public:
QPlatformMenu *createPlatformMenu() const override;
QPlatformMenuItem *createPlatformMenuItem() const override;
void showPlatformMenuBar() override;
Appearance appearance() const override;
const QPalette *palette(Palette type = SystemPalette) const override;
const QFont *font(Font type = SystemFont) const override;
QVariant themeHint(ThemeHint hint) const override;