Skip to content
This repository was archived by the owner on Mar 4, 2023. It is now read-only.

Commit 56fb054

Browse files
committed
more
1 parent 8684503 commit 56fb054

File tree

3 files changed

+35
-53
lines changed

3 files changed

+35
-53
lines changed

android/src/de/skycoder42/androidutils/AndroidUtils.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package de.skycoder42.androidutils;
22

3-
import androidnative.SystemDispatcher;
3+
import java.util.Map;
4+
import android.os.Build;
45
import android.os.Handler;
6+
import android.content.Context;
57
import android.app.Activity;
8+
import android.graphics.Color;
69
import android.view.View;
7-
import android.content.Context;
8-
import java.util.Map;
10+
import android.view.Window;
11+
import android.view.WindowManager;
912
import org.qtproject.qt5.android.QtNative;
13+
import androidnative.SystemDispatcher;
1014

1115
public class AndroidUtils {
1216

@@ -17,6 +21,8 @@ public void onDispatched(String name , Map data) {
1721

1822
if (name.equals("AndroidUtils.hapticFeedback"))
1923
AndroidUtils.hapticFeedback((Integer)data.get("feedbackConstant"));
24+
if (name.equals("AndroidUtils.setStatusBarColor"))
25+
AndroidUtils.setStatusBarColor((String)data.get("color"));
2026
}
2127
});
2228
}
@@ -32,4 +38,21 @@ public void run() {
3238
};
3339
activity.runOnUiThread(runnable);
3440
}
41+
42+
static void setStatusBarColor(String colorName) {
43+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
44+
final int color = Color.parseColor(colorName);
45+
final Activity activity = QtNative.activity();
46+
47+
Runnable runnable = new Runnable () {
48+
public void run() {
49+
Window window = activity.getWindow();
50+
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
51+
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
52+
window.setStatusBarColor(color);
53+
};
54+
};
55+
activity.runOnUiThread(runnable);
56+
}
57+
}
3558
}

androidutils.cpp

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,9 @@ AndroidUtils *AndroidUtils::instance()
4242
void AndroidUtils::setStatusBarColor(const QColor &color)
4343
{
4444
#ifdef Q_OS_ANDROID
45-
if(QtAndroid::androidSdkVersion() >= 21) {
46-
QtAndroid::runOnAndroidThreadSync([=](){
47-
auto activity = QtAndroid::androidActivity();
48-
if(activity.isValid()) {
49-
const auto FLAG_TRANSLUCENT_STATUS = QAndroidJniObject::getStaticField<jint>("android/view/WindowManager$LayoutParams",
50-
"FLAG_TRANSLUCENT_STATUS");
51-
const auto FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS = QAndroidJniObject::getStaticField<jint>("android/view/WindowManager$LayoutParams",
52-
"FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS");
53-
const auto aColor = QAndroidJniObject::callStaticMethod<jint>("android/graphics/Color",
54-
"parseColor",
55-
"(Ljava/lang/String;)I",
56-
QAndroidJniObject::fromString(color.name()).object());
57-
58-
QAndroidJniObject window = activity.callObjectMethod("getWindow", "()Landroid/view/Window;");
59-
if(window.isValid()) {
60-
window.callMethod<void>("clearFlags", "(I)V", FLAG_TRANSLUCENT_STATUS);
61-
window.callMethod<void>("addFlags", "(I)V", FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
62-
window.callMethod<void>("setStatusBarColor", "(I)V", aColor);
63-
}
64-
}
65-
});
66-
}
45+
AndroidNative::SystemDispatcher::instance()->dispatch("AndroidUtils.setStatusBarColor", {
46+
{"color", color.name()}
47+
});
6748
#else
6849
Q_UNUSED(color);
6950
#endif
@@ -85,30 +66,8 @@ void AndroidUtils::showToast(const QString &message, bool showLong)
8566
void AndroidUtils::hapticFeedback(HapticFeedbackConstant constant)
8667
{
8768
#ifdef Q_OS_ANDROID
88-
jint type = 0;
89-
switch (constant) {
90-
case AndroidUtils::LongPress:
91-
type = QAndroidJniObject::getStaticField<jint>("android/view/HapticFeedbackConstants", "LONG_PRESS");
92-
break;
93-
case AndroidUtils::VirtualKey:
94-
type = QAndroidJniObject::getStaticField<jint>("android/view/HapticFeedbackConstants", "VIRTUAL_KEY");
95-
break;
96-
case AndroidUtils::KeyboardTap:
97-
type = QAndroidJniObject::getStaticField<jint>("android/view/HapticFeedbackConstants", "KEYBOARD_TAP");
98-
break;
99-
case AndroidUtils::ClockTick:
100-
type = QAndroidJniObject::getStaticField<jint>("android/view/HapticFeedbackConstants", "CLOCK_TICK");
101-
break;
102-
case AndroidUtils::ContextClick:
103-
type = QAndroidJniObject::getStaticField<jint>("android/view/HapticFeedbackConstants", "CONTEXT_CLICK");
104-
break;
105-
default:
106-
Q_UNREACHABLE();
107-
return;
108-
}
109-
11069
AndroidNative::SystemDispatcher::instance()->dispatch("AndroidUtils.hapticFeedback", {
111-
{"feedbackConstant", (int)type}
70+
{"feedbackConstant", (int)constant}
11271
});
11372
#else
11473
Q_UNUSED(constant);

androidutils.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ class AndroidUtils : public QObject
1111

1212
public:
1313
enum HapticFeedbackConstant {
14-
LongPress,
15-
VirtualKey,
16-
KeyboardTap,
17-
ClockTick,
18-
ContextClick
14+
LongPress = 0,
15+
VirtualKey = 1,
16+
KeyboardTap = 3,
17+
ClockTick = 4,
18+
ContextClick = 6
1919
};
2020
Q_ENUM(HapticFeedbackConstant)
2121

0 commit comments

Comments
 (0)