diff options
author | Assam Boudjelthia <assam.boudjelthia@qt.io> | 2023-11-02 15:59:27 +0200 |
---|---|---|
committer | Assam Boudjelthia <assam.boudjelthia@qt.io> | 2023-11-10 21:57:11 +0200 |
commit | 2f706df6511e861ee7a038304e136273aa2907f7 (patch) | |
tree | 76886fcfb41da1f11c97cc9c745cae46fc5d87c7 | |
parent | f5e230285147efa95f32594f9b2e2581de819e47 (diff) |
Android: set the default theme directly in QtActivity without reflection
Currently the default theme is looked for using reflection in the
android.R package to get an id and then it's set. This is not needed
if we set the theme directly in the user's QtActivity which would have
access to that id directly pre-deployment.
To make setting a theme more in line with Android, retreat to using
setTheme() call directly for user projects instead of having to maintain
some internal field to store the theme name and have the user set that
if they want a different theme for their app.
Also, the Android plugin seems to set an env var for QT_ANDROID_THEME
with that default theme's name and that's used in QAndroidPlatformTheme
to check a theme specific extracted theme style.json, however, the Java
side doesn't seem to ever write to such a path, making this approach
totally redundant. For that reason, this code path is now removed.
Fixes: QTBUG-114075
Task-number: QTBUG-115017
Task-number: QTBUG-114593
Change-Id: I9ab3c025626aac2c09bc19eb7d846eca14a45070
Reviewed-by: Tinja Paavoseppä <tinja.paavoseppa@qt.io>
3 files changed, 11 insertions, 64 deletions
diff --git a/src/android/jar/src/org/qtproject/qt/android/QtActivityBase.java b/src/android/jar/src/org/qtproject/qt/android/QtActivityBase.java index 2e1a0228d3..b936bd4c95 100644 --- a/src/android/jar/src/org/qtproject/qt/android/QtActivityBase.java +++ b/src/android/jar/src/org/qtproject/qt/android/QtActivityBase.java @@ -19,8 +19,6 @@ import android.view.MotionEvent; import android.view.View; import android.view.Window; -import java.lang.reflect.Field; - public class QtActivityBase extends Activity { private boolean m_optionsMenuIsVisible = false; @@ -37,17 +35,7 @@ public class QtActivityBase extends Activity // Currently the following vars are used by the android plugin: // * QT_USE_ANDROID_NATIVE_DIALOGS - 1 to use the android native dialogs. public String ENVIRONMENT_VARIABLES = "QT_USE_ANDROID_NATIVE_DIALOGS=1"; - - // A list with all themes that your application want to use. - // The name of the theme must be the same with any theme from - // http://developer.android.com/reference/android/R.style.html - // The most used themes are: - // * "Theme_Light" - (default for API <=10) check http://developer.android.com/reference/android/R.style.html#Theme_Light - // * "Theme_Holo" - check http://developer.android.com/reference/android/R.style.html#Theme_Holo - // * "Theme_Holo_Light" - (default for API 11-13) check http://developer.android.com/reference/android/R.style.html#Theme_Holo_Light - public String[] QT_ANDROID_THEMES = null; - - public String QT_ANDROID_DEFAULT_THEME = null; // sets the default theme. + private boolean m_isCustomThemeSet = false; private QtActivityDelegate m_delegate; @@ -87,23 +75,10 @@ public class QtActivityBase extends Activity } } - void configureActivityTheme() { - if (QT_ANDROID_THEMES == null || QT_ANDROID_DEFAULT_THEME == null) { - if (Build.VERSION.SDK_INT < 29) { - QT_ANDROID_THEMES = new String[]{"Theme_Holo_Light"}; - QT_ANDROID_DEFAULT_THEME = "Theme_Holo_Light"; - } else { - QT_ANDROID_THEMES = new String[]{"Theme_DeviceDefault_DayNight"}; - QT_ANDROID_DEFAULT_THEME = "Theme_DeviceDefault_DayNight"; - } - } - try { - Field f = Class.forName("android.R$style").getDeclaredField(QT_ANDROID_DEFAULT_THEME); - int themeId = f.getInt(null); - setTheme(themeId); - } catch (Exception e) { - e.printStackTrace(); - } + @Override + public void setTheme(int resId) { + super.setTheme(resId); + m_isCustomThemeSet = true; } @Override @@ -112,16 +87,20 @@ public class QtActivityBase extends Activity super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_ACTION_BAR); + if (!m_isCustomThemeSet) { + setTheme(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q ? + android.R.style.Theme_DeviceDefault_DayNight : + android.R.style.Theme_Holo_Light); + } + m_delegate = new QtActivityDelegate(this); handleActivityRestart(); addReferrer(getIntent()); - configureActivityTheme(); QtActivityLoader loader = new QtActivityLoader(this); loader.setApplicationParameters(APPLICATION_PARAMETERS); loader.setEnvironmentVariables(ENVIRONMENT_VARIABLES); - loader.setEnvironmentVariable("QT_ANDROID_THEME", QT_ANDROID_DEFAULT_THEME); loader.loadQtLibraries(); m_delegate.startNativeApplication(loader.getApplicationParameters(), diff --git a/src/android/java/src/org/qtproject/qt/android/bindings/QtActivity.java b/src/android/java/src/org/qtproject/qt/android/bindings/QtActivity.java index 9fa8d3ffae..f31bd75fea 100644 --- a/src/android/java/src/org/qtproject/qt/android/bindings/QtActivity.java +++ b/src/android/java/src/org/qtproject/qt/android/bindings/QtActivity.java @@ -5,15 +5,6 @@ package org.qtproject.qt.android.bindings; import android.os.Bundle; -import android.provider.Browser; -import android.view.ContextMenu; -import android.view.ContextMenu.ContextMenuInfo; -import android.view.KeyEvent; -import android.view.Menu; -import android.view.MenuItem; -import android.view.MotionEvent; -import android.view.View; -import android.os.Build; import org.qtproject.qt.android.QtActivityBase; @@ -40,21 +31,5 @@ public class QtActivity extends QtActivityBase // Currently the following vars are used by the android plugin: // * QT_USE_ANDROID_NATIVE_DIALOGS - 1 to use the android native dialogs. ENVIRONMENT_VARIABLES = "QT_USE_ANDROID_NATIVE_DIALOGS=1"; - - // A list with all themes that your application want to use. - // The name of the theme must be the same with any theme from - // http://developer.android.com/reference/android/R.style.html - // The most used themes are: - // * "Theme_Light" - // * "Theme_Holo" - // * "Theme_Holo_Light" - - if (Build.VERSION.SDK_INT < 29) { - QT_ANDROID_THEMES = new String[] {"Theme_Holo_Light"}; - QT_ANDROID_DEFAULT_THEME = "Theme_Holo_Light"; - } else { - QT_ANDROID_THEMES = new String[] {"Theme_DeviceDefault_DayNight"}; - QT_ANDROID_DEFAULT_THEME = "Theme_DeviceDefault_DayNight"; - } } } diff --git a/src/plugins/platforms/android/qandroidplatformtheme.cpp b/src/plugins/platforms/android/qandroidplatformtheme.cpp index f863529057..016bfddc21 100644 --- a/src/plugins/platforms/android/qandroidplatformtheme.cpp +++ b/src/plugins/platforms/android/qandroidplatformtheme.cpp @@ -163,13 +163,6 @@ QJsonObject AndroidStyle::loadStyleData() Q_ASSERT(!stylePath.isEmpty()); - QString androidTheme = QLatin1StringView(qgetenv("QT_ANDROID_THEME")); - if (!androidTheme.isEmpty() && !androidTheme.endsWith(slashChar)) - androidTheme += slashChar; - - if (!androidTheme.isEmpty() && QFileInfo::exists(stylePath + androidTheme + "style.json"_L1)) - stylePath += androidTheme; - QFile f(stylePath + "style.json"_L1); if (!f.open(QIODevice::ReadOnly)) return QJsonObject(); |