The process com.htc.bgp has stopped unexpectedly

I recently got an HTC Hero phone (currently running Android 2.1 "Eclair"). Long story short, I haven't gotten my phone to properly sync with my Facebook contacts.

Originally, I wasn't able to see any Facebook contacts. Then something happened and I started getting updates, but I could only see contacts up to a certain letter, in my case the letter R. Well, that sucked so today I decided to figure out what was happening. I removed my facebook account from my phone, logged out of the app, uninstalled the app updates, etc. And of course now I was in the original state where no contacts would get synced, and I would get the dreaded error box:

Sorry!

The process com.htc.bgp has stopped unexpectedly. Please try again.

[Force Close]

Armed with a little bit of patience, I bing-ed for a tool to be able to get some diagnostic information and found an Android app called alogcat, with which I was able to send logs to myself via email. This was a good starting point, but for more convenience I connected my phone to my laptop and downloaded the Android SDK which includes adb (Android Debugging Bridge). Once the SDK is installed I ran adb logcat, which connects to the phone and spews all kinds of useful information.

Using adb logcat I can see all my contacts being read by my phone. Then my phone tries to read "event data" (e.g. updates), which it does through a SQL query to the phone's SQLite database (apparently this is how Facebook data is stored locally on the phone). The problem is that the query that the event sync code issues is malformed, which causes the SQLite parser to throw an exception, which only gets caught on the main thread (where the contacts sync code is running). This exception causes among other tihngs, the death of the whole background synchronization process. Apparently it also causes the databases changes to either be deleted or rolled back / not committed.

Here's the relevant log output (some output has been omitted/replaced for <number> etc.)

D/FBDbWriter( 1311): write event to contact db
D/FBDbWriter( 1311): account_name='Alexander Sklar' AND account_type='com.htc.socialnetwork.facebook' AND (sourceid=<number1> OR sourceid=<number2> OR sourceid= OR sourceid=<number3> OR sourceid=<number4>)
E/DatabaseUtils(  162): Writing exception to parcel
E/DatabaseUtils(  162): android.database.sqlite.SQLiteException: near "OR": syntax error: , while compiling: SELECT _id, sourceid FROM view_raw_contacts_restricted WHERE (1) AND (account_name='Alexander Sklar' AND account_type='com.htc.socialnetwork.facebook' AND (sourceid= <number1> OR sourceid= <number2> OR sourceid= OR sourceid= <number3> OR sourceid= <number4> ))
E/DatabaseUtils(  162):         at android.database.sqlite.SQLiteProgram.native_compile(Native Method)
E/DatabaseUtils(  162):         at android.database.sqlite.SQLiteProgram.compile(SQLiteProgram.java:117)
E/DatabaseUtils(  162):         at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:66)
E/DatabaseUtils(  162):         at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
E/DatabaseUtils(  162):         at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:59)
E/DatabaseUtils(  162):         at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1287)
E/DatabaseUtils(  162):         at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:337)
E/DatabaseUtils( 162): at com.android.providers.contacts.ContactsProvider2.query(ContactsProvider2.java:5136)
E/DatabaseUtils( 162): at com.android.providers.contacts.ContactsProvider2.query(ContactsProvider2.java:5126)
E/DatabaseUtils( 162): at com.android.providers.contacts.CContactsProvider2.query(CContactsProvider2.java:173)
E/DatabaseUtils( 162): at com.android.providers.contacts.HtcContactsProvider2.query(HtcContactsProvider2.java:3143)
E/DatabaseUtils(  162):         at android.content.ContentProvider$Transport.bulkQuery(ContentProvider.java:117)
E/DatabaseUtils(  162):         at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:121)
E/DatabaseUtils(  162):         at android.os.Binder.execTransact(Binder.java:287)
E/DatabaseUtils(  162):         at dalvik.system.NativeStart.run(Native Method)
W/dalvikvm( 1311): threadid=39: thread exiting with uncaught exception (group=0x4001e390)
E/AndroidRuntime( 1311): Uncaught handler: thread pool-4-thread-1 exiting due to uncaught exception
E/AndroidRuntime( 1311): android.database.sqlite.SQLiteException: near "OR": syntax error: , while compiling: SELECT _id, sourceid FROM view_raw_conta
cts_restricted WHERE (1) AND (account_name='Alexander Sklar' AND account_type='com.htc.socialnetwork.facebook' AND (sourceid=<number1> OR sourceid=
<number2> OR sourceid= OR sourceid=<number3> OR sourceid=<number4>))
E/AndroidRuntime( 1311):        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:158)
E/AndroidRuntime( 1311):        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
E/AndroidRuntime( 1311):        at android.content.ContentProviderProxy.bulkQuery(ContentProviderNative.java:369)
E/AndroidRuntime( 1311):        at android.content.ContentProviderProxy.query(ContentProviderNative.java:388)
E/AndroidRuntime( 1311):        at android.content.ContentResolver.query(ContentResolver.java:202)
E/AndroidRuntime( 1311):        at com.htc.socialnetwork.provider.task.FacebookDbWriter.syncEvents(FacebookDbWriter.java:716)
E/AndroidRuntime( 1311):        at com.htc.socialnetwork.provider.task.UserSyncTask.doSync(UserSyncTask.java:421)
E/AndroidRuntime( 1311):        at com.htc.socialnetwork.provider.task.SyncTask.run(SyncTask.java:53)
E/AndroidRuntime( 1311):        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
E/AndroidRuntime( 1311):        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
E/AndroidRuntime( 1311):        at java.lang.Thread.run(Thread.java:1102)
I/Process (   81): Sending signal. PID: 1311 SIG: 3

So one of two things isn't going right (in light blue in the stack trace)

  1. Either whatever code is obtaining the sourceid values to construct the query is failing, or
  2. The code constructing the query from the obtained sourceid values should deal with the null sourceid and not add it to the query string as it renders an invalid query

Now, if only I could get a hold of the code for the "Facebook for HTC Sense" app I'm pretty sure I'd figure this out soon enough...