Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Having Provider authority in the app project

An android library project contains a few providers whose authority is defined like the following in a contract class :

public static final String CONTENT_AUTHORITY = "my.com.library.providers.tester";
private static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);

Now there are a lot of app projects which uses this library project. The problem I am currently having is that for every app project, I need to have a separate branch in the library project for every app just for having a unique content authority. This is creating some version management problems (like propagating features/bug fixes from one branch to every other branch etc.,). Instead I would like to delegate the responsibility of defining the content authority to the app project. Is there a way to accomplish this?

like image 990
500865 Avatar asked May 28 '12 22:05

500865


People also ask

Why does the content provider need to be declared in the Android manifest?

A content provider is a subclass of ContentProvider that supplies structured access to data managed by the application. All content providers in your application must be defined in a <provider> element in the manifest file; otherwise, the system is unaware of them and doesn't run them.

What is the purpose of provider in Android content?

A content provider manages access to a central repository of data. A provider is part of an Android application, which often provides its own UI for working with the data. However, content providers are primarily intended to be used by other applications, which access the provider using a provider client object.

How can I use custom content provider in another Android app?

After creating the content provider , specify the content provider in the manifest file. You can mention content provider using the tag. Inside the provider tag dont forget to mention the name and authorities attributes. This declaration should be ..

What is the relation between content provider and content resolver in Android applications?

Content Resolver resolves a URI to a specific Content provider. Content Provider provides an interface to query content. The way to query a content provider is contentResolverInstance.


2 Answers

I know this is an old topic but came across this issue today and we have been developing for quite some time so was not ready to go through all statics in our Content Provider Contract and change them, also because our content provider and DB are generated by the Mechanoid Plugin for Eclipse (Yes, I am also the author! :))

The solution I came up with was to add a static initializer to our generated contract that uses reflection to look up a class and use a static CONTENT_AUTHORITY field on that if it exists, if not fall back to a default:

public class QuxContract  {
    public static final String CONTENT_AUTHORITY = initAuthority();

    private static String initAuthority() {
        String authority = "com.example.app.data.qux";

        try {

            ClassLoader loader = QuxContract.class.getClassLoader();

            Class<?> clz = loader.loadClass("com.example.app.data.QuxContentProviderAuthority");
            Field declaredField = clz.getDeclaredField("CONTENT_AUTHORITY");

            authority = declaredField.get(null).toString();
        } catch (ClassNotFoundException e) {} 
        catch (NoSuchFieldException e) {} 
        catch (IllegalArgumentException e) {
        } catch (IllegalAccessException e) {
        }

        return authority;
    }

    private static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);
// ...

Now in each project that links to the library project can provide their own authority:

package com.example.app.data;

public class QuxContentProviderAuthority {
    public static final String CONTENT_AUTHORITY = "com.example.app.data.baz";
}

Also, do not forget to change the authority in your manifest also

like image 69
Ian Warwick Avatar answered Oct 17 '22 23:10

Ian Warwick


The app is the only one that absolutely needs to know about the authority, as it is the one that declares the <provider> in the manifest with the android:authorities attribute.

Hence, in principle, it should "just work", so long as you remove all authority-specific logic from the provider, such as:

  • those static data members (which now move to the hosting app)
  • UriMatcher (roll something yourself that does not examine the authority, but focuses on the rest of the Uri)

If, for some reason, you are absolutely sure that your provider needs to know its authority, then the app will have to supply that to the provider before the provider is used for real work. Possible ways to do that include:

  • Since a ContentProvider is a natural singleton, assign it to a static data member, and then supply the authority string to it by a custom method from a custom Application class (as providers are initialized first, so this should work)

  • If you are only supporting API Level 11+, have the custom Application class use call() on ContentResolver to supply the authority to the ContentProvider

  • Assume that the only real calls (e.g., to query(), insert()) are valid, and just lazy-initialize your authority based on what comes in on the first Uri you see

like image 29
CommonsWare Avatar answered Oct 18 '22 00:10

CommonsWare