How to select a contact in NativeScript Android

While working on an app, I came across a requirement to select contact from the phone. I wanted to open the contact picker and select the contact but NativeScript does not have it inbuilt in the platform.

So, I decided to try my hand and here is the code:

    var intent = new android.content.Intent(android.content.Intent.ACTION_PICK, android.net.Uri.parse("content://contacts"));

    intent.setType(android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);

    application.android.foregroundActivity.startActivityForResult(intent, 1);

    application.android.onActivityResult = function (requestCode, resultCode, data) {

    var contactUri = data.getData();

    // We only need the NUMBER column, because there will be only one row in the result
    var projection = [android.provider.ContactsContract.CommonDataKinds.Phone.NUMBER];

    // 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)

    var cursor = utils.ad.getApplicationContext().getContentResolver().query(contactUri, projection, null, null, null);

    cursor.moveToFirst();

    // Retrieve the phone number from the NUMBER column
    var column = cursor.getColumnIndex(android.provider.ContactsContract.CommonDataKinds.Phone.NUMBER);

    var number = cursor.getString(column);

Put the above code on button tap event and you are good to go.