Mercurial > libervia-backend
annotate libervia/backend/plugins/plugin_misc_android.py @ 4332:71c939e34ca6
XEP-0373 (OX): Adjust to gpgme updates: generate with explicit algorithm and subkeys
author | Syndace <me@syndace.dev> |
---|---|
date | Sat, 13 Jul 2024 18:28:28 +0200 |
parents | 0d7bb4df2343 |
children |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
2100 | 3 # SAT plugin for file tansfer |
3479 | 4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
2100 | 5 |
6 # This program is free software: you can redistribute it and/or modify | |
7 # it under the terms of the GNU Affero General Public License as published by | |
8 # the Free Software Foundation, either version 3 of the License, or | |
9 # (at your option) any later version. | |
10 | |
11 # This program is distributed in the hope that it will be useful, | |
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 # GNU Affero General Public License for more details. | |
15 | |
16 # You should have received a copy of the GNU Affero General Public License | |
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | |
2841
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
19 import sys |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
20 import os |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
21 import os.path |
3154
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
22 import json |
3106
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
23 from pathlib import Path |
3245
2a0a16b906ac
plugin android: use `certifi` SSL root certicates
Goffi <goffi@goffi.org>
parents:
3172
diff
changeset
|
24 from zope.interface import implementer |
3162
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
25 from twisted.names import client as dns_client |
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
26 from twisted.python.procutils import which |
3124
b86060901278
plugin android: added a `profileAutoconnectGet` method:
Goffi <goffi@goffi.org>
parents:
3106
diff
changeset
|
27 from twisted.internet import defer |
2841
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 |
3162
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
30 from twisted.internet import abstract |
2841
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
31 from twisted.internet import error as int_error |
3245
2a0a16b906ac
plugin android: use `certifi` SSL root certicates
Goffi <goffi@goffi.org>
parents:
3172
diff
changeset
|
32 from twisted.internet import _sslverify |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4051
diff
changeset
|
33 from libervia.backend.core.i18n import _, D_ |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4051
diff
changeset
|
34 from libervia.backend.core.constants import Const as C |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4051
diff
changeset
|
35 from libervia.backend.core.log import getLogger |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4051
diff
changeset
|
36 from libervia.backend.core import exceptions |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4051
diff
changeset
|
37 from libervia.backend.tools.common import async_process |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4051
diff
changeset
|
38 from libervia.backend.memory import params |
3245
2a0a16b906ac
plugin android: use `certifi` SSL root certicates
Goffi <goffi@goffi.org>
parents:
3172
diff
changeset
|
39 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
40 |
2100 | 41 log = getLogger(__name__) |
42 | |
43 PLUGIN_INFO = { | |
3124
b86060901278
plugin android: added a `profileAutoconnectGet` method:
Goffi <goffi@goffi.org>
parents:
3106
diff
changeset
|
44 C.PI_NAME: "Android", |
2145
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2100
diff
changeset
|
45 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
|
46 C.PI_TYPE: C.PLUG_TYPE_MISC, |
3028 | 47 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
|
48 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
|
49 C.PI_HANDLER: "no", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
50 C.PI_DESCRIPTION: D_( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
51 """Manage Android platform specificities, like pause or notifications""" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
52 ), |
2100 | 53 } |
54 | |
55 if sys.platform != "android": | |
3028 | 56 raise exceptions.CancelError("this module is not needed on this platform") |
2100 | 57 |
2872
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
58 |
3162
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
59 import re |
3245
2a0a16b906ac
plugin android: use `certifi` SSL root certicates
Goffi <goffi@goffi.org>
parents:
3172
diff
changeset
|
60 import certifi |
3154
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
61 from plyer import vibrator |
3162
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
62 from android import api_version |
2887
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
63 from plyer.platforms.android import activity |
3154
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
64 from plyer.platforms.android.notification import AndroidNotification |
2887
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
65 from jnius import autoclass |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
66 from android.broadcast import BroadcastReceiver |
3154
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
67 from android import python_act |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
68 |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
69 |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
70 Context = autoclass("android.content.Context") |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
71 ConnectivityManager = autoclass("android.net.ConnectivityManager") |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
72 MediaPlayer = autoclass("android.media.MediaPlayer") |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
73 AudioManager = autoclass("android.media.AudioManager") |
3154
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
74 |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
75 # notifications |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
76 AndroidString = autoclass("java.lang.String") |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
77 PendingIntent = autoclass("android.app.PendingIntent") |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
78 Intent = autoclass("android.content.Intent") |
2100 | 79 |
3162
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
80 # DNS |
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
81 # regex to find dns server prop with "getprop" |
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
82 RE_DNS = re.compile(r"^\[net\.[a-z0-9]+\.dns[0-4]\]: \[(.*)\]$", re.MULTILINE) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
83 SystemProperties = autoclass("android.os.SystemProperties") |
3162
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
84 |
2872
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
85 #: 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
|
86 #: 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
|
87 #: quickly on an other app. |
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
88 CSI_DELAY = 30 |
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
89 |
3106
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
90 PARAM_RING_CATEGORY = "Notifications" |
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
91 PARAM_RING_NAME = "sound" |
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
92 PARAM_RING_LABEL = D_("sound on notifications") |
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
93 RING_OPTS = { |
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
94 "normal": D_("Normal"), |
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
95 "never": D_("Never"), |
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
96 } |
2100 | 97 PARAM_VIBRATE_CATEGORY = "Notifications" |
98 PARAM_VIBRATE_NAME = "vibrate" | |
3028 | 99 PARAM_VIBRATE_LABEL = D_("Vibrate on notifications") |
3106
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
100 VIBRATION_OPTS = { |
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
101 "always": D_("Always"), |
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
102 "vibrate": D_("In vibrate mode"), |
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
103 "never": D_("Never"), |
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
104 } |
3480
7550ae9cfbac
Renamed the project from "Salut à Toi" to "Libervia":
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
105 SOCKET_DIR = "/data/data/org.libervia.cagou/" |
2841
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
106 SOCKET_FILE = ".socket" |
3057
f91d0e6d9b13
plugin android: fixed encoding in states after Python 3 port
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
107 STATE_RUNNING = b"running" |
f91d0e6d9b13
plugin android: fixed encoding in states after Python 3 port
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
108 STATE_PAUSED = b"paused" |
f91d0e6d9b13
plugin android: fixed encoding in states after Python 3 port
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
109 STATE_STOPPED = b"stopped" |
2841
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
110 STATES = (STATE_RUNNING, STATE_PAUSED, STATE_STOPPED) |
2887
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
111 NET_TYPE_NONE = "no network" |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
112 NET_TYPE_WIFI = "wifi" |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
113 NET_TYPE_MOBILE = "mobile" |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
114 NET_TYPE_OTHER = "other" |
3154
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
115 INTENT_EXTRA_ACTION = AndroidString("org.salut-a-toi.IntentAction") |
2887
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
116 |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
117 |
3245
2a0a16b906ac
plugin android: use `certifi` SSL root certicates
Goffi <goffi@goffi.org>
parents:
3172
diff
changeset
|
118 @implementer(_sslverify.IOpenSSLTrustRoot) |
2a0a16b906ac
plugin android: use `certifi` SSL root certicates
Goffi <goffi@goffi.org>
parents:
3172
diff
changeset
|
119 class AndroidTrustPaths: |
2a0a16b906ac
plugin android: use `certifi` SSL root certicates
Goffi <goffi@goffi.org>
parents:
3172
diff
changeset
|
120 |
2a0a16b906ac
plugin android: use `certifi` SSL root certicates
Goffi <goffi@goffi.org>
parents:
3172
diff
changeset
|
121 def _addCACertsToContext(self, context): |
2a0a16b906ac
plugin android: use `certifi` SSL root certicates
Goffi <goffi@goffi.org>
parents:
3172
diff
changeset
|
122 # twisted doesn't have access to Android root certificates |
2a0a16b906ac
plugin android: use `certifi` SSL root certicates
Goffi <goffi@goffi.org>
parents:
3172
diff
changeset
|
123 # we use certifi to work around that (same thing is done in Kivy) |
2a0a16b906ac
plugin android: use `certifi` SSL root certicates
Goffi <goffi@goffi.org>
parents:
3172
diff
changeset
|
124 context.load_verify_locations(certifi.where()) |
2a0a16b906ac
plugin android: use `certifi` SSL root certicates
Goffi <goffi@goffi.org>
parents:
3172
diff
changeset
|
125 |
2a0a16b906ac
plugin android: use `certifi` SSL root certicates
Goffi <goffi@goffi.org>
parents:
3172
diff
changeset
|
126 |
2a0a16b906ac
plugin android: use `certifi` SSL root certicates
Goffi <goffi@goffi.org>
parents:
3172
diff
changeset
|
127 def platformTrust(): |
2a0a16b906ac
plugin android: use `certifi` SSL root certicates
Goffi <goffi@goffi.org>
parents:
3172
diff
changeset
|
128 return AndroidTrustPaths() |
2a0a16b906ac
plugin android: use `certifi` SSL root certicates
Goffi <goffi@goffi.org>
parents:
3172
diff
changeset
|
129 |
2a0a16b906ac
plugin android: use `certifi` SSL root certicates
Goffi <goffi@goffi.org>
parents:
3172
diff
changeset
|
130 |
3154
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
131 class Notification(AndroidNotification): |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
132 # We extend plyer's AndroidNotification instead of creating directly with jnius |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
133 # because it already handles issues like backward compatibility, and we just want to |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
134 # slightly modify the behaviour. |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
135 |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
136 @staticmethod |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
137 def _set_open_behavior(notification, sat_action): |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
138 # we reproduce plyer's AndroidNotification._set_open_behavior |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
139 # bu we add SàT specific extra action data |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
140 |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
141 app_context = activity.getApplication().getApplicationContext() |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
142 notification_intent = Intent(app_context, python_act) |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
143 |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
144 notification_intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP) |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
145 notification_intent.setAction(Intent.ACTION_MAIN) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
146 notification_intent.add_category(Intent.CATEGORY_LAUNCHER) |
3154
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
147 if sat_action is not None: |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
148 action_data = AndroidString(json.dumps(sat_action).encode()) |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
149 log.debug(f"adding extra {INTENT_EXTRA_ACTION} ==> {action_data}") |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
150 notification_intent = notification_intent.putExtra( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
151 INTENT_EXTRA_ACTION, action_data |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
152 ) |
3154
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
153 |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
154 # we use PendingIntent.FLAG_UPDATE_CURRENT here, otherwise extra won't be set |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
155 # in the new intent (the old ACTION_MAIN intent will be reused). This differs |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
156 # from plyers original behaviour which set no flag here |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
157 pending_intent = PendingIntent.getActivity( |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
158 app_context, 0, notification_intent, PendingIntent.FLAG_UPDATE_CURRENT |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
159 ) |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
160 |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
161 notification.setContentIntent(pending_intent) |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
162 notification.setAutoCancel(True) |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
163 |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
164 def _notify(self, **kwargs): |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
165 # we reproduce plyer's AndroidNotification._notify behaviour here |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
166 # and we add handling of "sat_action" attribute (SàT specific). |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
167 # we also set, where suitable, default values to empty string instead of |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
168 # original None, as a string is expected (in plyer the empty string is used |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
169 # in the generic "notify" method). |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
170 sat_action = kwargs.pop("sat_action", None) |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
171 noti = None |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
172 message = kwargs.get("message", "").encode("utf-8") |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
173 ticker = kwargs.get("ticker", "").encode("utf-8") |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
174 title = AndroidString(kwargs.get("title", "").encode("utf-8")) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
175 icon = kwargs.get("app_icon", "") |
3154
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
176 |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
177 if kwargs.get("toast", False): |
3154
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
178 self._toast(message) |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
179 return |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
180 else: |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
181 noti = self._build_notification(title) |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
182 |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
183 noti.setContentTitle(title) |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
184 noti.setContentText(AndroidString(message)) |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
185 noti.setTicker(AndroidString(ticker)) |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
186 |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
187 self._set_icons(noti, icon=icon) |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
188 self._set_open_behavior(noti, sat_action) |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
189 |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
190 self._open_notification(noti) |
2868
5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents:
2856
diff
changeset
|
191 |
5546613f5007
plugin android: workaround to seek() bug, fixing file upload:
Goffi <goffi@goffi.org>
parents:
2856
diff
changeset
|
192 |
2841
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
193 class FrontendStateProtocol(protocol.Protocol): |
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 __init__(self, android_plugin): |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
196 self.android_plugin = android_plugin |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
197 |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
198 def dataReceived(self, data): |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
199 if data in STATES: |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
200 self.android_plugin.state = data |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
201 else: |
3028 | 202 log.warning("Unexpected data: {data}".format(data=data)) |
2841
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
203 |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
204 |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
205 class FrontendStateFactory(protocol.Factory): |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
206 |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
207 def __init__(self, android_plugin): |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
208 self.android_plugin = android_plugin |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
209 |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
210 def buildProtocol(self, addr): |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
211 return FrontendStateProtocol(self.android_plugin) |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
212 |
2100 | 213 |
214 class AndroidPlugin(object): | |
215 | |
216 params = """ | |
217 <params> | |
218 <individual> | |
219 <category name="{category_name}" label="{category_label}"> | |
3106
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
220 <param name="{ring_param_name}" label="{ring_param_label}" type="list" security="0"> |
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
221 {ring_options} |
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
222 </param> |
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
223 <param name="{vibrate_param_name}" label="{vibrate_param_label}" type="list" security="0"> |
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
224 {vibrate_options} |
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
225 </param> |
2100 | 226 </category> |
227 </individual> | |
228 </params> | |
229 """.format( | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
230 category_name=PARAM_VIBRATE_CATEGORY, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
231 category_label=D_(PARAM_VIBRATE_CATEGORY), |
3106
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
232 vibrate_param_name=PARAM_VIBRATE_NAME, |
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
233 vibrate_param_label=PARAM_VIBRATE_LABEL, |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
234 vibrate_options=params.make_options(VIBRATION_OPTS, "always"), |
3106
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
235 ring_param_name=PARAM_RING_NAME, |
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
236 ring_param_label=PARAM_RING_LABEL, |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
237 ring_options=params.make_options(RING_OPTS, "normal"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
238 ) |
2100 | 239 |
240 def __init__(self, host): | |
3028 | 241 log.info(_("plugin Android initialization")) |
3162
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
242 log.info(f"using Android API {api_version}") |
2100 | 243 self.host = host |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
244 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
|
245 self._csi_timer = None |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
246 host.memory.update_params(self.params) |
2841
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
247 try: |
3028 | 248 os.mkdir(SOCKET_DIR, 0o700) |
2841
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
249 except OSError as e: |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
250 if e.errno == 17: |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
251 # dir already exists |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
252 pass |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
253 else: |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
254 raise e |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
255 self._state = None |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
256 factory = FrontendStateFactory(self) |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
257 socket_path = os.path.join(SOCKET_DIR, SOCKET_FILE) |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
258 try: |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
259 reactor.listenUNIX(socket_path, factory) |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
260 except int_error.CannotListenError as e: |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
261 if e.socketError.errno == 98: |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
262 # 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
|
263 os.unlink(socket_path) |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
264 reactor.listenUNIX(socket_path, factory) |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
265 else: |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
266 raise e |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
267 # 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
|
268 # plugins have done their job |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
269 host.trigger.add( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
270 "message_received", self.message_received_trigger, priority=-1000 |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
271 ) |
2100 | 272 |
3124
b86060901278
plugin android: added a `profileAutoconnectGet` method:
Goffi <goffi@goffi.org>
parents:
3106
diff
changeset
|
273 # profiles autoconnection |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
274 host.bridge.add_method( |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
275 "profile_autoconnect_get", |
3124
b86060901278
plugin android: added a `profileAutoconnectGet` method:
Goffi <goffi@goffi.org>
parents:
3106
diff
changeset
|
276 ".plugin", |
b86060901278
plugin android: added a `profileAutoconnectGet` method:
Goffi <goffi@goffi.org>
parents:
3106
diff
changeset
|
277 in_sign="", |
b86060901278
plugin android: added a `profileAutoconnectGet` method:
Goffi <goffi@goffi.org>
parents:
3106
diff
changeset
|
278 out_sign="s", |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
279 method=self._profile_autoconnect_get, |
3124
b86060901278
plugin android: added a `profileAutoconnectGet` method:
Goffi <goffi@goffi.org>
parents:
3106
diff
changeset
|
280 async_=True, |
b86060901278
plugin android: added a `profileAutoconnectGet` method:
Goffi <goffi@goffi.org>
parents:
3106
diff
changeset
|
281 ) |
b86060901278
plugin android: added a `profileAutoconnectGet` method:
Goffi <goffi@goffi.org>
parents:
3106
diff
changeset
|
282 |
3106
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
283 # audio manager, to get ring status |
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
284 self.am = activity.getSystemService(Context.AUDIO_SERVICE) |
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
285 |
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
286 # sound notification |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
287 media_dir = Path(host.memory.config_get("", "media_dir")) |
3106
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
288 assert media_dir is not None |
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
289 notif_path = media_dir / "sounds" / "notifications" / "music-box.mp3" |
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
290 self.notif_player = MediaPlayer() |
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
291 self.notif_player.setDataSource(str(notif_path)) |
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
292 self.notif_player.setAudioStreamType(AudioManager.STREAM_NOTIFICATION) |
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
293 self.notif_player.prepare() |
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
294 |
3245
2a0a16b906ac
plugin android: use `certifi` SSL root certicates
Goffi <goffi@goffi.org>
parents:
3172
diff
changeset
|
295 # SSL fix |
2a0a16b906ac
plugin android: use `certifi` SSL root certicates
Goffi <goffi@goffi.org>
parents:
3172
diff
changeset
|
296 _sslverify.platformTrust = platformTrust |
2a0a16b906ac
plugin android: use `certifi` SSL root certicates
Goffi <goffi@goffi.org>
parents:
3172
diff
changeset
|
297 log.info("SSL Android patch applied") |
2a0a16b906ac
plugin android: use `certifi` SSL root certicates
Goffi <goffi@goffi.org>
parents:
3172
diff
changeset
|
298 |
3162
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
299 # DNS fix |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
300 defer.ensureDeferred(self.update_resolver()) |
3162
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
301 |
2887
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
302 # Connectivity handling |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
303 self.cm = activity.getSystemService(Context.CONNECTIVITY_SERVICE) |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
304 self._net_type = None |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
305 d = defer.ensureDeferred(self._check_connectivity()) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
306 d.addErrback(host.log_errback) |
3125
c3ce8c997fdf
plugin android: reconnect profiles with autoconnect if network is activated:
Goffi <goffi@goffi.org>
parents:
3124
diff
changeset
|
307 |
2887
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
308 # XXX: we need to keep a reference to BroadcastReceiver to avoid |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
309 # "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
|
310 # https://github.com/kivy/pyjnius/issues/59) |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
311 self.br = BroadcastReceiver( |
3654
3910ad643e9d
plugin android: use `callFromThread` instead of `callLater`:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
312 callback=lambda *args, **kwargs: reactor.callFromThread( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
313 self.on_connectivity_change |
3654
3910ad643e9d
plugin android: use `callFromThread` instead of `callLater`:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
314 ), |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
315 actions=["android.net.conn.CONNECTIVITY_CHANGE"], |
3654
3910ad643e9d
plugin android: use `callFromThread` instead of `callLater`:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
316 ) |
2887
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
317 self.br.start() |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
318 |
2100 | 319 @property |
2841
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
320 def state(self): |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
321 return self._state |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
322 |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
323 @state.setter |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
324 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
|
325 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
|
326 previous_state = self._state |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
327 self._state = new_state |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
328 if new_state == STATE_RUNNING: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
329 self._on_running(previous_state) |
2841
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
330 elif new_state == STATE_PAUSED: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
331 self._on_paused(previous_state) |
2841
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
332 elif new_state == STATE_STOPPED: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
333 self._on_stopped(previous_state) |
2841
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
334 |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
335 @property |
2100 | 336 def cagou_active(self): |
2841
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
337 return self._state == STATE_RUNNING |
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
338 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
339 def _on_running(self, previous_state): |
2841
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
340 if previous_state is not None: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
341 self.host.bridge.bridge_reactivate_signals() |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
342 self.set_active() |
2841
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
343 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
344 def _on_paused(self, previous_state): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
345 self.host.bridge.bridge_deactivate_signals() |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
346 self.set_inactive() |
2841
90115cf4e731
plugin android: improved state handling:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
347 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
348 def _on_stopped(self, previous_state): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
349 self.set_inactive() |
2100 | 350 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
351 def _notify_message(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
|
352 """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
|
353 |
26d6ac4e4a66
plugin android: don't send notification if message comes from ourself (from an other device)
Goffi <goffi@goffi.org>
parents:
2841
diff
changeset
|
354 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
|
355 - 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
|
356 - 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
|
357 """ |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
358 if ( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
359 mess_data["message"] |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
360 and mess_data["type"] != C.MESS_TYPE_GROUPCHAT |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
361 and not mess_data["from"].userhostJID() == client.jid.userhostJID() |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
362 ): |
3028 | 363 message = next(iter(mess_data["message"].values())) |
2100 | 364 try: |
3028 | 365 subject = next(iter(mess_data["subject"].values())) |
2100 | 366 except StopIteration: |
3154
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
367 subject = D_("new message from {contact}").format( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
368 contact=mess_data["from"] |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
369 ) |
2100 | 370 |
3154
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
371 notification = Notification() |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
372 notification._notify( |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
373 title=subject, |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
374 message=message, |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
375 sat_action={ |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
376 "type": "open", |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
377 "widget": "chat", |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
378 "target": mess_data["from"].userhost(), |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
379 }, |
f2d3ab4390a3
plugin android: attach an action dict to notification:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
380 ) |
3106
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
381 |
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
382 ringer_mode = self.am.getRingerMode() |
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
383 vibrate_mode = ringer_mode == AudioManager.RINGER_MODE_VIBRATE |
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
384 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
385 ring_setting = self.host.memory.param_get_a( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
386 PARAM_RING_NAME, PARAM_RING_CATEGORY, profile_key=client.profile |
3106
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
387 ) |
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
388 |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
389 if ring_setting != "never" and ringer_mode == AudioManager.RINGER_MODE_NORMAL: |
3106
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
390 self.notif_player.start() |
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
391 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
392 vibration_setting = self.host.memory.param_get_a( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
393 PARAM_VIBRATE_NAME, PARAM_VIBRATE_CATEGORY, profile_key=client.profile |
3106
7f7cdc6ecfd8
plugin android: sound notification + change settings:
Goffi <goffi@goffi.org>
parents:
3095
diff
changeset
|
394 ) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
395 if ( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
396 vibration_setting == "always" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
397 or vibration_setting == "vibrate" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
398 and vibrate_mode |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
399 ): |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
400 try: |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
401 vibrator.vibrate() |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
402 except Exception as e: |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
403 log.warning("Can't use vibrator: {e}".format(e=e)) |
2100 | 404 return mess_data |
405 | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
406 def message_received_trigger(self, client, message_elt, post_treat): |
2100 | 407 if not self.cagou_active: |
408 # we only send notification is the frontend is not displayed | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
409 post_treat.addCallback(self._notify_message, client) |
2100 | 410 |
411 return True | |
2872
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
412 |
3124
b86060901278
plugin android: added a `profileAutoconnectGet` method:
Goffi <goffi@goffi.org>
parents:
3106
diff
changeset
|
413 # Profile autoconnection |
b86060901278
plugin android: added a `profileAutoconnectGet` method:
Goffi <goffi@goffi.org>
parents:
3106
diff
changeset
|
414 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
415 def _profile_autoconnect_get(self): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
416 return defer.ensureDeferred(self.profile_autoconnect_get()) |
3124
b86060901278
plugin android: added a `profileAutoconnectGet` method:
Goffi <goffi@goffi.org>
parents:
3106
diff
changeset
|
417 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
418 async def _get_profiles_autoconnect(self): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
419 autoconnect_dict = await self.host.memory.storage.get_ind_param_values( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
420 category="Connection", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
421 name="autoconnect_backend", |
3124
b86060901278
plugin android: added a `profileAutoconnectGet` method:
Goffi <goffi@goffi.org>
parents:
3106
diff
changeset
|
422 ) |
3125
c3ce8c997fdf
plugin android: reconnect profiles with autoconnect if network is activated:
Goffi <goffi@goffi.org>
parents:
3124
diff
changeset
|
423 return [p for p, v in autoconnect_dict.items() if C.bool(v)] |
c3ce8c997fdf
plugin android: reconnect profiles with autoconnect if network is activated:
Goffi <goffi@goffi.org>
parents:
3124
diff
changeset
|
424 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
425 async def profile_autoconnect_get(self): |
3125
c3ce8c997fdf
plugin android: reconnect profiles with autoconnect if network is activated:
Goffi <goffi@goffi.org>
parents:
3124
diff
changeset
|
426 """Return profile to connect automatically by frontend, if any""" |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
427 profiles_autoconnect = await self._get_profiles_autoconnect() |
3124
b86060901278
plugin android: added a `profileAutoconnectGet` method:
Goffi <goffi@goffi.org>
parents:
3106
diff
changeset
|
428 if not profiles_autoconnect: |
b86060901278
plugin android: added a `profileAutoconnectGet` method:
Goffi <goffi@goffi.org>
parents:
3106
diff
changeset
|
429 return None |
b86060901278
plugin android: added a `profileAutoconnectGet` method:
Goffi <goffi@goffi.org>
parents:
3106
diff
changeset
|
430 if len(profiles_autoconnect) > 1: |
b86060901278
plugin android: added a `profileAutoconnectGet` method:
Goffi <goffi@goffi.org>
parents:
3106
diff
changeset
|
431 log.warning( |
b86060901278
plugin android: added a `profileAutoconnectGet` method:
Goffi <goffi@goffi.org>
parents:
3106
diff
changeset
|
432 f"More that one profiles with backend autoconnection set found, picking " |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
433 f"up first one (full list: {profiles_autoconnect!r})" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
434 ) |
3124
b86060901278
plugin android: added a `profileAutoconnectGet` method:
Goffi <goffi@goffi.org>
parents:
3106
diff
changeset
|
435 return profiles_autoconnect[0] |
b86060901278
plugin android: added a `profileAutoconnectGet` method:
Goffi <goffi@goffi.org>
parents:
3106
diff
changeset
|
436 |
2872
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
437 # CSI |
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
438 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
439 def _set_inactive(self): |
2872
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
440 self._csi_timer = None |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
441 for client in self.host.get_clients(C.PROF_KEY_ALL): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
442 self._csi.set_inactive(client) |
2872
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
443 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
444 def set_inactive(self): |
2872
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
445 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
|
446 return |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
447 self._csi_timer = reactor.callLater(CSI_DELAY, self._set_inactive) |
2872
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
448 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
449 def set_active(self): |
2872
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
450 if self._csi is None: |
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
451 return |
6b00f88316bf
plugin android: use XEP-0352 to indicate (in)active state:
Goffi <goffi@goffi.org>
parents:
2868
diff
changeset
|
452 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
|
453 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
|
454 self._csi_timer = None |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
455 for client in self.host.get_clients(C.PROF_KEY_ALL): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
456 self._csi.set_active(client) |
2887
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
457 |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
458 # Connectivity |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
459 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
460 async def _handle_network_change(self, net_type): |
2887
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
461 """Notify the clients about network changes. |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
462 |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
463 This way the client can disconnect/reconnect transport, or change delays |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
464 """ |
3125
c3ce8c997fdf
plugin android: reconnect profiles with autoconnect if network is activated:
Goffi <goffi@goffi.org>
parents:
3124
diff
changeset
|
465 log.debug(f"handling network change ({net_type})") |
c3ce8c997fdf
plugin android: reconnect profiles with autoconnect if network is activated:
Goffi <goffi@goffi.org>
parents:
3124
diff
changeset
|
466 if net_type == NET_TYPE_NONE: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
467 for client in self.host.get_clients(C.PROF_KEY_ALL): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
468 client.network_disabled() |
3125
c3ce8c997fdf
plugin android: reconnect profiles with autoconnect if network is activated:
Goffi <goffi@goffi.org>
parents:
3124
diff
changeset
|
469 else: |
3162
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
470 # DNS servers may have changed |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
471 await self.update_resolver() |
3125
c3ce8c997fdf
plugin android: reconnect profiles with autoconnect if network is activated:
Goffi <goffi@goffi.org>
parents:
3124
diff
changeset
|
472 # client may be there but disabled (e.g. with stream management) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
473 for client in self.host.get_clients(C.PROF_KEY_ALL): |
3125
c3ce8c997fdf
plugin android: reconnect profiles with autoconnect if network is activated:
Goffi <goffi@goffi.org>
parents:
3124
diff
changeset
|
474 log.debug(f"enabling network for {client.profile}") |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
475 client.network_enabled() |
2887
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
476 |
3125
c3ce8c997fdf
plugin android: reconnect profiles with autoconnect if network is activated:
Goffi <goffi@goffi.org>
parents:
3124
diff
changeset
|
477 # profiles may have been disconnected and then purged, we try |
c3ce8c997fdf
plugin android: reconnect profiles with autoconnect if network is activated:
Goffi <goffi@goffi.org>
parents:
3124
diff
changeset
|
478 # to reconnect them in case |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
479 profiles_autoconnect = await self._get_profiles_autoconnect() |
3125
c3ce8c997fdf
plugin android: reconnect profiles with autoconnect if network is activated:
Goffi <goffi@goffi.org>
parents:
3124
diff
changeset
|
480 for profile in profiles_autoconnect: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
481 if not self.host.is_connected(profile): |
3125
c3ce8c997fdf
plugin android: reconnect profiles with autoconnect if network is activated:
Goffi <goffi@goffi.org>
parents:
3124
diff
changeset
|
482 log.info(f"{profile} is not connected, reconnecting it") |
c3ce8c997fdf
plugin android: reconnect profiles with autoconnect if network is activated:
Goffi <goffi@goffi.org>
parents:
3124
diff
changeset
|
483 try: |
c3ce8c997fdf
plugin android: reconnect profiles with autoconnect if network is activated:
Goffi <goffi@goffi.org>
parents:
3124
diff
changeset
|
484 await self.host.connect(profile) |
c3ce8c997fdf
plugin android: reconnect profiles with autoconnect if network is activated:
Goffi <goffi@goffi.org>
parents:
3124
diff
changeset
|
485 except Exception as e: |
c3ce8c997fdf
plugin android: reconnect profiles with autoconnect if network is activated:
Goffi <goffi@goffi.org>
parents:
3124
diff
changeset
|
486 log.error(f"Can't connect profile {profile}: {e}") |
c3ce8c997fdf
plugin android: reconnect profiles with autoconnect if network is activated:
Goffi <goffi@goffi.org>
parents:
3124
diff
changeset
|
487 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
488 async def _check_connectivity(self): |
2887
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
489 active_network = self.cm.getActiveNetworkInfo() |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
490 if active_network is None: |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
491 net_type = NET_TYPE_NONE |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
492 else: |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
493 net_type_android = active_network.getType() |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
494 if net_type_android == ConnectivityManager.TYPE_WIFI: |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
495 net_type = NET_TYPE_WIFI |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
496 elif net_type_android == ConnectivityManager.TYPE_MOBILE: |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
497 net_type = NET_TYPE_MOBILE |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
498 else: |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
499 net_type = NET_TYPE_OTHER |
3125
c3ce8c997fdf
plugin android: reconnect profiles with autoconnect if network is activated:
Goffi <goffi@goffi.org>
parents:
3124
diff
changeset
|
500 |
2887
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
501 if net_type != self._net_type: |
3028 | 502 log.info("connectivity has changed") |
2887
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
503 self._net_type = net_type |
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
504 if net_type == NET_TYPE_NONE: |
3028 | 505 log.info("no network active") |
2887
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
506 elif net_type == NET_TYPE_WIFI: |
3028 | 507 log.info("WIFI activated") |
2887
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
508 elif net_type == NET_TYPE_MOBILE: |
3028 | 509 log.info("mobile data activated") |
2887
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
510 else: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
511 log.info( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
512 "network activated (type={net_type_android})".format( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
513 net_type_android=net_type_android |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
514 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
515 ) |
2887
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
516 else: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
517 log.debug( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
518 "_check_connectivity called without network change ({net_type})".format( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
519 net_type=net_type |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
520 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
521 ) |
2887
9aadf11b315b
plugin android: check connectivity
Goffi <goffi@goffi.org>
parents:
2882
diff
changeset
|
522 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
523 # we always call _handle_network_change even if there is not connectivity change |
3125
c3ce8c997fdf
plugin android: reconnect profiles with autoconnect if network is activated:
Goffi <goffi@goffi.org>
parents:
3124
diff
changeset
|
524 # to be sure to reconnect when necessary |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
525 await self._handle_network_change(net_type) |
3125
c3ce8c997fdf
plugin android: reconnect profiles with autoconnect if network is activated:
Goffi <goffi@goffi.org>
parents:
3124
diff
changeset
|
526 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
527 def on_connectivity_change(self): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
528 log.debug("on_connectivity_change called") |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
529 d = defer.ensureDeferred(self._check_connectivity()) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
530 d.addErrback(self.host.log_errback) |
3162
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
531 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
532 async def update_resolver(self): |
3162
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
533 # There is no "/etc/resolv.conf" on Android, which confuse Twisted and makes |
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
534 # SRV record checking unusable. We fixe that by checking DNS server used, and |
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
535 # updating Twisted's resolver accordingly |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
536 dns_servers = await self.get_dns_servers() |
3162
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
537 |
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
538 log.info( |
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
539 "Patching Twisted to use Android DNS resolver ({dns_servers})".format( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
540 dns_servers=", ".join([s[0] for s in dns_servers]) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
541 ) |
3162
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
542 ) |
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
543 dns_client.theResolver = dns_client.createResolver(servers=dns_servers) |
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
544 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3654
diff
changeset
|
545 async def get_dns_servers(self): |
3162
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
546 servers = [] |
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
547 |
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
548 if api_version < 26: |
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
549 # thanks to A-IV at https://stackoverflow.com/a/11362271 for the way to go |
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
550 log.debug("Old API, using SystemProperties to find DNS") |
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
551 for idx in range(1, 5): |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
552 addr = SystemProperties.get(f"net.dns{idx}") |
3162
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
553 if abstract.isIPAddress(addr): |
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
554 servers.append((addr, 53)) |
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
555 else: |
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
556 log.debug(f"API {api_version} >= 26, using getprop to find DNS") |
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
557 # use of getprop inspired by various solutions at |
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
558 # https://stackoverflow.com/q/3070144 |
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
559 # it's the most simple option, and it fit wells with async_process |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
560 getprop_paths = which("getprop") |
3162
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
561 if getprop_paths: |
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
562 try: |
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
563 getprop_path = getprop_paths[0] |
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
564 props = await async_process.run(getprop_path) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
565 servers = [ |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
566 (ip, 53) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
567 for ip in RE_DNS.findall(props.decode()) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
568 if abstract.isIPAddress(ip) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
569 ] |
3162
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
570 except Exception as e: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
571 log.warning(f'Can\'t use "getprop" to find DNS server: {e}') |
3162
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
572 if not servers: |
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
573 # FIXME: Cloudflare's 1.1.1.1 seems to have a better privacy policy, to be |
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
574 # checked. |
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
575 log.warning( |
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
576 "no server found, we have to use factory Google DNS, this is not ideal " |
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
577 "for privacy" |
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
578 ) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
579 servers.append(("8.8.8.8", 53), ("8.8.4.4", 53)) |
3162
b5c058c7692e
core (xmpp), plugin android: fixed DNS, SRV handling on Android:
Goffi <goffi@goffi.org>
parents:
3154
diff
changeset
|
580 return servers |