Search code examples
pythonandroidkivybuildozer

Enable/Disable Fullscreen and Wakelock with a Button in Python Kivy?


I created a simple andriod app, and i want the user to be able to enable or disable the app full screen and wake lock when a button is press.. I know that i can easily enable/disable this in buildozer.spec file, but is there a way i can modify this in the main.py file when the user press a button? I tried editing the specific lines in the buildozer.spec file from main.py file using

    if self.FullScreen:
            # Check if FullScreen is False.
            UpdateSpecFile = open("buildozer.spec", "r")
            SpecFileLines = UpdateSpecFile.readlines()
            SpecFileLines[77] = "fullscreen = 0\n"
            UpdateSpecFile = open("buildozer.spec", "w")
            UpdateSpecFile.writelines(SpecFileLines)
            UpdateSpecFile.close()
        else:
            UpdateSpecFile = open("buildozer.spec", "r")
            SpecFileLines = UpdateSpecFile.readlines()
            SpecFileLines[77] = "fullscreen = 1\n"
            UpdateSpecFile = open("buildozer.spec", "w")
            UpdateSpecFile.writelines(SpecFileLines)
            UpdateSpecFile.close()
        # I used same function for the Wakelock

But am getting error " No such file or directory: 'buildozer.spec ".

NB: My buildozer.spec is relative to the main.py directory.

Please i need help, thanks..


Solution

  • As inclement says, the buildozer spec will not change anything during runtime.

    I have created this class to make changes during runtime. On class initialisation, it will acquire the default wakelock. Use the acquire method to swap states, or release method and end of program or on pause.

    from jnius import autoclass
    
    
    class WakeLock(object):
        """Object to create a wakelock, wake_type options are FULL, BRIGHT, DIM, PARTIAL
    
           Constructor will default to Bright and acquire().  Use set state to switch between options,
           there is no need to release or acquire when using this.  Set state to None to release"""
    
        state_dict = {'FULL': 'FULL_WAKE_LOCK',
                      'BRIGHT': 'SCREEN_BRIGHT_WAKE_LOCK',
                      'DIM': 'SCREEN_DIM_WAKE_LOCK',
                      'PARTIAL': 'PARTIAL_WAKE_LOCK'}
    
        def __init__(self, activity, wake_type='BRIGHT', acquire=True):
            self.tag = 'ArbitraryTag'
            Context = autoclass('android.content.Context')
            self.PowerManager = autoclass('android.os.PowerManager')
            self.ps = activity.getSystemService(Context.POWER_SERVICE)
            self.wl = None
            self._set_type(wake_type)
            if acquire:
                self.acquire(wake_type)
    
        def acquire(self, wake_type=None):
            self.release()
            if wake_type:
                self._set_type(wake_type)
            self.wl.acquire()
    
        def release(self):
            if self.wl.isHeld():
                self.wl.release()
    
        def _set_type(self, wake_type):
            wake_lock = getattr(self.PowerManager, self.state_dict[wake_type])
            self.wl = self.ps.newWakeLock(wake_lock, self.tag)
    

    I believe in order to get these functions to work though, you will also need the following functions:

    from android.runnable import run_on_ui_thread
    from wakelock import Wakelock  # our wakelock class we created
    
    @run_on_ui_thread
    def android_setflag():
        PythonActivity.mActivity.getWindow().addFlags(Params.FLAG_KEEP_SCREEN_ON)
    
    @run_on_ui_thread
    def android_clearflag():
        PythonActivity.mActivity.getWindow().clearFlags(Params.FLAG_KEEP_SCREEN_ON)
    

    I create the class in main.build() as:

    def build(self):
        # bunch of code
        self.wakelock = WakeLock(activity, level)  # FULL, PARTIAL, BRIGHT, DIM
        android_setflag()