0

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..

Aderoju Israel
  • 154
  • 1
  • 11
R.Phiz
  • 17
  • 1
  • 5
  • 1
    Don't know how to do what you want, but the `buildozer.spec` file is used during the creation of the `.apk`, and is not included in the `.apk`. And even if it was included, changing it after the `.apk` was created would have no effect. – John Anderson Jul 05 '20 at 00:52
  • @JohnAnderson thanks for your clarification, i put so much time trying to get that to work.. But is there no way i can enable/disable full screen and wake lock using button in the main.py file? – R.Phiz Jul 05 '20 at 10:12
  • You almost certainly _can_ do that, but trying to edit the buildozer.spec is pointless as this file has nothing to do with behaviour during runtime. Find the functions that do what you want in the android api, and call them using pyjnius. – inclement Jul 05 '20 at 11:19

1 Answers1

2

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()
VectorVictor
  • 820
  • 8
  • 17
  • Thank you!!, But how do i achieve this on ios? – R.Phiz Jul 05 '20 at 17:12
  • I do not know. This class is specifically accessing Android commands. Maybe someone with Kivy/iOS can supply an iOS solution, from this post here it looks like you will use different code on iOS - https://stackoverflow.com/questions/28329185/how-to-prevent-screen-lock-on-my-application-with-swift-on-ios – VectorVictor Jul 05 '20 at 18:20