diff --git a/examples/Demo/Builds/Android/src/com/juce/jucedemo/JuceDemo.java b/examples/Demo/Builds/Android/src/com/juce/jucedemo/JuceDemo.java index 751d5603d1..801b600573 100644 --- a/examples/Demo/Builds/Android/src/com/juce/jucedemo/JuceDemo.java +++ b/examples/Demo/Builds/Android/src/com/juce/jucedemo/JuceDemo.java @@ -143,6 +143,7 @@ public class JuceDemo extends Activity //============================================================================== private ViewHolder viewHolder; private boolean isScreenSaverEnabled; + private java.util.Timer keepAliveTimer; public final ComponentPeerView createNewView (boolean opaque, long host) { @@ -230,14 +231,39 @@ public class JuceDemo extends Activity if (isScreenSaverEnabled != enabled) { isScreenSaverEnabled = enabled; + + if (keepAliveTimer != null) + { + keepAliveTimer->cancel(); + keepAliveTimer = null; + } + if (enabled) + { getWindow().clearFlags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + } else + { getWindow().addFlags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + + // If no user input is received after about 3 seconds, the OS will lower the + // task's priority, so this timer forces it to be kept active. + keepAliveTimer = new java.util.Timer(); + + keepAliveTimer.scheduleAtFixedRate (new TimerTask() + { + @Override + public void run() + { + android.app.Instrumentation instrumentation = new android.app.Instrumentation(); + instrumentation.sendKeyDownUpSync (KeyEvent.KEYCODE_BREAK); + } + }, 2000, 2000); + } } } - public final boolean getScreenSaver () + public final boolean getScreenSaver() { return isScreenSaverEnabled; } diff --git a/modules/juce_core/native/java/JuceAppActivity.java b/modules/juce_core/native/java/JuceAppActivity.java index ea5f18577b..bf805bf9a0 100644 --- a/modules/juce_core/native/java/JuceAppActivity.java +++ b/modules/juce_core/native/java/JuceAppActivity.java @@ -43,6 +43,13 @@ import android.text.ClipboardManager; import android.text.InputType; import android.util.DisplayMetrics; import android.util.Log; +import java.lang.Runnable; +import java.util.List; +import java.util.Arrays; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Hashtable; +import java.util.TimerTask; import java.io.*; import java.net.URL; import java.net.HttpURLConnection; @@ -143,6 +150,7 @@ public class JuceAppActivity extends Activity //============================================================================== private ViewHolder viewHolder; private boolean isScreenSaverEnabled; + private java.util.Timer keepAliveTimer; public final ComponentPeerView createNewView (boolean opaque, long host) { @@ -230,14 +238,46 @@ public class JuceAppActivity extends Activity if (isScreenSaverEnabled != enabled) { isScreenSaverEnabled = enabled; + + if (keepAliveTimer != null) + { + keepAliveTimer.cancel(); + keepAliveTimer = null; + } + if (enabled) + { getWindow().clearFlags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + } else + { getWindow().addFlags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + + // If no user input is received after about 3 seconds, the OS will lower the + // task's priority, so this timer forces it to be kept active. + keepAliveTimer = new java.util.Timer(); + + keepAliveTimer.scheduleAtFixedRate (new TimerTask() + { + @Override + public void run() + { + android.app.Instrumentation instrumentation = new android.app.Instrumentation(); + + try + { + instrumentation.sendKeyDownUpSync (KeyEvent.KEYCODE_BREAK); + } + catch (Exception e) + { + } + } + }, 2000, 2000); + } } } - public final boolean getScreenSaver () + public final boolean getScreenSaver() { return isScreenSaverEnabled; }