1 package com.cooliris.media;
3 import java.io.IOException;
4 import java.net.URISyntaxException;
5 import java.util.HashMap;
6 import java.util.TimeZone;
8 import android.app.Activity;
9 import android.app.ProgressDialog;
10 import android.content.Context;
11 import android.content.Intent;
12 import android.content.res.Configuration;
13 import android.graphics.Bitmap;
14 import android.net.Uri;
15 import android.os.Bundle;
16 import android.os.Handler;
17 import android.os.PowerManager;
18 import android.os.PowerManager.WakeLock;
19 import android.provider.MediaStore.Images;
20 import android.util.DisplayMetrics;
21 import android.util.Log;
22 import android.view.KeyEvent;
23 import android.widget.Toast;
24 import android.media.MediaScannerConnection;
26 import com.cooliris.cache.CacheService;
27 import com.cooliris.wallpaper.RandomDataSource;
28 import com.cooliris.wallpaper.Slideshow;
30 public final class Gallery extends Activity {
31 public static final TimeZone CURRENT_TIME_ZONE = TimeZone.getDefault();
32 public static float PIXEL_DENSITY = 0.0f;
33 public static final int CROP_MSG_INTERNAL = 100;
35 private static final String TAG = "Gallery";
36 private static final int CROP_MSG = 10;
37 private RenderView mRenderView = null;
38 private GridLayer mGridLayer;
39 private final Handler mHandler = new Handler();
40 private ReverseGeocoder mReverseGeocoder;
41 private boolean mPause;
42 private MediaScannerConnection mConnection;
43 private WakeLock mWakeLock;
44 private HashMap<String, Boolean> mAccountsEnabled;
45 private static final boolean TEST_WALLPAPER = false;
48 public void onCreate(Bundle savedInstanceState) {
49 super.onCreate(savedInstanceState);
50 final boolean imageManagerHasStorage = ImageManager.quickHasStorage();
51 if (TEST_WALLPAPER || (isViewIntent() && getIntent().getData().equals(Images.Media.EXTERNAL_CONTENT_URI))) {
52 if (!imageManagerHasStorage) {
53 Toast.makeText(this, getResources().getString(R.string.no_sd_card), Toast.LENGTH_LONG).show();
56 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
57 mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "GridView.Slideshow.All");
59 Slideshow slideshow = new Slideshow(this);
60 slideshow.setDataSource(new RandomDataSource());
61 setContentView(slideshow);
65 final boolean isCacheReady = CacheService.isCacheReady(false);
66 CacheService.startCache(this, false);
67 if (PIXEL_DENSITY == 0.0f) {
68 DisplayMetrics metrics = new DisplayMetrics();
69 getWindowManager().getDefaultDisplay().getMetrics(metrics);
70 PIXEL_DENSITY = metrics.density;
72 mReverseGeocoder = new ReverseGeocoder(this);
73 mRenderView = new RenderView(this);
74 mGridLayer = new GridLayer(this, (int) (96.0f * PIXEL_DENSITY), (int) (72.0f * PIXEL_DENSITY), new GridLayoutInterface(4),
76 mRenderView.setRootLayer(mGridLayer);
77 setContentView(mRenderView);
79 // Creating the DataSource objects
80 final PicasaDataSource picasaDataSource = new PicasaDataSource(this);
81 final LocalDataSource localDataSource = new LocalDataSource(this);
82 final ConcatenatedDataSource combinedDataSource = new ConcatenatedDataSource(localDataSource, picasaDataSource);
84 // Depending upon the intent, we assign the right dataSource.
85 if (!isPickIntent() && !isViewIntent()) {
86 if (imageManagerHasStorage) {
87 mGridLayer.setDataSource(combinedDataSource);
89 mGridLayer.setDataSource(picasaDataSource);
91 if (!imageManagerHasStorage) {
92 Toast.makeText(this, getResources().getString(R.string.no_sd_card), Toast.LENGTH_LONG).show();
93 } else if (!isCacheReady) {
94 Toast.makeText(this, getResources().getString(R.string.loading_new), Toast.LENGTH_LONG).show();
96 } else if (!isViewIntent()) {
97 final Intent intent = getIntent();
99 final String type = intent.resolveType(this);
100 boolean includeImages = isImageType(type);
101 boolean includeVideos = isVideoType(type);
102 ((LocalDataSource) localDataSource).setMimeFilter(!includeImages, !includeVideos);
104 if (imageManagerHasStorage) {
105 mGridLayer.setDataSource(combinedDataSource);
107 mGridLayer.setDataSource(picasaDataSource);
110 mGridLayer.setDataSource(localDataSource);
112 mGridLayer.setPickIntent(true);
113 if (!imageManagerHasStorage) {
114 Toast.makeText(this, getResources().getString(R.string.no_sd_card), Toast.LENGTH_LONG).show();
116 Toast.makeText(this, getResources().getString(R.string.pick_prompt), Toast.LENGTH_LONG).show();
120 // View intent for images.
121 Uri uri = getIntent().getData();
122 boolean slideshow = getIntent().getBooleanExtra("slideshow", false);
123 final SingleDataSource singleDataSource = new SingleDataSource(this, uri.toString(), slideshow);
124 final ConcatenatedDataSource singleCombinedDataSource = new ConcatenatedDataSource(singleDataSource, picasaDataSource);
125 mGridLayer.setDataSource(singleCombinedDataSource);
126 mGridLayer.setViewIntent(true, Utils.getBucketNameFromUri(uri));
127 if (SingleDataSource.isSingleImageMode(uri.toString())) {
128 mGridLayer.setSingleImage(false);
129 } else if (slideshow) {
130 mGridLayer.setSingleImage(true);
131 mGridLayer.startSlideshow();
134 // We record the set of enabled accounts for picasa.
135 mAccountsEnabled = PicasaDataSource.getAccountStatus(this);
136 Log.i(TAG, "onCreate");
139 public ReverseGeocoder getReverseGeocoder() {
140 return mReverseGeocoder;
143 public Handler getHandler() {
148 public void onRestart() {
153 public void onStart() {
158 public void onResume() {
160 if (mRenderView != null)
161 mRenderView.onResume();
163 // We check to see if the authenticated accounts have changed, and
164 // if so, reload the datasource.
165 HashMap<String, Boolean> accountsEnabled = PicasaDataSource.getAccountStatus(this);
166 String[] keys = new String[accountsEnabled.size()];
167 keys = accountsEnabled.keySet().toArray(keys);
168 int numKeys = keys.length;
169 for (int i = 0; i < numKeys; ++i) {
170 String key = keys[i];
171 boolean newValue = accountsEnabled.get(key).booleanValue();
172 boolean oldValue = false;
173 Boolean oldValObj = mAccountsEnabled.get(key);
174 if (oldValObj != null) {
175 oldValue = oldValObj.booleanValue();
177 if (oldValue != newValue) {
178 // Reload the datasource.
179 mGridLayer.setDataSource(mGridLayer.getDataSource());
183 mAccountsEnabled = accountsEnabled;
189 public void onPause() {
191 if (mRenderView != null)
192 mRenderView.onPause();
196 public boolean isPaused() {
201 public void onStop() {
203 if (mGridLayer != null)
205 if (mReverseGeocoder != null) {
206 mReverseGeocoder.flushCache();
208 LocalDataSource.sThumbnailCache.flush();
209 LocalDataSource.sThumbnailCacheVideo.flush();
210 PicasaDataSource.sThumbnailCache.flush();
211 CacheService.startCache(this, true);
215 public void onDestroy() {
216 // Force GLThread to exit.
217 setContentView(R.layout.main);
218 if (mGridLayer != null) {
219 DataSource dataSource = mGridLayer.getDataSource();
220 if (dataSource != null) {
221 dataSource.shutdown();
223 mGridLayer.shutdown();
225 if (mWakeLock != null) {
226 if (mWakeLock.isHeld()) {
231 if (mReverseGeocoder != null)
232 mReverseGeocoder.shutdown();
233 if (mRenderView != null) {
234 mRenderView.shutdown();
239 Log.i(TAG, "onDestroy");
243 public void onConfigurationChanged(Configuration newConfig) {
244 super.onConfigurationChanged(newConfig);
245 if (mGridLayer != null) {
246 mGridLayer.markDirty(30);
248 if (mRenderView != null)
249 mRenderView.requestRender();
250 Log.i(TAG, "onConfigurationChanged");
254 public boolean onKeyDown(int keyCode, KeyEvent event) {
255 if (mRenderView != null) {
256 return mRenderView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
258 return super.onKeyDown(keyCode, event);
262 private boolean isPickIntent() {
263 String action = getIntent().getAction();
264 return (Intent.ACTION_PICK.equals(action) || Intent.ACTION_GET_CONTENT.equals(action));
267 private boolean isViewIntent() {
268 String action = getIntent().getAction();
269 return Intent.ACTION_VIEW.equals(action);
272 private boolean isImageType(String type) {
273 return type.equals("vnd.android.cursor.dir/image") || type.equals("image/*");
276 private boolean isVideoType(String type) {
277 return type.equals("vnd.android.cursor.dir/video") || type.equals("video/*");
281 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
282 switch (requestCode) {
284 if (resultCode == RESULT_OK) {
285 setResult(resultCode, data);
290 case CROP_MSG_INTERNAL: {
291 // We cropped an image, we must try to set the focus of the camera
293 if (resultCode == RESULT_OK) {
294 String contentUri = data.getAction();
295 if (mGridLayer != null) {
296 mGridLayer.focusItem(contentUri);
305 public void onLowMemory() {
306 if (mRenderView != null) {
307 mRenderView.handleLowMemory();
311 public void launchCropperOrFinish(final MediaItem item) {
312 final Bundle myExtras = getIntent().getExtras();
313 String cropValue = myExtras != null ? myExtras.getString("crop") : null;
314 final String contentUri = item.mContentUri;
315 if (cropValue != null) {
316 Bundle newExtras = new Bundle();
317 if (cropValue.equals("circle")) {
318 newExtras.putString("circleCrop", "true");
320 Intent cropIntent = new Intent();
321 cropIntent.setData(Uri.parse(contentUri));
322 cropIntent.setClass(this, CropImage.class);
323 cropIntent.putExtras(newExtras);
324 // Pass through any extras that were passed in.
325 cropIntent.putExtras(myExtras);
326 startActivityForResult(cropIntent, CROP_MSG);
328 if (contentUri.startsWith("http://")) {
329 // This is a http uri, we must save it locally first and
330 // generate a content uri from it.
331 final ProgressDialog dialog = ProgressDialog.show(this, this.getResources().getString(R.string.initializing),
332 getResources().getString(R.string.running_face_detection), true, false);
333 if (contentUri != null) {
334 MediaScannerConnection.MediaScannerConnectionClient client = new MediaScannerConnection.MediaScannerConnectionClient() {
335 public void onMediaScannerConnected() {
336 if (mConnection != null) {
338 final String path = UriTexture.writeHttpDataInDirectory(Gallery.this, contentUri,
339 LocalDataSource.DOWNLOAD_BUCKET_NAME);
341 mConnection.scanFile(path, item.mMimeType);
345 } catch (Exception e) {
351 public void onScanCompleted(String path, Uri uri) {
352 shutdown(uri.toString());
355 public void shutdown(String uri) {
357 performReturn(myExtras, uri.toString());
358 if (mConnection != null) {
359 mConnection.disconnect();
363 MediaScannerConnection connection = new MediaScannerConnection(Gallery.this, client);
364 connection.connect();
365 mConnection = connection;
368 performReturn(myExtras, contentUri);
373 private void performReturn(Bundle myExtras, String contentUri) {
374 Intent result = new Intent(null, Uri.parse(contentUri));
375 if (myExtras != null && myExtras.getBoolean("return-data")) {
376 // The size of a transaction should be below 100K.
377 Bitmap bitmap = null;
379 bitmap = UriTexture.createFromUri(this, contentUri, 1024, 1024, 0, null);
380 } catch (IOException e) {
382 } catch (URISyntaxException e) {
385 if (bitmap != null) {
386 result.putExtra("data", bitmap);
389 setResult(RESULT_OK, result);