Java Question ContentProvider get the Context (packagename) from the calling Application

DonManfred

Expert
Licensed User
Longtime User
i am using a contentprovider
B4X:
public class dataprovider extends ContentProvider {
and i use this code to get a list of allowed apps from the Contentprovider host apps Manifest. This is working fine.

B4X:
Context context = getContext();
        ApplicationInfo ai;
        try {
            ai = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
            Object value = (Object)ai.metaData.getString("allowedapps");
            String dummy = (String) value;
            String[] allowedapps = dummy.split(";");
            allowed = allowedapps;
            for (int i = 0; i < allowedapps.length; i++) {
                //BA.Log(allowedapps[i]);
            }
        } catch (NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


If i now query data from within another app it also gets the context to the Hostapp.
B4X:
   public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        Context context = getContext();
        String sender = new String (context.getPackageName());
        boolean ok = false;
        for (int i = 0; i < allowed.length; i++) {
          String app = allowed[i];
          if (app.equals(sender)){
              ok = true;
              BA.Log(sender+" is allowed");
          }
      }
        if (ok == false){
            throw new IllegalArgumentException("Unsupported Senderapp: " + uri);
        }
I was expecting to get the packagename from the client app to appear but it always print the packagename from the Host app. But ok, the contentprovider itself is in App A and this is the Context i am getting.

I was hoping that i could get the context from the app using the Contentresolver to query/write to it.

How could i get the correct Context (the app which is using the Content Resolver to query/insert something?

Any help is highly welcome ;-)
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
Top