forked from thorin-schiffer/kognitivo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
322 lines (255 loc) · 10.9 KB
/
app.py
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
from kivy.logger import Logger
from kivy.app import App
from kivy.utils import platform
import settings
class KognitivoApp(App):
use_kivy_settings = False
user_data_dir = settings.DATABASE_PATH
def open_settings(self, *largs):
if not self.settings_cls:
from settings_widget import KognitivoSettings
self.settings_cls = KognitivoSettings
super(KognitivoApp, self).open_settings(*largs)
def on_stop(self):
if platform == 'linux':
self.profile.disable()
self.profile.dump_stats('kognitivo.profile')
self.profiler.print_all()
if self.billing:
self.billing.unbind()
@property
def tracker(self):
if not self._tracker:
Logger.info("Tracker: initialization")
from tracking import Tracker
self._tracker = Tracker()
return self._tracker
def _initialize_storage(self):
Logger.info("Storage: initialization")
from kivy.storage.jsonstore import JsonStore
self._storage = JsonStore("storage-%s.json" % self.name)
import settings
for key, config in settings.DEFAULT_STORAGE_CONFIG.items():
if key not in self._storage:
self._storage.put(key, **config)
records = self._storage["task_records"]
for key in settings.TASKS:
if key not in records:
records[key] = 0
self._storage.put("task_records", **records)
starts_count = self._storage['starts']['count']
self.tracker.send_event('general', 'start', value=starts_count + 1)
self._storage['starts'] = {"count": starts_count + 1}
Logger.info("Storage: started %s times" % self._storage['starts']['count'])
@property
def storage(self):
if not self._storage:
self._initialize_storage()
return self._storage
def _initialize_gplay_services(self):
from gplay import GoogleClient
import settings
self._google_client = GoogleClient()
if not settings.PROFILE:
auto_login = self.config.get('preferences', 'google_auto_login')
if auto_login == "1":
self._google_client.connect()
@property
def google_client(self):
if not self._google_client:
Logger.info("Google: initialization")
self._initialize_gplay_services()
return self._google_client
def initialize_cprofile(self):
if platform == 'linux':
import cProfile
self.profile = cProfile.Profile()
self.profile.enable()
def __init__(self, name=None, **kwargs):
from utils import NaiveProfiler
self.profiler = NaiveProfiler()
self.initialize_cprofile()
self.profiler.fix_from("---app-init")
# lazy attributes
self._tracker = None
self._sounds = None
self.billing = None
self._storage = None
self.lang = None
self.manager = None
self._google_client = None
self.profiler.fix_from("super init")
super(KognitivoApp, self).__init__(**kwargs)
self.profiler.fix_to("super init")
self.db_path = None
self.profiler.fix_from("import-configure")
from configure import configure
self.profiler.fix_to("import-configure")
self.profiler.fix_from("configure")
configure()
self.profiler.fix_to("configure")
from settings import DEVELOPMENT_VERSION
if name is None:
KognitivoApp.name = "kognitivo-dev" if DEVELOPMENT_VERSION else "kognitivo"
else:
KognitivoApp.name = name
self.service = None
self.profiler.fix_to("---app-init")
def build_service_params(self):
params = {
"enable_notifications": self.config.get('preferences', 'enable_notifications'),
"morning_notification_time": self.config.get('preferences', 'morning_notification_time'),
"lunch_notification_time": self.config.get('preferences', 'lunch_notification_time'),
"evening_notification_time": self.config.get('preferences', 'evening_notification_time'),
"language": self.config.get('preferences', 'language'),
}
return params
def schedule_notifications(self, force=False):
if platform == 'android':
from notifications_scheduler import Scheduler
scheduler = Scheduler([self.config.get('preferences', 'morning_notification_time'),
self.config.get('preferences', 'lunch_notification_time'),
self.config.get('preferences', 'evening_notification_time')])
enable_notifications = int(self.config.get('preferences', 'enable_notifications'))
if enable_notifications == 1:
Logger.debug("Notifications: Creating schedules...")
if force:
scheduler.clean_schedules()
scheduler.create_schedule()
Logger.debug("Notifications: Finished creating schedules")
elif enable_notifications == 0:
if force:
scheduler.clean_schedules()
return
else:
Logger.info("Notifications: no notifications on %s" % platform)
def set_language(self):
if platform == 'android':
if self.config.get('preferences', 'language') == 'system':
Logger.info("Locale: Setting locale to system preference...")
from jnius import autoclass
Locale = autoclass('java.util.Locale')
try:
system_language = Locale.getDefault().getLanguage()
except Exception:
system_language = 'en'
Logger.info("Locale: System locale is %s" % system_language)
from settings import LANGUAGES
if system_language in LANGUAGES:
self.lang = system_language
self.config.set('preferences', 'language', system_language)
else:
Logger.info("Locale: System locale is unknown. Falling back to english...")
self.lang = 'en'
self.config.set('preferences', 'language', 'en')
self.config.write()
self.lang = self.config.get('preferences', 'language')
from utils import _
_.switch_lang(self.lang)
def _initialize_sounds(self):
Logger.info('App: initialize sounds')
if settings.SOUND_ENABLED and int(self.config.get('preferences', 'sound')) == 1:
self._sounds = {}
from settings import SOUNDS
from kivy.core.audio import SoundLoader
for name, path in SOUNDS.items():
self._sounds[name] = SoundLoader.load(path)
else:
from collections import defaultdict
from kivy.core.audio import Sound
self._sounds = defaultdict(Sound)
def initialize_billing(self, callback=None, *args):
if not self.billing:
Logger.info("Billing: initialization")
self.profiler.fix_from('billing')
from billing import BillingService
self.billing = BillingService()
self.billing.bind(callback)
self.profiler.fix_to('billing')
else:
if callback:
callback()
@property
def sounds(self):
if not self._sounds:
self._initialize_sounds()
return self._sounds
def build(self):
self.profiler.fix_from('---build')
self.profiler.fix_from('set_language')
self.set_language()
self.profiler.fix_to('set_language')
self.profiler.fix_from('schedule_notifications')
self.schedule_notifications()
self.profiler.fix_to('schedule_notifications')
self.profiler.fix_from('import-root-widget')
from root_manager import RootManager
self.profiler.fix_to('import-root-widget')
self.profiler.fix_from('build-root-widget')
import settings
root = RootManager()
from managers.database import database_manager
if database_manager.total_count() > 1:
from screens.activity.activity import ActivityScreen
start_screen = ActivityScreen()
menu_state = 'open'
else:
from screens.tutorial import TutorialScreen
start_screen = TutorialScreen()
menu_state = 'closed'
root.add_widget(start_screen)
from screens.menu import KognitivoNavigator
navigator = KognitivoNavigator()
navigator.add_widget(root)
navigator.state = menu_state
from kivy.core.window import Window
Window.clearcolor = (1, 1, 1, 1)
if settings.DEVELOPMENT_VERSION and settings.INSPECT and platform == 'linux':
from kivy.modules import inspector
inspector.create_inspector(Window, root)
self.profiler.fix_to('build-root-widget')
self.manager = root
self.bind(on_start=self.post_build_init)
self.profiler.fix_to('---build')
return navigator
def post_build_init(self, *_):
self.profiler.fix_from('post-build')
from managers.facebook import facebook_manager
facebook_manager.initialize()
from kivy.core.window import Window
win = Window
win.bind(on_keyboard=self.key_handler)
self.root.attach_toggle(win)
self.profiler.fix_to('post-build')
# noinspection PyUnusedLocal
def key_handler(self, window, keycode1, *_):
if keycode1 in [27, 1001]:
if not self.close_settings():
self.manager.go_back()
return True
return False
def build_settings(self, settings):
with open("settings.json", "r") as settings_json:
from utils import _
settings.add_json_panel(_('SETTINGS'), self.config, data=settings_json.read())
def build_config(self, config):
from settings import KIVY_DEFAULT_CONFIG
for section, conf in KIVY_DEFAULT_CONFIG.items():
config.setdefaults(section, conf)
def on_pause(self):
from managers.facebook import facebook_manager
facebook_manager.activate()
return True
def on_resume(self):
from managers.facebook import facebook_manager
facebook_manager.deactivate()
def on_config_change(self, config, section, key, value):
self.schedule_notifications(force=True)
if section == 'preferences':
if key == 'language':
Logger.info("Settings: change locale to %s" % value)
self.lang = value
if key == 'sound':
self._initialize_sounds()
if key == 'enable_notifications':
self.tracker.send_event('general', 'notifications', 'disabled' if value == "0" else 'enabled')