view cagou/core/platform_/android.py @ 382:c7f1176cd2a9

android: handle Android specific content (wich `content:` scheme) in intent manager: when an Android content is received, it is dumped to a temporary file, and a friendly filename is used when possible.
author Goffi <goffi@goffi.org>
date Tue, 04 Feb 2020 20:47:17 +0100
parents eb3f622d8791
children a5457241c17f
line wrap: on
line source

#!/usr/bin/env python3

# Cagou: desktop/mobile frontend for Salut à Toi XMPP client
# Copyright (C) 2016-2020 Jérôme Poisson (goffi@goffi.org)

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import sys
import os
import socket
import json
from functools import partial
from urllib.parse import urlparse
from pathlib import Path
import shutil
import mimetypes
from jnius import autoclass, cast
from android import activity
from sat.core.i18n import _
from sat.core import log as logging
from sat_frontends.tools import jid
from cagou.core.constants import Const as C
from cagou.core import dialog
from cagou import G
from kivy.clock import Clock
from .base import Platform as BasePlatform


log = logging.getLogger(__name__)

service = autoclass('org.salutatoi.cagou.ServiceBackend')
PythonActivity = autoclass('org.kivy.android.PythonActivity')
mActivity = PythonActivity.mActivity
Intent = autoclass('android.content.Intent')
AndroidString = autoclass('java.lang.String')
Uri = autoclass('android.net.Uri')
ImagesMedia = autoclass('android.provider.MediaStore$Images$Media')
AudioMedia = autoclass('android.provider.MediaStore$Audio$Media')
VideoMedia = autoclass('android.provider.MediaStore$Video$Media')

DISPLAY_NAME = '_display_name'
DATA = '_data'


STATE_RUNNING = b"running"
STATE_PAUSED = b"paused"
STATE_STOPPED = b"stopped"
SOCKET_DIR = "/data/data/org.salutatoi.cagou/"
SOCKET_FILE = ".socket"
INTENT_EXTRA_ACTION = AndroidString("org.salut-a-toi.IntentAction")


class Platform(BasePlatform):

    def __init__(self):
        super().__init__()
        # cache for callbacks to run when profile is plugged
        self.cache = []

    def init_platform(self):
        # sys.platform is "linux" on android by default
        # so we change it to allow backend to detect android
        sys.platform = "android"
        C.PLUGIN_EXT = 'pyc'

    def on_host_init(self, host):
        argument = ''
        service.start(mActivity, argument)

        activity.bind(on_new_intent=self.on_new_intent)
        self.cache.append((self.on_new_intent, mActivity.getIntent()))
        host.addListener('profilePlugged', self.onProfilePlugged)
        local_dir = Path(host.getConfig('', 'local_dir')).resolve()
        self.tmp_dir = local_dir / 'tmp'
        # we assert to avoid disaster if `/ 'tmp'` is removed by mistake on the line
        # above
        assert self.tmp_dir.resolve() != local_dir
        # we reset tmp dir on each run, to be sure that there is no residual file
        if self.tmp_dir.exists():
            shutil.rmtree(self.tmp_dir)
        self.tmp_dir.mkdir(0o700)

    def on_initFrontendState(self):
        # XXX: we use a separated socket instead of bridge because if we
        #      try to call a bridge method in on_pause method, the call data
        #      is not written before the actual pause
        s = self._frontend_status_socket = socket.socket(
            socket.AF_UNIX, socket.SOCK_STREAM)
        s.connect(os.path.join(SOCKET_DIR, SOCKET_FILE))
        s.sendall(STATE_RUNNING)

    def profileAutoconnectGetCb(self, profile=None):
        if profile is not None:
            G.host.options.profile = profile
        G.host.postInit()

    def profileAutoconnectGetEb(self, failure_):
        log.error(f"Error while getting profile to autoconnect: {failure_}")
        G.host.postInit()

    def do_postInit(self):
        G.host.bridge.profileAutoconnectGet(
            callback=self.profileAutoconnectGetCb,
            errback=self.profileAutoconnectGetEb
        )
        return False

    def onProfilePlugged(self, profile):
        log.debug("ANDROID profilePlugged")
        G.host.bridge.setParam(
            "autoconnect_backend", C.BOOL_TRUE, "Connection", -1, profile,
            callback=lambda: log.info(f"profile {profile} autoconnection set"),
            errback=lambda: log.error(f"can't set {profile} autoconnection"))
        for method, *args in self.cache:
            method(*args)
        del self.cache
        G.host.removeListener("profilePlugged", self.onProfilePlugged)

    def on_pause(self):
        G.host.sync = False
        self._frontend_status_socket.sendall(STATE_PAUSED)
        return True

    def on_resume(self):
        self._frontend_status_socket.sendall(STATE_RUNNING)
        G.host.sync = True

    def on_stop(self):
        self._frontend_status_socket.sendall(STATE_STOPPED)
        self._frontend_status_socket.close()

    def on_key_back_root(self):
        PythonActivity.moveTaskToBack(True)
        return True

    def on_key_back_share(self, share_widget):
        share_widget.close()
        PythonActivity.moveTaskToBack(True)
        return True

    def _disconnect(self, profile):
        G.host.bridge.setParam(
            "autoconnect_backend", C.BOOL_FALSE, "Connection", -1, profile,
            callback=lambda: log.info(f"profile {profile} autoconnection unset"),
            errback=lambda: log.error(f"can't unset {profile} autoconnection"))
        G.host.profiles.unplug(profile)
        G.host.bridge.disconnect(profile)
        G.host.app.showProfileManager()
        G.host.closeUI()

    def _on_disconnect(self):
        current_profile = next(iter(G.host.profiles))
        wid = dialog.ConfirmDialog(
            title=_("Are you sure to disconnect?"),
            message=_(
                "If you disconnect the current user ({profile}), you won't receive "
                "any notification until you connect it again, is this really what you "
                "want?").format(profile=current_profile),
            yes_cb=partial(self._disconnect, profile=current_profile),
            no_cb=G.host.closeUI,
        )
        G.host.showExtraUI(wid)

    def on_extra_menu_init(self, extra_menu):
        extra_menu.addItem(_('disconnect'), self._on_disconnect)

    def updateParamsExtra(self, extra):
        # on Android, we handle autoconnection automatically,
        # user must not modify those parameters
        extra.update(
            {
                "ignore": [
                    ["Connection", "autoconnect_backend"],
                    ["Connection", "autoconnect"],
                    ["Connection", "autodisconnect"],
                ],
            }
        )

    def getColDataFromUri(self, uri, col_name):
        cursor = mActivity.getContentResolver().query(uri, None, None, None, None)
        if cursor is None:
            return None
        try:
            cursor.moveToFirst()
            col_idx = cursor.getColumnIndex(col_name);
            if col_idx == -1:
                return None
            return cursor.getString(col_idx)
        finally:
            cursor.close()

    def getFilenameFromUri(self, uri, media_type):
        filename = self.getColDataFromUri(uri, DISPLAY_NAME)
        if filename is None:
            uri_p = Path(uri.toString())
            filename = uri_p.name or "unnamed"
            if not uri_p.suffix and media_type:
                suffix = mimetypes.guess_extension(media_type, strict=False)
                if suffix:
                    filename = filename + suffix
        return filename

    def getPathFromUri(self, uri):
        # FIXME: using DATA is not recommended (and DATA is deprecated)
        # we should read directly the file with
        # ContentResolver#openFileDescriptor(Uri, String)
        path = self.getColDataFromUri(uri, DATA)
        return uri.getPath() if path is None else path

    def on_new_intent(self, intent):
        log.debug("on_new_intent")
        action = intent.getAction();
        intent_type = intent.getType();
        if action == Intent.ACTION_MAIN:
            action_str = intent.getStringExtra(INTENT_EXTRA_ACTION)
            if action_str is not None:
                action = json.loads(action_str)
                log.debug(f"Extra action found: {action}")
                action_type = action.get('type')
                if action_type == "open":
                    try:
                        widget = action['widget']
                        target = action['target']
                    except KeyError as e:
                        log.warning(f"incomplete action {action}: {e}")
                    else:
                        current_profile = next(iter(G.host.profiles))
                        Clock.schedule_once(
                            lambda *args: G.host.doAction(
                                widget, jid.JID(target), [current_profile]),
                            0)
                else:
                    log.warning(f"unexpected action: {action}")

            text = None
            uri = None
            path = None
        elif action == Intent.ACTION_SEND:
            # we have receiving data to share, we parse the intent data
            # and show the share widget
            data = {}
            text = intent.getStringExtra(Intent.EXTRA_TEXT)
            if text is not None:
                data['text'] = text

            item = intent.getParcelableExtra(Intent.EXTRA_STREAM)
            if item is not None:
                uri = cast('android.net.Uri', item)
                if uri.getScheme() == 'content':
                    # Android content, we'll dump it to a temporary file
                    filename = self.getFilenameFromUri(uri, intent_type)
                    filepath = self.tmp_dir / filename
                    input_stream = mActivity.getContentResolver().openInputStream(uri)
                    buff = bytearray(4096)
                    with open(filepath, 'wb') as f:
                        while True:
                            ret = input_stream.read(buff, 0, 4096)
                            if ret != -1:
                                f.write(buff[:ret])
                            else:
                                break
                    input_stream.close()
                    data['path'] = path = str(filepath)
                else:
                    data['uri'] = uri.toString()
                    path = self.getPathFromUri(uri)
                    if path is not None and path not in data:
                        data['path'] = path
            else:
                uri = None
                path = None


            Clock.schedule_once(lambda *args: G.host.share(intent_type, data), 0)
        else:
            text = None
            uri = None
            path = None

        msg = (f"NEW INTENT RECEIVED\n"
               f"type: {intent_type}\n"
               f"action: {action}\n"
               f"text: {text}\n"
               f"uri: {uri}\n"
               f"path: {path}")

        log.debug(msg)

    def open_url(self, url, wid=None):
        parsed_url = urlparse(url)
        if parsed_url.scheme == "geo":
            intent = Intent(Intent.ACTION_VIEW)
            intent.setData(Uri.parse(url))
            if mActivity.getPackageManager() is not None:
                activity = cast('android.app.Activity', mActivity)
                activity.startActivity(intent)
        else:
            super().open_url(self, url, wid)