summaryrefslogtreecommitdiffstats
path: root/src/com/android/dreams/phototable/PicasaSource.java
blob: bb1f1ec2325d7be801e803c20c9d78613a5f21df (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
 * Copyright (C) 2012 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.dreams.phototable;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Set;

/**
 * Loads images from Picasa.
 */
public class PicasaSource extends PhotoSource {
    private static final String TAG = "PhotoTable.PicasaSource";

    private static final String PICASA_AUTHORITY =
            "com.google.android.gallery3d.GooglePhotoProvider";

    private static final String PICASA_PHOTO_PATH = "photos";
    private static final String PICASA_ALBUM_PATH = "albums";
    private static final String PICASA_USER_PATH = "users";

    private static final String PICASA_ID = "_id";
    private static final String PICASA_URL = "content_url";
    private static final String PICASA_ROTATION = "rotation";
    private static final String PICASA_ALBUM_ID = "album_id";
    private static final String PICASA_TITLE = "title";
    private static final String PICASA_THUMB = "thumbnail_url";
    private static final String PICASA_ALBUM_TYPE = "album_type";
    private static final String PICASA_ALBUM_USER = "user_id";
    private static final String PICASA_ALBUM_UPDATED = "date_updated";
    private static final String PICASA_ACCOUNT = "account";

    private static final String PICASA_URL_KEY = "content_url";
    private static final String PICASA_TYPE_KEY = "type";
    private static final String PICASA_TYPE_FULL_VALUE = "full";
    private static final String PICASA_TYPE_SCREEN_VALUE = "screennail";
    private static final String PICASA_TYPE_THUMB_VALUE = "thumbnail";
    private static final String PICASA_TYPE_IMAGE_VALUE = "image";
    private static final String PICASA_POSTS_TYPE = "Buzz";
    private static final String PICASA_UPLOAD_TYPE = "InstantUpload";

    private final int mMaxPostAblums;
    private final String mPostsAlbumName;
    private final String mUploadsAlbumName;
    private final String mUnknownAlbumName;

    private Set<String> mFoundAlbumIds;
    private int mNextPosition;

    public PicasaSource(Context context, SharedPreferences settings) {
        super(context, settings);
        mSourceName = TAG;
        mNextPosition = -1;
        mMaxPostAblums = mResources.getInteger(R.integer.max_post_albums);
        mPostsAlbumName = mResources.getString(R.string.posts_album_name, "Posts");
        mUploadsAlbumName = mResources.getString(R.string.uploads_album_name, "Instant Uploads");
        mUnknownAlbumName = mResources.getString(R.string.unknown_album_name, "Unknown");
        fillQueue();
    }

    @Override
    protected Collection<ImageData> findImages(int howMany) {
        log(TAG, "finding images");
        LinkedList<ImageData> foundImages = new LinkedList<ImageData>();
        String[] projection = {PICASA_ID, PICASA_URL, PICASA_ROTATION, PICASA_ALBUM_ID};
        boolean usePosts = false;
        LinkedList<String> albumIds = new LinkedList<String>();
        for (String id : getFoundAlbums()) {
            if (mSettings.isAlbumEnabled(id)) {
                String[] parts = id.split(":");
                if (parts.length > 2) {
                    albumIds.addAll(resolveAlbumIds(id));
                } else {
                    albumIds.add(parts[1]);
                }
            }
        }

        StringBuilder selection = new StringBuilder();
        for (String albumId : albumIds) {
            if (albumIds.size() < mMaxPostAblums) {
                if (selection.length() > 0) {
                    selection.append(" OR ");
                }
                selection.append(PICASA_ALBUM_ID + " = '" + albumId + "'");
                log(TAG, "adding: " + albumId);
            }
        }

        if (selection.length() == 0) {
            return foundImages;
        }

        log(TAG, "selection is: " + selection.toString());

        Uri.Builder picasaUriBuilder = new Uri.Builder()
                .scheme("content")
                .authority(PICASA_AUTHORITY)
                .appendPath(PICASA_PHOTO_PATH);
        Cursor cursor = mResolver.query(picasaUriBuilder.build(),
                projection, selection.toString(), null, null);
        if (cursor != null) {
            if (cursor.getCount() > howMany && mNextPosition == -1) {
                mNextPosition =
                        (int) Math.abs(mRNG.nextInt() % (cursor.getCount() - howMany));
            }
            if (mNextPosition == -1) {
                mNextPosition = 0;
            }
            log(TAG, "moving to position: " + mNextPosition);
            cursor.moveToPosition(mNextPosition);

            int idIndex = cursor.getColumnIndex(PICASA_ID);
            int urlIndex = cursor.getColumnIndex(PICASA_URL);
            int orientationIndex = cursor.getColumnIndex(PICASA_ROTATION);
            int bucketIndex = cursor.getColumnIndex(PICASA_ALBUM_ID);

            if (idIndex < 0) {
                log(TAG, "can't find the ID column!");
            } else {
                while (foundImages.size() < howMany && !cursor.isAfterLast()) {
                    if (idIndex >= 0) {
                        ImageData data = new ImageData();
                        data.id = cursor.getString(idIndex);

                        if (urlIndex >= 0) {
                            data.url = cursor.getString(urlIndex);
                        }

                        foundImages.offer(data);
                    }
                    if (cursor.moveToNext()) {
                        mNextPosition++;
                    }
                }
                if (cursor.isAfterLast()) {
                    mNextPosition = 0;
                }
            }

            cursor.close();
        }
        log(TAG, "found " + foundImages.size() + " items.");
        return foundImages;
    }

    private String resolveAccount(String id) {
        String displayName = "unknown";
        String[] projection = {PICASA_ACCOUNT};
        Uri.Builder picasaUriBuilder = new Uri.Builder()
                .scheme("content")
                .authority(PICASA_AUTHORITY)
                .appendPath(PICASA_USER_PATH)
                .appendPath(id);
        Cursor cursor = mResolver.query(picasaUriBuilder.build(),
                projection, null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();
            int accountIndex = cursor.getColumnIndex(PICASA_ACCOUNT);
            if (accountIndex >= 0) {
                displayName = cursor.getString(accountIndex);
            }
            cursor.close();
        }
        return displayName;
    }

    private Collection<String> resolveAlbumIds(String id) {
        LinkedList<String> albumIds = new LinkedList<String>();
        log(TAG, "resolving " + id);

        String[] parts = id.split(":");
        if (parts.length < 3) {
            return albumIds;
        }

        String[] projection = {PICASA_ID, PICASA_ALBUM_TYPE, PICASA_ALBUM_UPDATED,
                               PICASA_ALBUM_USER};
        String order = PICASA_ALBUM_UPDATED + " DESC";
        String selection = (PICASA_ALBUM_USER + " = '" + parts[2] + "' AND " +
                            PICASA_ALBUM_TYPE + " = '" + parts[1] + "'");
        Uri.Builder picasaUriBuilder = new Uri.Builder()
                .scheme("content")
                .authority(PICASA_AUTHORITY)
                .appendPath(PICASA_ALBUM_PATH)
                .appendQueryParameter(PICASA_TYPE_KEY, PICASA_TYPE_IMAGE_VALUE);
        Cursor cursor = mResolver.query(picasaUriBuilder.build(),
                projection, selection, null, order);
        if (cursor != null) {
            cursor.moveToFirst();

            int idIndex = cursor.getColumnIndex(PICASA_ID);
            int typeIndex = cursor.getColumnIndex(PICASA_ALBUM_TYPE);

            if (idIndex < 0) {
                log(TAG, "can't find the ID column!");
            } else {
                while (!cursor.isAfterLast()) {
                    albumIds.add(cursor.getString(idIndex));
                    cursor.moveToNext();
                }
            }
            cursor.close();
        }
        return albumIds;
    }

    private Set<String> getFoundAlbums() {
        if (mFoundAlbumIds == null) {
            findAlbums();
        }
        return mFoundAlbumIds;
    }

    @Override
    public Collection<AlbumData> findAlbums() {
        log(TAG, "finding albums");
        HashMap<String, AlbumData> foundAlbums = new HashMap<String, AlbumData>();
        HashMap<String, String> accounts = new HashMap<String, String>();
        String[] projection = {PICASA_ID, PICASA_TITLE, PICASA_THUMB, PICASA_ALBUM_TYPE,
                               PICASA_ALBUM_USER, PICASA_ALBUM_UPDATED};
        Uri.Builder picasaUriBuilder = new Uri.Builder()
                .scheme("content")
                .authority(PICASA_AUTHORITY)
                .appendPath(PICASA_ALBUM_PATH)
                .appendQueryParameter(PICASA_TYPE_KEY, PICASA_TYPE_IMAGE_VALUE);
        Cursor cursor = mResolver.query(picasaUriBuilder.build(),
                projection, null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();

            int idIndex = cursor.getColumnIndex(PICASA_ID);
            int thumbIndex = cursor.getColumnIndex(PICASA_THUMB);
            int titleIndex = cursor.getColumnIndex(PICASA_TITLE);
            int typeIndex = cursor.getColumnIndex(PICASA_ALBUM_TYPE);
            int updatedIndex = cursor.getColumnIndex(PICASA_ALBUM_UPDATED);
            int userIndex = cursor.getColumnIndex(PICASA_ALBUM_USER);

            if (idIndex < 0) {
                log(TAG, "can't find the ID column!");
            } else {
                while (!cursor.isAfterLast()) {
                    String id = TAG + ":" + cursor.getString(idIndex);
                    String user = (userIndex >= 0 ? cursor.getString(userIndex) : "-1");
                    String type = (typeIndex >= 0 ? cursor.getString(typeIndex) : "none");
                    boolean isPosts = (typeIndex >= 0 && PICASA_POSTS_TYPE.equals(type));
                    boolean isUpload = (typeIndex >= 0 && PICASA_UPLOAD_TYPE.equals(type));

                    String account = accounts.get(user);
                    if (account == null) {
                        account = resolveAccount(user);
                        accounts.put(user, account);
                    }

                    if (isPosts) {
                        id = TAG + ":" + PICASA_POSTS_TYPE + ":" + user;
                    }

                    if (isUpload) {
                        id = TAG + ":" + PICASA_UPLOAD_TYPE + ":" + user;
                    }

                    String thumbnailUrl = null;
                    long updated = 0;
                    AlbumData data = foundAlbums.get(id);
                    if (data == null) {
                        data = new AlbumData();
                        data.id = id;
                        data.account = account;

                        if (isPosts) {
                            data.title = mPostsAlbumName;
                        } else if (isUpload) {
                            data.title = mUploadsAlbumName;
                        } else if (titleIndex >= 0) {
                            data.title = cursor.getString(titleIndex);
                        } else {
                            data.title = mUnknownAlbumName;
                        }

                        if (thumbIndex >= 0) {
                            thumbnailUrl = cursor.getString(thumbIndex);
                        }

                        if (updatedIndex >= 0) {
                            updated = cursor.getLong(updatedIndex);
                        }

                        log(TAG, "found " + data.title + "(" + data.id + ")" +
                            " of type " + type + " owned by " + user);
                        foundAlbums.put(id, data);
                    }

                    if (updatedIndex >= 0) {
                        data.updated = (long) Math.max(data.updated, updated);
                    }

                    if (data.thumbnailUrl == null || data.updated == updated) {
                        data.thumbnailUrl = thumbnailUrl;
                    }

                    cursor.moveToNext();
                }
            }
            cursor.close();

        }
        log(TAG, "found " + foundAlbums.size() + " items.");
        mFoundAlbumIds = foundAlbums.keySet();
        return foundAlbums.values();
    }

    @Override
    protected InputStream getStream(ImageData data) {
        InputStream is = null;
        try {
            Uri.Builder photoUriBuilder = new Uri.Builder()
                    .scheme("content")
                    .authority(PICASA_AUTHORITY)
                    .appendPath(PICASA_PHOTO_PATH)
                    .appendPath(data.id)
                    .appendQueryParameter(PICASA_TYPE_KEY, PICASA_TYPE_FULL_VALUE);
            if (data.url != null) {
                photoUriBuilder.appendQueryParameter(PICASA_URL_KEY, data.url);
            }
            is = mResolver.openInputStream(photoUriBuilder.build());
        } catch (FileNotFoundException fnf) {
            log(TAG, "file not found: " + fnf);
            is = null;
        } catch (IOException ioe) {
            log(TAG, "i/o exception: " + ioe);
            is = null;
        }

        return is;
    }
}