2 * Copyright (C) 2008 Esmertec AG.
3 * Copyright (C) 2008 The Android Open Source Project
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package com.android.im.app;
20 import android.content.ContentResolver;
21 import android.content.ContentUris;
22 import android.content.ContentValues;
23 import android.database.Cursor;
24 import android.graphics.Bitmap;
25 import android.graphics.BitmapFactory;
26 import android.graphics.drawable.BitmapDrawable;
27 import android.graphics.drawable.Drawable;
28 import android.net.Uri;
29 import android.provider.Im;
31 public class DatabaseUtils {
32 private DatabaseUtils() {
35 public static Cursor queryAccountsForProvider(ContentResolver cr,
36 String[] projection, long providerId) {
37 StringBuilder where = new StringBuilder(Im.Account.ACTIVE);
38 where.append("=1 AND ").append(Im.Account.PROVIDER).append('=').append(providerId);
39 Cursor c = cr.query(Im.Account.CONTENT_URI, projection, where.toString(), null, null);
40 if (c != null && !c.moveToFirst()) {
47 public static Drawable getAvatarFromCursor(Cursor cursor, int dataColumn) {
48 byte[] rawData = cursor.getBlob(dataColumn);
49 if (rawData == null) {
52 return decodeAvatar(rawData);
55 public static Uri getAvatarUri(Uri baseUri, long providerId, long accountId) {
56 Uri.Builder builder = baseUri.buildUpon();
57 ContentUris.appendId(builder, providerId);
58 ContentUris.appendId(builder, accountId);
59 return builder.build();
62 public static Drawable getAvatarFromCursor(Cursor cursor, int dataColumn,
63 int encodedDataColumn, String username, boolean updateBlobUseCursor,
64 ContentResolver resolver, Uri updateBlobUri) {
66 * Optimization: the avatar table in IM content provider have two
67 * columns, one for the raw blob data, another for the base64 encoded
68 * data. The reason for this is when the avatars are initially
69 * downloaded, they are in the base64 encoded form, and instead of
70 * base64 decode the avatars for all the buddies up front, we can just
71 * simply store the encoded data in the table, and decode them on demand
72 * when displaying them. Once we decode the avatar, we store the decoded
73 * data as a blob, and null out the encoded column in the avatars table.
74 * query the raw blob data first, if present, great; if not, query the
75 * encoded data, decode it and store as the blob, and null out the
78 byte[] rawData = cursor.getBlob(dataColumn);
80 if (rawData == null) {
81 String encodedData = cursor.getString(encodedDataColumn);
82 if (encodedData == null) {
83 // Log.e(LogTag.LOG_TAG, "getAvatarFromCursor for " + username +
84 // ", no raw or encoded data!");
88 rawData = android.os.Base64Utils.decodeBase64(encodedData);
91 // log("getAvatarFromCursor for " + username + ": found encoded
93 // + " update blob with data, len=" + rawData.length);
96 if (updateBlobUseCursor) {
97 cursor.updateBlob(dataColumn, rawData);
98 cursor.updateString(encodedDataColumn, null);
99 cursor.commitUpdates();
101 updateAvatarBlob(resolver, updateBlobUri, rawData, username);
105 return decodeAvatar(rawData);
108 private static void updateAvatarBlob(ContentResolver resolver, Uri updateUri, byte[] data,
110 ContentValues values = new ContentValues(3);
111 values.put(Im.Avatars.DATA, data);
113 StringBuilder buf = new StringBuilder(Im.Avatars.CONTACT);
116 String[] selectionArgs = new String[] {
120 resolver.update(updateUri, values, buf.toString(), selectionArgs);
123 private static Drawable decodeAvatar(byte[] data) {
124 Bitmap b = BitmapFactory.decodeByteArray(data, 0, data.length);
125 Drawable avatar = new BitmapDrawable(b);