Created
September 30, 2015 07:58
-
-
Save pskink/6564e6a66b95e228db7b to your computer and use it in GitHub Desktop.
ghost contact cursor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// add in Activity#onCreate | |
ListView lv = new ListView(this); | |
setContentView(lv); | |
Uri uri = ContactsContract.Contacts.CONTENT_URI; | |
Wrapper c = Wrapper.query(getContentResolver(), uri, null, null, null, ContactsContract.Contacts.DISPLAY_NAME, getResources()); | |
String[] from = { | |
ContactsContract.Contacts.DISPLAY_NAME, | |
Wrapper.PHONES, | |
}; | |
int[] to = { | |
android.R.id.text1, | |
android.R.id.text2, | |
}; | |
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, c, from, to, 0); | |
lv.setAdapter(adapter); | |
// end of Activity#onCreate | |
// class Wrapper (modify it to add other "fake" columns): | |
static class Wrapper extends ColumnExtenderCursor { | |
public static final String PHONES = "phones"; | |
private Resources res; | |
private int idIdx; | |
private int phonesIdx; | |
private ContentResolver contentResolver; | |
private SparseArray<String> phonesCache = new SparseArray<>(); | |
public Wrapper(Cursor cursor, ContentResolver contentResolver, Resources res) { | |
// here we are adding PHONES "fake" column: you can add more coluns like: | |
// super(cursor, PHONES, FULL_NAME); | |
super(cursor, PHONES); | |
this.contentResolver = contentResolver; | |
this.res = res; | |
idIdx = getColumnIndex(BaseColumns._ID); | |
phonesIdx = getColumnIndex(PHONES); | |
} | |
// a helper method to get a wrapper | |
public static Wrapper query(ContentResolver contentResolver, Uri uri, String[] proj, String sel, String[] selArgs, String sortOrder, Resources res) { | |
Cursor c = contentResolver.query(uri, proj, sel, selArgs, sortOrder); | |
return new Wrapper(c, contentResolver, res); | |
} | |
@Override | |
public String getString(int columnIndex) { | |
if (columnIndex == phonesIdx) { | |
int id = super.getInt(idIdx); | |
String phones = phonesCache.get(id); | |
if (phones != null) { | |
Log.d(TAG, "phones from cache " + getPosition()); | |
return phones; | |
} | |
Log.d(TAG, "phones from content provider " + getPosition()); | |
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; | |
String[] proj = { | |
ContactsContract.CommonDataKinds.Phone.NUMBER, | |
ContactsContract.CommonDataKinds.Phone.TYPE, | |
}; | |
String sel = ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?"; | |
String[] selArgs = { | |
Integer.toString(id), | |
}; | |
Cursor c = contentResolver.query(uri, proj, sel, selArgs, null); | |
int i = 0; | |
StringBuilder sb = new StringBuilder(); | |
while (c.moveToNext()) { | |
sb.append(c.getString(0)) | |
.append(": ") | |
.append(ContactsContract.CommonDataKinds.Phone.getTypeLabel(res, c.getInt(1), "unknown phone type")); | |
i++; | |
if (i != c.getCount()) { | |
sb.append(", "); | |
} | |
} | |
c.close(); | |
phones = sb.toString(); | |
phonesCache.put(id, phones); | |
return phones; | |
} | |
return super.getString(columnIndex); | |
} | |
} | |
// class ColumnExtenderCursor: | |
import android.database.Cursor; | |
import android.database.CursorWrapper; | |
public class ColumnExtenderCursor extends CursorWrapper { | |
private final String[] columnNames; | |
public ColumnExtenderCursor(Cursor cursor, String... newColumns) { | |
super(cursor); | |
String[] oldColumnNames = cursor.getColumnNames(); | |
int oldColumnCount = cursor.getColumnCount(); | |
columnNames = new String[oldColumnCount + newColumns.length]; | |
System.arraycopy(oldColumnNames, 0, columnNames, 0, oldColumnNames.length); | |
System.arraycopy(newColumns, 0, columnNames, oldColumnCount, newColumns.length); | |
} | |
@Override | |
public int getColumnCount() { | |
return columnNames.length; | |
} | |
@Override | |
public int getColumnIndex(String columnName) { | |
for (int i = 0; i < columnNames.length; i++) { | |
if (columnNames[i].equals(columnName)) { | |
return i; | |
} | |
} | |
return -1; | |
} | |
@Override | |
public int getColumnIndexOrThrow(String columnName) throws IllegalArgumentException { | |
int idx = getColumnIndex(columnName); | |
if (idx >= 0) { | |
return idx; | |
} | |
throw new IllegalArgumentException("column '" + columnName + "' does not exist"); | |
} | |
@Override | |
public String getColumnName(int columnIndex) { | |
return columnNames[columnIndex]; | |
} | |
@Override | |
public String[] getColumnNames() { | |
return columnNames; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment