Repository: kivy/kivy
Pull request: Added an allow_screensaver property for Window
Currently SDL2 automatically disables the screensaver, but has functions for enabling or disabling manually. Kivy doesn't do anything other than the default, so this affects all Kivy apps. It isn't very noticeable on the desktop, but probably isn't desirable as a default, and on mobile devices it prevents them from sleeping.

This PR adds a property to Window that exposes the SDL2 setting. As such, it will only work with the SDL2 provider, but since it's not the only one that doesn't seem too controversial. Other providers could potentially support it too.

I considered just disabling this behaviour on Android, but since it does affect desktop and iOS as well, and the SDL2 management of it is quite nice, it seemed better to resolve it with an option.

Resolves https://github.com/kivy/python-for-android/issues/969.

Problem statement:
device doesnt go on sleep mode
Hello I have successfully compiled and installed my kivy app on android 6 device , but the device doesnt  go on sleep mode even though I havent enabled wake lock

I am using the master branch of kivy , python for android and buildozer. Thank you

Hints from issue discussion:
Are you sure you haven't enabled it in `Developer settings` or in a similar OS settings? Don't you have it connected via USB or something? The basic app isn't able to make Android stay awake.
the same happens even without usb attached , besides I havent enabled 'Stay Awake' mode in developers settings (Off) 
Can you provide the APK and/or the code?
let me send apk , do I upload it here?

App Uploaded here : http://playslack.com/mediamate.apk
I have opensourced the core part (as planned ) docs is not very good , but that should at least help. https://github.com/razzbee/kivy-android-webview
any solution yet?
The apk seems corrupted for some reason, can't open it on both phone or pc. However, the code doesn't seem to have any issues with the lock itself. Have you tried it on some other device with higher/lower/same android version? Apparently if the apk is really corrupted, you'll need to paste here the `AndroidManifest.xml` file.

I still think it's something enabled on your device that forbids the phone to sleep.
I have reuploaded the apk , http://playslack.com/mediamate.apk , again the app crashes for android version less than 4.4 
Confirmed, yet I have no clue why. App doesn't use any related permission to wake lock (check manifest), I have no such option enabled and even the code seems to be correct. Webview bootstrap [too](https://github.com/kivy/python-for-android/blob/ef9e75bbdd6f626da01e9d153e967ebdc89b1482/pythonforandroid/bootstraps/webview/build/src/org/kivy/android/PythonActivity.java#L172).

Android 4.4.2

Commit message: Added allow_screensaver property for Window

Patch to review:
diff --git a/kivy/config.py b/kivy/config.py
index 0e916a382a..4f3e841cfc 100644
--- a/kivy/config.py
+++ b/kivy/config.py
@@ -318,7 +318,7 @@
 _is_rpi = exists('/opt/vc/include/bcm_host.h')
 
 # Version number of current configuration format
-KIVY_CONFIG_VERSION = 17
+KIVY_CONFIG_VERSION = 18
 
 Config = None
 '''The default Kivy configuration object. This is a :class:`ConfigParser`
@@ -819,6 +819,9 @@ def name(self, value):
                 'data/fonts/Roboto-Bold.ttf',
                 'data/fonts/Roboto-BoldItalic.ttf'])
 
+        elif version == 17:
+            Config.setdefault('graphics', 'allow_screensaver', '1')
+
         # elif version == 1:
         #    # add here the command for upgrading from configuration 0 to 1
 
diff --git a/kivy/core/window/__init__.py b/kivy/core/window/__init__.py
index 5f07ab9e2b..ba49de5f8b 100755
--- a/kivy/core/window/__init__.py
+++ b/kivy/core/window/__init__.py
@@ -215,6 +215,10 @@ class WindowBase(EventDispatcher):
             Minimum width of the window (only works for sdl2 window provider).
         `minimum_height`: int
             Minimum height of the window (only works for sdl2 window provider).
+        `allow_screensaver`: bool
+            Allow the device to show a screen saver, or to go to sleep
+            on mobile devices. Defaults to True. Only works for sdl2 window
+            provider.
 
     :Events:
         `on_motion`: etype, motionevent
@@ -392,6 +396,16 @@ def _set_size(self, size):
     defaults to 0.
     '''
 
+    allow_screensaver = BooleanProperty(True)
+    '''Whether the screen saver is enabled, or on mobile devices whether the
+    device is allowed to go to sleep while the app is open.
+
+    .. versionadded:: 1.10.0
+
+    :attr:`allow_screensaver` is a :class:`~kivy.properties.BooleanProperty`
+    and defaults to True.
+    '''
+
     size = AliasProperty(_get_size, _set_size, bind=('_size', ))
     '''Get the rotated size of the window. If :attr:`rotation` is set, then the
     size will change to reflect the rotation.
@@ -748,6 +762,9 @@ def __init__(self, **kwargs):
         if 'minimum_height' not in kwargs:
             kwargs['minimum_height'] = Config.getint('graphics',
                                                      'minimum_height')
+        if 'allow_screensaver' not in kwargs:
+            kwargs['allow_screensaver'] = Config.getint('graphics',
+                                                        'allow_screensaver')
         if 'rotation' not in kwargs:
             kwargs['rotation'] = Config.getint('graphics', 'rotation')
         if 'position' not in kwargs:
diff --git a/kivy/core/window/_window_sdl2.pyx b/kivy/core/window/_window_sdl2.pyx
index b3ddd31193..fa93d05cc1 100644
--- a/kivy/core/window/_window_sdl2.pyx
+++ b/kivy/core/window/_window_sdl2.pyx
@@ -190,6 +190,12 @@ cdef class _WindowSDL2Storage:
     def set_minimum_size(self, w, h):
         SDL_SetWindowMinimumSize(self.win, w, h)
 
+    def set_allow_screensaver(self, allow_screensaver):
+        if allow_screensaver:
+            SDL_EnableScreenSaver()
+        else:
+            SDL_DisableScreenSaver()
+
     def maximize_window(self):
         SDL_MaximizeWindow(self.win)
 
diff --git a/kivy/core/window/window_sdl2.py b/kivy/core/window/window_sdl2.py
index 1379ac9e51..fa26867fb6 100644
--- a/kivy/core/window/window_sdl2.py
+++ b/kivy/core/window/window_sdl2.py
@@ -185,6 +185,8 @@ def __init__(self, **kwargs):
         self.bind(minimum_width=self._set_minimum_size,
                   minimum_height=self._set_minimum_size)
 
+        self.bind(allow_screensaver=self._set_allow_screensaver)
+
     def _set_minimum_size(self, *args):
         minimum_width = self.minimum_width
         minimum_height = self.minimum_height
@@ -195,6 +197,10 @@ def _set_minimum_size(self, *args):
                 'Both Window.minimum_width and Window.minimum_height must be '
                 'bigger than 0 for the size restriction to take effect.')
 
+    def _set_allow_screensaver(self, *args):
+        allow_screensaver = self.allow_screensaver
+        self._win.set_allow_screensaver(allow_screensaver)
+
     def _event_filter(self, action):
         from kivy.app import App
         if action == 'app_terminating':
@@ -272,6 +278,7 @@ def create_window(self, *largs):
             # will be fired.
             self._pos = (0, 0)
             self._set_minimum_size()
+            self._set_allow_screensaver()
 
             if state == 'hidden':
                 self._focus = False


Code review comments:
diff_hunk: @@ -195,6 +197,10 @@ def _set_minimum_size(self, *args):
                 'Both Window.minimum_width and Window.minimum_height must be '
                 'bigger than 0 for the size restriction to take effect.')
 
+    def _set_allow_screensaver(self, *args):
+        allow_screensaver = self.allow_screensaver
+        self._win.set_allow_screensaver(allow_screensaver)
path: kivy/core/window/window_sdl2.py
text: @user1:
Could these lines be merged?