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 com.android.gallery3d.data.DataManager; 20 import com.android.gallery3d.data.DownloadCache; 21 import com.android.gallery3d.data.ImageCacheService; 22 import com.android.gallery3d.gadget.WidgetUtils; 23 import com.android.gallery3d.picasasource.PicasaSource; 24 import com.android.gallery3d.util.GalleryUtils; 25 import com.android.gallery3d.util.ThreadPool; 26 27 import android.app.Application; 28 import android.content.Context; 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 DataManager mDataManager; 39 private ThreadPool mThreadPool; 40 private DownloadCache mDownloadCache; 41 42 @Override 43 public void onCreate() { 44 super.onCreate(); 45 GalleryUtils.initialize(this); 46 WidgetUtils.initialize(this); 47 PicasaSource.initialize(this); 48 } 49 50 public Context getAndroidContext() { 51 return this; 52 } 53 54 public synchronized DataManager getDataManager() { 55 if (mDataManager == null) { 56 mDataManager = new DataManager(this); 57 mDataManager.initializeSourceMap(); 58 } 59 return mDataManager; 60 } 61 62 public synchronized ImageCacheService getImageCacheService() { 63 if (mImageCacheService == null) { 64 mImageCacheService = new ImageCacheService(getAndroidContext()); 65 } 66 return mImageCacheService; 67 } 68 69 public synchronized ThreadPool getThreadPool() { 70 if (mThreadPool == null) { 71 mThreadPool = new ThreadPool(); 72 } 73 return mThreadPool; 74 } 75 76 public synchronized DownloadCache getDownloadCache() { 77 if (mDownloadCache == null) { 78 File cacheDir = new File(getExternalCacheDir(), DOWNLOAD_FOLDER); 79 80 if (!cacheDir.isDirectory()) cacheDir.mkdirs(); 81 82 if (!cacheDir.isDirectory()) { 83 throw new RuntimeException( 84 "fail to create: " + cacheDir.getAbsolutePath()); 85 } 86 mDownloadCache = new DownloadCache(this, cacheDir, DOWNLOAD_CAPACITY); 87 } 88 return mDownloadCache; 89 } 90 } 91