This repository was archived by the owner on Oct 24, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
197 lines (168 loc) · 6.64 KB
/
Copy pathmain.py
File metadata and controls
197 lines (168 loc) · 6.64 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
import sys
import threading
sys.path.append("./src")
# embit ships as a git submodule under vendor/; its package lives in the
# repo's src/ dir, so expose that path for `import embit`.
sys.path.append("./vendor/embit/src")
from kivy.app import App
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.core.text import LabelBase
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
from kivy.utils import platform
from kivy.clock import Clock
from kivy.clock import mainthread
from android_permissions import AndroidPermissions
import mocks.load_mocks
from mocks.ft6x36 import touch_control
from src.krux.power import power_manager
from src.krux.buttons import buttons_control
from src.krux.context import Context
from src.krux.pages.login import Login
from src.krux.pages.home_pages.home import Home
Builder.load_string("""
<RootWidget>:
label_1: label_1
but_1: but_1
Label:
id: label_1
font_size: root.height // 32
font_name: 'NotoSansCJK_CY_JP_SC_KR_VI_Krux.ttf'
size_hint: 1, 0.8
pos_hint: {'top': 1}
text_size: self.width, None
height: self.texture_size[1]
text: 'Krux Android app is intended for learning about Krux and Bitcoin air-gapped transactions.\\nDue to many possible vulnerabilities inherent in phones such as the lack of control of the OS, libraries and hardware peripherals, Krux app should NOT be used to manage wallets containing savings or important keys and mnemonics. For that, a dedicated device is recommended.'
halign: 'center'
Button:
id: but_1
font_size: root.height // 30
font_name: 'NotoSansCJK_CY_JP_SC_KR_VI_Krux.ttf'
background_color: 0, 0, 0, 1
color: 0, 1, 0, 1
halign: 'center'
text: '| Start Krux |'
size_hint: 1, 0.2
pos_hint: {'y': 0}
on_release: root.start_thread()
""")
if platform == 'android':
from jnius import autoclass
from android.runnable import run_on_ui_thread
from android import mActivity
View = autoclass('android.view.View')
@run_on_ui_thread
def hide_landscape_status_bar(instance, width, height):
# width,height gives false layout events, on pinch/spread
# so use Window.width and Window.height
if Window.width > Window.height:
# Hide status bar
option = View.SYSTEM_UI_FLAG_FULLSCREEN
else:
# Show status bar
option = View.SYSTEM_UI_FLAG_VISIBLE
mActivity.getWindow().getDecorView().setSystemUiVisibility(option)
elif platform != 'ios':
# Dispose of that nasty red dot, required for gestures4kivy.
from kivy.config import Config
Config.set('input', 'mouse', 'mouse, disable_multitouch')
class PhysicalButtons(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.orientation = 'horizontal'
self.add_widget(Button(text='↓'))
self.add_widget(Button(text='↑'))
self.add_widget(Button(text='↳'))
self.size_hint= (1, 0.3)
class RootWidget(FloatLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.orientation = 'vertical'
self.stop = False
self.touch_control = touch_control
self.lcd_widget = mocks.load_mocks.lcd
self.camera_widget = mocks.load_mocks.main_sensor.qrreader
@mainthread
def btn_pressed(self, instance, pos):
self.touch_control.feed_position(pos)
@mainthread
def btn_released(self, instance, pos):
self.touch_control.feed_position(pos, release=True)
self.touch_control.release()
def camera_release(self, event):
self.touch_control.release()
self.touch_control.feed_position(None)
def camera_pressed(self, instance, pos):
self.touch_control.feed_position((1,1))
self.camera_widget.pressed = False
Clock.schedule_once(self.camera_release, 0.1)
@mainthread
def camera_on(self, instance, on):
if on:
# Set up and add the camera widget
self.camera_widget.__init__()
self.camera_widget.size_hint = (1, 0.8) # Full width, 80% height
self.camera_widget.pos = (0, self.height * 0.2)
self.camera_widget.connect_camera(
enable_photo = False,
analyze_pixels_resolution = 640,
enable_analyze_pixels = True,
)
self.add_widget(self.camera_widget)
else:
# Remove the camera widget and reset LCD widget
self.camera_widget.disconnect_camera()
self.remove_widget(self.camera_widget)
def android_back_click(self, window,key,*largs):
if key in [27, 1001]:
buttons_control.page_event_flag = True
return True
def start_thread(self):
Window.bind(on_keyboard=self.android_back_click)
self.lcd_widget.bind(pressed=self.btn_pressed)
self.lcd_widget.bind(released=self.btn_released)
self.camera_widget.bind(pressed=self.camera_pressed)
mocks.load_mocks.main_sensor.bind(running=self.camera_on)
self.remove_widget(self.label_1)
self.remove_widget(self.but_1)
self.add_widget(self.lcd_widget)
self.ctx = Context()
self.ctx.power_manager = power_manager
self.ctx.input.touch.index -= 1
self.t = threading.Thread(target=self.main_loop)
Clock.schedule_once(self.start_mainloop, 0.1)
Clock.schedule_interval(self.shut_down_monitor, 1)
def shut_down_monitor(self, dt):
if self.stop:
# self.qrreader.disconnect_camera()
App.get_running_app().stop()
def start_mainloop(self, dt):
self.t.start()
def main_loop(self):
while True:
if not Login(self.ctx).run():
break
if self.ctx.wallet is None:
continue
if not Home(self.ctx).run():
break
self.stop = True
from src.krux.krux_settings import t
self.ctx.display.clear()
self.ctx.display.draw_centered_text(t("Shutting down.."))
class KruxApp(App):
def build(self):
if platform == 'android':
Window.bind(on_resize=hide_landscape_status_bar)
return RootWidget()
def on_start(self):
self.dont_gc = AndroidPermissions(self.start_app)
def start_app(self):
self.dont_gc = None
# Can't connect camera till after on_start()
# registering our new custom fontstyle
LabelBase.register(name='NotoSans',
fn_regular='NotoSansCJK_CY_JP_SC_KR_VI_Krux.ttf')
KruxApp().run()