CameraApp.java revision 67063a294955285efd6b3f038aafecc92293f803
1/*
2 * Copyright (C) 2013 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.camera.app;
18
19import android.app.Application;
20import android.app.NotificationManager;
21import android.content.Context;
22import android.os.Debug;
23
24import com.android.camera.MediaSaverImpl;
25import com.android.camera.Storage;
26import com.android.camera.debug.LogHelper;
27import com.android.camera.processing.ProcessingServiceManager;
28import com.android.camera.remote.RemoteShutterListener;
29import com.android.camera.session.CaptureSessionManager;
30import com.android.camera.session.CaptureSessionManagerImpl;
31import com.android.camera.session.PlaceholderManager;
32import com.android.camera.session.SessionStorageManager;
33import com.android.camera.session.SessionStorageManagerImpl;
34import com.android.camera.session.StackSaverFactory;
35import com.android.camera.settings.SettingsManager;
36import com.android.camera.util.CameraUtil;
37import com.android.camera.util.RemoteShutterHelper;
38import com.android.camera.stats.SessionStatsCollector;
39import com.android.camera.stats.UsageStatistics;
40
41/**
42 * The Camera application class containing important services and functionality
43 * to be used across modules.
44 */
45public class CameraApp extends Application implements CameraServices {
46    /**
47     * This is for debugging only: If set to true, application will not start
48     * until a debugger is attached.
49     * <p>
50     * Use this if you need to debug code that is executed while the app starts
51     * up and it would be too late to attach a debugger afterwards.
52     */
53    private static final boolean WAIT_FOR_DEBUGGER_ON_START = false;
54
55    private MediaSaver mMediaSaver;
56    private CaptureSessionManager mSessionManager;
57    private StackSaverFactory mStackSaverFactory;
58    private SessionStorageManager mSessionStorageManager;
59    private MemoryManagerImpl mMemoryManager;
60    private PlaceholderManager mPlaceHolderManager;
61    private RemoteShutterListener mRemoteShutterListener;
62    private MotionManager mMotionManager;
63    private SettingsManager mSettingsManager;
64
65    @Override
66    public void onCreate() {
67        super.onCreate();
68
69        if (WAIT_FOR_DEBUGGER_ON_START) {
70            Debug.waitForDebugger();
71        }
72
73        Context context = getApplicationContext();
74        LogHelper.initialize(context);
75
76        // It is important that this gets called early in execution before the
77        // app has had the opportunity to create any shared preferences.
78        FirstRunDetector.instance().initializeTimeOfFirstRun(context);
79
80        UsageStatistics.instance().initialize(this);
81        SessionStatsCollector.instance().initialize(this);
82        CameraUtil.initialize(this);
83
84        ProcessingServiceManager.initSingleton(context);
85
86        mMediaSaver = new MediaSaverImpl();
87        mPlaceHolderManager = new PlaceholderManager(context);
88        mSessionStorageManager = SessionStorageManagerImpl.create(this);
89
90        mStackSaverFactory = new StackSaverFactory(Storage.DIRECTORY, getContentResolver());
91        mSessionManager = new CaptureSessionManagerImpl(mMediaSaver, getContentResolver(),
92                mPlaceHolderManager, mSessionStorageManager, mStackSaverFactory);
93        mMemoryManager = MemoryManagerImpl.create(getApplicationContext(), mMediaSaver);
94        mRemoteShutterListener = RemoteShutterHelper.create(this);
95        mSettingsManager = new SettingsManager(this);
96
97        clearNotifications();
98
99        mMotionManager = new MotionManager(context);
100    }
101
102    @Override
103    public CaptureSessionManager getCaptureSessionManager() {
104        return mSessionManager;
105    }
106
107    @Override
108    public MemoryManager getMemoryManager() {
109        return mMemoryManager;
110    }
111
112    @Override
113    public MotionManager getMotionManager() {
114        return mMotionManager;
115    }
116
117    @Override
118    @Deprecated
119    public MediaSaver getMediaSaver() {
120        return mMediaSaver;
121    }
122
123    @Override
124    public RemoteShutterListener getRemoteShutterListener() {
125        return mRemoteShutterListener;
126    }
127
128    @Override
129    public SettingsManager getSettingsManager() {
130        return mSettingsManager;
131    }
132
133    /**
134     * Clears all notifications. This cleans up notifications that we might have
135     * created earlier but remained after a crash.
136     */
137    private void clearNotifications() {
138        NotificationManager manager = (NotificationManager) getSystemService(
139                Context.NOTIFICATION_SERVICE);
140        if (manager != null) {
141            manager.cancelAll();
142        }
143    }
144}
145