annotate cagou/core/platform_/android.py @ 381:eb3f622d8791

android: create/clean a temporary directory on startup: directory returned by tempfiles on Android is actually the root directory of the application, so it is not cleaned. To work around that a `/tmp` directory inside it is now created, and cleaned on startup if it already exists.
author Goffi <goffi@goffi.org>
date Tue, 04 Feb 2020 20:47:17 +0100
parents 9d3481663964
children c7f1176cd2a9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
322
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client
378
4d660b252487 dates update
Goffi <goffi@goffi.org>
parents: 377
diff changeset
4 # Copyright (C) 2016-2020 Jérôme Poisson (goffi@goffi.org)
322
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
5
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
10
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
15
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
18
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 import sys
342
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
20 import os
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
21 import socket
380
9d3481663964 android: handle new SàT specific "action" dict which may be attach to an intent:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
22 import json
377
b2a87239af25 android: platform specific menu to disconnect profile:
Goffi <goffi@goffi.org>
parents: 373
diff changeset
23 from functools import partial
380
9d3481663964 android: handle new SàT specific "action" dict which may be attach to an intent:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
24 from urllib.parse import urlparse
381
eb3f622d8791 android: create/clean a temporary directory on startup:
Goffi <goffi@goffi.org>
parents: 380
diff changeset
25 from pathlib import Path
eb3f622d8791 android: create/clean a temporary directory on startup:
Goffi <goffi@goffi.org>
parents: 380
diff changeset
26 import shutil
322
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from jnius import autoclass, cast
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from android import activity
377
b2a87239af25 android: platform specific menu to disconnect profile:
Goffi <goffi@goffi.org>
parents: 373
diff changeset
29 from sat.core.i18n import _
322
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from sat.core import log as logging
380
9d3481663964 android: handle new SàT specific "action" dict which may be attach to an intent:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
31 from sat_frontends.tools import jid
322
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from cagou.core.constants import Const as C
377
b2a87239af25 android: platform specific menu to disconnect profile:
Goffi <goffi@goffi.org>
parents: 373
diff changeset
33 from cagou.core import dialog
322
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 from cagou import G
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 from kivy.clock import Clock
342
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
36 from .base import Platform as BasePlatform
322
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
37
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
38
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 log = logging.getLogger(__name__)
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
40
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 service = autoclass('org.salutatoi.cagou.ServiceBackend')
357
4d3a0c4f2430 core: better back key (ESC) management:
Goffi <goffi@goffi.org>
parents: 345
diff changeset
42 PythonActivity = autoclass('org.kivy.android.PythonActivity')
4d3a0c4f2430 core: better back key (ESC) management:
Goffi <goffi@goffi.org>
parents: 345
diff changeset
43 mActivity = PythonActivity.mActivity
345
a3cefa7158dc android: open "geo:" URL using an Intent
Goffi <goffi@goffi.org>
parents: 342
diff changeset
44 Intent = autoclass('android.content.Intent')
a3cefa7158dc android: open "geo:" URL using an Intent
Goffi <goffi@goffi.org>
parents: 342
diff changeset
45 AndroidString = autoclass('java.lang.String')
a3cefa7158dc android: open "geo:" URL using an Intent
Goffi <goffi@goffi.org>
parents: 342
diff changeset
46 Uri = autoclass('android.net.Uri')
322
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 ImagesMedia = autoclass('android.provider.MediaStore$Images$Media')
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 AudioMedia = autoclass('android.provider.MediaStore$Audio$Media')
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 VideoMedia = autoclass('android.provider.MediaStore$Video$Media')
345
a3cefa7158dc android: open "geo:" URL using an Intent
Goffi <goffi@goffi.org>
parents: 342
diff changeset
50
322
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 DATA = '_data'
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
52
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
53
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 STATE_RUNNING = b"running"
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 STATE_PAUSED = b"paused"
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 STATE_STOPPED = b"stopped"
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 SOCKET_DIR = "/data/data/org.salutatoi.cagou/"
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 SOCKET_FILE = ".socket"
380
9d3481663964 android: handle new SàT specific "action" dict which may be attach to an intent:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
59 INTENT_EXTRA_ACTION = AndroidString("org.salut-a-toi.IntentAction")
322
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
60
342
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
61
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
62 class Platform(BasePlatform):
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
63
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
64 def __init__(self):
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
65 super().__init__()
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
66 # cache for callbacks to run when profile is plugged
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
67 self.cache = []
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
68
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
69 def init_platform(self):
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
70 # sys.platform is "linux" on android by default
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
71 # so we change it to allow backend to detect android
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
72 sys.platform = "android"
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
73 C.PLUGIN_EXT = 'pyc'
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
74
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
75 def on_host_init(self, host):
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
76 argument = ''
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
77 service.start(mActivity, argument)
322
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
78
342
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
79 activity.bind(on_new_intent=self.on_new_intent)
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
80 self.cache.append((self.on_new_intent, mActivity.getIntent()))
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
81 host.addListener('profilePlugged', self.onProfilePlugged)
381
eb3f622d8791 android: create/clean a temporary directory on startup:
Goffi <goffi@goffi.org>
parents: 380
diff changeset
82 local_dir = Path(host.getConfig('', 'local_dir')).resolve()
eb3f622d8791 android: create/clean a temporary directory on startup:
Goffi <goffi@goffi.org>
parents: 380
diff changeset
83 self.tmp_dir = local_dir / 'tmp'
eb3f622d8791 android: create/clean a temporary directory on startup:
Goffi <goffi@goffi.org>
parents: 380
diff changeset
84 # we assert to avoid disaster if `/ 'tmp'` is removed by mistake on the line
eb3f622d8791 android: create/clean a temporary directory on startup:
Goffi <goffi@goffi.org>
parents: 380
diff changeset
85 # above
eb3f622d8791 android: create/clean a temporary directory on startup:
Goffi <goffi@goffi.org>
parents: 380
diff changeset
86 assert self.tmp_dir.resolve() != local_dir
eb3f622d8791 android: create/clean a temporary directory on startup:
Goffi <goffi@goffi.org>
parents: 380
diff changeset
87 # we reset tmp dir on each run, to be sure that there is no residual file
eb3f622d8791 android: create/clean a temporary directory on startup:
Goffi <goffi@goffi.org>
parents: 380
diff changeset
88 if self.tmp_dir.exists():
eb3f622d8791 android: create/clean a temporary directory on startup:
Goffi <goffi@goffi.org>
parents: 380
diff changeset
89 shutil.rmtree(self.tmp_dir)
eb3f622d8791 android: create/clean a temporary directory on startup:
Goffi <goffi@goffi.org>
parents: 380
diff changeset
90 self.tmp_dir.mkdir(0o700)
342
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
91
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
92 def on_initFrontendState(self):
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
93 # XXX: we use a separated socket instead of bridge because if we
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
94 # try to call a bridge method in on_pause method, the call data
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
95 # is not written before the actual pause
370
1a12bbd80943 android: autoconnect profile using the new `profileAutoconnectGet` backend method:
Goffi <goffi@goffi.org>
parents: 357
diff changeset
96 s = self._frontend_status_socket = socket.socket(
1a12bbd80943 android: autoconnect profile using the new `profileAutoconnectGet` backend method:
Goffi <goffi@goffi.org>
parents: 357
diff changeset
97 socket.AF_UNIX, socket.SOCK_STREAM)
342
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
98 s.connect(os.path.join(SOCKET_DIR, SOCKET_FILE))
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
99 s.sendall(STATE_RUNNING)
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
100
370
1a12bbd80943 android: autoconnect profile using the new `profileAutoconnectGet` backend method:
Goffi <goffi@goffi.org>
parents: 357
diff changeset
101 def profileAutoconnectGetCb(self, profile=None):
1a12bbd80943 android: autoconnect profile using the new `profileAutoconnectGet` backend method:
Goffi <goffi@goffi.org>
parents: 357
diff changeset
102 if profile is not None:
1a12bbd80943 android: autoconnect profile using the new `profileAutoconnectGet` backend method:
Goffi <goffi@goffi.org>
parents: 357
diff changeset
103 G.host.options.profile = profile
1a12bbd80943 android: autoconnect profile using the new `profileAutoconnectGet` backend method:
Goffi <goffi@goffi.org>
parents: 357
diff changeset
104 G.host.postInit()
1a12bbd80943 android: autoconnect profile using the new `profileAutoconnectGet` backend method:
Goffi <goffi@goffi.org>
parents: 357
diff changeset
105
1a12bbd80943 android: autoconnect profile using the new `profileAutoconnectGet` backend method:
Goffi <goffi@goffi.org>
parents: 357
diff changeset
106 def profileAutoconnectGetEb(self, failure_):
1a12bbd80943 android: autoconnect profile using the new `profileAutoconnectGet` backend method:
Goffi <goffi@goffi.org>
parents: 357
diff changeset
107 log.error(f"Error while getting profile to autoconnect: {failure_}")
1a12bbd80943 android: autoconnect profile using the new `profileAutoconnectGet` backend method:
Goffi <goffi@goffi.org>
parents: 357
diff changeset
108 G.host.postInit()
1a12bbd80943 android: autoconnect profile using the new `profileAutoconnectGet` backend method:
Goffi <goffi@goffi.org>
parents: 357
diff changeset
109
1a12bbd80943 android: autoconnect profile using the new `profileAutoconnectGet` backend method:
Goffi <goffi@goffi.org>
parents: 357
diff changeset
110 def do_postInit(self):
1a12bbd80943 android: autoconnect profile using the new `profileAutoconnectGet` backend method:
Goffi <goffi@goffi.org>
parents: 357
diff changeset
111 G.host.bridge.profileAutoconnectGet(
1a12bbd80943 android: autoconnect profile using the new `profileAutoconnectGet` backend method:
Goffi <goffi@goffi.org>
parents: 357
diff changeset
112 callback=self.profileAutoconnectGetCb,
1a12bbd80943 android: autoconnect profile using the new `profileAutoconnectGet` backend method:
Goffi <goffi@goffi.org>
parents: 357
diff changeset
113 errback=self.profileAutoconnectGetEb
1a12bbd80943 android: autoconnect profile using the new `profileAutoconnectGet` backend method:
Goffi <goffi@goffi.org>
parents: 357
diff changeset
114 )
1a12bbd80943 android: autoconnect profile using the new `profileAutoconnectGet` backend method:
Goffi <goffi@goffi.org>
parents: 357
diff changeset
115 return False
1a12bbd80943 android: autoconnect profile using the new `profileAutoconnectGet` backend method:
Goffi <goffi@goffi.org>
parents: 357
diff changeset
116
342
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
117 def onProfilePlugged(self, profile):
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
118 log.debug("ANDROID profilePlugged")
370
1a12bbd80943 android: autoconnect profile using the new `profileAutoconnectGet` backend method:
Goffi <goffi@goffi.org>
parents: 357
diff changeset
119 G.host.bridge.setParam(
1a12bbd80943 android: autoconnect profile using the new `profileAutoconnectGet` backend method:
Goffi <goffi@goffi.org>
parents: 357
diff changeset
120 "autoconnect_backend", C.BOOL_TRUE, "Connection", -1, profile,
377
b2a87239af25 android: platform specific menu to disconnect profile:
Goffi <goffi@goffi.org>
parents: 373
diff changeset
121 callback=lambda: log.info(f"profile {profile} autoconnection set"),
b2a87239af25 android: platform specific menu to disconnect profile:
Goffi <goffi@goffi.org>
parents: 373
diff changeset
122 errback=lambda: log.error(f"can't set {profile} autoconnection"))
342
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
123 for method, *args in self.cache:
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
124 method(*args)
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
125 del self.cache
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
126 G.host.removeListener("profilePlugged", self.onProfilePlugged)
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
127
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
128 def on_pause(self):
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
129 G.host.sync = False
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
130 self._frontend_status_socket.sendall(STATE_PAUSED)
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
131 return True
322
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
132
342
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
133 def on_resume(self):
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
134 self._frontend_status_socket.sendall(STATE_RUNNING)
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
135 G.host.sync = True
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
136
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
137 def on_stop(self):
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
138 self._frontend_status_socket.sendall(STATE_STOPPED)
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
139 self._frontend_status_socket.close()
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
140
357
4d3a0c4f2430 core: better back key (ESC) management:
Goffi <goffi@goffi.org>
parents: 345
diff changeset
141 def on_key_back_root(self):
4d3a0c4f2430 core: better back key (ESC) management:
Goffi <goffi@goffi.org>
parents: 345
diff changeset
142 PythonActivity.moveTaskToBack(True)
4d3a0c4f2430 core: better back key (ESC) management:
Goffi <goffi@goffi.org>
parents: 345
diff changeset
143 return True
4d3a0c4f2430 core: better back key (ESC) management:
Goffi <goffi@goffi.org>
parents: 345
diff changeset
144
4d3a0c4f2430 core: better back key (ESC) management:
Goffi <goffi@goffi.org>
parents: 345
diff changeset
145 def on_key_back_share(self, share_widget):
4d3a0c4f2430 core: better back key (ESC) management:
Goffi <goffi@goffi.org>
parents: 345
diff changeset
146 share_widget.close()
4d3a0c4f2430 core: better back key (ESC) management:
Goffi <goffi@goffi.org>
parents: 345
diff changeset
147 PythonActivity.moveTaskToBack(True)
4d3a0c4f2430 core: better back key (ESC) management:
Goffi <goffi@goffi.org>
parents: 345
diff changeset
148 return True
4d3a0c4f2430 core: better back key (ESC) management:
Goffi <goffi@goffi.org>
parents: 345
diff changeset
149
377
b2a87239af25 android: platform specific menu to disconnect profile:
Goffi <goffi@goffi.org>
parents: 373
diff changeset
150 def _disconnect(self, profile):
b2a87239af25 android: platform specific menu to disconnect profile:
Goffi <goffi@goffi.org>
parents: 373
diff changeset
151 G.host.bridge.setParam(
b2a87239af25 android: platform specific menu to disconnect profile:
Goffi <goffi@goffi.org>
parents: 373
diff changeset
152 "autoconnect_backend", C.BOOL_FALSE, "Connection", -1, profile,
b2a87239af25 android: platform specific menu to disconnect profile:
Goffi <goffi@goffi.org>
parents: 373
diff changeset
153 callback=lambda: log.info(f"profile {profile} autoconnection unset"),
b2a87239af25 android: platform specific menu to disconnect profile:
Goffi <goffi@goffi.org>
parents: 373
diff changeset
154 errback=lambda: log.error(f"can't unset {profile} autoconnection"))
b2a87239af25 android: platform specific menu to disconnect profile:
Goffi <goffi@goffi.org>
parents: 373
diff changeset
155 G.host.profiles.unplug(profile)
b2a87239af25 android: platform specific menu to disconnect profile:
Goffi <goffi@goffi.org>
parents: 373
diff changeset
156 G.host.bridge.disconnect(profile)
b2a87239af25 android: platform specific menu to disconnect profile:
Goffi <goffi@goffi.org>
parents: 373
diff changeset
157 G.host.app.showProfileManager()
b2a87239af25 android: platform specific menu to disconnect profile:
Goffi <goffi@goffi.org>
parents: 373
diff changeset
158 G.host.closeUI()
b2a87239af25 android: platform specific menu to disconnect profile:
Goffi <goffi@goffi.org>
parents: 373
diff changeset
159
b2a87239af25 android: platform specific menu to disconnect profile:
Goffi <goffi@goffi.org>
parents: 373
diff changeset
160 def _on_disconnect(self):
b2a87239af25 android: platform specific menu to disconnect profile:
Goffi <goffi@goffi.org>
parents: 373
diff changeset
161 current_profile = next(iter(G.host.profiles))
b2a87239af25 android: platform specific menu to disconnect profile:
Goffi <goffi@goffi.org>
parents: 373
diff changeset
162 wid = dialog.ConfirmDialog(
b2a87239af25 android: platform specific menu to disconnect profile:
Goffi <goffi@goffi.org>
parents: 373
diff changeset
163 title=_("Are you sure to disconnect?"),
b2a87239af25 android: platform specific menu to disconnect profile:
Goffi <goffi@goffi.org>
parents: 373
diff changeset
164 message=_(
b2a87239af25 android: platform specific menu to disconnect profile:
Goffi <goffi@goffi.org>
parents: 373
diff changeset
165 "If you disconnect the current user ({profile}), you won't receive "
b2a87239af25 android: platform specific menu to disconnect profile:
Goffi <goffi@goffi.org>
parents: 373
diff changeset
166 "any notification until you connect it again, is this really what you "
b2a87239af25 android: platform specific menu to disconnect profile:
Goffi <goffi@goffi.org>
parents: 373
diff changeset
167 "want?").format(profile=current_profile),
b2a87239af25 android: platform specific menu to disconnect profile:
Goffi <goffi@goffi.org>
parents: 373
diff changeset
168 yes_cb=partial(self._disconnect, profile=current_profile),
b2a87239af25 android: platform specific menu to disconnect profile:
Goffi <goffi@goffi.org>
parents: 373
diff changeset
169 no_cb=G.host.closeUI,
b2a87239af25 android: platform specific menu to disconnect profile:
Goffi <goffi@goffi.org>
parents: 373
diff changeset
170 )
b2a87239af25 android: platform specific menu to disconnect profile:
Goffi <goffi@goffi.org>
parents: 373
diff changeset
171 G.host.showExtraUI(wid)
b2a87239af25 android: platform specific menu to disconnect profile:
Goffi <goffi@goffi.org>
parents: 373
diff changeset
172
b2a87239af25 android: platform specific menu to disconnect profile:
Goffi <goffi@goffi.org>
parents: 373
diff changeset
173 def on_extra_menu_init(self, extra_menu):
b2a87239af25 android: platform specific menu to disconnect profile:
Goffi <goffi@goffi.org>
parents: 373
diff changeset
174 extra_menu.addItem(_('disconnect'), self._on_disconnect)
b2a87239af25 android: platform specific menu to disconnect profile:
Goffi <goffi@goffi.org>
parents: 373
diff changeset
175
372
1481f09c9175 settings (android): don't expose autoconnect parameters:
Goffi <goffi@goffi.org>
parents: 370
diff changeset
176 def updateParamsExtra(self, extra):
1481f09c9175 settings (android): don't expose autoconnect parameters:
Goffi <goffi@goffi.org>
parents: 370
diff changeset
177 # on Android, we handle autoconnection automatically,
1481f09c9175 settings (android): don't expose autoconnect parameters:
Goffi <goffi@goffi.org>
parents: 370
diff changeset
178 # user must not modify those parameters
1481f09c9175 settings (android): don't expose autoconnect parameters:
Goffi <goffi@goffi.org>
parents: 370
diff changeset
179 extra.update(
1481f09c9175 settings (android): don't expose autoconnect parameters:
Goffi <goffi@goffi.org>
parents: 370
diff changeset
180 {
1481f09c9175 settings (android): don't expose autoconnect parameters:
Goffi <goffi@goffi.org>
parents: 370
diff changeset
181 "ignore": [
1481f09c9175 settings (android): don't expose autoconnect parameters:
Goffi <goffi@goffi.org>
parents: 370
diff changeset
182 ["Connection", "autoconnect_backend"],
1481f09c9175 settings (android): don't expose autoconnect parameters:
Goffi <goffi@goffi.org>
parents: 370
diff changeset
183 ["Connection", "autoconnect"],
1481f09c9175 settings (android): don't expose autoconnect parameters:
Goffi <goffi@goffi.org>
parents: 370
diff changeset
184 ["Connection", "autodisconnect"],
1481f09c9175 settings (android): don't expose autoconnect parameters:
Goffi <goffi@goffi.org>
parents: 370
diff changeset
185 ],
1481f09c9175 settings (android): don't expose autoconnect parameters:
Goffi <goffi@goffi.org>
parents: 370
diff changeset
186 }
1481f09c9175 settings (android): don't expose autoconnect parameters:
Goffi <goffi@goffi.org>
parents: 370
diff changeset
187 )
1481f09c9175 settings (android): don't expose autoconnect parameters:
Goffi <goffi@goffi.org>
parents: 370
diff changeset
188
342
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
189 def getPathFromUri(self, uri):
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
190 cursor = mActivity.getContentResolver().query(uri, None, None, None, None)
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
191 if cursor is None:
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
192 return uri.getPath()
322
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
193 else:
342
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
194 cursor.moveToFirst()
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
195 # FIXME: using DATA is not recommended (and DATA is deprecated)
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
196 # we should read directly the file with
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
197 # ContentResolver#openFileDescriptor(Uri, String)
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
198 col_idx = cursor.getColumnIndex(DATA);
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
199 if col_idx == -1:
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
200 return uri.getPath()
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
201 return cursor.getString(col_idx)
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
202
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
203 def on_new_intent(self, intent):
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
204 log.debug("on_new_intent")
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
205 action = intent.getAction();
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
206 intent_type = intent.getType();
380
9d3481663964 android: handle new SàT specific "action" dict which may be attach to an intent:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
207 if action == Intent.ACTION_MAIN:
9d3481663964 android: handle new SàT specific "action" dict which may be attach to an intent:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
208 action_str = intent.getStringExtra(INTENT_EXTRA_ACTION)
9d3481663964 android: handle new SàT specific "action" dict which may be attach to an intent:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
209 if action_str is not None:
9d3481663964 android: handle new SàT specific "action" dict which may be attach to an intent:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
210 action = json.loads(action_str)
9d3481663964 android: handle new SàT specific "action" dict which may be attach to an intent:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
211 log.debug(f"Extra action found: {action}")
9d3481663964 android: handle new SàT specific "action" dict which may be attach to an intent:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
212 action_type = action.get('type')
9d3481663964 android: handle new SàT specific "action" dict which may be attach to an intent:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
213 if action_type == "open":
9d3481663964 android: handle new SàT specific "action" dict which may be attach to an intent:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
214 try:
9d3481663964 android: handle new SàT specific "action" dict which may be attach to an intent:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
215 widget = action['widget']
9d3481663964 android: handle new SàT specific "action" dict which may be attach to an intent:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
216 target = action['target']
9d3481663964 android: handle new SàT specific "action" dict which may be attach to an intent:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
217 except KeyError as e:
9d3481663964 android: handle new SàT specific "action" dict which may be attach to an intent:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
218 log.warning(f"incomplete action {action}: {e}")
9d3481663964 android: handle new SàT specific "action" dict which may be attach to an intent:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
219 else:
9d3481663964 android: handle new SàT specific "action" dict which may be attach to an intent:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
220 current_profile = next(iter(G.host.profiles))
9d3481663964 android: handle new SàT specific "action" dict which may be attach to an intent:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
221 Clock.schedule_once(
9d3481663964 android: handle new SàT specific "action" dict which may be attach to an intent:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
222 lambda *args: G.host.doAction(
9d3481663964 android: handle new SàT specific "action" dict which may be attach to an intent:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
223 widget, jid.JID(target), [current_profile]),
9d3481663964 android: handle new SàT specific "action" dict which may be attach to an intent:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
224 0)
9d3481663964 android: handle new SàT specific "action" dict which may be attach to an intent:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
225 else:
9d3481663964 android: handle new SàT specific "action" dict which may be attach to an intent:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
226 log.warning(f"unexpected action: {action}")
9d3481663964 android: handle new SàT specific "action" dict which may be attach to an intent:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
227
9d3481663964 android: handle new SàT specific "action" dict which may be attach to an intent:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
228 text = None
9d3481663964 android: handle new SàT specific "action" dict which may be attach to an intent:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
229 uri = None
9d3481663964 android: handle new SàT specific "action" dict which may be attach to an intent:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
230 path = None
9d3481663964 android: handle new SàT specific "action" dict which may be attach to an intent:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
231 elif action == Intent.ACTION_SEND:
342
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
232 # we have receiving data to share, we parse the intent data
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
233 # and show the share widget
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
234 data = {}
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
235 text = intent.getStringExtra(Intent.EXTRA_TEXT)
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
236 if text is not None:
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
237 data['text'] = text
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
238 item = intent.getParcelableExtra(Intent.EXTRA_STREAM)
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
239 if item is not None:
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
240 uri = cast('android.net.Uri', item)
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
241 data['uri'] = uri.toString()
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
242 path = self.getPathFromUri(uri)
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
243 if path is not None:
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
244 data['path'] = path
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
245 else:
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
246 uri = None
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
247 path = None
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
248
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
249 Clock.schedule_once(lambda *args: G.host.share(intent_type, data), 0)
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
250 else:
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
251 text = None
322
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
252 uri = None
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
253 path = None
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
254
342
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
255 msg = (f"NEW INTENT RECEIVED\n"
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
256 f"type: {intent_type}\n"
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
257 f"action: {action}\n"
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
258 f"text: {text}\n"
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
259 f"uri: {uri}\n"
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
260 f"path: {path}")
322
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents:
diff changeset
261
342
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents: 322
diff changeset
262 log.debug(msg)
345
a3cefa7158dc android: open "geo:" URL using an Intent
Goffi <goffi@goffi.org>
parents: 342
diff changeset
263
a3cefa7158dc android: open "geo:" URL using an Intent
Goffi <goffi@goffi.org>
parents: 342
diff changeset
264 def open_url(self, url, wid=None):
a3cefa7158dc android: open "geo:" URL using an Intent
Goffi <goffi@goffi.org>
parents: 342
diff changeset
265 parsed_url = urlparse(url)
a3cefa7158dc android: open "geo:" URL using an Intent
Goffi <goffi@goffi.org>
parents: 342
diff changeset
266 if parsed_url.scheme == "geo":
a3cefa7158dc android: open "geo:" URL using an Intent
Goffi <goffi@goffi.org>
parents: 342
diff changeset
267 intent = Intent(Intent.ACTION_VIEW)
a3cefa7158dc android: open "geo:" URL using an Intent
Goffi <goffi@goffi.org>
parents: 342
diff changeset
268 intent.setData(Uri.parse(url))
a3cefa7158dc android: open "geo:" URL using an Intent
Goffi <goffi@goffi.org>
parents: 342
diff changeset
269 if mActivity.getPackageManager() is not None:
a3cefa7158dc android: open "geo:" URL using an Intent
Goffi <goffi@goffi.org>
parents: 342
diff changeset
270 activity = cast('android.app.Activity', mActivity)
a3cefa7158dc android: open "geo:" URL using an Intent
Goffi <goffi@goffi.org>
parents: 342
diff changeset
271 activity.startActivity(intent)
a3cefa7158dc android: open "geo:" URL using an Intent
Goffi <goffi@goffi.org>
parents: 342
diff changeset
272 else:
a3cefa7158dc android: open "geo:" URL using an Intent
Goffi <goffi@goffi.org>
parents: 342
diff changeset
273 super().open_url(self, url, wid)