Skip to content

Allow adding ContentProviders #1964

@alecvn

Description

@alecvn

While trying to get the Android camera to work, I found a few hoops I needed to jump through to get the FileProvider accessible. So I was wondering if we couldn't somehow make it configurable, sort of like Activities?

I needed to clone the python-for-android repo and add the following to pythonforandroid/bootstraps/sdl2/build/templates/AndroidManifest.tmpl.xml:

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="org.content.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
  <meta-data
          android:name="android.support.FILE_PROVIDER_PATHS"
          android:resource="@xml/file_paths" />
</provider>

I also needed to create the directory pythonforandroid/bootstraps/sdl2/build/src/main/res/xml, and in it the file file_paths.xml, with the following content:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="." />
</paths>

I also had to add support-compat-28.0.0.aar and had to use API 28 as the target for my build. All of this is required to save an image taken with the camera to external storage.

Having thought about all this, I think it might be tricky to get a generic solution for all ContentProviders, so maybe we can add some predefined ones to choose from, starting with FileProvider?

I'm not 100% sure what all this would entail, but I was thinking something along the lines of:

  1. Adding the following to the default.buildozer.spec:
    # (list) Java providers to the manifest.
    # Only FileProvider is supported at this point.
    # This requires support-compat-28.0.0.aar.
    #android.add_providers = android.support.v4.content.FileProvider
  1. Adding the following to the buildozer repo in targets/android.py:
    # add Java provider
    add_providers = config.getlist('app', 'android.add_providers', [])
    for provider in add_providers:
        build_cmd += [("--add-provider", provider)]
  1. Adding the following to pythonforandroid/bootstraps/common/build/build.py:

    ap.add_argument('--add-provider', dest='add_provider', action='append',
    help='Add this Java class as a Provider to the manifest.')

  2. Adding the following to pythonforandroid/bootstraps/sdl2/build/templates/AndroidManifest.tmpl.xml (although here we might require the authorities attribute to configurable as well, and in general this solution isn't fantastic):

    {% for p in args.add_provider  %}
        {% if p == "android.support.v4.content.FileProvider" %}
            <provider
	        android:name="android.support.v4.content.FileProvider"
	        android:authorities="org.content.fileprovider"
	        android:exported="false"
	        android:grantUriPermissions="true">
  	      <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
	    </provider>
        {% endif %}
    {% endfor %}
  1. And finally - the part that I have the least of an idea of how to implement - just creating the pythonforandroid/bootstraps/sdl2/build/src/main/res/xml/file_paths.xml file as described above.

I might be missing part of the puzzle here, but I'm happy to make pull requests if this is something that seems like we could implement and with some guidance for where I'm coming short.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions