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 
17 package com.android.camera.app;
18 
19 import android.content.Context;
20 import android.util.SparseArray;
21 
22 import com.android.camera.debug.Log;
23 import com.android.camera.settings.Keys;
24 import com.android.camera.settings.SettingsManager;
25 import com.android.camera2.R;
26 
27 import java.util.ArrayList;
28 import java.util.List;
29 
30 /**
31  * A class which implements {@link com.android.camera.app.ModuleManager}.
32  */
33 public class ModuleManagerImpl implements ModuleManager {
34     private static final Log.Tag TAG = new Log.Tag("ModuleManagerImpl");
35 
36     private final SparseArray<ModuleAgent> mRegisteredModuleAgents = new
37             SparseArray<ModuleAgent>(2);
38     private int mDefaultModuleId = MODULE_INDEX_NONE;
39 
ModuleManagerImpl()40     public ModuleManagerImpl() {
41     }
42 
43     @Override
registerModule(ModuleAgent agent)44     public void registerModule(ModuleAgent agent) {
45         if (agent == null) {
46             throw new NullPointerException("Registering a null ModuleAgent.");
47         }
48         final int moduleId = agent.getModuleId();
49         if (moduleId == MODULE_INDEX_NONE) {
50             throw new IllegalArgumentException(
51                     "ModuleManager: The module ID can not be " + "MODULE_INDEX_NONE");
52         }
53         if (mRegisteredModuleAgents.get(moduleId) != null) {
54             throw new IllegalArgumentException("Module ID is registered already:" + moduleId);
55         }
56         mRegisteredModuleAgents.put(moduleId, agent);
57     }
58 
59     @Override
unregisterModule(int moduleId)60     public boolean unregisterModule(int moduleId) {
61         if (mRegisteredModuleAgents.get(moduleId) == null) {
62             return false;
63         }
64         mRegisteredModuleAgents.delete(moduleId);
65         if (moduleId == mDefaultModuleId) {
66             mDefaultModuleId = -1;
67         }
68         return true;
69     }
70 
71     @Override
getRegisteredModuleAgents()72     public List<ModuleAgent> getRegisteredModuleAgents() {
73         List<ModuleAgent> agents = new ArrayList<ModuleAgent>();
74         for (int i = 0; i < mRegisteredModuleAgents.size(); i++) {
75             agents.add(mRegisteredModuleAgents.valueAt(i));
76         }
77         return agents;
78     }
79 
80     @Override
getSupportedModeIndexList()81     public List<Integer> getSupportedModeIndexList() {
82         List<Integer> modeIndexList = new ArrayList<Integer>();
83         for (int i = 0; i < mRegisteredModuleAgents.size(); i++) {
84             modeIndexList.add(mRegisteredModuleAgents.keyAt(i));
85         }
86         return modeIndexList;
87     }
88 
89     @Override
setDefaultModuleIndex(int moduleId)90     public boolean setDefaultModuleIndex(int moduleId) {
91         if (mRegisteredModuleAgents.get(moduleId) != null) {
92             mDefaultModuleId = moduleId;
93             return true;
94         }
95         return false;
96     }
97 
98     @Override
getDefaultModuleIndex()99     public int getDefaultModuleIndex() {
100         return mDefaultModuleId;
101     }
102 
103     @Override
getModuleAgent(int moduleId)104     public ModuleAgent getModuleAgent(int moduleId) {
105         ModuleAgent agent = mRegisteredModuleAgents.get(moduleId);
106         if (agent == null) {
107             return mRegisteredModuleAgents.get(mDefaultModuleId);
108         }
109         return agent;
110     }
111 
112     @Override
getQuickSwitchToModuleId(int moduleId, SettingsManager settingsManager, Context context)113     public int getQuickSwitchToModuleId(int moduleId, SettingsManager settingsManager,
114             Context context) {
115         final int photoModuleId = context.getResources().getInteger(R.integer.camera_mode_photo);
116         final int videoModuleId = context.getResources().getInteger(R.integer.camera_mode_video);
117 
118         int quickSwitchTo = moduleId;
119         if (moduleId == photoModuleId
120             || moduleId == context.getResources().getInteger(R.integer.camera_mode_gcam)) {
121             // Quick switch from camera to video.
122             quickSwitchTo = videoModuleId;
123         } else if (moduleId == videoModuleId) {
124             // Quick switch from video to last used camera (i.e. simple camera or hdr+)
125             quickSwitchTo = settingsManager.getInteger(
126                 SettingsManager.SCOPE_GLOBAL, Keys.KEY_CAMERA_MODULE_LAST_USED);
127         }
128 
129         if (mRegisteredModuleAgents.get(quickSwitchTo) != null) {
130             return quickSwitchTo;
131         } else {
132             return moduleId;
133         }
134     }
135 
136 }
137