GithubHelp home page GithubHelp logo

juniorise / android_content_provider Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nt4f04und/android_content_provider

0.0 0.0 0.0 149 KB

Flutter plugin to use ContentProvider/ContentResolver APIs on Android

License: BSD 2-Clause "Simplified" License

Java 7.45% Kotlin 31.17% Dart 61.38%

android_content_provider's Introduction

android_content_provider pub package

This plugin exposes ContentProvider and related ContentResolver APIs on Android.

Android 11 package visibility

Android 11 introduced a security mechanism that is called a package visibility.

If you are using AndroidContentResolver and trying to access some content provider within a package that is not visible by default, your app will fail to connect to it.

To fix this, add to your AndroidManifest.xml a new <queries> element:

<manifest>
...
    <queries>
        <package android:name="com.example.app" />
    </queries>
...
</manifest>

Configuring AndroidContentProvider

You may ignore these steps if you only want to use AndroidContentResolver.

  1. Use the FlutterEngineGroup, provided by the plugin, in your MainActivity to improve performance and reduce memory footprint from engine creation.

    This step is optional, but is strongly recommended.

  • Kotlin
import android.content.Context
import com.nt4f04und.android_content_provider.AndroidContentProvider
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine

class MainActivity : FlutterActivity() {
    override fun provideFlutterEngine(context: Context): FlutterEngine? {
        return AndroidContentProvider.getFlutterEngineGroup(this)
                .createAndRunDefaultEngine(this)
    }
}
  • Java
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.nt4f04und.android_content_provider.AndroidContentProvider;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;

public class MainActivity extends FlutterActivity {
    @Nullable
    @Override
    public FlutterEngine provideFlutterEngine(@NonNull Context context) {
        return AndroidContentProvider.Companion.getFlutterEngineGroup(this)
                .createAndRunDefaultEngine(this);
    }
}
  1. Subclass AndroidContentProvider in native code, setting the authority and Dart entrypoint name you want to use
  • Kotlin
package com.example.myapp // <-- replace this with your package

import com.nt4f04und.android_content_provider.AndroidContentProvider

class MyAndroidContentProvider : AndroidContentProvider() {
   override val authority: String = "com.example.myapp.MyAndroidContentProvider"
   override val entrypointName = "exampleContentProviderEntrypoint"
}
  • Java
package com.example.myapp; // <-- replace this with your package

import com.nt4f04und.android_content_provider.AndroidContentProvider;

import org.jetbrains.annotations.NotNull;

public class MyAndroidContentProvider extends AndroidContentProvider {
    @NotNull
    @Override
    public String getAuthority() {
        return "com.example.myapp.MyAndroidContentProvider";
    }

    @NotNull
    @Override
    public String getEntrypointName() {
        return "exampleContentProviderEntrypoint";
    }
}
  1. Declare your ContentProvider in the AndroidManifest.xml.
  • If you want to allow other apps to access the provider unconditionally
<provider
   android:name=".MyAndroidContentProvider"
   android:authorities="com.example.myapp.MyAndroidContentProvider"
   android:exported="true" />
  • If you want to make other apps declare <uses-permission>
<provider
   android:name=".MyAndroidContentProvider"
   android:authorities="com.example.myapp.MyAndroidContentProvider"
   android:exported="false"
   android:readPermission="com.example.myapp.permission.READ"
   android:writePermission="com.example.myapp.permission.WRITE" />
  1. Subclass AndroidContentProvider in Dart code and override needed methods
import 'package:android_content_provider/android_content_provider.dart';

class MyAndroidContentProvider extends AndroidContentProvider {
  MyAndroidContentProvider(String authority) : super(authority);

  @override
  Future<int> delete(
    String uri,
    String? selection,
    List<String>? selectionArgs,
  ) async {
    return 0;
  }

  @override
  Future<String?> getType(String uri) async {
    return null;
  }

  @override
  Future<String?> insert(String uri, ContentValues? values) async {
    return null;
  }

  @override
  Future<CursorData?> query(
    String uri,
    List<String>? projection,
    String? selection,
    List<String>? selectionArgs,
    String? sortOrder,
  ) async {
    return null;
  }

  @override
  Future<int> update(
    String uri,
    ContentValues? values,
    String? selection,
    List<String>? selectionArgs,
  ) async {
    return 0;
  }
}
  1. Create the Dart entrypoint
@pragma('vm:entry-point')
void exampleContentProviderEntrypoint() {
  MyAndroidContentProvider('com.example.myapp.MyAndroidContentProvider');
}

android_content_provider's People

Contributors

nt4f04und avatar ryanheise avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.