OSDN Git Service

90d04336ecb04996ce344173ffaf194e74360a71
[android-x86/packages-apps-IM.git] / src / com / android / im / app / DatabaseUtils.java
1 /*
2  * Copyright (C) 2008 Esmertec AG.
3  * Copyright (C) 2008 The Android Open Source Project
4  *
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package com.android.im.app;
19
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;
30
31 public class DatabaseUtils {
32     private DatabaseUtils() {
33     }
34
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()) {
41             c.close();
42             return null;
43         }
44         return c;
45     }
46
47     public static Drawable getAvatarFromCursor(Cursor cursor, int dataColumn) {
48         byte[] rawData = cursor.getBlob(dataColumn);
49         if (rawData == null) {
50             return null;
51         }
52         return decodeAvatar(rawData);
53     }
54
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();
60     }
61
62     public static Drawable getAvatarFromCursor(Cursor cursor, int dataColumn,
63             int encodedDataColumn, String username, boolean updateBlobUseCursor,
64             ContentResolver resolver, Uri updateBlobUri) {
65         /**
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
76          * encoded column.
77          */
78         byte[] rawData = cursor.getBlob(dataColumn);
79
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!");
85                 return null;
86             }
87
88             rawData = android.os.Base64Utils.decodeBase64(encodedData);
89
90             // if (DBG) {
91             // log("getAvatarFromCursor for " + username + ": found encoded
92             // data,"
93             // + " update blob with data, len=" + rawData.length);
94             // }
95
96             if (updateBlobUseCursor) {
97                 cursor.updateBlob(dataColumn, rawData);
98                 cursor.updateString(encodedDataColumn, null);
99                 cursor.commitUpdates();
100             } else {
101                 updateAvatarBlob(resolver, updateBlobUri, rawData, username);
102             }
103         }
104
105         return decodeAvatar(rawData);
106     }
107
108     private static void updateAvatarBlob(ContentResolver resolver, Uri updateUri, byte[] data,
109             String username) {
110         ContentValues values = new ContentValues(3);
111         values.put(Im.Avatars.DATA, data);
112
113         StringBuilder buf = new StringBuilder(Im.Avatars.CONTACT);
114         buf.append("=?");
115
116         String[] selectionArgs = new String[] {
117             username
118         };
119
120         resolver.update(updateUri, values, buf.toString(), selectionArgs);
121     }
122
123     private static Drawable decodeAvatar(byte[] data) {
124         Bitmap b = BitmapFactory.decodeByteArray(data, 0, data.length);
125         Drawable avatar = new BitmapDrawable(b);
126         return avatar;
127     }
128 }