chroa chroa -3 years ago 97
Android Question

How to get package name in a Cordova plugin dynamically?

I have developed a Cordova plugin that opens a new view containing a Twitter search timeline. The following is the class that builds it:

package com.example.hello;

import com.twitter.sdk.android.tweetui.TweetTimelineListAdapter;
import com.twitter.sdk.android.tweetui.SearchTimeline;
import android.os.Bundle;
import android.app.ListActivity;

public class TimelineListActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
Bundle recdData = getIntent().getExtras();
String hashtag = recdData.getString("hashtag");

super.onCreate(savedInstanceState);
setContentView(R.layout.timeline);

SearchTimeline searchTimeline = new SearchTimeline.Builder().query(hashtag).build();
final TweetTimelineListAdapter adapter = new TweetTimelineListAdapter(this, searchTimeline);
setListAdapter(adapter);
}
}


I was having some difficulties compiling the project due to R package not being found. The only solution I've found was for my class to be located near the MainActivity.java file and to be in the same package as the MainActivity, therefore the:

package com.example.hello;


I have also added a line to plugin.xml in order to put my TimelineListActivity.java in the same folder as MainActivity.java, whenever the project was built:

<source-file src="src/android/TimelineListActivity.java" target-dir="src/com/example/hello" />


It works like this but there's a problem. Since this is a plugin, I want to add it to other Cordova projects and I don't know their packages names. The MainActivity.java file is generated when adding the android platform to the Cordova project. Is there a way that I can get the package name dynamically? Or is there a better approach to my structure in order to make this work?

Answer Source

My initial question was probably dumb in the first place but here's the solution:

I have removed the usage of R package, therefore eliminating the project's dependency and no longer need to know the project's package name. I get the resource Id from the caller method (as it was convenient for me since the class extended CordovaPlugin).

private void showTimeline(final String query, final Activity activity, final CallbackContext callbackContext) {
    try {
        Context context = cordova.getActivity().getApplicationContext();
        Intent intent = new Intent(context, Class.forName("com.manifestwebdesign.twitterconnect.TimelineListActivity"));
        intent.putExtra("query", query);
        int resourceId = cordova.getActivity().getResources().getIdentifier("timeline", "layout", cordova.getActivity().getPackageName());
        intent.putExtra("resourceid", resourceId);
        cordova.startActivityForResult(this, intent, 1);
        callbackContext.success();
    }
    catch(Exception e) {
        System.err.println("Exception: " + e.getMessage());
        callbackContext.error(e.getMessage());
    }

}

Passed it through the intent, therefore avoiding the usage of R.

public class TimelineListActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        Bundle recdData = getIntent().getExtras();
        String query = recdData.getString("query");
        int resourceId = recdData.getInt("resourceid");

        super.onCreate(savedInstanceState);
        setContentView(resourceId);

        SearchTimeline searchTimeline = new SearchTimeline.Builder().query(query).build();
        final TweetTimelineListAdapter adapter = new TweetTimelineListAdapter(this, searchTimeline);
        setListAdapter(adapter);
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download