annotate src/plugins/plugin_misc_android.py @ 2320:f4a0723042ee

jp (pubsub/search): fixed encoding for stdin in external commands
author Goffi <goffi@goffi.org>
date Sun, 09 Jul 2017 15:20:31 +0200
parents 33c8c4973743
children 8b37a62336c3
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
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
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
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 log = getLogger(__name__)
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from sat.core import exceptions
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 import sys
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 import mmap
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
27
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 PLUGIN_INFO = {
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2100
diff changeset
30 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
31 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
32 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
33 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
34 C.PI_HANDLER: "no",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2100
diff changeset
35 C.PI_DESCRIPTION: D_("""Manage Android platform specificities, like pause or notifications""")
2100
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 }
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
37
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 if sys.platform != "android":
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 raise exceptions.CancelError(u"this module is not needed on this platform")
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 from plyer import notification, vibrator
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
42
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 PARAM_VIBRATE_CATEGORY = "Notifications"
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 PARAM_VIBRATE_NAME = "vibrate"
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 PARAM_VIBRATE_LABEL = D_(u"Vibrate on notifications")
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 class AndroidPlugin(object):
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
48
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 params = """
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 <params>
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 <individual>
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 <category name="{category_name}" label="{category_label}">
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 <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
54 </category>
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 </params>
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 """.format(
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 category_name = PARAM_VIBRATE_CATEGORY,
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 category_label = D_(PARAM_VIBRATE_CATEGORY),
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 param_name = PARAM_VIBRATE_NAME,
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 param_label = PARAM_VIBRATE_LABEL,
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 )
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
63
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 def __init__(self, host):
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 log.info(_("plugin Android initialization"))
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 self.host = host
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 host.memory.updateParams(self.params)
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 self.cagou_status_fd = open('.cagou_status', 'rb')
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 self.cagou_status = mmap.mmap(self.cagou_status_fd.fileno(), 1, prot=mmap.PROT_READ)
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 # 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
71 host.trigger.add("MessageReceived", self.messageReceivedTrigger, priority=-1000)
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
72
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 @property
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 def cagou_active(self):
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 # 'R' status means Cagou is running in front
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 return self.cagou_status[0] == 'R'
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
77
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 def _notifyMessage(self, mess_data, client):
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 # send notification if there is a message and it is not a groupchat
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 if mess_data['message'] and mess_data['type'] != C.MESS_TYPE_GROUPCHAT:
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 message = mess_data['message'].itervalues().next()
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 try:
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 subject = mess_data['subject'].itervalues().next()
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 except StopIteration:
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 subject = u'Cagou new message'
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
86
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 notification.notify(
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 title = subject,
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 message = message
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 )
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 if self.host.memory.getParamA(PARAM_VIBRATE_NAME, PARAM_VIBRATE_CATEGORY, profile_key=client.profile):
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 vibrator.vibrate()
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 return mess_data
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
94
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 def messageReceivedTrigger(self, client, message_elt, post_treat):
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 if not self.cagou_active:
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 # we only send notification is the frontend is not displayed
fbeeba721954 plugin android: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 post_treat.addCallback(self._notifyMessage, client)
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 return True