DataUsageStatUpdaterpublic class DataUsageStatUpdater extends Object Convenient class for updating usage statistics in ContactsProvider.
Applications like Email, Sms, etc. can promote recipients for better sorting with this class |
Fields Summary |
---|
private static final String | TAG | private final android.content.ContentResolver | mResolver |
Constructors Summary |
---|
public DataUsageStatUpdater(android.content.Context context)
mResolver = context.getContentResolver();
|
Methods Summary |
---|
private boolean | update(java.util.Collection contactIds, java.util.Collection dataIds, java.lang.String type)
final long currentTimeMillis = System.currentTimeMillis();
boolean successful = false;
// From ICS (SDK_INT 14) we can use per-contact-method structure. We'll check if the device
// supports it and call the API.
if (Build.VERSION.SDK_INT >= 14) {
if (dataIds.isEmpty()) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Given list for data IDs is null. Ignoring.");
}
} else {
final Uri uri = DataUsageFeedback.FEEDBACK_URI.buildUpon()
.appendPath(TextUtils.join(",", dataIds))
.appendQueryParameter(DataUsageFeedback.USAGE_TYPE, type)
.build();
if (mResolver.update(uri, new ContentValues(), null, null) > 0) {
successful = true;
} else {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "update toward data rows " + dataIds + " failed");
}
}
}
} else {
// Use older API.
if (contactIds.isEmpty()) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Given list for contact IDs is null. Ignoring.");
}
} else {
final StringBuilder whereBuilder = new StringBuilder();
final ArrayList<String> whereArgs = new ArrayList<String>();
final String[] questionMarks = new String[contactIds.size()];
for (long contactId : contactIds) {
whereArgs.add(String.valueOf(contactId));
}
Arrays.fill(questionMarks, "?");
whereBuilder.append(ContactsContract.Contacts._ID + " IN (").
append(TextUtils.join(",", questionMarks)).
append(")");
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "contactId where: " + whereBuilder.toString());
Log.d(TAG, "contactId selection: " + whereArgs);
}
final ContentValues values = new ContentValues();
values.put(ContactsContract.Contacts.LAST_TIME_CONTACTED, currentTimeMillis);
if (mResolver.update(ContactsContract.Contacts.CONTENT_URI, values,
whereBuilder.toString(), whereArgs.toArray(new String[0])) > 0) {
successful = true;
} else {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "update toward raw contacts " + contactIds + " failed");
}
}
}
}
return successful;
| public boolean | updateWithAddress(java.util.Collection addresses)Update usage statistics information using a list of email addresses.
This will cause Disk access so should be called in a background thread.
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "updateWithAddress: " + Arrays.toString(addresses.toArray()));
}
if (addresses != null && !addresses.isEmpty()) {
final ArrayList<String> whereArgs = new ArrayList<String>();
final StringBuilder whereBuilder = new StringBuilder();
final String[] questionMarks = new String[addresses.size()];
whereArgs.addAll(addresses);
Arrays.fill(questionMarks, "?");
// Email.ADDRESS == Email.DATA1. Email.ADDRESS can be available from API Level 11.
whereBuilder.append(Email.DATA1 + " IN (")
.append(TextUtils.join(",", questionMarks))
.append(")");
final Cursor cursor = mResolver.query(Email.CONTENT_URI,
new String[] {Email.CONTACT_ID, Email._ID}, whereBuilder.toString(),
whereArgs.toArray(new String[0]), null);
if (cursor == null) {
Log.w(TAG, "Cursor for Email.CONTENT_URI became null.");
} else {
final Set<Long> contactIds = new HashSet<Long>(cursor.getCount());
final Set<Long> dataIds = new HashSet<Long>(cursor.getCount());
try {
cursor.move(-1);
while(cursor.moveToNext()) {
contactIds.add(cursor.getLong(0));
dataIds.add(cursor.getLong(1));
}
} finally {
cursor.close();
}
return update(contactIds, dataIds, DataUsageFeedback.USAGE_TYPE_LONG_TEXT);
}
}
return false;
| public boolean | updateWithPhoneNumber(java.util.Collection numbers)Update usage statistics information using a list of phone numbers.
This will cause Disk access so should be called in a background thread.
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "updateWithPhoneNumber: " + Arrays.toString(numbers.toArray()));
}
if (numbers != null && !numbers.isEmpty()) {
final ArrayList<String> whereArgs = new ArrayList<String>();
final StringBuilder whereBuilder = new StringBuilder();
final String[] questionMarks = new String[numbers.size()];
whereArgs.addAll(numbers);
Arrays.fill(questionMarks, "?");
// Phone.NUMBER == Phone.DATA1. NUMBER can be available from API Level 11.
whereBuilder.append(Phone.DATA1 + " IN (")
.append(TextUtils.join(",", questionMarks))
.append(")");
final Cursor cursor = mResolver.query(Phone.CONTENT_URI,
new String[] {Phone.CONTACT_ID, Phone._ID}, whereBuilder.toString(),
whereArgs.toArray(new String[0]), null);
if (cursor == null) {
Log.w(TAG, "Cursor for Phone.CONTENT_URI became null.");
} else {
final Set<Long> contactIds = new HashSet<Long>(cursor.getCount());
final Set<Long> dataIds = new HashSet<Long>(cursor.getCount());
try {
cursor.move(-1);
while(cursor.moveToNext()) {
contactIds.add(cursor.getLong(0));
dataIds.add(cursor.getLong(1));
}
} finally {
cursor.close();
}
return update(contactIds, dataIds, DataUsageFeedback.USAGE_TYPE_SHORT_TEXT);
}
}
return false;
| public boolean | updateWithRfc822Address(java.util.Collection texts)Updates usage statistics using comma-separated RFC822 address like
"Joe , Due ".
This will cause Disk access so should be called in a background thread.
if (texts == null) {
return false;
} else {
final Set<String> addresses = new HashSet<String>();
for (CharSequence text : texts) {
Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(text.toString().trim());
for (Rfc822Token token : tokens) {
addresses.add(token.getAddress());
}
}
return updateWithAddress(addresses);
}
|
|