How to inject custom code in DHIS2 Android Capture App/ Customize DHIS2 Android Capture App

Hi community

Could you assist me in incorporating custom code into the DHSI2 Android capture app? Our intention is to populate specific fields within the program stage from other android App. For instance, when the user clicks on the person contact fields, it would activate the Contact App to display the contact list. Once a contact is chosen, the system would then retrieve the phone number and display name information, seamlessly populating the designated field in the DHSI2 Android capture app. My challenge lies in determining the process to inject this code effectively into the DHSI2 app.

Best Regards
Beruck


cc: @Gassim

Hi @Beruck

The @dhis2-android team would be best to answer this type of question. It’s an interesting idea to have the Contacts app in android easily populate the dhis2 capture app, so the idea is to get the DHIS2 Capture android app to import contact information from the phone into the system just the same way as when social and messenger apps ask for permission to make a copy of phone contacts.

Would you please share more about the use case and why this is a necessary feature? There might be a way to inject a code into an app but this means the app will not be the secure android app which receives updates so a better approach is to either create a feature request for the team to add it as a feature, or create an app that will populate the information independently and directly into the dhis2 server.

My point is that injecting a code is 100% only a temporary solution and doesn’t work with the secure app. :slight_smile:

Thanks!

1 Like

Hi @Beruck
It is an interesting idea, I think the best way is to open a feature request, but you always can create your own app and use the SDK or fork the Android Capture app, modify and publish it.
For now inject custom code into the Android Capture App is not available and we need to be very careful when implementing this for security reasons.
Thanks!

1 Like

Hi @andresmr,
Can you assist me or point me to How to start adding new buttons Which package or class is responsible for this task.
Please
Best Beruck

Hi @Beruck, can you be more specific?. Do you mean buttons by code, existing or new buttons, which is the use case?, do you have a fork or a pull request that we can check to understand which is your objective?

1 Like

Hi @Andres, I had some personal problems and was not able to respond quickly, I forked the dhis2-android-capture-app, my use case, and I want to add a new button at the activity_program_event_detail that will call the Contact App and get the result (Contact Name and Phone number) and populate on Program event Detail with specifically on Data elements (contact name and contatc_Phoone Number), like the coordinate Data type call Map app get the latitude and longitude and populate in the coordinate fields. I want the new button to be in the middle of the formal stage and near the contact data element
The challenge are
1.I am now able to locate where to insert the new button in activity_program_event_detail.xml file
2. Not able to know the android Id for the two data elements, how can I get the ID of the data element

The snipe code I want to insert is as follows

public void pickContact(View view) {
    Intent pickContactIntent = new Intent(Intent.ACTION_PICK,  Uri.parse("content://contacts"));
    pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
    // Verify it resolves
    PackageManager packageManager = getPackageManager();
    List<ResolveInfo> activities = packageManager.queryIntentActivities(pickContactIntent, 0);
    boolean isIntentSafe = activities.size() > 0;


        startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);


}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request it is that we're responding to    

    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_CONTACT_REQUEST) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            // Get the URI that points to the selected contact
            Uri contactUri = data.getData();
            // We only need the NUMBER column, because there will be only one row in the result
            String[] projection = {ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME};

            // Perform the query on the contact to get the NUMBER column
            // We don't need a selection or sort order (there's only one result for the given URI)
            // CAUTION: The query() method should be called from a separate thread to avoid blocking
            // your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
            // Consider using CursorLoader to perform the query.
            Cursor cursor = getContentResolver()
                    .query(contactUri, projection, null, null, null);
            cursor.moveToFirst();

            // Retrieve the phone number from the NUMBER column
            int column = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            int column1 = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
            String number = cursor.getString(column);
            String named = cursor.getString(column1);
            EditText editText = (EditText) findViewById(R.id.contact_phone_number);
            editText.setText(number);
            TextView displayName= findViewById(R.id.contactName);
            displayName.setText(named);
          
        }
    }
}

Thanks
Beruck

1 Like

Hi @andresmr, how are you, is there anything you want to suggest on my problems?

Best
Beruck

Hi @Beruck,

What you need to modify is the form.
I would modify the form_phone_number.xml which is the layout for data elements of value type PHONE_NUMBER and add the button next to the one for launching the call.

You can copy/follow the same format as this call button and modify the UiEventType enum to add a new event called CONTACT for example. Then handle it in the UiEventFactoryImpl class and have a new RecyclerViewUiEvents data class and finally in the “FormView” you can launch the requestActivityForResult. All info for the data element should be in the RecyclerViewUiEvent class.

Hope this helps a little bit :slight_smile: