diff options
Diffstat (limited to 'src/android/jar/src/org/qtproject/qt/android/ExtractStyle.java')
-rw-r--r-- | src/android/jar/src/org/qtproject/qt/android/ExtractStyle.java | 74 |
1 files changed, 69 insertions, 5 deletions
diff --git a/src/android/jar/src/org/qtproject/qt/android/ExtractStyle.java b/src/android/jar/src/org/qtproject/qt/android/ExtractStyle.java index 6792247eeb..6780634317 100644 --- a/src/android/jar/src/org/qtproject/qt/android/ExtractStyle.java +++ b/src/android/jar/src/org/qtproject/qt/android/ExtractStyle.java @@ -5,8 +5,10 @@ package org.qtproject.qt.android; import android.annotation.SuppressLint; +import android.app.Activity; import android.content.Context; import android.content.res.ColorStateList; +import android.content.res.Configuration; import android.content.res.Resources; import android.content.res.TypedArray; import android.content.res.XmlResourceParser; @@ -59,7 +61,7 @@ import java.util.Map; import java.util.Objects; -public class ExtractStyle { +class ExtractStyle { // This used to be retrieved from android.R.styleable.ViewDrawableStates field via reflection, // but since the access to that is restricted, we need to have hard-coded here. @@ -135,6 +137,72 @@ public class ExtractStyle { Context m_context; private final HashMap<String, DrawableCache> m_drawableCache = new HashMap<>(); + private static boolean m_missingNormalStyle = false; + private static boolean m_missingDarkStyle = false; + private static String m_stylePath = null; + private static boolean m_extractMinimal = false; + + private static final String QtTAG = "QtExtractStyle"; + + private static boolean isUiModeDark(Configuration config) + { + return (config.uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES; + } + + public static String setup(Context context, String extractOption, int dpi) { + + String dataDir = context.getApplicationInfo().dataDir; + m_stylePath = dataDir + "/qt-reserved-files/android-style/" + dpi + "/"; + + if (extractOption.equals("none")) + return m_stylePath; + + if (extractOption.isEmpty()) + extractOption = "minimal"; + + if (!extractOption.equals("default") && !extractOption.equals("full") + && !extractOption.equals("minimal") && !extractOption.equals("none")) { + Log.e(QtTAG, "Invalid extract_android_style option \"" + extractOption + + "\", defaulting to \"minimal\""); + extractOption = "minimal"; + } + + // QTBUG-69810: The extraction code will trigger compatibility warnings on Android + // SDK version >= 28 when the target SDK version is set to something lower then 28, + // so default to "none" and issue a warning if that is the case. + if (extractOption.equals("default")) { + int targetSdk = context.getApplicationInfo().targetSdkVersion; + if (targetSdk < 28 && Build.VERSION.SDK_INT >= 28) { + Log.e(QtTAG, "extract_android_style option set to \"none\" when " + + "targetSdkVersion is less then 28"); + extractOption = "none"; + } + } + + boolean darkModeFileMissing = !(new File(m_stylePath + "darkUiMode/style.json").exists()); + m_missingDarkStyle = Build.VERSION.SDK_INT > 28 && darkModeFileMissing; + m_missingNormalStyle = !(new File(m_stylePath + "style.json").exists()); + m_extractMinimal = extractOption.equals("minimal"); + + ExtractStyle.runIfNeeded(context, isUiModeDark(context.getResources().getConfiguration())); + + return m_stylePath; + } + + public static void runIfNeeded(Context context, boolean extractDarkMode) { + if (m_stylePath == null) + return; + if (extractDarkMode) { + if (m_missingDarkStyle) { + new ExtractStyle(context, m_stylePath + "darkUiMode/", m_extractMinimal); + m_missingDarkStyle = false; + } + } else if (m_missingNormalStyle) { + new ExtractStyle(context, m_stylePath, m_extractMinimal); + m_missingNormalStyle = false; + } + } + public ExtractStyle(Context context, String extractPath, boolean minimal) { m_minimal = minimal; m_extractPath = extractPath + "/"; @@ -1067,10 +1135,6 @@ public class ExtractStyle { return json; } - public JSONObject extractTextAppearanceInformation(int styleName, String qtClass, AttributeSet attributeSet) { - return extractTextAppearanceInformation(styleName, qtClass, android.R.attr.textAppearance, attributeSet); - } - public JSONObject extractTextAppearanceInformation(int styleName, String qtClass) { return extractTextAppearanceInformation(styleName, qtClass, android.R.attr.textAppearance, null); } |