annotate sat/plugins/plugin_misc_android.py @ 2873:e1207b8ad97c

plugin text syntaxes: disable raw HTML parsing in mardown by default
author Goffi <goffi@goffi.org>
date Mon, 25 Mar 2019 08:14:00 +0100
parents 6b00f88316bf
children 0c54970d8e6e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2100
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SAT plugin for file tansfer
2771
003b8b4b56a7 date update
Goffi <goffi@goffi.org>
parents: 2756
diff changeset
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org)
2100
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from sat.core.i18n import _, D_
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from sat.core.constants import Const as C
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 log = getLogger(__name__)
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
34
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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,
2872
6b00f88316bf plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents: 2868
diff changeset
39 C.PI_RECOMMENDATIONS: [u"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
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 }
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
46
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 if sys.platform != "android":
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 raise exceptions.CancelError(u"this module is not needed on this platform")
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
49
2872
6b00f88316bf plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents: 2868
diff changeset
50
2100
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 from plyer import notification, vibrator
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
52
2872
6b00f88316bf plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents: 2868
diff changeset
53 #: 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
54 #: 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
55 #: quickly on an other app.
6b00f88316bf plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents: 2868
diff changeset
56 CSI_DELAY = 30
6b00f88316bf plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents: 2868
diff changeset
57
2100
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 PARAM_VIBRATE_CATEGORY = "Notifications"
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 PARAM_VIBRATE_NAME = "vibrate"
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 PARAM_VIBRATE_LABEL = D_(u"Vibrate on notifications")
2841
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
61 SOCKET_DIR = "/data/data/org.salutatoi.cagou/"
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
62 SOCKET_FILE = ".socket"
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
63 STATE_RUNNING = "running"
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
64 STATE_PAUSED = "paused"
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
65 STATE_STOPPED = "stopped"
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
66 STATES = (STATE_RUNNING, STATE_PAUSED, STATE_STOPPED)
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
67
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
68
2868
5546613f5007 plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents: 2856
diff changeset
69 def determineLength_workaround(self, fObj):
5546613f5007 plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents: 2856
diff changeset
70 """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
71 try:
5546613f5007 plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents: 2856
diff changeset
72 seek = fObj.seek
5546613f5007 plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents: 2856
diff changeset
73 tell = fObj.tell
5546613f5007 plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents: 2856
diff changeset
74 except AttributeError:
5546613f5007 plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents: 2856
diff changeset
75 return web_client.UNKNOWN_LENGTH
5546613f5007 plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents: 2856
diff changeset
76 originalPosition = tell()
5546613f5007 plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents: 2856
diff changeset
77 seek(os.SEEK_END)
5546613f5007 plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents: 2856
diff changeset
78 end = tell()
5546613f5007 plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents: 2856
diff changeset
79 seek(os.SEEK_SET, originalPosition)
5546613f5007 plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents: 2856
diff changeset
80 return end - originalPosition
5546613f5007 plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents: 2856
diff changeset
81
5546613f5007 plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents: 2856
diff changeset
82
5546613f5007 plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents: 2856
diff changeset
83 def patch_seek_bug():
5546613f5007 plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents: 2856
diff changeset
84 """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
85
5546613f5007 plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents: 2856
diff changeset
86 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
87 """
5546613f5007 plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents: 2856
diff changeset
88 with tempfile.TemporaryFile() as f:
5546613f5007 plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents: 2856
diff changeset
89 f.write(b'1234567890')
5546613f5007 plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents: 2856
diff changeset
90 f.seek(0, os.SEEK_END)
5546613f5007 plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents: 2856
diff changeset
91 size = f.tell()
5546613f5007 plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents: 2856
diff changeset
92 if size == 10:
5546613f5007 plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents: 2856
diff changeset
93 log.info(u"seek() bug not present anymore, workaround code can be removed")
5546613f5007 plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents: 2856
diff changeset
94 else:
5546613f5007 plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents: 2856
diff changeset
95 log.warning(u"seek() bug detected, applying a workaround")
5546613f5007 plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents: 2856
diff changeset
96 web_client.FileBodyProducer._determineLength = determineLength_workaround
5546613f5007 plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents: 2856
diff changeset
97
5546613f5007 plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents: 2856
diff changeset
98 patch_seek_bug()
5546613f5007 plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents: 2856
diff changeset
99
5546613f5007 plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents: 2856
diff changeset
100
2841
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
101 class FrontendStateProtocol(protocol.Protocol):
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
102
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
103 def __init__(self, android_plugin):
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
104 self.android_plugin = android_plugin
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
105
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
106 def dataReceived(self, data):
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
107 if data in STATES:
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
108 self.android_plugin.state = data
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
109 else:
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
110 log.warning(u"Unexpected data: {data}".format(data=data))
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
111
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
112
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
113 class FrontendStateFactory(protocol.Factory):
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
114
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
115 def __init__(self, android_plugin):
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
116 self.android_plugin = android_plugin
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
117
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
118 def buildProtocol(self, addr):
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
119 return FrontendStateProtocol(self.android_plugin)
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
120
2100
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
121
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
122
2100
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 class AndroidPlugin(object):
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
124
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 params = """
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 <params>
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 <individual>
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 <category name="{category_name}" label="{category_label}">
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 <param name="{param_name}" label="{param_label}" value="true" type="bool" security="0" />
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 </category>
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 </individual>
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 </params>
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 """.format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
134 category_name=PARAM_VIBRATE_CATEGORY,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
135 category_label=D_(PARAM_VIBRATE_CATEGORY),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
136 param_name=PARAM_VIBRATE_NAME,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
137 param_label=PARAM_VIBRATE_LABEL,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
138 )
2100
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
139
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 def __init__(self, host):
2841
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
141 log.info(_(u"plugin Android initialization"))
2100
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 self.host = host
2872
6b00f88316bf plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents: 2868
diff changeset
143 self._csi = host.plugins.get(u'XEP-0352')
6b00f88316bf plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents: 2868
diff changeset
144 self._csi_timer = None
2100
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 host.memory.updateParams(self.params)
2841
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
146 try:
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
147 os.mkdir(SOCKET_DIR, 0700)
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
148 except OSError as e:
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
149 if e.errno == 17:
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
150 # dir already exists
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
151 pass
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
152 else:
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
153 raise e
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
154 self._state = None
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
155 factory = FrontendStateFactory(self)
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
156 socket_path = os.path.join(SOCKET_DIR, SOCKET_FILE)
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
157 try:
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
158 reactor.listenUNIX(socket_path, factory)
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
159 except int_error.CannotListenError as e:
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
160 if e.socketError.errno == 98:
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
161 # 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
162 os.unlink(socket_path)
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
163 reactor.listenUNIX(socket_path, factory)
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
164 else:
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
165 raise e
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
166 # 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
167 # plugins have done their job
2100
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 host.trigger.add("MessageReceived", self.messageReceivedTrigger, priority=-1000)
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
169
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 @property
2841
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
171 def state(self):
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
172 return self._state
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
173
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
174 @state.setter
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
175 def state(self, new_state):
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
176 log.debug(u"frontend state has changed: {state}".format(state=new_state))
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
177 previous_state = self._state
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
178 self._state = new_state
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
179 if new_state == STATE_RUNNING:
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
180 self._onRunning(previous_state)
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
181 elif new_state == STATE_PAUSED:
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
182 self._onPaused(previous_state)
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
183 elif new_state == STATE_STOPPED:
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
184 self._onStopped(previous_state)
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
185
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
186 @property
2100
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 def cagou_active(self):
2841
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
188 return self._state == STATE_RUNNING
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
189
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
190 def _onRunning(self, previous_state):
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
191 if previous_state is not None:
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
192 self.host.bridge.bridgeReactivateSignals()
2872
6b00f88316bf plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents: 2868
diff changeset
193 self.setActive()
2841
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
194
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
195 def _onPaused(self, previous_state):
90115cf4e731 plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
196 self.host.bridge.bridgeDeactivateSignals()
2872
6b00f88316bf plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents: 2868
diff changeset
197 self.setInactive()
2841
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 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
200 self.setInactive()
2100
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
201
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
202 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
203 """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
204
26d6ac4e4a66 plugin android: don't send notification if message comes from ourself (from an other device)
Goffi <goffi@goffi.org>
parents: 2841
diff changeset
205 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
206 - 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
207 - 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
208 """
26d6ac4e4a66 plugin android: don't send notification if message comes from ourself (from an other device)
Goffi <goffi@goffi.org>
parents: 2841
diff changeset
209 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
210 and not mess_data["from"].userhostJID() == client.jid.userhostJID()):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
211 message = mess_data["message"].itervalues().next()
2100
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
213 subject = mess_data["subject"].itervalues().next()
2100
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 except StopIteration:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
215 subject = u"Cagou new message"
2100
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
216
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
217 notification.notify(title=subject, message=message)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
218 if self.host.memory.getParamA(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
219 PARAM_VIBRATE_NAME, PARAM_VIBRATE_CATEGORY, profile_key=client.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
220 ):
2100
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
221 vibrator.vibrate()
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
222 return mess_data
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
223
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
224 def messageReceivedTrigger(self, client, message_elt, post_treat):
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
225 if not self.cagou_active:
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 # we only send notification is the frontend is not displayed
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
227 post_treat.addCallback(self._notifyMessage, client)
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
228
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
229 return True
2872
6b00f88316bf plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents: 2868
diff changeset
230
6b00f88316bf plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents: 2868
diff changeset
231 # CSI
6b00f88316bf plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents: 2868
diff changeset
232
6b00f88316bf plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents: 2868
diff changeset
233 def _setInactive(self):
6b00f88316bf plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents: 2868
diff changeset
234 self._csi_timer = None
6b00f88316bf plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents: 2868
diff changeset
235 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
236 self._csi.setInactive(client)
6b00f88316bf plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents: 2868
diff changeset
237
6b00f88316bf plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents: 2868
diff changeset
238 def setInactive(self):
6b00f88316bf plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents: 2868
diff changeset
239 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
240 return
6b00f88316bf plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents: 2868
diff changeset
241 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
242
6b00f88316bf plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents: 2868
diff changeset
243 def setActive(self):
6b00f88316bf plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents: 2868
diff changeset
244 if self._csi is None:
6b00f88316bf plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents: 2868
diff changeset
245 return
6b00f88316bf plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents: 2868
diff changeset
246 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
247 self._csi_timer.cancel()
6b00f88316bf plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents: 2868
diff changeset
248 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
249 self._csi.setActive(client)