DatabaseUtilspublic class DatabaseUtils extends Object
Fields Summary |
---|
private static final String | TAG |
Constructors Summary |
---|
private DatabaseUtils()
|
Methods Summary |
---|
private static int | clearBrandingResourceMapCache(android.content.ContentResolver cr, long providerId)Clear the branding resource map cache.
StringBuilder where = new StringBuilder();
where.append(Im.BrandingResourceMapCache.PROVIDER_ID);
where.append('=");
where.append(providerId);
return cr.delete(Im.BrandingResourceMapCache.CONTENT_URI, where.toString(), null);
| private static android.graphics.drawable.Drawable | decodeAvatar(byte[] data)
Bitmap b = BitmapFactory.decodeByteArray(data, 0, data.length);
Drawable avatar = new BitmapDrawable(b);
return avatar;
| public static android.graphics.drawable.Drawable | getAvatarFromCursor(android.database.Cursor cursor, int dataColumn)
byte[] rawData = cursor.getBlob(dataColumn);
if (rawData == null) {
return null;
}
return decodeAvatar(rawData);
| public static android.graphics.drawable.Drawable | getAvatarFromCursor(android.database.Cursor cursor, int dataColumn, int encodedDataColumn, java.lang.String username, boolean updateBlobUseCursor, android.content.ContentResolver resolver, android.net.Uri updateBlobUri)
/**
* Optimization: the avatar table in IM content provider have two
* columns, one for the raw blob data, another for the base64 encoded
* data. The reason for this is when the avatars are initially
* downloaded, they are in the base64 encoded form, and instead of
* base64 decode the avatars for all the buddies up front, we can just
* simply store the encoded data in the table, and decode them on demand
* when displaying them. Once we decode the avatar, we store the decoded
* data as a blob, and null out the encoded column in the avatars table.
* query the raw blob data first, if present, great; if not, query the
* encoded data, decode it and store as the blob, and null out the
* encoded column.
*/
byte[] rawData = cursor.getBlob(dataColumn);
if (rawData == null) {
String encodedData = cursor.getString(encodedDataColumn);
if (encodedData == null) {
// Log.e(LogTag.LOG_TAG, "getAvatarFromCursor for " + username +
// ", no raw or encoded data!");
return null;
}
rawData = android.os.Base64Utils.decodeBase64(encodedData);
// if (DBG) {
// log("getAvatarFromCursor for " + username + ": found encoded
// data,"
// + " update blob with data, len=" + rawData.length);
// }
if (updateBlobUseCursor) {
cursor.updateBlob(dataColumn, rawData);
cursor.updateString(encodedDataColumn, null);
cursor.commitUpdates();
} else {
updateAvatarBlob(resolver, updateBlobUri, rawData, username);
}
}
return decodeAvatar(rawData);
| public static android.net.Uri | getAvatarUri(android.net.Uri baseUri, long providerId, long accountId)
Uri.Builder builder = baseUri.buildUpon();
ContentUris.appendId(builder, providerId);
ContentUris.appendId(builder, accountId);
return builder.build();
| private static long | insertProviderRow(android.content.ContentResolver cr, java.lang.String providerName, java.lang.String providerFullName, java.lang.String signUpUrl)Insert a new plugin provider to the provider table.
ContentValues values = new ContentValues(3);
values.put(Im.Provider.NAME, providerName);
values.put(Im.Provider.FULLNAME, providerFullName);
values.put(Im.Provider.CATEGORY, ImApp.IMPS_CATEGORY);
values.put(Im.Provider.SIGNUP_URL, signUpUrl);
Uri result = cr.insert(Im.Provider.CONTENT_URI, values);
return ContentUris.parseId(result);
| private static boolean | isPluginVersionChanged(android.content.ContentResolver cr, long providerId, java.lang.String newVersion)Compare the saved version of a plugin provider with the newly loaded version.
String oldVersion = Im.ProviderSettings.getStringValue(cr, providerId,
ImConfigNames.PLUGIN_VERSION);
if (oldVersion == null) {
return true;
}
return !oldVersion.equals(newVersion);
| public static android.database.Cursor | queryAccountsForProvider(android.content.ContentResolver cr, java.lang.String[] projection, long providerId)
StringBuilder where = new StringBuilder(Im.Account.ACTIVE);
where.append("=1 AND ").append(Im.Account.PROVIDER).append('=").append(providerId);
Cursor c = cr.query(Im.Account.CONTENT_URI, projection, where.toString(), null, null);
if (c != null && !c.moveToFirst()) {
c.close();
return null;
}
return c;
| private static int | saveProviderSettings(android.content.ContentResolver cr, long providerId, java.util.Map config)Insert the plugin settings into the database.
ContentValues[] settingValues = new ContentValues[config.size()];
int index = 0;
for (Map.Entry<String, String> entry : config.entrySet()) {
ContentValues settingValue = new ContentValues();
settingValue.put(Im.ProviderSettings.PROVIDER, providerId);
settingValue.put(Im.ProviderSettings.NAME, entry.getKey());
settingValue.put(Im.ProviderSettings.VALUE, entry.getValue());
settingValues[index++] = settingValue;
}
return cr.bulkInsert(Im.ProviderSettings.CONTENT_URI, settingValues);
| private static void | updateAvatarBlob(android.content.ContentResolver resolver, android.net.Uri updateUri, byte[] data, java.lang.String username)
ContentValues values = new ContentValues(3);
values.put(Im.Avatars.DATA, data);
StringBuilder buf = new StringBuilder(Im.Avatars.CONTACT);
buf.append("=?");
String[] selectionArgs = new String[] {
username
};
resolver.update(updateUri, values, buf.toString(), selectionArgs);
| public static long | updateProviderDb(android.content.ContentResolver cr, java.lang.String providerName, java.lang.String providerFullName, java.lang.String signUpUrl, java.util.Map config)Update IM provider database for a plugin using newly loaded information.
boolean versionChanged;
// query provider data
long providerId = Im.Provider.getProviderIdForName(cr, providerName);
if (providerId > 0) {
// already loaded, check if version changed
String pluginVersion = config.get(ImConfigNames.PLUGIN_VERSION);
if (!isPluginVersionChanged(cr, providerId, pluginVersion)) {
// no change, just return
return providerId;
}
// changed, update provider meta data
updateProviderRow(cr, providerId, providerFullName, signUpUrl);
// clear branding resource map cache
clearBrandingResourceMapCache(cr, providerId);
Log.d(TAG, "Plugin " + providerName + "(" + providerId +
") has a version change. Database updated.");
} else {
// new plugin, not loaded before, insert the provider data
providerId = insertProviderRow(cr, providerName, providerFullName, signUpUrl);
Log.d(TAG, "Plugin " + providerName + "(" + providerId +
") is new. Provider added to IM db.");
}
// plugin provider has been inserted/updated, we need to update settings
saveProviderSettings(cr, providerId, config);
return providerId;
| private static int | updateProviderRow(android.content.ContentResolver cr, long providerId, java.lang.String providerFullName, java.lang.String signUpUrl)Update the data of a plugin provider.
// Update the full name, signup url and category each time when the plugin change
// instead of specific version change because this is called only once.
// It's ok to update them even the values are not changed.
// Note that we don't update the provider name because it's used as
// identifier at some place and the plugin should never change it.
ContentValues values = new ContentValues(3);
values.put(Im.Provider.FULLNAME, providerFullName);
values.put(Im.Provider.SIGNUP_URL, signUpUrl);
values.put(Im.Provider.CATEGORY, ImApp.IMPS_CATEGORY);
Uri uri = ContentUris.withAppendedId(Im.Provider.CONTENT_URI, providerId);
return cr.update(uri, values, null, null);
|
|