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