OSDN Git Service

Updates to 3D gallery. Build 1203.
[android-x86/packages-apps-Gallery2.git] / src / com / cooliris / media / Gallery.java
1 package com.cooliris.media;
2
3 import java.io.IOException;
4 import java.net.URISyntaxException;
5 import java.util.HashMap;
6 import java.util.TimeZone;
7
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;
25
26 import com.cooliris.cache.CacheService;
27 import com.cooliris.wallpaper.RandomDataSource;
28 import com.cooliris.wallpaper.Slideshow;
29
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;
34
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;
46
47         @Override
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();
54                                 finish();
55                         } else {
56                                 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
57                                 mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "GridView.Slideshow.All");
58                                 mWakeLock.acquire();
59                                 Slideshow slideshow = new Slideshow(this);
60                                 slideshow.setDataSource(new RandomDataSource());
61                                 setContentView(slideshow);
62                         }
63                         return;
64                 }
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;
71                 }
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),
75                         mRenderView);
76                 mRenderView.setRootLayer(mGridLayer);
77                 setContentView(mRenderView);
78                 
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);
83                 
84                 // Depending upon the intent, we assign the right dataSource.
85                 if (!isPickIntent() && !isViewIntent()) {
86                         if (imageManagerHasStorage) {
87                                 mGridLayer.setDataSource(combinedDataSource);
88                         } else {
89                                 mGridLayer.setDataSource(picasaDataSource);
90                         }
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();
95                         }
96                 } else if (!isViewIntent()) {
97                         final Intent intent = getIntent();
98                         if (intent != null) {
99                                 final String type = intent.resolveType(this);
100                                 boolean includeImages = isImageType(type);
101                                 boolean includeVideos = isVideoType(type);
102                                 ((LocalDataSource) localDataSource).setMimeFilter(!includeImages, !includeVideos);
103                                 if (includeImages) {
104                                         if (imageManagerHasStorage) {
105                                                 mGridLayer.setDataSource(combinedDataSource);
106                                         } else {
107                                                 mGridLayer.setDataSource(picasaDataSource);
108                                         }
109                                 } else {
110                                         mGridLayer.setDataSource(localDataSource);
111                                 }
112                                 mGridLayer.setPickIntent(true);
113                                 if (!imageManagerHasStorage) {
114                                         Toast.makeText(this, getResources().getString(R.string.no_sd_card), Toast.LENGTH_LONG).show();
115                                 } else {
116                                         Toast.makeText(this, getResources().getString(R.string.pick_prompt), Toast.LENGTH_LONG).show();
117                                 }
118                         }
119                 } else {
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();
132                         }
133                 }
134                 // We record the set of enabled accounts for picasa.
135                 mAccountsEnabled = PicasaDataSource.getAccountStatus(this);
136                 Log.i(TAG, "onCreate");
137         }
138
139         public ReverseGeocoder getReverseGeocoder() {
140                 return mReverseGeocoder;
141         }
142
143         public Handler getHandler() {
144                 return mHandler;
145         }
146
147         @Override
148         public void onRestart() {
149                 super.onRestart();
150         }
151
152         @Override
153         public void onStart() {
154                 super.onStart();
155         }
156
157         @Override
158         public void onResume() {
159                 super.onResume();
160                 if (mRenderView != null)
161                         mRenderView.onResume();
162                 if (mPause) {
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();
176                                 }
177                                 if (oldValue != newValue) {
178                                         // Reload the datasource.
179                                         mGridLayer.setDataSource(mGridLayer.getDataSource());
180                                         break;
181                                 }
182                         }
183                         mAccountsEnabled = accountsEnabled;
184                         mPause = false;
185                 }
186         }
187
188         @Override
189         public void onPause() {
190                 super.onPause();
191                 if (mRenderView != null)
192                         mRenderView.onPause();
193                 mPause = true;
194         }
195
196         public boolean isPaused() {
197                 return mPause;
198         }
199
200         @Override
201         public void onStop() {
202                 super.onStop();
203                 if (mGridLayer != null)
204                         mGridLayer.stop();
205                 if (mReverseGeocoder != null) {
206                         mReverseGeocoder.flushCache();
207                 }
208                 LocalDataSource.sThumbnailCache.flush();
209                 LocalDataSource.sThumbnailCacheVideo.flush();
210                 PicasaDataSource.sThumbnailCache.flush();
211                 CacheService.startCache(this, true);
212         }
213
214         @Override
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();
222                         }
223                         mGridLayer.shutdown();
224                 }
225                 if (mWakeLock != null) {
226                         if (mWakeLock.isHeld()) {
227                                 mWakeLock.release();
228                         }
229                         mWakeLock = null;
230                 }
231                 if (mReverseGeocoder != null)
232                         mReverseGeocoder.shutdown();
233                 if (mRenderView != null) {
234                         mRenderView.shutdown();
235                         mRenderView = null;
236                 }
237                 mGridLayer = null;
238                 super.onDestroy();
239                 Log.i(TAG, "onDestroy");
240         }
241
242         @Override
243         public void onConfigurationChanged(Configuration newConfig) {
244                 super.onConfigurationChanged(newConfig);
245                 if (mGridLayer != null) {
246                         mGridLayer.markDirty(30);
247                 }
248                 if (mRenderView != null)
249                         mRenderView.requestRender();
250                 Log.i(TAG, "onConfigurationChanged");
251         }
252
253         @Override
254         public boolean onKeyDown(int keyCode, KeyEvent event) {
255                 if (mRenderView != null) {
256                         return mRenderView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
257                 } else {
258                         return super.onKeyDown(keyCode, event);
259                 }
260         }
261
262         private boolean isPickIntent() {
263                 String action = getIntent().getAction();
264                 return (Intent.ACTION_PICK.equals(action) || Intent.ACTION_GET_CONTENT.equals(action));
265         }
266
267         private boolean isViewIntent() {
268                 String action = getIntent().getAction();
269                 return Intent.ACTION_VIEW.equals(action);
270         }
271
272         private boolean isImageType(String type) {
273                 return type.equals("vnd.android.cursor.dir/image") || type.equals("image/*");
274         }
275
276         private boolean isVideoType(String type) {
277                 return type.equals("vnd.android.cursor.dir/video") || type.equals("video/*");
278         }
279
280         @Override
281         protected void onActivityResult(int requestCode, int resultCode, Intent data) {
282                 switch (requestCode) {
283                 case CROP_MSG: {
284                         if (resultCode == RESULT_OK) {
285                                 setResult(resultCode, data);
286                                 finish();
287                         }
288                         break;
289                 }
290                 case CROP_MSG_INTERNAL: {
291                         // We cropped an image, we must try to set the focus of the camera
292                         // to that image.
293                         if (resultCode == RESULT_OK) {
294                                 String contentUri = data.getAction();
295                                 if (mGridLayer != null) {
296                                         mGridLayer.focusItem(contentUri);
297                                 }
298                         }
299                         break;
300                 }
301                 }
302         }
303
304         @Override
305         public void onLowMemory() {
306                 if (mRenderView != null) {
307                         mRenderView.handleLowMemory();
308                 }
309         }
310
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");
319                         }
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);
327                 } else {
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) {
337                                                                 try {
338                                                                         final String path = UriTexture.writeHttpDataInDirectory(Gallery.this, contentUri,
339                                                                                 LocalDataSource.DOWNLOAD_BUCKET_NAME);
340                                                                         if (path != null) {
341                                                                                 mConnection.scanFile(path, item.mMimeType);
342                                                                         } else {
343                                                                                 shutdown("");
344                                                                         }
345                                                                 } catch (Exception e) {
346                                                                         shutdown("");
347                                                                 }
348                                                         }
349                                                 }
350
351                                                 public void onScanCompleted(String path, Uri uri) {
352                                                         shutdown(uri.toString());
353                                                 }
354
355                                                 public void shutdown(String uri) {
356                                                         dialog.dismiss();
357                                                         performReturn(myExtras, uri.toString());
358                                                         if (mConnection != null) {
359                                                                 mConnection.disconnect();
360                                                         }
361                                                 }
362                                         };
363                                         MediaScannerConnection connection = new MediaScannerConnection(Gallery.this, client);
364                                         connection.connect();
365                                         mConnection = connection;
366                                 }
367                         } else {
368                                 performReturn(myExtras, contentUri);
369                         }
370                 }
371         }
372
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;
378                         try {
379                                 bitmap = UriTexture.createFromUri(this, contentUri, 1024, 1024, 0, null);
380                         } catch (IOException e) {
381                                 ;
382                         } catch (URISyntaxException e) {
383                                 ;
384                         }
385                         if (bitmap != null) {
386                                 result.putExtra("data", bitmap);
387                         }
388                 }
389                 setResult(RESULT_OK, result);
390                 finish();
391         }
392 }