Skip to content

Commit 48e77a0

Browse files
committed
android: Another race condition with keep awake
Keep awake is sometimes turned off after a few seconds. Check this every second for a couple of seconds and turn it on again if necessary. The problem was observed both in the example app and a separate app using proprietary Unity content, and this patch fixes it in both apps.
1 parent 53e118e commit 48e77a0

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

android/src/main/java/no/fuse/rnunity/RNUnityManager.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,32 @@ public void run() {
9494
}
9595
});
9696

97+
// Race condition: Keep awake is sometimes turned off after a few
98+
// seconds. Check this every second for a couple of seconds and turn it
99+
// on again if necessary.
100+
for (int i = 2; i < 9; i++) {
101+
final int delay = i * 1000;
102+
handler.postDelayed(new Runnable() {
103+
@Override
104+
public void run() {
105+
activity.runOnUiThread(new Runnable() {
106+
@Override
107+
public void run() {
108+
final Window window = activity.getWindow();
109+
final int windowFlags = window.getAttributes().flags;
110+
final boolean existing = (windowFlags & WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) == WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
111+
final boolean keepAwake = module.getKeepAwake();
112+
113+
if (existing != keepAwake) {
114+
Log.d("RNUnityManager", "Keep awake flag out of sync; delay=" + delay);
115+
module.setKeepAwake(keepAwake);
116+
}
117+
}
118+
});
119+
}
120+
}, delay);
121+
}
122+
97123
player.addOnAttachStateChangeListener(this);
98124
player.windowFocusChanged(true);
99125
player.requestFocus();

0 commit comments

Comments
 (0)