Skip to content

Commit

Permalink
android version:
Browse files Browse the repository at this point in the history
- screen rotation lock added as proposed by issue #320
- triggered (maybe experimental) by a long tap (5 seconds) to the play ground.
  • Loading branch information
lufebe16 committed Sep 16, 2023
1 parent 27e11a6 commit e7a1979
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pysollib/kivy/LApp.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@
from kivy.uix.treeview import TreeViewLabel
from kivy.uix.widget import Widget
from kivy.utils import platform
from kivy.properties import NumericProperty

from pysollib.kivy.androidperms import requestStoragePerm
from pysollib.kivy.androidrot import AndroidScreenRotation
from pysollib.resource import CSI

if platform != 'android':
Expand Down Expand Up @@ -1583,6 +1585,9 @@ def size(self):


class LMainWindow(BoxLayout, LTkBase):

longPress = NumericProperty(0)

def __init__(self, **kw):
super(LMainWindow, self).__init__(orientation='vertical')
LTkBase.__init__(self)
Expand Down Expand Up @@ -1680,6 +1685,14 @@ def on_touch_down(self, touch):

def on_touch_up(self, touch):
ret = False
# long press only on playground.
pgs = self.workStack.peek('playground')
if pgs:
pg = pgs[1]
if pg.collide_point(*touch.pos):
if (touch.time_end-touch.time_start) > 4.5:
self.longPress = touch.time_end

# standard notifikation:
for c in self.children:
ret = c.on_touch_up(touch)
Expand All @@ -1693,6 +1706,10 @@ def on_touch_up(self, touch):
'''
return ret

def on_longPress(self, instance, timestamp):
print('longPressed at {time}'.format(time=timestamp))
AndroidScreenRotation.toggle()

# Menubar:

def setMenu(self, menu):
Expand Down Expand Up @@ -1914,6 +1931,8 @@ def on_resume(self):
if app is None:
return

AndroidScreenRotation.unlock()

so = get_screen_ori()
go = so # flake8: F841 nonsense!
so = go
Expand Down
46 changes: 46 additions & 0 deletions pysollib/kivy/androidrot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# flake8: noqa
# LB230914.

from pysollib.kivy.androidtoast import AndroidToaster
from pysollib.mygettext import _
try:
import jnius
except ImportError:
jnius = None

class AndroidRot(object):
def __init__(self):
self.locked = False
if jnius is None:
return
self.PythonActivity = jnius.autoclass('org.kivy.android.PythonActivity')
self.ActivityInfo = jnius.autoclass('android.content.pm.ActivityInfo')
self.currentActivity = jnius.cast('android.app.Activity', self.PythonActivity.mActivity)

def isLocked(self):
return self.locked

def lock(self):
if jnius is not None:
self.currentActivity.setRequestedOrientation(
self.ActivityInfo.SCREEN_ORIENTATION_LOCKED)
self.locked = True

def unlock(self):
if jnius is not None:
self.currentActivity.setRequestedOrientation(
self.ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR)
self.locked = False

def toggle(self):
if self.locked:
self.unlock()
if jnius is not None:
AndroidToaster.toastShort(_("screen rotation enabled"))
else:
self.lock()
if jnius is not None:
AndroidToaster.toastShort(_("screen rotation disabled"))
return self.isLocked()

AndroidScreenRotation = AndroidRot()
37 changes: 37 additions & 0 deletions pysollib/kivy/androidtoast.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# flake8: noqa
# LB230914.

try:
import jnius
from android.runnable import run_on_ui_thread
except ImportError:
jnius = None
def run_on_ui_thread(a):
pass

class AndroidToast(object):
def __init__(self):
if jnius is None:
return
self.PythonActivity = jnius.autoclass('org.kivy.android.PythonActivity')
self.Toast = jnius.autoclass('android.widget.Toast')
self.String = jnius.autoclass('java.lang.String')
self.context = self.PythonActivity.mActivity

@run_on_ui_thread
def toastShort(self, message):
if jnius is not None:
jtext = jnius.cast('java.lang.CharSequence', self.String(message))
toast = self.Toast.makeText(
self.context, jtext, self.Toast.LENGTH_SHORT)
toast.show()

@run_on_ui_thread
def toastLong(self, message):
if jnius is not None:
jtext = jnius.cast('java.lang.CharSequence', self.String(message))
toast = self.Toast.makeText(
self.context, jtext, self.Toast.LENGTH_LONG)
toast.show()

AndroidToaster = AndroidToast()

0 comments on commit e7a1979

Please sign in to comment.