Mercurial > libervia-backend
annotate sat/plugins/plugin_misc_android.py @ 3091:2e1c3d33099b
tools (common/files_utils): get_unique_name now uses and returns Path instances.
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 20 Dec 2019 12:28:04 +0100 |
parents | f91d0e6d9b13 |
children | fb49587f55c2 |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
2100 | 2 # -*- coding: utf-8 -*- |
3 | |
4 # SAT plugin for file tansfer | |
2771 | 5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) |
2100 | 6 |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
2841
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
20 import sys |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
21 import os |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
22 import os.path |
2868
5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents:
2856
diff
changeset
|
23 import tempfile |
2100 | 24 from sat.core.i18n import _, D_ |
25 from sat.core.constants import Const as C | |
26 from sat.core.log import getLogger | |
2841
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
27 from sat.core import exceptions |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
28 from twisted.internet import reactor |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
29 from twisted.internet import protocol |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
30 from twisted.internet import error as int_error |
2868
5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents:
2856
diff
changeset
|
31 from twisted.web import client as web_client |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
32 |
2100 | 33 log = getLogger(__name__) |
34 | |
35 PLUGIN_INFO = { | |
2145
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2100
diff
changeset
|
36 C.PI_NAME: "Android ", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2100
diff
changeset
|
37 C.PI_IMPORT_NAME: "android", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2100
diff
changeset
|
38 C.PI_TYPE: C.PLUG_TYPE_MISC, |
3028 | 39 C.PI_RECOMMENDATIONS: ["XEP-0352"], |
2145
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2100
diff
changeset
|
40 C.PI_MAIN: "AndroidPlugin", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2100
diff
changeset
|
41 C.PI_HANDLER: "no", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
42 C.PI_DESCRIPTION: D_( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
43 """Manage Android platform specificities, like pause or notifications""" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
44 ), |
2100 | 45 } |
46 | |
47 if sys.platform != "android": | |
3028 | 48 raise exceptions.CancelError("this module is not needed on this platform") |
2100 | 49 |
2872
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
50 |
2100 | 51 from plyer import notification, vibrator |
2887
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
52 from plyer.platforms.android import activity |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
53 from jnius import autoclass |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
54 from android.broadcast import BroadcastReceiver |
2100 | 55 |
2872
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
56 #: delay between a pause event and sending the inactive indication to server, in seconds |
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
57 #: we don't send the indication immediately because user can be just checking something |
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
58 #: quickly on an other app. |
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
59 CSI_DELAY = 30 |
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
60 |
2100 | 61 PARAM_VIBRATE_CATEGORY = "Notifications" |
62 PARAM_VIBRATE_NAME = "vibrate" | |
3028 | 63 PARAM_VIBRATE_LABEL = D_("Vibrate on notifications") |
2841
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
64 SOCKET_DIR = "/data/data/org.salutatoi.cagou/" |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
65 SOCKET_FILE = ".socket" |
3057
f91d0e6d9b13
plugin android: fixed encoding in states after Python 3 port
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
66 STATE_RUNNING = b"running" |
f91d0e6d9b13
plugin android: fixed encoding in states after Python 3 port
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
67 STATE_PAUSED = b"paused" |
f91d0e6d9b13
plugin android: fixed encoding in states after Python 3 port
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
68 STATE_STOPPED = b"stopped" |
2841
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
69 STATES = (STATE_RUNNING, STATE_PAUSED, STATE_STOPPED) |
2887
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
70 NET_TYPE_NONE = "no network" |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
71 NET_TYPE_WIFI = "wifi" |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
72 NET_TYPE_MOBILE = "mobile" |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
73 NET_TYPE_OTHER = "other" |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
74 |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
75 |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
76 Context = autoclass('android.content.Context') |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
77 ConnectivityManager = autoclass('android.net.ConnectivityManager') |
2841
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
78 |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
79 |
2868
5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents:
2856
diff
changeset
|
80 def determineLength_workaround(self, fObj): |
5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents:
2856
diff
changeset
|
81 """Method working around seek() bug on Android""" |
5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents:
2856
diff
changeset
|
82 try: |
5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents:
2856
diff
changeset
|
83 seek = fObj.seek |
5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents:
2856
diff
changeset
|
84 tell = fObj.tell |
5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents:
2856
diff
changeset
|
85 except AttributeError: |
5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents:
2856
diff
changeset
|
86 return web_client.UNKNOWN_LENGTH |
5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents:
2856
diff
changeset
|
87 originalPosition = tell() |
5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents:
2856
diff
changeset
|
88 seek(os.SEEK_END) |
5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents:
2856
diff
changeset
|
89 end = tell() |
5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents:
2856
diff
changeset
|
90 seek(os.SEEK_SET, originalPosition) |
5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents:
2856
diff
changeset
|
91 return end - originalPosition |
5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents:
2856
diff
changeset
|
92 |
5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents:
2856
diff
changeset
|
93 |
5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents:
2856
diff
changeset
|
94 def patch_seek_bug(): |
5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents:
2856
diff
changeset
|
95 """Check seek bug and apply a workaround if still here |
5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents:
2856
diff
changeset
|
96 |
5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents:
2856
diff
changeset
|
97 cf. https://github.com/kivy/python-for-android/issues/1768 |
5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents:
2856
diff
changeset
|
98 """ |
5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents:
2856
diff
changeset
|
99 with tempfile.TemporaryFile() as f: |
5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents:
2856
diff
changeset
|
100 f.write(b'1234567890') |
5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents:
2856
diff
changeset
|
101 f.seek(0, os.SEEK_END) |
5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents:
2856
diff
changeset
|
102 size = f.tell() |
5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents:
2856
diff
changeset
|
103 if size == 10: |
3028 | 104 log.info("seek() bug not present anymore, workaround code can be removed") |
2868
5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents:
2856
diff
changeset
|
105 else: |
3028 | 106 log.warning("seek() bug detected, applying a workaround") |
2868
5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents:
2856
diff
changeset
|
107 web_client.FileBodyProducer._determineLength = determineLength_workaround |
5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents:
2856
diff
changeset
|
108 |
5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents:
2856
diff
changeset
|
109 patch_seek_bug() |
5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents:
2856
diff
changeset
|
110 |
5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents:
2856
diff
changeset
|
111 |
2841
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
112 class FrontendStateProtocol(protocol.Protocol): |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
113 |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
114 def __init__(self, android_plugin): |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
115 self.android_plugin = android_plugin |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
116 |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
117 def dataReceived(self, data): |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
118 if data in STATES: |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
119 self.android_plugin.state = data |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
120 else: |
3028 | 121 log.warning("Unexpected data: {data}".format(data=data)) |
2841
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
122 |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
123 |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
124 class FrontendStateFactory(protocol.Factory): |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
125 |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
126 def __init__(self, android_plugin): |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
127 self.android_plugin = android_plugin |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
128 |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
129 def buildProtocol(self, addr): |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
130 return FrontendStateProtocol(self.android_plugin) |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
131 |
2100 | 132 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
133 |
2100 | 134 class AndroidPlugin(object): |
135 | |
136 params = """ | |
137 <params> | |
138 <individual> | |
139 <category name="{category_name}" label="{category_label}"> | |
140 <param name="{param_name}" label="{param_label}" value="true" type="bool" security="0" /> | |
141 </category> | |
142 </individual> | |
143 </params> | |
144 """.format( | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
145 category_name=PARAM_VIBRATE_CATEGORY, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
146 category_label=D_(PARAM_VIBRATE_CATEGORY), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
147 param_name=PARAM_VIBRATE_NAME, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
148 param_label=PARAM_VIBRATE_LABEL, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
149 ) |
2100 | 150 |
151 def __init__(self, host): | |
3028 | 152 log.info(_("plugin Android initialization")) |
2100 | 153 self.host = host |
3028 | 154 self._csi = host.plugins.get('XEP-0352') |
2872
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
155 self._csi_timer = None |
2100 | 156 host.memory.updateParams(self.params) |
2841
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
157 try: |
3028 | 158 os.mkdir(SOCKET_DIR, 0o700) |
2841
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
159 except OSError as e: |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
160 if e.errno == 17: |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
161 # dir already exists |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
162 pass |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
163 else: |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
164 raise e |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
165 self._state = None |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
166 factory = FrontendStateFactory(self) |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
167 socket_path = os.path.join(SOCKET_DIR, SOCKET_FILE) |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
168 try: |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
169 reactor.listenUNIX(socket_path, factory) |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
170 except int_error.CannotListenError as e: |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
171 if e.socketError.errno == 98: |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
172 # the address is already in use, we need to remove it |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
173 os.unlink(socket_path) |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
174 reactor.listenUNIX(socket_path, factory) |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
175 else: |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
176 raise e |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
177 # we set a low priority because we want the notification to be sent after all |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
178 # plugins have done their job |
2100 | 179 host.trigger.add("MessageReceived", self.messageReceivedTrigger, priority=-1000) |
180 | |
2887
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
181 # Connectivity handling |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
182 self.cm = activity.getSystemService(Context.CONNECTIVITY_SERVICE) |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
183 self._net_type = None |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
184 self._checkConnectivity() |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
185 # XXX: we need to keep a reference to BroadcastReceiver to avoid |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
186 # "XXX has no attribute 'invoke'" error (looks like the same issue as |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
187 # https://github.com/kivy/pyjnius/issues/59) |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
188 self.br = BroadcastReceiver( |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
189 callback=lambda *args, **kwargs: reactor.callLater(0, |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
190 self.onConnectivityChange), |
3028 | 191 actions=["android.net.conn.CONNECTIVITY_CHANGE"]) |
2887
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
192 self.br.start() |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
193 |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
194 |
2100 | 195 @property |
2841
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
196 def state(self): |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
197 return self._state |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
198 |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
199 @state.setter |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
200 def state(self, new_state): |
3057
f91d0e6d9b13
plugin android: fixed encoding in states after Python 3 port
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
201 log.debug(f"frontend state has changed: {new_state.decode()}") |
2841
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
202 previous_state = self._state |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
203 self._state = new_state |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
204 if new_state == STATE_RUNNING: |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
205 self._onRunning(previous_state) |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
206 elif new_state == STATE_PAUSED: |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
207 self._onPaused(previous_state) |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
208 elif new_state == STATE_STOPPED: |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
209 self._onStopped(previous_state) |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
210 |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
211 @property |
2100 | 212 def cagou_active(self): |
2841
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
213 return self._state == STATE_RUNNING |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
214 |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
215 def _onRunning(self, previous_state): |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
216 if previous_state is not None: |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
217 self.host.bridge.bridgeReactivateSignals() |
2872
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
218 self.setActive() |
2841
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
219 |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
220 def _onPaused(self, previous_state): |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
221 self.host.bridge.bridgeDeactivateSignals() |
2872
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
222 self.setInactive() |
2841
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
223 |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
224 def _onStopped(self, previous_state): |
2872
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
225 self.setInactive() |
2100 | 226 |
227 def _notifyMessage(self, mess_data, client): | |
2856
26d6ac4e4a66
plugin android: don't send notification if message comes from ourself (from an other device)
Goffi <goffi@goffi.org>
parents:
2841
diff
changeset
|
228 """Send notification when suitable |
26d6ac4e4a66
plugin android: don't send notification if message comes from ourself (from an other device)
Goffi <goffi@goffi.org>
parents:
2841
diff
changeset
|
229 |
26d6ac4e4a66
plugin android: don't send notification if message comes from ourself (from an other device)
Goffi <goffi@goffi.org>
parents:
2841
diff
changeset
|
230 notification is sent if: |
26d6ac4e4a66
plugin android: don't send notification if message comes from ourself (from an other device)
Goffi <goffi@goffi.org>
parents:
2841
diff
changeset
|
231 - there is a message and it is not a groupchat |
26d6ac4e4a66
plugin android: don't send notification if message comes from ourself (from an other device)
Goffi <goffi@goffi.org>
parents:
2841
diff
changeset
|
232 - message is not coming from ourself |
26d6ac4e4a66
plugin android: don't send notification if message comes from ourself (from an other device)
Goffi <goffi@goffi.org>
parents:
2841
diff
changeset
|
233 """ |
26d6ac4e4a66
plugin android: don't send notification if message comes from ourself (from an other device)
Goffi <goffi@goffi.org>
parents:
2841
diff
changeset
|
234 if (mess_data["message"] and mess_data["type"] != C.MESS_TYPE_GROUPCHAT |
26d6ac4e4a66
plugin android: don't send notification if message comes from ourself (from an other device)
Goffi <goffi@goffi.org>
parents:
2841
diff
changeset
|
235 and not mess_data["from"].userhostJID() == client.jid.userhostJID()): |
3028 | 236 message = next(iter(mess_data["message"].values())) |
2100 | 237 try: |
3028 | 238 subject = next(iter(mess_data["subject"].values())) |
2100 | 239 except StopIteration: |
3028 | 240 subject = "Cagou new message" |
2100 | 241 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
242 notification.notify(title=subject, message=message) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
243 if self.host.memory.getParamA( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
244 PARAM_VIBRATE_NAME, PARAM_VIBRATE_CATEGORY, profile_key=client.profile |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
245 ): |
3018
758bee45612b
plugin android: catch and log exception when using the vibrator:
Goffi <goffi@goffi.org>
parents:
2887
diff
changeset
|
246 try: |
758bee45612b
plugin android: catch and log exception when using the vibrator:
Goffi <goffi@goffi.org>
parents:
2887
diff
changeset
|
247 vibrator.vibrate() |
758bee45612b
plugin android: catch and log exception when using the vibrator:
Goffi <goffi@goffi.org>
parents:
2887
diff
changeset
|
248 except Exception as e: |
758bee45612b
plugin android: catch and log exception when using the vibrator:
Goffi <goffi@goffi.org>
parents:
2887
diff
changeset
|
249 # FIXME: vibrator is currently not working, |
758bee45612b
plugin android: catch and log exception when using the vibrator:
Goffi <goffi@goffi.org>
parents:
2887
diff
changeset
|
250 # cf. https://github.com/kivy/plyer/issues/509 |
3028 | 251 log.warning("Can't use vibrator: {e}".format(e=e)) |
2100 | 252 return mess_data |
253 | |
254 def messageReceivedTrigger(self, client, message_elt, post_treat): | |
255 if not self.cagou_active: | |
256 # we only send notification is the frontend is not displayed | |
257 post_treat.addCallback(self._notifyMessage, client) | |
258 | |
259 return True | |
2872
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
260 |
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
261 # CSI |
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
262 |
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
263 def _setInactive(self): |
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
264 self._csi_timer = None |
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
265 for client in self.host.getClients(C.PROF_KEY_ALL): |
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
266 self._csi.setInactive(client) |
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
267 |
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
268 def setInactive(self): |
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
269 if self._csi is None or self._csi_timer is not None: |
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
270 return |
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
271 self._csi_timer = reactor.callLater(CSI_DELAY, self._setInactive) |
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
272 |
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
273 def setActive(self): |
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
274 if self._csi is None: |
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
275 return |
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
276 if self._csi_timer is not None: |
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
277 self._csi_timer.cancel() |
2882
0c54970d8e6e
plugin android: fixed csi_timer reset in setActive + crash on call of isActive before session initialisation
Goffi <goffi@goffi.org>
parents:
2872
diff
changeset
|
278 self._csi_timer = None |
2872
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
279 for client in self.host.getClients(C.PROF_KEY_ALL): |
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
280 self._csi.setActive(client) |
2887
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
281 |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
282 # Connectivity |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
283 |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
284 def _handleNetworkChange(self, previous, new): |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
285 """Notify the clients about network changes. |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
286 |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
287 This way the client can disconnect/reconnect transport, or change delays |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
288 """ |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
289 if new == NET_TYPE_NONE: |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
290 for client in self.host.getClients(C.PROF_KEY_ALL): |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
291 client.networkDisabled() |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
292 elif previous == NET_TYPE_NONE: |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
293 for client in self.host.getClients(C.PROF_KEY_ALL): |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
294 client.networkEnabled() |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
295 |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
296 def _checkConnectivity(self): |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
297 active_network = self.cm.getActiveNetworkInfo() |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
298 if active_network is None: |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
299 net_type = NET_TYPE_NONE |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
300 else: |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
301 net_type_android = active_network.getType() |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
302 if net_type_android == ConnectivityManager.TYPE_WIFI: |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
303 net_type = NET_TYPE_WIFI |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
304 elif net_type_android == ConnectivityManager.TYPE_MOBILE: |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
305 net_type = NET_TYPE_MOBILE |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
306 else: |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
307 net_type = NET_TYPE_OTHER |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
308 if net_type != self._net_type: |
3028 | 309 log.info("connectivity has changed") |
2887
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
310 previous = self._net_type |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
311 self._net_type = net_type |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
312 if net_type == NET_TYPE_NONE: |
3028 | 313 log.info("no network active") |
2887
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
314 elif net_type == NET_TYPE_WIFI: |
3028 | 315 log.info("WIFI activated") |
2887
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
316 elif net_type == NET_TYPE_MOBILE: |
3028 | 317 log.info("mobile data activated") |
2887
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
318 else: |
3028 | 319 log.info("network activated (type={net_type_android})" |
2887
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
320 .format(net_type_android=net_type_android)) |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
321 self._handleNetworkChange(previous, net_type) |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
322 else: |
3028 | 323 log.debug("_checkConnectivity called without network change ({net_type})" |
2887
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
324 .format(net_type = net_type)) |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
325 |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
326 |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
327 def onConnectivityChange(self): |
3028 | 328 log.debug("onConnectivityChange called") |
2887
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
329 self._checkConnectivity() |