Question

I am trying to design a plugin system for my Activity. To do that I created new class called PluginClass, external to my project and into a different package, that I load with DEXClassLoader from my main app.

I can call all the PluginClass methods from the main app easily and it works fine. The problem come out when I try to access the resources of the PluginClass project. Let me explain: the PluginClass inflates a local layout into a main activity object with the method getInflatedBox done like that:

public void getInflatedBox(ViewGroup root) {
        View.inflate(context, R.layout.boxeslayout, root);
        this.imgBoxX = (ImageView) root.findViewById(R.id.imgBoxX);
        this.imgNotify = (ImageView) root.findViewById(R.id.imgNotify);
        this.txtNotify = (TextView) root.findViewById(R.id.txtNotify);
        this.txtBoxXName = (TextView) root.findViewById(R.id.txtBoxXName);
        this.llShadow = (LinearLayout) root.findViewById(R.id.llShadow);
}

that should inflate the layout "boxeslayout" into the root object, coming from main activity with this call (I removed the try/catch to make it readable):

Method getInflatedBox = null;
BoxX root;
Class boxesPlugin = getPlugin();     // Another main activity method, 
                                     // where I obtain the class with DexClassLoader
pgItem = boxesPlugin.newInstance();
getInflatedBox = boxesPlugin.getMethod("getInflatedBox", ViewGroup.class);
getInflatedBox.invoke( pgItem, root );

where BoxX is a custom class extending LinearLayout.

Here is where the problerm come out: I get a layout inflated into root instance, but not the layout I wanted. Practically the R.layout.boxeslayout access the local main activity resources, inflating into root the layout from the activity that happens to have the same id value.

As my PluginClass is not an activity, I can't use context.getResources. Any idea how I could force the methods of pluginclass to access the plugin resources?

thank you very much for any help.

Était-ce utile?

La solution

OK, not sure there is anyone interested in, as I had so few viewers in more than two weeks, but I figured out a solution, and maybe it could be usefull for someone else, so I'm posting here.

The point is that I cannot access plugin R class, so I have to find a workaround into the method getInflatedBox:

Resources pgResources = null;
Context pgCtx = actContext.createPackageContext(packName, Context.CONTEXT_IGNORE_SECURITY);
pgResources = pgCtx.getResources();
int lLayoutID = pgResources.getIdentifier(layoutName, "layout", pgPackName);
View.inflate(pgCtx , lLayoutID, root);

where actContext is the context of the calling main activity.

Practically I obtain the right ID for the layout I want to inflate obtaining a pointer to the actual resources of the plugin throught the local context pgCtx, built from the plugin package pgPackName.

Hope this helps.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top