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