My favorites | Sign in
Logo
                
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
/*
* Copyright (C) 2008 Google Inc.
*
* 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.google.android.photostream;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpVersion;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.params.HttpParams;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.net.URL;

import android.util.Xml;
import android.view.InflateException;
import android.net.Uri;
import android.os.Parcelable;
import android.os.Parcel;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

/**
* Utility class to interact with the Flickr REST-based web services.
*
* This class uses a default Flickr API key that you should replace with your own if
* you reuse this code to redistribute it with your application(s).
*
* This class is used as a singleton and cannot be instanciated. Instead, you must use
* {@link #get()} to retrieve the unique instance of this class.
*/
class Flickr {
static final String LOG_TAG = "Photostream";

static final String URI_SCHEME = "flickr";
static final String URI_PHOTOS_AUTHORITY = "photos";

// IMPORTANT: Replace this Flickr API key with your own
private static final String API_KEY = "730e3a4f253b30adf30177df803d38c4";

private static final String API_REST_HOST = "api.flickr.com";
private static final String API_REST_URL = "/services/rest/";
private static final String API_FEED_URL = "/services/feeds/photos_public.gne";

private static final String API_PEOPLE_FIND_BY_USERNAME = "flickr.people.findByUsername";
private static final String API_PEOPLE_GET_INFO = "flickr.people.getInfo";
private static final String API_PEOPLE_GET_PUBLIC_PHOTOS = "flickr.people.getPublicPhotos";
private static final String API_PEOPLE_GET_LOCATION = "flickr.photos.geo.getLocation";

private static final String PARAM_API_KEY = "api_key";
private static final String PARAM_METHOD= "method";
private static final String PARAM_USERNAME = "username";
private static final String PARAM_USERID = "user_id";
private static final String PARAM_PER_PAGE = "per_page";
private static final String PARAM_PAGE = "page";
private static final String PARAM_EXTRAS = "extras";
private static final String PARAM_PHOTO_ID = "photo_id";
private static final String PARAM_FEED_ID = "id";
private static final String PARAM_FEED_FORMAT = "format";

private static final String VALUE_DEFAULT_EXTRAS = "date_taken";
private static final String VALUE_DEFAULT_FORMAT = "atom";

private static final String RESPONSE_TAG_RSP = "rsp";
private static final String RESPONSE_ATTR_STAT = "stat";
private static final String RESPONSE_STATUS_OK = "ok";

private static final String RESPONSE_TAG_USER = "user";
private static final String RESPONSE_ATTR_NSID = "nsid";

private static final String RESPONSE_TAG_PHOTOS = "photos";
private static final String RESPONSE_ATTR_PAGE = "page";
private static final String RESPONSE_ATTR_PAGES = "pages";

private static final String RESPONSE_TAG_PHOTO = "photo";
private static final String RESPONSE_ATTR_ID = "id";
private static final String RESPONSE_ATTR_SECRET = "secret";
private static final String RESPONSE_ATTR_SERVER = "server";
private static final String RESPONSE_ATTR_FARM = "farm";
private static final String RESPONSE_ATTR_TITLE = "title";
private static final String RESPONSE_ATTR_DATE_TAKEN = "datetaken";

private static final String RESPONSE_TAG_PERSON = "person";
private static final String RESPONSE_ATTR_ISPRO = "ispro";
private static final String RESPONSE_ATTR_ICONSERVER = "iconserver";
private static final String RESPONSE_ATTR_ICONFARM = "iconfarm";
private static final String RESPONSE_TAG_USERNAME = "username";
private static final String RESPONSE_TAG_REALNAME = "realname";
private static final String RESPONSE_TAG_LOCATION = "location";
private static final String RESPONSE_ATTR_LATITUDE = "latitude";
private static final String RESPONSE_ATTR_LONGITUDE = "longitude";
private static final String RESPONSE_TAG_PHOTOSURL = "photosurl";
private static final String RESPONSE_TAG_PROFILEURL = "profileurl";
private static final String RESPONSE_TAG_MOBILEURL = "mobileurl";

private static final String RESPONSE_TAG_FEED = "feed";
private static final String RESPONSE_TAG_UPDATED = "updated";

private static final String PHOTO_IMAGE_URL = "http://farm%s.static.flickr.com/%s/%s_%s%s.jpg";
private static final String BUDDY_ICON_URL =
"http://farm%s.static.flickr.com/%s/buddyicons/%s.jpg";
private static final String DEFAULT_BUDDY_ICON_URL =
"http://www.flickr.com/images/buddyicon.jpg";

private static final int IO_BUFFER_SIZE = 4 * 1024;

private static final boolean FLAG_DECODE_PHOTO_STREAM_WITH_SKIA = false;

private static final Flickr sInstance = new Flickr();

private HttpClient mClient;

/**
* Defines the size of the image to download from Flickr.
*
* @see com.google.android.photostream.Flickr.Photo
*/
enum PhotoSize {
/**
* Small square image (75x75 px).
*/
SMALL_SQUARE("_s", 75),
/**
* Thumbnail image (the longest side measures 100 px).
*/
THUMBNAIL("_t", 100),
/**
* Small image (the longest side measures 240 px).
*/
SMALL("_m", 240),
/**
* Medium image (the longest side measures 500 px).
*/
MEDIUM("", 500),
/**
* Large image (the longest side measures 1024 px).
*/
LARGE("_b", 1024);

private final String mSize;
private final int mLongSide;

private PhotoSize(String size, int longSide) {
mSize = size;
mLongSide = longSide;
}

/**
* Returns the size in pixels of the longest side of the image.
*
* @return THe dimension in pixels of the longest side.
*/
int longSide() {
return mLongSide;
}

/**
* Returns the name of the size, as defined by Flickr. For instance,
* the LARGE size is defined by the String "_b".
*
* @return
*/
String size() {
return mSize;
}

@Override
public String toString() {
return name() + ", longSide=" + mLongSide;
}
}

/**
* Represents the geographical location of a photo.
*/
static class Location {
private float mLatitude;
private float mLongitude;

private Location(float latitude, float longitude) {
mLatitude = latitude;
mLongitude = longitude;
}

float getLatitude() {
return mLatitude;
}

float getLongitude() {
return mLongitude;
}
}

/**
* A Flickr user, in the strictest sense, is only defined by its NSID. The NSID
* is usually obtained by {@link Flickr#findByUserName(String)
* looking up a user by its user name}.
*
* To obtain more information about a given user, refer to the UserInfo class.
*
* @see Flickr#findByUserName(String)
* @see Flickr#getUserInfo(com.google.android.photostream.Flickr.User)
* @see com.google.android.photostream.Flickr.UserInfo
*/
static class User implements Parcelable {
private final String mId;

private User(String id) {
mId = id;
}

private User(Parcel in) {
mId = in.readString();
}

/**
* Returns the Flickr NSDID of the user. The NSID is used to identify the
* user with any operation performed on Flickr.
*
* @return The user's NSID.
*/
String getId() {
return mId;
}

/**
* Creates a new instance of this class from the specified Flickr NSID.
*
* @param id The NSID of the Flickr user.
*
* @return An instance of User whose id might not be valid.
*/
static User fromId(String id) {
return new User(id);
}

@Override
public String toString() {
return "User[" + mId + "]";
}

public int describeContents() {
return 0;
}

public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mId);
}

public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() {
public User createFromParcel(Parcel in) {
return new User(in);
}

public User[] newArray(int size) {
return new User[size];
}
};
}

/**
* A set of information for a given Flickr user. The information exposed include:
* - The user's NSDID
* - The user's name
* - The user's real name
* - The user's location
* - The URL to the user's photos
* - The URL to the user's profile
* - The URL to the user's mobile web site
* - Whether the user has a pro account
*/
static class UserInfo implements Parcelable {
private String mId;
private String mUserName;
private String mRealName;
private String mLocation;
private String mPhotosUrl;
private String mProfileUrl;
private String mMobileUrl;
private boolean mIsPro;
private String mIconServer;
private String mIconFarm;

private UserInfo(String nsid) {
mId = nsid;
}

private UserInfo(Parcel in) {
mId = in.readString();
mUserName = in.readString();
mRealName = in.readString();
mLocation = in.readString();
mPhotosUrl = in.readString();
mProfileUrl = in.readString();
mMobileUrl = in.readString();
mIsPro = in.readInt() == 1;
mIconServer = in.readString();
mIconFarm = in.readString();
}

/**
* Returns the Flickr NSID that identifies this user.
*
* @return The Flickr NSID.
*/
String getId() {
return mId;
}

/**
* Returns the user's name. This is the name that the user authenticates with,
* and the name that Flickr uses in the URLs
* (for instance, http://flickr.com/photos/romainguy, where romainguy is the user
* name.)
*
* @return The user's Flickr name.
*/
String getUserName() {
return mUserName;
}

/**
* Returns the user's real name. The real name is chosen by the user when
* creating his account and might not reflect his civil name.
*
* @return The real name of the user.
*/
String getRealName() {
return mRealName;
}

/**
* Returns the user's location, if publicly exposed.
*
* @return The location of the user.
*/
String getLocation() {
return mLocation;
}

/**
* Returns the URL to the photos of the user. For instance,
* http://flickr.com/photos/romainguy.
*
* @return The URL to the photos of the user.
*/
String getPhotosUrl() {
return mPhotosUrl;
}

/**
* Returns the URL to the profile of the user. For instance,
* http://flickr.com/people/romainguy/.
*
* @return The URL to the photos of the user.
*/
String getProfileUrl() {
return mProfileUrl;
}

/**
* Returns the mobile URL of the user.
*
* @return The mobile URL of the user.
*/
String getMobileUrl() {
return mMobileUrl;
}

/**
* Indicates whether the user owns a pro account.
*
* @return true, if the user has a pro account, false otherwise.
*/
boolean isPro() {
return mIsPro;
}

/**
* Returns the URL to the user's buddy icon. The buddy icon is a 48x48
* image chosen by the user. If no icon can be found, a default image
* URL is returned.
*
* @return The URL to the user's buddy icon.
*/
String getBuddyIconUrl() {
if (mIconFarm == null || mIconServer == null || mId == null) {
return DEFAULT_BUDDY_ICON_URL;
}
return String.format(BUDDY_ICON_URL, mIconFarm, mIconServer, mId);
}

/**
* Loads the user's buddy icon as a Bitmap. The user's buddy icon is loaded
* from the URL returned by {@link #getBuddyIconUrl()}. The buddy icon is
* not cached locally.
*
* @return A 48x48 bitmap if the icon was loaded successfully or null otherwise.
*/
Bitmap loadBuddyIcon() {
Bitmap bitmap = null;
InputStream in = null;
OutputStream out = null;

try {
in = new BufferedInputStream(new URL(getBuddyIconUrl()).openStream(),
IO_BUFFER_SIZE);

if (FLAG_DECODE_PHOTO_STREAM_WITH_SKIA) {
bitmap = BitmapFactory.decodeStream(in);
} else {
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
copy(in, out);
out.flush();

final byte[] data = dataStream.toByteArray();
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
}
} catch (IOException e) {
android.util.Log.e(Flickr.LOG_TAG, "Could not load buddy icon: " + this, e);
} finally {
closeStream(in);
closeStream(out);
}

return bitmap;
}

@Override
public String toString() {
return mRealName + " (" + mUserName + ", " + mId + ")";
}

public int describeContents() {
return 0;
}

public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mId);
dest.writeString(mUserName);
dest.writeString(mRealName);
dest.writeString(mLocation);
dest.writeString(mPhotosUrl);
dest.writeString(mProfileUrl);
dest.writeString(mMobileUrl);
dest.writeInt(mIsPro ? 1 : 0);
dest.writeString(mIconServer);
dest.writeString(mIconFarm);
}

public static final Parcelable.Creator<UserInfo> CREATOR =
new Parcelable.Creator<UserInfo>() {
public UserInfo createFromParcel(Parcel in) {
return new UserInfo(in);
}

public UserInfo[] newArray(int size) {
return new UserInfo[size];
}
};
}

/**
* A photo is represented by a title, the date at which it was taken and a URL.
* The URL depends on the desired {@link com.google.android.photostream.Flickr.PhotoSize}.
*/
static class Photo implements Parcelable {
private String mId;
private String mSecret;
private String mServer;
private String mFarm;
private String mTitle;
private String mDate;

private Photo() {
}

private Photo(Parcel in) {
mId = in.readString();
mSecret = in.readString();
mServer = in.readString();
mFarm = in.readString();
mTitle = in.readString();
mDate = in.readString();
}

/**
* Returns the title of the photo, if specified.
*
* @return The title of the photo. The returned value can be empty or null.
*/
String getTitle() {
return mTitle;
}

/**
* Returns the date at which the photo was taken, formatted in the current locale
* with the following pattern: MMMM d, yyyy.
*
* @return The title of the photo. The returned value can be empty or null.
*/
String getDate() {
return mDate;
}

/**
* Returns the URL to the photo for the specified size.
*
* @param photoSize The required size of the photo.
*
* @return A URL to the photo for the specified size.
*
* @see com.google.android.photostream.Flickr.PhotoSize
*/
String getUrl(PhotoSize photoSize) {
return String.format(PHOTO_IMAGE_URL, mFarm, mServer, mId, mSecret, photoSize.size());
}

/**
* Loads a Bitmap representing the photo for the specified size. The Bitmap is loaded
* from the URL returned by
* {@link #getUrl(com.google.android.photostream.Flickr.PhotoSize)}.
*
* @param size The size of the photo to load.
*
* @return A Bitmap whose longest size is the same as the longest side of the
* specified {@link com.google.android.photostream.Flickr.PhotoSize}, or null
* if the photo could not be loaded.
*/
Bitmap loadPhotoBitmap(PhotoSize size) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;

try {
in = new BufferedInputStream(new URL(getUrl(size)).openStream(),
IO_BUFFER_SIZE);

if (FLAG_DECODE_PHOTO_STREAM_WITH_SKIA) {
bitmap = BitmapFactory.decodeStream(in);
} else {
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
copy(in, out);
out.flush();

final byte[] data = dataStream.toByteArray();
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
}
} catch (IOException e) {
android.util.Log.e(Flickr.LOG_TAG, "Could not load photo: " + this, e);
} finally {
closeStream(in);
closeStream(out);
}

return bitmap;
}

@Override
public String toString() {
return mTitle + ", " + mDate + " @" + mId;
}

public int describeContents() {
return 0;
}

public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mId);
dest.writeString(mSecret);
dest.writeString(mServer);
dest.writeString(mFarm);
dest.writeString(mTitle);
dest.writeString(mDate);
}

public static final Parcelable.Creator<Photo> CREATOR = new Parcelable.Creator<Photo>() {
public Photo createFromParcel(Parcel in) {
return new Photo(in);
}

public Photo[] newArray(int size) {
return new Photo[size];
}
};
}

/**
* A list of {@link com.google.android.photostream.Flickr.Photo photos}. A list
* represents a series of photo on a page from the user's photostream, a list is
* therefore associated with a page index and a page count. The page index and the
* page count both depend on the number of photos per page.
*/
static class PhotoList {
private ArrayList<Photo> mPhotos;
private int mPage;
private int mPageCount;

private void add(Photo photo) {
mPhotos.add(photo);
}

/**
* Returns the photo at the specified index in the current set. An
* {@link ArrayIndexOutOfBoundsException} can be thrown if the index is
* less than 0 or greater then or equals to {@link #getCount()}.
*
* @param index The index of the photo to retrieve from the list.
*
* @return A valid {@link com.google.android.photostream.Flickr.Photo}.
*/
public Photo get(int index) {
return mPhotos.get(index);
}

/**
* Returns the number of photos in the list.
*
* @return A positive integer, or 0 if the list is empty.
*/
public int getCount() {
return mPhotos.size();
}

/**
* Returns the page index of the photos from this list.
*
* @return The index of the Flickr page that contains the photos of this list.
*/
public int getPage() {
return mPage;
}

/**
* Returns the total number of photo pages.
*
* @return A positive integer, or 0 if the photostream is empty.
*/
public int getPageCount() {
return mPageCount;
}
}

/**
* Returns the unique instance of this class.
*
* @return The unique instance of this class.
*/
static Flickr get() {
return sInstance;
}

private Flickr() {
final HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");

final SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));

final ThreadSafeClientConnManager manager =
new ThreadSafeClientConnManager(params, registry);

mClient = new DefaultHttpClient(manager, params);
}

/**
* Finds a user by its user name. This method will return an instance of
* {@link com.google.android.photostream.Flickr.User} containing the user's
* NSID, or null if the user could not be found.
*
* The returned User contains only the user's NSID. To retrieve more information
* about the user, please refer to
* {@link #getUserInfo(com.google.android.photostream.Flickr.User)}
*
* @param userName The name of the user to find.
*
* @return A User instance with a valid NSID, or null if the user cannot be found.
*
* @see #getUserInfo(com.google.android.photostream.Flickr.User)
* @see com.google.android.photostream.Flickr.User
* @see com.google.android.photostream.Flickr.UserInfo
*/
User findByUserName(String userName) {
final Uri.Builder uri = buildGetMethod(API_PEOPLE_FIND_BY_USERNAME);
uri.appendQueryParameter(PARAM_USERNAME, userName);

final HttpGet get = new HttpGet(uri.build().toString());
final String[] userId = new String[1];

try {
executeRequest(get, new ResponseHandler() {
public void handleResponse(InputStream in) throws IOException {
parseResponse(in, new ResponseParser() {
public void parseResponse(XmlPullParser parser)
throws XmlPullParserException, IOException {
parseUser(parser, userId);
}
});
}
});

if (userId[0] != null) {
return new User(userId[0]);
}
} catch (IOException e) {
android.util.Log.e(LOG_TAG, "Could not find the user with name: " + userName);
}

return null;
}

/**
* Retrieves a public set of information about the specified user. The user can
* either be {@link com.google.android.photostream.Flickr.User#fromId(String) created manually}
* or {@link #findByUserName(String) obtained from a user name}.
*
* @param user The user, whose NSID is valid, to retrive public information for.
*
* @return An instance of {@link com.google.android.photostream.Flickr.UserInfo} or null
* if the user could not be found.
*
* @see com.google.android.photostream.Flickr.UserInfo
* @see com.google.android.photostream.Flickr.User
* @see #findByUserName(String)
*/
UserInfo getUserInfo(User user) {
final String nsid = user.getId();
final Uri.Builder uri = buildGetMethod(API_PEOPLE_GET_INFO);
uri.appendQueryParameter(PARAM_USERID, nsid);

final HttpGet get = new HttpGet(uri.build().toString());

try {
final UserInfo info = new UserInfo(nsid);

executeRequest(get, new ResponseHandler() {
public void handleResponse(InputStream in) throws IOException {
parseResponse(in, new ResponseParser() {
public void parseResponse(XmlPullParser parser)
throws XmlPullParserException, IOException {
parseUserInfo(parser, info);
}
});
}
});

return info;
} catch (IOException e) {
android.util.Log.e(LOG_TAG, "Could not find the user with id: " + nsid);
}

return null;
}

/**
* Retrives a list of photos for the specified user. The list contains at most the
* number of photos specified by <code>perPage</code>. The photos are retrieved
* starting a the specified page index. For instance, if a user has 10 photos in
* his photostream, calling getPublicPhotos(user, 5, 2) will return the last 5 photos
* of the photo stream.
*
* The page index starts at 1, not 0.
*
* @param user The user to retrieve photos from.
* @param perPage The maximum number of photos to retrieve.
* @param page The index (starting at 1) of the page in the photostream.
*
* @return A list of at most perPage photos.
*
* @see com.google.android.photostream.Flickr.Photo
* @see com.google.android.photostream.Flickr.PhotoList
* @see #downloadPhoto(com.google.android.photostream.Flickr.Photo,
* com.google.android.photostream.Flickr.PhotoSize, java.io.OutputStream)
*/
PhotoList getPublicPhotos(User user, int perPage, int page) {
final Uri.Builder uri = buildGetMethod(API_PEOPLE_GET_PUBLIC_PHOTOS);
uri.appendQueryParameter(PARAM_USERID, user.getId());
uri.appendQueryParameter(PARAM_PER_PAGE, String.valueOf(perPage));
uri.appendQueryParameter(PARAM_PAGE, String.valueOf(page));
uri.appendQueryParameter(PARAM_EXTRAS, VALUE_DEFAULT_EXTRAS);

final HttpGet get = new HttpGet(uri.build().toString());
final PhotoList photos = new PhotoList();

try {
executeRequest(get, new ResponseHandler() {
public void handleResponse(InputStream in) throws IOException {
parseResponse(in, new ResponseParser() {
public void parseResponse(XmlPullParser parser)
throws XmlPullParserException, IOException {
parsePhotos(parser, photos);
}
});
}
});
} catch (IOException e) {
android.util.Log.e(LOG_TAG, "Could not find photos for user: " + user);
}

return photos;
}

/**
* Retrieves the geographical location of the specified photo. If the photo
* has no geodata associated with it, this method returns null.
*
* @param photo The photo to get the location of.
*
* @return The geo location of the photo, or null if the photo has no geodata
* or the photo cannot be found.
*
* @see com.google.android.photostream.Flickr.Location
*/
Location getLocation(Flickr.Photo photo) {
final Uri.Builder uri = buildGetMethod(API_PEOPLE_GET_LOCATION);
uri.appendQueryParameter(PARAM_PHOTO_ID, photo.mId);

final HttpGet get = new HttpGet(uri.build().toString());
final Location location = new Location(0.0f, 0.0f);

try {
executeRequest(get, new ResponseHandler() {
public void handleResponse(InputStream in) throws IOException {
parseResponse(in, new ResponseParser() {
public void parseResponse(XmlPullParser parser)
throws XmlPullParserException, IOException {
parsePhotoLocation(parser, location);
}
});
}
});
return location;
} catch (IOException e) {
android.util.Log.e(LOG_TAG, "Could not find location for photo: " + photo);
}

return null;
}

/**
* Checks the specified user's feed to see if any updated occured after the
* specified date.
*
* @param user The user whose feed must be checked.
* @param reference The date after which to check for updates.
*
* @return True if any update occured after the reference date, false otherwise.
*/
boolean hasUpdates(User user, final Calendar reference) {
final Uri.Builder uri = new Uri.Builder();
uri.path(API_FEED_URL);
uri.appendQueryParameter(PARAM_FEED_ID, user.getId());
uri.appendQueryParameter(PARAM_FEED_FORMAT, VALUE_DEFAULT_FORMAT);

final HttpGet get = new HttpGet(uri.build().toString());
final boolean[] updated = new boolean[1];

try {
executeRequest(get, new ResponseHandler() {
public void handleResponse(InputStream in) throws IOException {
parseFeedResponse(in, new ResponseParser() {
public void parseResponse(XmlPullParser parser)
throws XmlPullParserException, IOException {
updated[0] = parseUpdated(parser, reference);
}
});
}
});
} catch (IOException e) {
android.util.Log.e(LOG_TAG, "Could not find feed for user: " + user);
}

return updated[0];
}

/**
* Downloads the specified photo at the specified size in the specified destination.
*
* @param photo The photo to download.
* @param size The size of the photo to download.
* @param destination The output stream in which to write the downloaded photo.
*
* @throws IOException If any network exception occurs during the download.
*/
void downloadPhoto(Photo photo, PhotoSize size, OutputStream destination) throws IOException {
final BufferedOutputStream out = new BufferedOutputStream(destination, IO_BUFFER_SIZE);
final String url = photo.getUrl(size);
final HttpGet get = new HttpGet(url);

HttpEntity entity = null;
try {
final HttpResponse response = mClient.execute(get);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
entity = response.getEntity();
entity.writeTo(out);
out.flush();
}
} finally {
if (entity != null) {
entity.consumeContent();
}
}
}

private boolean parseUpdated(XmlPullParser parser, Calendar reference) throws IOException,
XmlPullParserException {

int type;
String name;
final int depth = parser.getDepth();

while (((type = parser.next()) != XmlPullParser.END_TAG ||
parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
if (type != XmlPullParser.START_TAG) {
continue;
}

name = parser.getName();
if (RESPONSE_TAG_UPDATED.equals(name)) {
if (parser.next() == XmlPullParser.TEXT) {
final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
final String text = parser.getText().replace('T', ' ').replace('Z', ' ');
final Calendar calendar = new GregorianCalendar();
calendar.setTimeInMillis(format.parse(text).getTime());

return calendar.after(reference);
} catch (ParseException e) {
// Ignore
}
}
}
}

return false;
}

private void parsePhotos(XmlPullParser parser, PhotoList photos)
throws XmlPullParserException, IOException {
int type;
String name;
SimpleDateFormat parseFormat = null;
SimpleDateFormat outputFormat = null;

final int depth = parser.getDepth();

while (((type = parser.next()) != XmlPullParser.END_TAG ||
parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
if (type != XmlPullParser.START_TAG) {
continue;
}

name = parser.getName();
if (RESPONSE_TAG_PHOTOS.equals(name)) {
photos.mPage = Integer.parseInt(parser.getAttributeValue(null, RESPONSE_ATTR_PAGE));
photos.mPageCount = Integer.parseInt(parser.getAttributeValue(null,
RESPONSE_ATTR_PAGES));
photos.mPhotos = new ArrayList<Photo>();
} else if (RESPONSE_TAG_PHOTO.equals(name)) {
final Photo photo = new Photo();
photo.mId = parser.getAttributeValue(null, RESPONSE_ATTR_ID);
photo.mSecret = parser.getAttributeValue(null, RESPONSE_ATTR_SECRET);
photo.mServer = parser.getAttributeValue(null, RESPONSE_ATTR_SERVER);
photo.mFarm = parser.getAttributeValue(null, RESPONSE_ATTR_FARM);
photo.mTitle = parser.getAttributeValue(null, RESPONSE_ATTR_TITLE);
photo.mDate = parser.getAttributeValue(null, RESPONSE_ATTR_DATE_TAKEN);

if (parseFormat == null) {
parseFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
outputFormat = new SimpleDateFormat("MMMM d, yyyy");
}

try {
photo.mDate = outputFormat.format(parseFormat.parse(photo.mDate));
} catch (ParseException e) {
android.util.Log.w(LOG_TAG, "Could not parse photo date", e);
}

photos.add(photo);
}
}
}

private void parsePhotoLocation(XmlPullParser parser, Location location)
throws XmlPullParserException, IOException {
int type;
String name;
final int depth = parser.getDepth();

while (((type = parser.next()) != XmlPullParser.END_TAG ||
parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
if (type != XmlPullParser.START_TAG) {
continue;
}

name = parser.getName();
if (RESPONSE_TAG_LOCATION.equals(name)) {
try {
location.mLatitude = Float.parseFloat(parser.getAttributeValue(null,
RESPONSE_ATTR_LATITUDE));
location.mLongitude = Float.parseFloat(parser.getAttributeValue(null,
RESPONSE_ATTR_LONGITUDE));
} catch (NumberFormatException e) {
throw new XmlPullParserException("Could not parse lat/lon", parser, e);
}
}
}
}

private void parseUser(XmlPullParser parser, String[] userId)
throws XmlPullParserException, IOException {
int type;
String name;
final int depth = parser.getDepth();

while (((type = parser.next()) != XmlPullParser.END_TAG ||
parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
if (type != XmlPullParser.START_TAG) {
continue;
}

name = parser.getName();
if (RESPONSE_TAG_USER.equals(name)) {
userId[0] = parser.getAttributeValue(null, RESPONSE_ATTR_NSID);
}
}
}

private void parseUserInfo(XmlPullParser parser, UserInfo info)
throws XmlPullParserException, IOException {
int type;
String name;
final int depth = parser.getDepth();

while (((type = parser.next()) != XmlPullParser.END_TAG ||
parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
if (type != XmlPullParser.START_TAG) {
continue;
}

name = parser.getName();
if (RESPONSE_TAG_PERSON.equals(name)) {
info.mIsPro = "1".equals(parser.getAttributeValue(null, RESPONSE_ATTR_ISPRO));
info.mIconServer = parser.getAttributeValue(null, RESPONSE_ATTR_ICONSERVER);
info.mIconFarm = parser.getAttributeValue(null, RESPONSE_ATTR_ICONFARM);
} else if (RESPONSE_TAG_USERNAME.equals(name)) {
if (parser.next() == XmlPullParser.TEXT) {
info.mUserName = parser.getText();
}
} else if (RESPONSE_TAG_REALNAME.equals(name)) {
if (parser.next() == XmlPullParser.TEXT) {
info.mRealName = parser.getText();
}
} else if (RESPONSE_TAG_LOCATION.equals(name)) {
if (parser.next() == XmlPullParser.TEXT) {
info.mLocation = parser.getText();
}
} else if (RESPONSE_TAG_PHOTOSURL.equals(name)) {
if (parser.next() == XmlPullParser.TEXT) {
info.mPhotosUrl = parser.getText();
}
} else if (RESPONSE_TAG_PROFILEURL.equals(name)) {
if (parser.next() == XmlPullParser.TEXT) {
info.mProfileUrl = parser.getText();
}
} else if (RESPONSE_TAG_MOBILEURL.equals(name)) {
if (parser.next() == XmlPullParser.TEXT) {
info.mMobileUrl = parser.getText();
}
}
}
}

/**
* Parses a valid Flickr XML response from the specified input stream. When the Flickr
* response contains the OK tag, the response is sent to the specified response parser.
*
* @param in The input stream containing the response sent by Flickr.
* @param responseParser The parser to use when the response is valid.
*
* @throws IOException
*/
private void parseResponse(InputStream in, ResponseParser responseParser) throws IOException {
final XmlPullParser parser = Xml.newPullParser();
try {
parser.setInput(new InputStreamReader(in));

int type;
while ((type = parser.next()) != XmlPullParser.START_TAG &&
type != XmlPullParser.END_DOCUMENT) {
// Empty
}

if (type != XmlPullParser.START_TAG) {
throw new InflateException(parser.getPositionDescription()
+ ": No start tag found!");
}

String name = parser.getName();
if (RESPONSE_TAG_RSP.equals(name)) {
final String value = parser.getAttributeValue(null, RESPONSE_ATTR_STAT);
if (!RESPONSE_STATUS_OK.equals(value)) {
throw new IOException("Wrong status: " + value);
}
}

responseParser.parseResponse(parser);

} catch (XmlPullParserException e) {
final IOException ioe = new IOException("Could not parser the response");
ioe.initCause(e);
throw ioe;
}
}

/**
* Parses a valid Flickr Atom feed response from the specified input stream.
*
* @param in The input stream containing the response sent by Flickr.
* @param responseParser The parser to use when the response is valid.
*
* @throws IOException
*/
private void parseFeedResponse(InputStream in, ResponseParser responseParser)
throws IOException {

final XmlPullParser parser = Xml.newPullParser();
try {
parser.setInput(new InputStreamReader(in));

int type;
while ((type = parser.next()) != XmlPullParser.START_TAG &&
type != XmlPullParser.END_DOCUMENT) {
// Empty
}

if (type != XmlPullParser.START_TAG) {
throw new InflateException(parser.getPositionDescription()
+ ": No start tag found!");
}

String name = parser.getName();
if (RESPONSE_TAG_FEED.equals(name)) {
responseParser.parseResponse(parser);
} else {
throw new IOException("Wrong start tag: " + name);
}

} catch (XmlPullParserException e) {
final IOException ioe = new IOException("Could not parser the response");
ioe.initCause(e);
throw ioe;
}
}

/**
* Executes an HTTP request on Flickr's web service. If the response is ok, the content
* is sent to the specified response handler.
*
* @param get The GET request to executed.
* @param handler The handler which will parse the response.
*
* @throws IOException
*/
private void executeRequest(HttpGet get, ResponseHandler handler) throws IOException {
HttpEntity entity = null;
HttpHost host = new HttpHost(API_REST_HOST, 80, "http");
try {
final HttpResponse response = mClient.execute(host, get);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
entity = response.getEntity();
final InputStream in = entity.getContent();
handler.handleResponse(in);
}
} finally {
if (entity != null) {
entity.consumeContent();
}
}
}

/**
* Builds an HTTP GET request for the specified Flickr API method. The returned request
* contains the web service path, the query parameter for the API KEY and the query
* parameter for the specified method.
*
* @param method The Flickr API method to invoke.
*
* @return A Uri.Builder containing the GET path, the API key and the method already
* encoded.
*/
private static Uri.Builder buildGetMethod(String method) {
final Uri.Builder builder = new Uri.Builder();
builder.path(API_REST_URL).appendQueryParameter(PARAM_API_KEY, API_KEY);
builder.appendQueryParameter(PARAM_METHOD, method);
return builder;
}

/**
* Copy the content of the input stream into the output stream, using a temporary
* byte array buffer whose size is defined by {@link #IO_BUFFER_SIZE}.
*
* @param in The input stream to copy from.
* @param out The output stream to copy to.
*
* @throws IOException If any error occurs during the copy.
*/
private static void copy(InputStream in, OutputStream out) throws IOException {
byte[] b = new byte[IO_BUFFER_SIZE];
int read;
while ((read = in.read(b)) != -1) {
out.write(b, 0, read);
}
}

/**
* Closes the specified stream.
*
* @param stream The stream to close.
*/
private static void closeStream(Closeable stream) {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
android.util.Log.e(Flickr.LOG_TAG, "Could not close stream", e);
}
}
}

/**
* Response handler used with
* {@link Flickr#executeRequest(org.apache.http.client.methods.HttpGet,
* com.google.android.photostream.Flickr.ResponseHandler)}. The handler is invoked when
* a response is sent by the server. The response is made available as an input stream.
*/
private static interface ResponseHandler {
/**
* Processes the responses sent by the HTTP server following a GET request.
*
* @param in The stream containing the server's response.
*
* @throws IOException
*/
public void handleResponse(InputStream in) throws IOException;
}

/**
* Response parser used with {@link Flickr#parseResponse(java.io.InputStream,
* com.google.android.photostream.Flickr.ResponseParser)}. When Flickr returns a valid
* response, this parser is invoked to process the XML response.
*/
private static interface ResponseParser {
/**
* Processes the XML response sent by the Flickr web service after a successful
* request.
*
* @param parser The parser containing the XML responses.
*
* @throws XmlPullParserException
* @throws IOException
*/
public void parseResponse(XmlPullParser parser) throws XmlPullParserException, IOException;
}
}
Show details Hide details

Change log

r55 by romain...@google.com on Sep 17, 2008   Diff
Fixed a few bugs
Go to: 
Project members, sign in to write a code review

Older revisions

r53 by romain...@google.com on Sep 16, 2008   Diff
Added a contacts list, added a service
that checks for new photos every 24
hours and notifies the user
r52 by romain...@google.com on Sep 11, 2008   Diff
Add the ability to display the photo
on the Radar activity
r21 by romain...@google.com on Sep 02, 2008   Diff
More robust network code. Instead of
decoding the photos input streams
using Skia, use a temporary in-memory
byte array. This seems to fix the
flakyness of the images download over
...
All revisions of this file

File info

Size: 47244 bytes, 1305 lines