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