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