From 8ca034c3dbb9fa80078cd8685a93e215fdd4f865 Mon Sep 17 00:00:00 2001 From: Jesse Vincent Date: Tue, 23 Mar 2010 03:19:12 +0000 Subject: [PATCH] Patch for issue 946: Only able to get contacts from a primary google account by cketti --- AndroidManifest.xml | 2 +- build.properties | 2 +- src/com/fsck/k9/EmailAddressAdapter.java | 113 ++++++++---------- .../fsck/k9/EmailAddressAdapterSdk3_4.java | 84 +++++++++++++ src/com/fsck/k9/EmailAddressAdapterSdk5.java | 78 ++++++++++++ src/com/fsck/k9/activity/MessageCompose.java | 2 +- 6 files changed, 215 insertions(+), 66 deletions(-) create mode 100644 src/com/fsck/k9/EmailAddressAdapterSdk3_4.java create mode 100644 src/com/fsck/k9/EmailAddressAdapterSdk5.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 8a86735e1..8d5b4c3ad 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -4,7 +4,7 @@ android:versionName="2.513" package="com.fsck.k9"> clazz = + Class.forName(className).asSubclass(EmailAddressAdapter.class); + sInstance = clazz.newInstance(); + } + catch (Exception e) + { + throw new IllegalStateException(e); + } } - return mContentResolver.query(CONTENT_EMAIL_URI, PROJECTION, where, null, SORT_ORDER); + return sInstance; + } + + public static Context getContext() + { + return sContext; + } + + protected ContentResolver mContentResolver; + + public EmailAddressAdapter() + { + super(getContext(), R.layout.recipient_dropdown_item, null); + mContentResolver = getContext().getContentResolver(); } } diff --git a/src/com/fsck/k9/EmailAddressAdapterSdk3_4.java b/src/com/fsck/k9/EmailAddressAdapterSdk3_4.java new file mode 100644 index 000000000..c1fdf7aa5 --- /dev/null +++ b/src/com/fsck/k9/EmailAddressAdapterSdk3_4.java @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2007 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.fsck.k9; + +import android.content.Context; +import android.database.Cursor; +import android.database.DatabaseUtils; +import android.provider.Contacts.ContactMethods; +import android.provider.Contacts.People; +import android.view.View; +import android.widget.TextView; +import com.fsck.k9.mail.Address; + +import static android.provider.Contacts.ContactMethods.CONTENT_EMAIL_URI; + +public class EmailAddressAdapterSdk3_4 extends EmailAddressAdapter +{ + public static final int NAME_INDEX = 1; + + public static final int DATA_INDEX = 2; + + private static final String SORT_ORDER = People.TIMES_CONTACTED + " DESC, " + People.NAME; + + private static final String[] PROJECTION = + { + ContactMethods._ID, // 0 + ContactMethods.NAME, // 1 + ContactMethods.DATA // 2 + }; + + @Override + public final String convertToString(Cursor cursor) + { + String name = cursor.getString(NAME_INDEX); + String address = cursor.getString(DATA_INDEX); + + return new Address(address, name).toString(); + } + + @Override + public final void bindView(View view, Context context, Cursor cursor) + { + TextView text1 = (TextView)view.findViewById(R.id.text1); + TextView text2 = (TextView)view.findViewById(R.id.text2); + text1.setText(cursor.getString(NAME_INDEX)); + text2.setText(cursor.getString(DATA_INDEX)); + } + + @Override + public Cursor runQueryOnBackgroundThread(CharSequence constraint) + { + String where = null; + + if (constraint != null) + { + String filter = DatabaseUtils.sqlEscapeString(constraint.toString() + '%'); + + StringBuilder s = new StringBuilder(); + s.append("("+People.NAME+" LIKE "); + s.append(filter); + s.append(") OR ("+ContactMethods.DATA+" LIKE "); + s.append(filter); + s.append(")"); + + where = s.toString(); + } + + return mContentResolver.query(CONTENT_EMAIL_URI, PROJECTION, where, null, SORT_ORDER); + } +} diff --git a/src/com/fsck/k9/EmailAddressAdapterSdk5.java b/src/com/fsck/k9/EmailAddressAdapterSdk5.java new file mode 100644 index 000000000..1b894308b --- /dev/null +++ b/src/com/fsck/k9/EmailAddressAdapterSdk5.java @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2007 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.fsck.k9; + +import android.content.Context; +import android.database.Cursor; +import android.net.Uri; +import android.provider.ContactsContract.Contacts; +import android.provider.ContactsContract.CommonDataKinds.Email; +import android.provider.ContactsContract.Contacts.Data; +import android.view.View; +import android.widget.TextView; +import com.fsck.k9.mail.Address; + +public class EmailAddressAdapterSdk5 extends EmailAddressAdapter +{ + public static final int NAME_INDEX = 1; + public static final int DATA_INDEX = 2; + + private static final String SORT_ORDER = Contacts.TIMES_CONTACTED + + " DESC, " + Contacts.DISPLAY_NAME; + + private static final String[] PROJECTION = { + Data._ID, // 0 + Contacts.DISPLAY_NAME, // 1 + Email.DATA // 2 + }; + + @Override + public final String convertToString(Cursor cursor) + { + String name = cursor.getString(NAME_INDEX); + String address = cursor.getString(DATA_INDEX); + + return new Address(address, name).toString(); + } + + @Override + public final void bindView(View view, Context context, Cursor cursor) + { + TextView text1 = (TextView) view.findViewById(R.id.text1); + TextView text2 = (TextView) view.findViewById(R.id.text2); + text1.setText(cursor.getString(NAME_INDEX)); + text2.setText(cursor.getString(DATA_INDEX)); + } + + @Override + public Cursor runQueryOnBackgroundThread(CharSequence constraint) + { + String filter = constraint == null ? "" : constraint.toString(); + Uri uri = Uri.withAppendedPath(Email.CONTENT_FILTER_URI, Uri.encode(filter)); + Cursor c = mContentResolver.query(uri, PROJECTION, null, null, SORT_ORDER); + // To prevent expensive execution in the UI thread + // Cursors get lazily executed, so if you don't call anything on the cursor before + // returning it from the background thread you'll have a complied program for the cursor, + // but it won't have been executed to generate the data yet. Often the execution is more + // expensive than the compilation... + if (c != null) + { + c.getCount(); + } + return c; + } +} diff --git a/src/com/fsck/k9/activity/MessageCompose.java b/src/com/fsck/k9/activity/MessageCompose.java index 1b1130890..ac683cf3d 100644 --- a/src/com/fsck/k9/activity/MessageCompose.java +++ b/src/com/fsck/k9/activity/MessageCompose.java @@ -276,7 +276,7 @@ public class MessageCompose extends K9Activity implements OnClickListener, OnFoc } - mAddressAdapter = new EmailAddressAdapter(this); + mAddressAdapter = EmailAddressAdapter.getInstance(this); mAddressValidator = new EmailAddressValidator(); mFromView = (TextView)findViewById(R.id.from);