annotate sat/plugins/plugin_misc_android.py @ 2717:e3f6de6ce320

plugin XEP-0059: added serialise method
author Goffi <goffi@goffi.org>
date Mon, 10 Dec 2018 20:34:45 +0100
parents 56f94936df1e
children 1048d8dc8a9d
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
2483
0046283a285d dates update
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
5 # Copyright (C) 2009-2018 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
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from sat.core.i18n import _, D_
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from sat.core.constants import Const as C
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from sat.core.log import getLogger
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
23
2100
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 log = getLogger(__name__)
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from sat.core import exceptions
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 import sys
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 import mmap
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
28
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
29
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 PLUGIN_INFO = {
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2100
diff changeset
31 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
32 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
33 C.PI_TYPE: C.PLUG_TYPE_MISC,
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2100
diff changeset
34 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
35 C.PI_HANDLER: "no",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
36 C.PI_DESCRIPTION: D_(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
37 """Manage Android platform specificities, like pause or notifications"""
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
38 ),
2100
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 }
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
40
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 if sys.platform != "android":
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 raise exceptions.CancelError(u"this module is not needed on this platform")
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
43
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 from plyer import notification, vibrator
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 PARAM_VIBRATE_CATEGORY = "Notifications"
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 PARAM_VIBRATE_NAME = "vibrate"
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 PARAM_VIBRATE_LABEL = D_(u"Vibrate on notifications")
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
49
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
50
2100
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 class AndroidPlugin(object):
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
52
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 params = """
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 <params>
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 <individual>
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 <category name="{category_name}" label="{category_label}">
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 <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
58 </category>
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 </individual>
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 </params>
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 """.format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
62 category_name=PARAM_VIBRATE_CATEGORY,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
63 category_label=D_(PARAM_VIBRATE_CATEGORY),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
64 param_name=PARAM_VIBRATE_NAME,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
65 param_label=PARAM_VIBRATE_LABEL,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
66 )
2100
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 def __init__(self, host):
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 log.info(_("plugin Android initialization"))
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 self.host = host
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 host.memory.updateParams(self.params)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
72 self.cagou_status_fd = open(".cagou_status", "rb")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
73 self.cagou_status = mmap.mmap(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
74 self.cagou_status_fd.fileno(), 1, prot=mmap.PROT_READ
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
75 )
2100
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 # we set a low priority because we want the notification to be sent after all plugins have done their job
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 host.trigger.add("MessageReceived", self.messageReceivedTrigger, priority=-1000)
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
78
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 @property
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 def cagou_active(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
81 #  'R' status means Cagou is running in front
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
82 return self.cagou_status[0] == "R"
2100
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
83
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 def _notifyMessage(self, mess_data, client):
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 # send notification if there is a message and it is not a groupchat
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
86 if mess_data["message"] and mess_data["type"] != C.MESS_TYPE_GROUPCHAT:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
87 message = mess_data["message"].itervalues().next()
2100
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
89 subject = mess_data["subject"].itervalues().next()
2100
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 except StopIteration:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
91 subject = u"Cagou new message"
2100
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
92
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
93 notification.notify(title=subject, message=message)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
94 if self.host.memory.getParamA(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
95 PARAM_VIBRATE_NAME, PARAM_VIBRATE_CATEGORY, profile_key=client.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
96 ):
2100
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 vibrator.vibrate()
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 return mess_data
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
99
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 def messageReceivedTrigger(self, client, message_elt, post_treat):
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 if not self.cagou_active:
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 # we only send notification is the frontend is not displayed
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 post_treat.addCallback(self._notifyMessage, client)
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
104
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 return True