Question

I have Contacts app and need to update my local ORMLite database with the ContentObserver. I need to check to Contacts updatings. And if it is, I need to update name of the contact if it is not stored in my local database.

I have two items - CallItem (which has foreign Contact field) and ContactItem.

My ContentObserver:

class CallsContentObserver extends ContentObserver {
    Context _context;
    Handler _handler;
    public CallsContentObserver(Handler handler, Context context) {
        super(handler);
        this._context = context;
        this._handler = handler;

    }

    @Override
    public boolean deliverSelfNotifications() {
        return true;
    }

    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);


           ArrayList<CallItem> contactsList = null;
            DatabaseHandler db = new DatabaseHandler(getApplicationContext());
            Cursor contactLookupCursor = null;
            try{
                Dao<CallItem,Integer> daoSubject = db.getCallDao();
                contactsList = (ArrayList<CallItem>) daoSubject.queryForAll();
                for (CallItem contact : contactsList)
                    {

                        if (contact.getCall_contact() == null)
                        {
                            String contactNumber = Uri.encode(contact.getNumber());
                            String new_name = null;
                          contactLookupCursor =getContentResolver().query(
                                    Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,contactNumber),
                                    new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID}, null, null, null);
                                while(contactLookupCursor.moveToNext()){
                                  new_name = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
                                    }
                              contactLookupCursor.close();
                              contact.setName(new_name);
                              daoSubject.update(contact);
                        }

                    }
            }
            catch(Exception e)
            {e.printStackTrace();}
            finally{db.close();}

       }
    }

so I got all my CallItems. Then I check if every CallItem contains ContactItem, if not, I get new name of it by its number and update this CallItem with new value with DAO. But this solution is quite slow, I want make it more fluent. How can I do this?

Was it helpful?

Solution

I found a solution to this. I do not parse all the call log when user first appears in the application. I parse only first 20 calls, then download more with onScroll. So names proceeding and synchronising with local database do not take so much time.

ORMLite is pretty good nevertheless I feel that it is better to use ContentResolver in these cases.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top