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 | |
| 19 | import android.util.SparseArray; |
| 20 | |
| 21 | /** |
| 22 | * A class which implements {@link com.android.camera.app.ModuleManager}. |
| 23 | */ |
| 24 | public class ModuleManagerImpl implements ModuleManager { |
| 25 | private static final String TAG = "ModuleManagerImpl"; |
| 26 | |
| 27 | private SparseArray<ModuleAgent> mRegisteredModuleAgents = new |
| 28 | SparseArray<ModuleAgent>(2); |
| 29 | private int mDefaultModuleId = MODULE_INDEX_NONE; |
| 30 | |
| 31 | public ModuleManagerImpl() { |
| 32 | } |
| 33 | |
| 34 | @Override |
| 35 | public void registerModule(ModuleAgent agent) { |
| 36 | if (agent == null) { |
| 37 | throw new NullPointerException("Registering a null ModuleAgent."); |
| 38 | } |
| 39 | final int moduleId = agent.getModuleId(); |
| 40 | if (moduleId == MODULE_INDEX_NONE) { |
| 41 | throw new IllegalArgumentException( |
| 42 | "ModuleManager: The module ID can not be " + "MODULE_INDEX_NONE"); |
| 43 | } |
| 44 | if (mRegisteredModuleAgents.get(moduleId) != null) { |
| 45 | throw new IllegalArgumentException("Module ID is registered already:" + moduleId); |
| 46 | } |
| 47 | mRegisteredModuleAgents.put(moduleId, agent); |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public boolean unregisterModule(int moduleId) { |
| 52 | if (mRegisteredModuleAgents.get(moduleId) == null) { |
| 53 | return false; |
| 54 | } |
| 55 | mRegisteredModuleAgents.delete(moduleId); |
| 56 | if (moduleId == mDefaultModuleId) { |
| 57 | mDefaultModuleId = -1; |
| 58 | } |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public boolean setDefaultModuleIndex(int moduleId) { |
| 64 | if (mRegisteredModuleAgents.get(moduleId) != null) { |
| 65 | mDefaultModuleId = moduleId; |
| 66 | return true; |
| 67 | } |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public int getDefaultModuleIndex() { |
| 73 | return mDefaultModuleId; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public ModuleAgent getModuleAgent(int moduleId) { |
| 78 | ModuleAgent agent = mRegisteredModuleAgents.get(moduleId); |
| 79 | if (agent == null) { |
| 80 | return mRegisteredModuleAgents.get(mDefaultModuleId); |
| 81 | } |
| 82 | return mRegisteredModuleAgents.get(moduleId); |
| 83 | } |
| 84 | } |