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
/*
* 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 android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;
import android.content.Intent;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.widget.ImageView;
import android.widget.ViewAnimator;

import java.util.Random;
import java.util.List;

/**
* Activity used to display a Flickr user's photostream. This activity shows a fixed
* number of photos at a time. The activity is invoked either by LoginActivity, when
* the application is launched normally, or by a Home shortcut, or by an Intent with
* the view action and a flickr://photos/nsid URI.
*/
public class PhotostreamActivity extends Activity implements
View.OnClickListener, Animation.AnimationListener {

static final String ACTION = "com.google.android.photostream.FLICKR_STREAM";

static final String EXTRA_NOTIFICATION = "com.google.android.photostream.extra_notify_id";
static final String EXTRA_NSID = "com.google.android.photostream.extra_nsid";
static final String EXTRA_USER = "com.google.android.photostream.extra_user";

private static final String STATE_USER = "com.google.android.photostream.state_user";
private static final String STATE_PAGE = "com.google.android.photostream.state_page";
private static final String STATE_PAGE_COUNT = "com.google.android.photostream.state_pagecount";

private static final int PHOTOS_COUNT_PER_PAGE = 6;

private Flickr.User mUser;
private int mCurrentPage = 1;
private int mPageCount = 0;

private LayoutInflater mInflater;

private ViewAnimator mSwitcher;
private View mMenuNext;
private View mMenuBack;
private View mMenuSeparator;
private GridLayout mGrid;

private LayoutAnimationController mNextAnimation;
private LayoutAnimationController mBackAnimation;

private UserTask<?, ?, ?> mTask;
private String mUsername;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

clearNotification();

// Try to find a user name in the saved instance state or the intent
// that launched the activity. If no valid user NSID can be found, we
// just close the activity.
if (!initialize(savedInstanceState)) {
finish();
return;
}

setContentView(R.layout.screen_photostream);
setupViews();

loadPhotos();
}

private void clearNotification() {
final int notification = getIntent().getIntExtra(EXTRA_NOTIFICATION, -1);
if (notification != -1) {
NotificationManager manager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(notification);
}
}

/**
* Starts the PhotostreamActivity for the specified user.
*
* @param context The application's environment.
* @param user The user whose photos to display with a PhotostreamActivity.
*/
static void show(Context context, Flickr.User user) {
final Intent intent = new Intent(ACTION);
intent.putExtra(EXTRA_USER, user);
context.startActivity(intent);
}

/**
* Restores a previously saved state or, if missing, finds the user's NSID
* from the intent used to start the activity.
*
* @param savedInstanceState The saved state, if any.
*
* @return true if a {@link com.google.android.photostream.Flickr.User} was
* found either in the saved state or the intent.
*/
private boolean initialize(Bundle savedInstanceState) {
Flickr.User user;
if (savedInstanceState != null) {
user = savedInstanceState.getParcelable(STATE_USER);
mCurrentPage = savedInstanceState.getInt(STATE_PAGE);
mPageCount = savedInstanceState.getInt(STATE_PAGE_COUNT);
} else {
user = getUser();
}
mUser = user;
return mUser != null || mUsername != null;
}

/**
* Creates a {@link com.google.android.photostream.Flickr.User} instance
* from the intent used to start this activity.
*
* @return The user whose photos will be displayed, or null if no
* user was found.
*/
private Flickr.User getUser() {
final Intent intent = getIntent();
final String action = intent.getAction();

Flickr.User user = null;

if (ACTION.equals(action)) {
final Bundle extras = intent.getExtras();
if (extras != null) {
user = extras.getParcelable(EXTRA_USER);

if (user == null) {
final String nsid = extras.getString(EXTRA_NSID);
if (nsid != null) {
user = Flickr.User.fromId(nsid);
}
}
}
} else if (Intent.ACTION_VIEW.equals(action)) {
final List<String> segments = intent.getData().getPathSegments();
if (segments.size() > 1) {
mUsername = segments.get(1);
}
}

return user;
}

private void setupViews() {
mInflater = LayoutInflater.from(PhotostreamActivity.this);
mNextAnimation = AnimationUtils.loadLayoutAnimation(this, R.anim.layout_slide_next);
mBackAnimation = AnimationUtils.loadLayoutAnimation(this, R.anim.layout_slide_back);

mSwitcher = (ViewAnimator) findViewById(R.id.switcher_menu);
mMenuNext = findViewById(R.id.menu_next);
mMenuBack = findViewById(R.id.menu_back);
mMenuSeparator = findViewById(R.id.menu_separator);
mGrid = (GridLayout) findViewById(R.id.grid_photos);

mMenuNext.setOnClickListener(this);
mMenuBack.setOnClickListener(this);
mMenuBack.setVisibility(View.GONE);
mMenuSeparator.setVisibility(View.GONE);
mGrid.setClipToPadding(false);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);

outState.putParcelable(STATE_USER, mUser);
outState.putInt(STATE_PAGE, mCurrentPage);
outState.putInt(STATE_PAGE_COUNT, mPageCount);
}

@Override
protected void onDestroy() {
super.onDestroy();
if (mTask != null && mTask.getStatus() == UserTask.Status.RUNNING) {
mTask.cancel(true);
}
}

public void onClick(View v) {
switch (v.getId()) {
case R.id.menu_next:
onNext();
break;
case R.id.menu_back:
onBack();
break;
default:
onShowPhoto((Flickr.Photo) v.getTag());
break;
}
}

@Override
public Object onRetainNonConfigurationInstance() {
final GridLayout grid = mGrid;
final int count = grid.getChildCount();
final LoadedPhoto[] list = new LoadedPhoto[count];

for (int i = 0; i < count; i++) {
final ImageView v = (ImageView) grid.getChildAt(i);
list[i] = new LoadedPhoto(((BitmapDrawable) v.getDrawable()).getBitmap(),
(Flickr.Photo) v.getTag());
}

return list;
}

private void prepareMenu(int pageCount) {
final boolean backVisible = mCurrentPage > 1;
final boolean nextVisible = mCurrentPage < pageCount;

mMenuBack.setVisibility(backVisible ? View.VISIBLE : View.GONE);
mMenuNext.setVisibility(nextVisible ? View.VISIBLE : View.GONE);

mMenuSeparator.setVisibility(backVisible && nextVisible ? View.VISIBLE : View.GONE);
}

private void loadPhotos() {
final Object data = getLastNonConfigurationInstance();
if (data == null) {
mTask = new GetPhotoListTask().execute(mCurrentPage);
} else {
final LoadedPhoto[] photos = (LoadedPhoto[]) data;
for (LoadedPhoto photo : photos) {
addPhoto(photo);
}
prepareMenu(mPageCount);
mSwitcher.showNext();
}
}

private void showPhotos(Flickr.PhotoList photos) {
mTask = new LoadPhotosTask().execute(photos);
}

private void onShowPhoto(Flickr.Photo photo) {
ViewPhotoActivity.show(this, photo);
}

private void onNext() {
mCurrentPage++;
animateAndLoadPhotos(mNextAnimation);
}

private void onBack() {
mCurrentPage--;
animateAndLoadPhotos(mBackAnimation);
}

private void animateAndLoadPhotos(LayoutAnimationController animation) {
mSwitcher.showNext();
mGrid.setLayoutAnimationListener(this);
mGrid.setLayoutAnimation(animation);
mGrid.invalidate();
}

public void onAnimationEnd(Animation animation) {
mGrid.setLayoutAnimationListener(null);
mGrid.setLayoutAnimation(null);
mGrid.removeAllViews();
loadPhotos();
}

public void onAnimationStart(Animation animation) {
}

public void onAnimationRepeat(Animation animation) {
}

private static Animation createAnimationForChild(int childIndex) {
boolean firstColumn = (childIndex & 0x1) == 0;

Animation translate = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, firstColumn ? -1.1f : 1.1f,
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f);

translate.setInterpolator(new AccelerateDecelerateInterpolator());
translate.setFillAfter(false);
translate.setDuration(300);

return translate;
}

private void addPhoto(LoadedPhoto... value) {
ImageView image = (ImageView) mInflater.inflate(R.layout.grid_item_photo, mGrid, false);
image.setImageBitmap(value[0].mBitmap);
image.startAnimation(createAnimationForChild(mGrid.getChildCount()));
image.setTag(value[0].mPhoto);
image.setOnClickListener(PhotostreamActivity.this);
mGrid.addView(image);
}

/**
* Background task used to load each individual photo. The task loads each photo
* in order and publishes each loaded Bitmap as a progress unit. The tasks ends
* by hiding the progress bar and showing the menu.
*/
private class LoadPhotosTask extends UserTask<Flickr.PhotoList, LoadedPhoto, Flickr.PhotoList> {
private final Random mRandom;

private LoadPhotosTask() {
mRandom = new Random();
}

public Flickr.PhotoList doInBackground(Flickr.PhotoList... params) {
final Flickr.PhotoList list = params[0];
final int count = list.getCount();

for (int i = 0; i < count; i++) {
if (isCancelled()) break;

final Flickr.Photo photo = list.get(i);
Bitmap bitmap = photo.loadPhotoBitmap(Flickr.PhotoSize.THUMBNAIL);
if (!isCancelled()) {
if (bitmap == null) {
final boolean portrait = mRandom.nextFloat() >= 0.5f;
bitmap = BitmapFactory.decodeResource(getResources(), portrait ?
R.drawable.not_found_small_1 : R.drawable.not_found_small_2);
}
publishProgress(new LoadedPhoto(ImageUtilities.rotateAndFrame(bitmap), photo));
bitmap.recycle();
}
}

return list;
}

/**
* Whenever a photo's Bitmap is loaded from the background thread, it is
* displayed in this method by adding a new ImageView in the photos grid.
* Each ImageView's tag contains the {@link com.google.android.photostream.Flickr.Photo}
* it was loaded from.
*
* @param value The photo and its bitmap.
*/
@Override
public void onProgressUpdate(LoadedPhoto... value) {
addPhoto(value);
}

@Override
public void onPostExecute(Flickr.PhotoList result) {
mPageCount = result.getPageCount();
prepareMenu(mPageCount);
mSwitcher.showNext();
mTask = null;
}
}

/**
* Background task used to load the list of photos. The tasks queries Flickr for the
* list of photos to display and ends by starting the LoadPhotosTask.
*/
private class GetPhotoListTask extends UserTask<Integer, Void, Flickr.PhotoList> {
public Flickr.PhotoList doInBackground(Integer... params) {
if (mUsername != null) {
mUser = Flickr.get().findByUserName(mUsername);
mUsername = null;
}
return Flickr.get().getPublicPhotos(mUser, PHOTOS_COUNT_PER_PAGE, params[0]);
}

@Override
public void onPostExecute(Flickr.PhotoList photoList) {
showPhotos(photoList);
mTask = null;
}
}

/**
* A LoadedPhoto contains the Flickr photo and the Bitmap loaded for that photo.
*/
private static class LoadedPhoto {
Bitmap mBitmap;
Flickr.Photo mPhoto;

LoadedPhoto(Bitmap bitmap, Flickr.Photo photo) {
mBitmap = bitmap;
mPhoto = photo;
}
}
}
Show details Hide details

Change log

r79 by romain...@google.com on Oct 05, 2008   Diff
Subactivities would not start

Go to: 
Project members, sign in to write a code review

Older revisions

r75 by romain...@google.com on Oct 02, 2008   Diff
When the user clicks a
flickr.com/photos/<user> link from a
non-Browser app (IM, email, etc.), he
can open the URL in Photostream
instead of the Browser
r73 by romain...@google.com on Sep 30, 2008   Diff
Change VIEW action to custom action
r72 by romain...@google.com on Sep 30, 2008   Diff
Compiles against SDK 1.0, fixes a few
crashes, remove the use of a custom
data scheme

All revisions of this file

File info

Size: 14439 bytes, 417 lines