1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.gallery3d.app; 18 19 import android.app.Application; 20 import android.content.Context; 21 22 import com.android.gallery3d.data.DataManager; 23 import com.android.gallery3d.data.DownloadCache; 24 import com.android.gallery3d.data.ImageCacheService; 25 import com.android.gallery3d.gadget.WidgetUtils; 26 import com.android.gallery3d.picasasource.PicasaSource; 27 import com.android.gallery3d.util.GalleryUtils; 28 import com.android.gallery3d.util.ThreadPool; 29 30 import java.io.File; 31 32 public class GalleryAppImpl extends Application implements GalleryApp { 33 34 private static final String DOWNLOAD_FOLDER = "download"; 35 private static final long DOWNLOAD_CAPACITY = 64 * 1024 * 1024; // 64M 36 37 private ImageCacheService mImageCacheService; 38 private Object mLock = new Object(); 39 private DataManager mDataManager; 40 private ThreadPool mThreadPool; 41 private DownloadCache mDownloadCache; 42 43 @Override 44 public void onCreate() { 45 super.onCreate(); 46 com.android.camera.Util.initialize(this); 47 GalleryUtils.initialize(this); 48 WidgetUtils.initialize(this); 49 PicasaSource.initialize(this); 50 } 51 52 public Context getAndroidContext() { 53 return this; 54 } 55 56 public synchronized DataManager getDataManager() { 57 if (mDataManager == null) { 58 mDataManager = new DataManager(this); 59 mDataManager.initializeSourceMap(); 60 } 61 return mDataManager; 62 } 63 64 public ImageCacheService getImageCacheService() { 65 // This method may block on file I/O so a dedicated lock is needed here. 66 synchronized (mLock) { 67 if (mImageCacheService == null) { 68 mImageCacheService = new ImageCacheService(getAndroidContext()); 69 } 70 return mImageCacheService; 71 } 72 } 73 74 public synchronized ThreadPool getThreadPool() { 75 if (mThreadPool == null) { 76 mThreadPool = new ThreadPool(); 77 } 78 return mThreadPool; 79 } 80 81 public synchronized DownloadCache getDownloadCache() { 82 if (mDownloadCache == null) { 83 File cacheDir = new File(getExternalCacheDir(), DOWNLOAD_FOLDER); 84 85 if (!cacheDir.isDirectory()) cacheDir.mkdirs(); 86 87 if (!cacheDir.isDirectory()) { 88 throw new RuntimeException( 89 "fail to create: " + cacheDir.getAbsolutePath()); 90 } 91 mDownloadCache = new DownloadCache(this, cacheDir, DOWNLOAD_CAPACITY); 92 } 93 return mDownloadCache; 94 } 95 } 96