Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get sorting order preference selected user in Contacts>settings in Android Device and How to reduce query time?

I have two questions:

1: How can my application know Default sorting order used for sorting native "Android Device Contacts"?

Inside "Contacts" -> "Settings" in android, we have "List by" and "Display Contacts by" options. How can I get these prefrences in my application.

For example: suppose device contacts are sorted by "First Name", then application should get some constant(or something similar). On the other hand, if contacts are sorted by "Last Name" then application should get constants(or info) regarding the same.

I searched SO and I got this link, but the problem is that the solution was deprecated in API level 5.

Here is my query for fetching contacts

String userPreference = getPrefContacts();/* **getPrefContacts() will return either ContactsContract.Data.DATA2 or ContactsContract.Data.DATA3** */
    try {
                ContentResolver cr = getActivity().getContentResolver();
                String[] projectionName = new String[] {
                        ContactsContract.Data.CONTACT_ID,
                        ContactsContract.Data.DATA2, ContactsContract.Data.DATA5,
                        ContactsContract.Data.DATA3, ContactsContract.Data.DATA1 };
                String sortOrder = userPrefrence
                        + " COLLATE LOCALIZED ASC";

                Cursor nameCursor = cr.query(ContactsContract.Data.CONTENT_URI,
                        projectionName, null, null, sortOrder);
                nameCursor.moveToFirst();
                if (nameCursor.getCount() > 0) {
                    do {
                        String fName = nameCursor
                                .getString(nameCursor
                                        .getColumnIndexOrThrow(ContactsContract.Data.DATA2));
                        String mName = nameCursor
                                .getString(nameCursor
                                        .getColumnIndexOrThrow(ContactsContract.Data.DATA5));
                        String lName = nameCursor
                                .getString(nameCursor
                                        .getColumnIndexOrThrow(ContactsContract.Data.DATA3));
                        String name = nameCursor
                                .getString(nameCursor
                                        .getColumnIndexOrThrow(ContactsContract.Data.DATA1));
                        if (name != null) {
                            String[] projectionCommon = new String[] {
                                    ContactsContract.Contacts._ID,
                                    ContactsContract.Contacts.HAS_PHONE_NUMBER };
                            String selectionCommon = ContactsContract.Contacts.DISPLAY_NAME
                                    + " = ?";
                            String[] selectionArgCommon = new String[] { name };

                            Cursor common = cr.query(
                                    ContactsContract.Contacts.CONTENT_URI,
                                    projectionCommon, selectionCommon,
                                    selectionArgCommon, null);
                            if (common.getCount() > 0) {
                                while (common.moveToNext()) {
                                    String contactID = common
                                            .getString(common
                                                    .getColumnIndexOrThrow(ContactsContract.Contacts._ID));
                                    int hasPhone = common
                                            .getInt(common
                                                    .getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                                    if (hasPhone > 0) {
                                        String[] projectionPhone = new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER };
                                        String selectionPhone = ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                                + " = ?";
                                        String[] selectionArgPhone = new String[] { contactID };
                                        Cursor phoneCursor = cr
                                                .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                                        projectionPhone,
                                                        selectionPhone,
                                                        selectionArgPhone, null);

                                        if (phoneCursor.getCount() > 0) {
                                            while (phoneCursor.moveToNext()) {
                                                String phone = phoneCursor
                                                        .getString(phoneCursor
                                                                .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                                if (phone != null) {
                                                    ContactData tmp = new ContactData(
                                                            name, fName, mName,
                                                            lName, phone, contactID);
                                                    if (raw.size() == 0) {
                                                        raw.add(tmp);
                                                    } else if (!(raw
                                                            .get(raw.size() - 1))
                                                            .getContactID()
                                                            .equalsIgnoreCase(
                                                                    tmp.getContactID())) {
                                                        raw.add(tmp);
                                                    }
                                                }
                                            }
                                        }
                                        phoneCursor.close();
                                    }
                                }
                            }
                            common.close();
                        }
                    } while (nameCursor.moveToNext());
                    nameCursor.close();
                }
            } catch (NumberFormatException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }

I need code for getPrefContacts() method

2: How to reduce the query time consumed with respect to above code or any other way to do so??

Hope anybody can help me.

Thanks in advance.

like image 485
Susomrof Avatar asked Oct 24 '13 15:10

Susomrof


People also ask

How do I change order of contacts on Android?

Open the Contacts app, then tap on the hamburger button (three lines) in the top-left corner, then choose "Settings." Now, tap on the "Sort by" button, then a popup will appear and you will have the option to choose how to sort your contacts list.

How do I select and display my phone number from a contact list on Android?

Display Only Android Contacts with Phone Numbers. Open your Contacts app and tap the Options button (three dots), and select Contacts Manager. On the next screen, tap on Contacts to display from the menu. Next, if you only want contacts with a phone number, tap on Phone.

What is ContactsContract?

* ContactsContract defines an extensible database of contact-related. * information. Contact information is stored in a three-tier data model: * </p>


2 Answers

I can answer your second ques only.

You can use the join attribute of SQL when querying the content providers. Thereby, removing nested while loops and hence, reducing the query time.


Edit: For your first ques, try this:

int sort_order=Settings.system.getInt (getApplicationContext ().getContentResolver (),"android.contacts.SORT_ORDER");

On my Samsung S2 device, sort_order = 1 when contacts are listed by firstname and sort_order = 2 when contacts are listed by lastname.

like image 181
Abhinav Avatar answered Nov 14 '22 16:11

Abhinav


you can manage the sorting order in your List Activity when you are creating your own CustomAdapter for the list activity .

Filtering and sorting of the data is handled by the adapter. You need to implement the logic in your custom adapter implementation.

The adapter is assigned to the ListView via the setAdapter method on the ListView object.

for eg

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Cursor mCursor = getContacts();
    startManagingCursor(mCursor);
    // now create a new list adapter bound to the cursor.
    // SimpleListAdapter is designed for binding to a Cursor.
    ListAdapter adapter = new SimpleCursorAdapter(this, // Context.
        android.R.layout.two_line_list_item, // Specify the row template
                            // to use (here, two
                            // columns bound to the
                            // two retrieved cursor
                            // rows).
        mCursor, // Pass in the cursor to bind to.
        // Array of cursor columns to bind to.
        new String[] { ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME },
        // Parallel array of which template objects to bind to those
        // columns.
        new int[] { android.R.id.text1, android.R.id.text2 });

    // Bind to our new adapter.
    setListAdapter(adapter);
  }

  private Cursor getContacts() {
    // Run query
    Uri uri = ContactsContract.Contacts.CONTENT_URI;
    String[] projection = new String[] { ContactsContract.Contacts._ID,
        ContactsContract.Contacts.DISPLAY_NAME };
    String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"
        + ("1") + "'";
    String[] selectionArgs = null;
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
        + " COLLATE LOCALIZED ASC";

    return managedQuery(uri, projection, selection, selectionArgs,
        sortOrder);
  }

Here Sorting order is managed through the cursor , which contains the data for the list .

like image 27
abhishek rathore Avatar answered Nov 14 '22 16:11

abhishek rathore