summaryrefslogtreecommitdiff
path: root/src/com/android/im/app/DatabaseUtils.java
blob: 90d04336ecb04996ce344173ffaf194e74360a71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * Copyright (C) 2008 Esmertec AG.
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.im.app;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.provider.Im;

public class DatabaseUtils {
    private DatabaseUtils() {
    }

    public static Cursor queryAccountsForProvider(ContentResolver cr,
            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;
    }

    public static Drawable getAvatarFromCursor(Cursor cursor, int dataColumn) {
        byte[] rawData = cursor.getBlob(dataColumn);
        if (rawData == null) {
            return null;
        }
        return decodeAvatar(rawData);
    }

    public static Uri getAvatarUri(Uri baseUri, long providerId, long accountId) {
        Uri.Builder builder = baseUri.buildUpon();
        ContentUris.appendId(builder, providerId);
        ContentUris.appendId(builder, accountId);
        return builder.build();
    }

    public static Drawable getAvatarFromCursor(Cursor cursor, int dataColumn,
            int encodedDataColumn, String username, boolean updateBlobUseCursor,
            ContentResolver resolver, 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);
    }

    private static void updateAvatarBlob(ContentResolver resolver, Uri updateUri, byte[] data,
            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);
    }

    private static Drawable decodeAvatar(byte[] data) {
        Bitmap b = BitmapFactory.decodeByteArray(data, 0, data.length);
        Drawable avatar = new BitmapDrawable(b);
        return avatar;
    }
}