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 import android.os.AsyncTask; 22 23 import com.android.gallery3d.data.DataManager; 24 import com.android.gallery3d.data.DownloadCache; 25 import com.android.gallery3d.data.ImageCacheService; 26 import com.android.gallery3d.gadget.WidgetUtils; 27 import com.android.gallery3d.picasasource.PicasaSource; 28 import com.android.gallery3d.util.GalleryUtils; 29 import com.android.gallery3d.util.LightCycleHelper; 30 import com.android.gallery3d.util.ThreadPool; 31 import com.android.gallery3d.util.UsageStatistics; 32 33 import java.io.File; 34 35 public class GalleryAppImpl extends Application implements GalleryApp { 36 37 private static final String DOWNLOAD_FOLDER = "download"; 38 private static final long DOWNLOAD_CAPACITY = 64 * 1024 * 1024; // 64M 39 40 private ImageCacheService mImageCacheService; 41 private Object mLock = new Object(); 42 private DataManager mDataManager; 43 private ThreadPool mThreadPool; 44 private DownloadCache mDownloadCache; 45 46 @Override 47 public void onCreate() { 48 super.onCreate(); 49 initializeAsyncTask(); 50 GalleryUtils.initialize(this); 51 WidgetUtils.initialize(this); 52 PicasaSource.initialize(this); 53 UsageStatistics.initialize(this); 54 } 55 56 @Override 57 public Context getAndroidContext() { 58 return this; 59 } 60 61 @Override 62 public synchronized DataManager getDataManager() { 63 if (mDataManager == null) { 64 mDataManager = new DataManager(this); 65 mDataManager.initializeSourceMap(); 66 } 67 return mDataManager; 68 } 69 70 71 @Override 72 public ImageCacheService getImageCacheService() { 73 // This method may block on file I/O so a dedicated lock is needed here. 74 synchronized (mLock) { 75 if (mImageCacheService == null) { 76 mImageCacheService = new ImageCacheService(getAndroidContext()); 77 } 78 return mImageCacheService; 79 } 80 } 81 82 @Override 83 public synchronized ThreadPool getThreadPool() { 84 if (mThreadPool == null) { 85 mThreadPool = new ThreadPool(); 86 } 87 return mThreadPool; 88 } 89 90 @Override 91 public synchronized DownloadCache getDownloadCache() { 92 if (mDownloadCache == null) { 93 File cacheDir = new File(getExternalCacheDir(), DOWNLOAD_FOLDER); 94 95 if (!cacheDir.isDirectory()) cacheDir.mkdirs(); 96 97 if (!cacheDir.isDirectory()) { 98 throw new RuntimeException( 99 "fail to create: " + cacheDir.getAbsolutePath()); 100 } 101 mDownloadCache = new DownloadCache(this, cacheDir, DOWNLOAD_CAPACITY); 102 } 103 return mDownloadCache; 104 } 105 106 private void initializeAsyncTask() { 107 // AsyncTask class needs to be loaded in UI thread. 108 // So we load it here to comply the rule. 109 try { 110 Class.forName(AsyncTask.class.getName()); 111 } catch (ClassNotFoundException e) { 112 } 113 } 114 } 115