diff cagou/core/menu.py @ 400:71f51198478c

android: handle runtime permissions: - some mandatory permissions are requested on Cagou start, Cagou won't start at all and display a warning message if they are not granted (we request 5 times before showing the warning) - transfer plugin can now use "android_permissions" in plugin_info, to indicate what is necessary. The permissions will then be requested, and the plugin widget won't be shown if they are not granted (and a warning not will then be displayed)
author Goffi <goffi@goffi.org>
date Sun, 09 Feb 2020 23:47:29 +0100
parents 1da3c379205b
children f7476818f9fb
line wrap: on
line diff
--- a/cagou/core/menu.py	Sun Feb 09 23:47:29 2020 +0100
+++ b/cagou/core/menu.py	Sun Feb 09 23:47:29 2020 +0100
@@ -227,28 +227,42 @@
                 )
             self.items_layout.add_widget(item)
 
+    def _onTransferCb(self, file_path, external, wid_cont, cleaning_cb=None):
+        if not external:
+            wid = wid_cont[0]
+            self._closeUI(wid)
+        self.callback(
+            file_path,
+            transfer_type = (C.TRANSFER_UPLOAD
+                if self.ids['upload_btn'].state == "down" else C.TRANSFER_SEND),
+            cleaning_cb=cleaning_cb,
+        )
+
+    def _check_plugin_permissions_cb(self, plug_info):
+        external = plug_info.get('external', False)
+        wid_cont = []
+        wid_cont.append(plug_info['factory'](
+            plug_info,
+            partial(self._onTransferCb, external=external, wid_cont=wid_cont),
+            self.cancel_cb,
+            self.profiles))
+        if not external:
+            G.host.showExtraUI(wid_cont[0])
+
     def do_callback(self, plug_info):
         self.parent.remove_widget(self)
         if self.callback is None:
             log.warning("TransferMenu callback is not set")
         else:
-            wid = None
-            external = plug_info.get('external', False)
-            def onTransferCb(file_path, cleaning_cb=None):
-                if not external:
-                    self._closeUI(wid)
-                self.callback(
-                    file_path,
-                    transfer_type = (C.TRANSFER_UPLOAD
-                        if self.ids['upload_btn'].state == "down" else C.TRANSFER_SEND),
-                    cleaning_cb=cleaning_cb,
-                )
-            wid = plug_info['factory'](plug_info,
-                                       onTransferCb,
-                                       self.cancel_cb,
-                                       self.profiles)
-            if not external:
-                G.host.showExtraUI(wid)
+            G.local_platform.check_plugin_permissions(
+                plug_info,
+                callback=partial(self._check_plugin_permissions_cb, plug_info),
+                errback=lambda: G.host.addNote(
+                    _("permission refused"),
+                    _("this transfer menu can't be used if you refuse the requested "
+                      "permission"),
+                    C.XMLUI_DATA_LVL_WARNING)
+            )
 
 
 class EntitiesSelectorMenu(SideMenu, FilterBehavior):