Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 44727298 authored by Casper Bonde's avatar Casper Bonde Committed by Pavlin Radoslavov
Browse files

MAP: Fix of cursor closed too early



A cursor variable was reused for a new query, which lead to
the cursor being closed too early.
This fix introduces a second cursor variable and appropriate
close calls.

Bug: 20227071
Bug: 20565456

Change-Id: I1fa271497a9eb37bb3e435b61dc5daa2b9727af4
Signed-off-by: default avatarCasper Bonde <c.bonde@samsung.com>
parent 3f7f217a
Loading
Loading
Loading
Loading
+26 −46
Original line number Diff line number Diff line
@@ -3227,7 +3227,12 @@ public class BluetoothMapContent {
            String phone,
            boolean incoming) {
        String contactId = null, contactName = null;
        String[] phoneNumbers = null;
        String[] phoneNumbers = new String[1];
        //
        // Use only actual phone number, because the MCE cannot know which
        // number the message is from.
        //
        phoneNumbers[0] = phone;
        String[] emailAddresses = null;
        Cursor p;

@@ -3242,61 +3247,36 @@ public class BluetoothMapContent {
        // Get the contact _ID and name
        p = mResolver.query(uri, projection, selection, null, orderBy);
        try {
            if (p != null && p.getCount() >= 1) {
                p.moveToFirst();
            if (p != null && p.moveToFirst()) {
                contactId = p.getString(p.getColumnIndex(Contacts._ID));
                contactName = p.getString(p.getColumnIndex(Contacts.DISPLAY_NAME));
            }

        } finally {
            if (p != null) p.close();
            close(p);
        }


        // Bail out if we are unable to find a contact, based on the phone number
        if(contactId == null) {
            phoneNumbers = new String[1];
            phoneNumbers[0] = phone;
        } else {
            // use only actual phone number
            phoneNumbers = new String[1];
            phoneNumbers[0] = phone;

        if (contactId != null) {
            Cursor q = null;
            // Fetch the contact e-mail addresses
            try {
                if (p != null && p.moveToFirst()) {
                    contactId = p.getString(p.getColumnIndex(Contacts._ID));
                    contactName = p.getString(p.getColumnIndex(Contacts.DISPLAY_NAME));
                }

                // Bail out if we are unable to find a contact, based on the phone number
                if(contactId == null) {
                    phoneNumbers = new String[1];
                    phoneNumbers[0] = phone;
                } else {
                    // use only actual phone number
                    phoneNumbers = new String[1];
                    phoneNumbers[0] = phone;

                    // Fetch contact e-mail addresses
                    close (p);
                    p = mResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                q = mResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                        new String[]{contactId},
                        null);
                    if (p != null) {
                if (q != null && q.moveToFirst()) {
                    int i = 0;
                        emailAddresses = new String[p.getCount()];
                        while (p != null && p.moveToNext()) {
                            String emailAddress = p.getString(
                                    p.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
                    emailAddresses = new String[q.getCount()];
                    do {
                        String emailAddress = q.getString(q.getColumnIndex(
                                ContactsContract.CommonDataKinds.Email.ADDRESS));
                        emailAddresses[i++] = emailAddress;
                        }
                    }
                    } while (q != null && q.moveToNext());
                }
            } finally {
                close(p);
                close(q);
            }
        }

        if (incoming == true) {
            if(V) Log.d(TAG, "Adding originator for phone:" + phone);
            // Use version 3.0 as we only have a formatted name