|
| 1 | +print('main.py was successfully called') |
| 2 | + |
| 3 | +import os |
| 4 | +print('imported os') |
| 5 | + |
| 6 | +print('contents of ./lib/python2.7/site-packages/ etc.') |
| 7 | +print(os.listdir('./lib')) |
| 8 | +print(os.listdir('./lib/python2.7')) |
| 9 | +print(os.listdir('./lib/python2.7/site-packages')) |
| 10 | + |
| 11 | +print('this dir is', os.path.abspath(os.curdir)) |
| 12 | + |
| 13 | +print('contents of this dir', os.listdir('./')) |
| 14 | + |
| 15 | +with open('./lib/python2.7/site-packages/kivy/app.pyo', 'rb') as fileh: |
| 16 | + print('app.pyo size is', len(fileh.read())) |
| 17 | + |
| 18 | +import sys |
| 19 | +print('pythonpath is', sys.path) |
| 20 | + |
| 21 | +import kivy |
| 22 | +print('imported kivy') |
| 23 | +print('file is', kivy.__file__) |
| 24 | + |
| 25 | +from kivy.app import App |
| 26 | + |
| 27 | +from kivy.lang import Builder |
| 28 | +from kivy.properties import StringProperty |
| 29 | + |
| 30 | +from kivy.uix.popup import Popup |
| 31 | +from kivy.clock import Clock |
| 32 | + |
| 33 | +print('Imported kivy') |
| 34 | +from kivy.utils import platform |
| 35 | +print('platform is', platform) |
| 36 | + |
| 37 | + |
| 38 | +########### |
| 39 | +from kivy import platform |
| 40 | +if platform == 'android': |
| 41 | + from jnius import PythonJavaClass, java_method, autoclass |
| 42 | + |
| 43 | + _PythonActivity = autoclass('org.kivy.android.PythonActivity') |
| 44 | + |
| 45 | + |
| 46 | + class Runnable(PythonJavaClass): |
| 47 | + '''Wrapper around Java Runnable class. This class can be used to schedule a |
| 48 | + call of a Python function into the PythonActivity thread. |
| 49 | + ''' |
| 50 | + |
| 51 | + __javainterfaces__ = ['java/lang/Runnable'] |
| 52 | + __runnables__ = [] |
| 53 | + |
| 54 | + def __init__(self, func): |
| 55 | + super(Runnable, self).__init__() |
| 56 | + self.func = func |
| 57 | + |
| 58 | + def __call__(self, *args, **kwargs): |
| 59 | + self.args = args |
| 60 | + self.kwargs = kwargs |
| 61 | + Runnable.__runnables__.append(self) |
| 62 | + _PythonActivity.mActivity.runOnUiThread(self) |
| 63 | + |
| 64 | + @java_method('()V') |
| 65 | + def run(self): |
| 66 | + try: |
| 67 | + self.func(*self.args, **self.kwargs) |
| 68 | + except: |
| 69 | + import traceback |
| 70 | + traceback.print_exc() |
| 71 | + |
| 72 | + Runnable.__runnables__.remove(self) |
| 73 | + |
| 74 | + def run_on_ui_thread(f): |
| 75 | + '''Decorator to create automatically a :class:`Runnable` object with the |
| 76 | + function. The function will be delayed and call into the Activity thread. |
| 77 | + ''' |
| 78 | + def f2(*args, **kwargs): |
| 79 | + Runnable(f)(*args, **kwargs) |
| 80 | + return f2 |
| 81 | + |
| 82 | +else: |
| 83 | + def run_on_ui_thread(f): |
| 84 | + return f |
| 85 | +############# |
| 86 | + |
| 87 | + |
| 88 | +kv = ''' |
| 89 | +#:import Metrics kivy.metrics.Metrics |
| 90 | +
|
| 91 | +<FixedSizeButton@Button>: |
| 92 | + size_hint_y: None |
| 93 | + height: dp(60) |
| 94 | +
|
| 95 | +
|
| 96 | +BoxLayout: |
| 97 | + orientation: 'vertical' |
| 98 | + ScrollView: |
| 99 | + GridLayout: |
| 100 | + cols: 1 |
| 101 | + size_hint_y: None |
| 102 | + height: self.minimum_height |
| 103 | + FixedSizeButton: |
| 104 | + text: 'test pyjnius' |
| 105 | + on_press: app.test_pyjnius() |
| 106 | + Image: |
| 107 | + keep_ratio: False |
| 108 | + allow_stretch: True |
| 109 | + source: 'colours.png' |
| 110 | + size_hint_y: None |
| 111 | + height: dp(100) |
| 112 | + Label: |
| 113 | + height: self.texture_size[1] |
| 114 | + size_hint_y: None |
| 115 | + font_size: 100 |
| 116 | + text_size: self.size[0], None |
| 117 | + markup: True |
| 118 | + text: '[b]Kivy[/b] on [b]SDL2[/b] on [b]Android[/b]!' |
| 119 | + halign: 'center' |
| 120 | + Widget: |
| 121 | + size_hint_y: None |
| 122 | + height: 20 |
| 123 | + Label: |
| 124 | + height: self.texture_size[1] |
| 125 | + size_hint_y: None |
| 126 | + font_size: 50 |
| 127 | + text_size: self.size[0], None |
| 128 | + markup: True |
| 129 | + text: 'dpi: {}\\ndensity: {}\\nfontscale: {}'.format(Metrics.dpi, Metrics.density, Metrics.fontscale) |
| 130 | + halign: 'center' |
| 131 | + Widget: |
| 132 | + size_hint_y: None |
| 133 | + height: 1000 |
| 134 | + on_touch_down: print 'touched at', args[-1].pos |
| 135 | + TextInput: |
| 136 | + size_hint_y: None |
| 137 | + height: dp(100) |
| 138 | + text: 'textinput!' |
| 139 | +
|
| 140 | +<ErrorPopup>: |
| 141 | + title: 'Error' |
| 142 | + size_hint: 0.75, 0.75 |
| 143 | + Label: |
| 144 | + text: root.error_text |
| 145 | +''' |
| 146 | + |
| 147 | + |
| 148 | +class ErrorPopup(Popup): |
| 149 | + error_text = StringProperty('') |
| 150 | + |
| 151 | +def raise_error(error): |
| 152 | + print('ERROR:', error) |
| 153 | + ErrorPopup(error_text=error).open() |
| 154 | + |
| 155 | +class TestApp(App): |
| 156 | + def build(self): |
| 157 | + root = Builder.load_string(kv) |
| 158 | + Clock.schedule_interval(self.print_something, 2) |
| 159 | + # Clock.schedule_interval(self.test_pyjnius, 5) |
| 160 | + print('testing metrics') |
| 161 | + from kivy.metrics import Metrics |
| 162 | + print('dpi is', Metrics.dpi) |
| 163 | + print('density is', Metrics.density) |
| 164 | + print('fontscale is', Metrics.fontscale) |
| 165 | + Clock.schedule_once(self.android_init, 0) |
| 166 | + return root |
| 167 | + |
| 168 | + def android_init(self, *args): |
| 169 | + self.set_softinput_mode() |
| 170 | + |
| 171 | + def print_something(self, *args): |
| 172 | + print('App print tick', Clock.get_boottime()) |
| 173 | + |
| 174 | + def on_pause(self): |
| 175 | + return True |
| 176 | + |
| 177 | + def test_pyjnius(self, *args): |
| 178 | + try: |
| 179 | + from jnius import autoclass |
| 180 | + except ImportError: |
| 181 | + raise_error('Could not import pyjnius') |
| 182 | + return |
| 183 | + |
| 184 | + print('Attempting to vibrate with pyjnius') |
| 185 | + # PythonActivity = autoclass('org.renpy.android.PythonActivity') |
| 186 | + # activity = PythonActivity.mActivity |
| 187 | + PythonActivity = autoclass('org.kivy.android.PythonActivity') |
| 188 | + activity = PythonActivity.mActivity |
| 189 | + Intent = autoclass('android.content.Intent') |
| 190 | + Context = autoclass('android.content.Context') |
| 191 | + vibrator = activity.getSystemService(Context.VIBRATOR_SERVICE) |
| 192 | + |
| 193 | + vibrator.vibrate(1000) |
| 194 | + |
| 195 | + def test_ctypes(self, *args): |
| 196 | + import ctypes |
| 197 | + |
| 198 | + def test_numpy(self, *args): |
| 199 | + import numpy |
| 200 | + |
| 201 | + print(numpy.zeros(5)) |
| 202 | + print(numpy.arange(5)) |
| 203 | + print(numpy.random.random((3, 3))) |
| 204 | + |
| 205 | + @run_on_ui_thread |
| 206 | + def set_softinput_mode(self): |
| 207 | + return |
| 208 | + from jnius import autoclass |
| 209 | + PythonActivity = autoclass('org.kivy.android.PythonActivity') |
| 210 | + WindowManager = autoclass('android.view.WindowManager') |
| 211 | + LayoutParams = autoclass('android.view.WindowManager$LayoutParams') |
| 212 | + activity = PythonActivity.mActivity |
| 213 | + |
| 214 | + activity.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_ADJUST_PAN) |
| 215 | + |
| 216 | + |
| 217 | +TestApp().run() |
0 commit comments