forked from kivy/python-for-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
235 lines (190 loc) · 6.1 KB
/
Copy pathmain.py
File metadata and controls
235 lines (190 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
print('main.py was successfully called')
import os
print('imported os')
print('this dir is', os.path.abspath(os.curdir))
print('contents of this dir', os.listdir('./'))
import sys
print('pythonpath is', sys.path)
import kivy
print('imported kivy')
print('file is', kivy.__file__)
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.uix.popup import Popup
from kivy.clock import Clock
print('Imported kivy')
from kivy.utils import platform
print('platform is', platform)
import peewee
import requests
import sqlite3
try:
inclemnet = requests.get('http://inclem.net/')
print('got inclem.net request')
except:
inclemnet = 'failed inclemnet'
try:
kivy = requests.get('https://kivy.org/')
print('got kivy request (https)')
except:
kivy = 'failed kivy'
from peewee import *
db = SqliteDatabase('test.db')
class Person(Model):
name = CharField()
birthday = DateField()
is_relative = BooleanField()
class Meta:
database = db
def __repr__(self):
return '<Person: {}, {}>'.format(self.name, self.birthday)
def __str__(self):
return repr(self)
db.connect()
try:
db.create_tables([Person])
except:
import traceback
traceback.print_exc()
import random
from datetime import date
test_person = Person(name='person{}'.format(random.randint(0, 1000)),
birthday=date(random.randint(1900, 2000), random.randint(1, 9), random.randint(1, 20)),
is_relative=False)
test_person.save()
kv = '''
#:import Metrics kivy.metrics.Metrics
#:import sys sys
<FixedSizeButton@Button>:
size_hint_y: None
height: dp(60)
ScrollView:
GridLayout:
cols: 1
size_hint_y: None
height: self.minimum_height
FixedSizeButton:
text: 'test pyjnius'
on_press: app.test_pyjnius()
Label:
height: self.texture_size[1]
size_hint_y: None
text_size: self.size[0], None
markup: True
text: 'kivy request: {}\\ninclemnet request: {}'.format(app.kivy_request, app.inclemnet_request)
halign: 'center'
Label:
height: self.texture_size[1]
size_hint_y: None
text_size: self.size[0], None
markup: True
text: 'people: {}'.format(app.people)
halign: 'center'
Image:
keep_ratio: False
allow_stretch: True
source: 'colours.png'
size_hint_y: None
height: dp(100)
Label:
height: self.texture_size[1]
size_hint_y: None
font_size: 100
text_size: self.size[0], None
markup: True
text: '[b]Kivy[/b] on [b]SDL2[/b] on [b]Android[/b]!'
halign: 'center'
Label:
height: self.texture_size[1]
size_hint_y: None
text_size: self.size[0], None
markup: True
text: sys.version
halign: 'center'
padding_y: dp(10)
Widget:
size_hint_y: None
height: 20
Label:
height: self.texture_size[1]
size_hint_y: None
font_size: 50
text_size: self.size[0], None
markup: True
text: 'dpi: {}\\ndensity: {}\\nfontscale: {}'.format(Metrics.dpi, Metrics.density, Metrics.fontscale)
halign: 'center'
FixedSizeButton:
text: 'test ctypes'
on_press: app.test_ctypes()
FixedSizeButton:
text: 'test numpy'
on_press: app.test_numpy()
Widget:
size_hint_y: None
height: 1000
on_touch_down: print('touched at', args[-1].pos)
<ErrorPopup>:
title: 'Error'
size_hint: 0.75, 0.75
Label:
text: root.error_text
'''
class ErrorPopup(Popup):
error_text = StringProperty('')
def raise_error(error):
print('ERROR:', error)
ErrorPopup(error_text=error).open()
class TestApp(App):
kivy_request = kivy
inclemnet_request = inclemnet
people = ', '.join(map(str, list(Person.select())))
def build(self):
root = Builder.load_string(kv)
Clock.schedule_interval(self.print_something, 2)
# Clock.schedule_interval(self.test_pyjnius, 5)
print('testing metrics')
from kivy.metrics import Metrics
print('dpi is', Metrics.dpi)
print('density is', Metrics.density)
print('fontscale is', Metrics.fontscale)
return root
def print_something(self, *args):
print('App print tick', Clock.get_boottime())
def on_pause(self):
return True
def test_pyjnius(self, *args):
try:
from jnius import autoclass, cast
except ImportError:
raise_error('Could not import pyjnius')
return
print('Attempting to vibrate with pyjnius')
ANDROID_VERSION = autoclass('android.os.Build$VERSION')
SDK_INT = ANDROID_VERSION.SDK_INT
Context = autoclass("android.content.Context")
PythonActivity = autoclass('org.kivy.android.PythonActivity')
activity = PythonActivity.mActivity
vibrator_service = activity.getSystemService(Context.VIBRATOR_SERVICE)
vibrator = cast("android.os.Vibrator", vibrator_service)
if vibrator and SDK_INT >= 26:
print("Using android's `VibrationEffect` (SDK >= 26)")
VibrationEffect = autoclass("android.os.VibrationEffect")
vibrator.vibrate(
VibrationEffect.createOneShot(
1000, VibrationEffect.DEFAULT_AMPLITUDE,
),
)
elif vibrator:
print("Using deprecated android's vibrate (SDK < 26)")
vibrator.vibrate(1000)
else:
print('Something happened...vibrator service disabled?')
def test_ctypes(self, *args):
pass
def test_numpy(self, *args):
import numpy
print(numpy.zeros(5))
print(numpy.arange(5))
print(numpy.random.random((3, 3)))
TestApp().run()