Steve Paik | 875616c | 2016-02-05 10:55:59 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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.car; |
| 18 | |
Steve Paik | 875616c | 2016-02-05 10:55:59 -0800 | [diff] [blame] | 19 | import android.car.hardware.camera.CarCameraState; |
| 20 | import android.car.hardware.camera.ICarCamera; |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 21 | import android.content.Context; |
Steve Paik | 875616c | 2016-02-05 10:55:59 -0800 | [diff] [blame] | 22 | import android.graphics.Rect; |
| 23 | import android.util.Log; |
| 24 | |
| 25 | import java.io.PrintWriter; |
| 26 | import java.util.Collection; |
| 27 | import java.util.HashMap; |
| 28 | import java.util.Set; |
| 29 | |
| 30 | public class CarCameraService extends ICarCamera.Stub implements CarServiceBase { |
| 31 | public static final boolean DBG = false; |
| 32 | public static final String TAG = CarLog.TAG_CAMERA + ".CarCameraService"; |
| 33 | |
| 34 | private final Context mContext; |
| 35 | private long mModule; |
| 36 | private final HashMap<Integer, Long> mDeviceMap; |
| 37 | |
Steve Paik | 875616c | 2016-02-05 10:55:59 -0800 | [diff] [blame] | 38 | public CarCameraService(Context context) { |
| 39 | mContext = context; |
| 40 | mDeviceMap = new HashMap<Integer, Long>(); |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public synchronized void init() { |
| 45 | if (DBG) { |
| 46 | Log.d(TAG, "init called"); |
| 47 | } |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 48 | try { |
| 49 | mModule = nativeOpen(); |
| 50 | } catch (UnsatisfiedLinkError ex) { |
| 51 | Log.e(TAG, "Failed to open camera module: " + ex.getMessage()); |
| 52 | } |
Steve Paik | 875616c | 2016-02-05 10:55:59 -0800 | [diff] [blame] | 53 | |
| 54 | if (mModule != 0) { |
| 55 | int[] cameraType = nativeGetSupportedCameras(mModule); |
| 56 | |
| 57 | if (cameraType != null) { |
| 58 | for (int i : cameraType) { |
| 59 | long devicePtr = nativeGetDevice(mModule, i); |
| 60 | if (devicePtr == 0) { |
| 61 | Log.e(TAG, "Null device pointer returned for cameraType = " + i); |
| 62 | } else { |
| 63 | mDeviceMap.put(i, devicePtr); |
| 64 | } |
| 65 | } |
| 66 | } else { |
| 67 | Log.e(TAG, "No car cameras are supported"); |
| 68 | } |
| 69 | } else { |
| 70 | Log.w(TAG, "Cannot load camera module"); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public synchronized void release() { |
| 76 | if (DBG) { |
| 77 | Log.d(TAG, "release called"); |
| 78 | } |
| 79 | Collection<Long> devices = mDeviceMap.values(); |
| 80 | for (Long device : devices) { |
| 81 | nativeClose(device); |
| 82 | } |
| 83 | mDeviceMap.clear(); |
| 84 | mModule = 0; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public void dump(PrintWriter writer) { |
| 89 | // TODO |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public int[] getCameraList() { |
| 94 | if (DBG) { |
| 95 | Log.d(TAG, "getCameraList called"); |
| 96 | } |
| 97 | Set<Integer> keySet; |
| 98 | synchronized (this) { |
| 99 | keySet = mDeviceMap.keySet(); |
| 100 | } |
| 101 | |
| 102 | int keySetSize = keySet.size(); |
| 103 | |
| 104 | if (keySetSize > 0) { |
| 105 | int[] keyArray = new int[keySet.size()]; |
| 106 | int i = 0; |
| 107 | for (Integer key : keySet) { |
| 108 | keyArray[i++] = key.intValue(); |
| 109 | } |
| 110 | return keyArray; |
| 111 | } else { |
| 112 | return null; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public int getCapabilities(int cameraType) { |
| 118 | if (DBG) { |
| 119 | Log.d(TAG, "getCapabilities called, type = " + String.valueOf(cameraType)); |
| 120 | } |
| 121 | synchronized (this) { |
| 122 | long device = getDeviceIdLocked(cameraType); |
| 123 | return nativeGetCapabilities(device); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public Rect getCameraCrop(int cameraType) { |
| 129 | Rect rect; |
| 130 | synchronized (this) { |
| 131 | long device = getDeviceIdLocked(cameraType); |
| 132 | rect = nativeGetCameraCrop(device); |
| 133 | } |
| 134 | if(DBG && (rect != null)) { |
| 135 | Log.d(TAG, "getCameraCrop called: " + rect.toString()); |
| 136 | } |
| 137 | return rect; |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public void setCameraCrop(int cameraType, Rect rect) { |
| 142 | if (DBG) { |
| 143 | Log.d(TAG, "setCameraCrop called." + rect.toString()); |
| 144 | } |
| 145 | synchronized (this) { |
| 146 | long device = getDeviceIdLocked(cameraType); |
| 147 | nativeSetCameraCrop(device, rect); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public Rect getCameraPosition(int cameraType) { |
| 153 | Rect rect; |
| 154 | synchronized (this) { |
| 155 | long device = getDeviceIdLocked(cameraType); |
| 156 | rect = nativeGetCameraPosition(device); |
| 157 | } |
| 158 | if(DBG && (rect != null)) { |
| 159 | Log.d(TAG, "getCameraPosition called: " + rect.toString()); |
| 160 | } |
| 161 | return rect; |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public void setCameraPosition(int cameraType, Rect rect) { |
| 166 | if (DBG) { |
| 167 | Log.d(TAG, "setCameraPosition called." + rect.toString()); |
| 168 | } |
| 169 | synchronized (this) { |
| 170 | long device = getDeviceIdLocked(cameraType); |
| 171 | nativeSetCameraPosition(device, rect); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public CarCameraState getCameraState(int cameraType) { |
| 177 | CarCameraState state; |
| 178 | synchronized (this) { |
| 179 | long device = getDeviceIdLocked(cameraType); |
| 180 | state = nativeGetCameraState(device); |
| 181 | } |
| 182 | if(DBG && (state != null)) { |
| 183 | Log.d(TAG, "getCameraState called: " + state.toString()); |
| 184 | } |
| 185 | return state; |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public void setCameraState(int cameraType, CarCameraState state) { |
| 190 | if (DBG) { |
| 191 | Log.d(TAG, "setCameraState called. state: " + state.toString()); |
| 192 | } |
| 193 | synchronized (this) { |
| 194 | long device = getDeviceIdLocked(cameraType); |
| 195 | nativeSetCameraState(device, state); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Validates that the cameraType is available and ready to be used. |
| 201 | * @param cameraType |
| 202 | * @return |
| 203 | */ |
| 204 | private long getDeviceIdLocked(int cameraType) { |
| 205 | Long deviceId = mDeviceMap.get(cameraType); |
| 206 | |
| 207 | if (deviceId == null) { |
| 208 | throw new IllegalArgumentException("cameraType " + cameraType + " doesn't exist in" |
| 209 | + "device map"); |
| 210 | } |
| 211 | return deviceId; |
| 212 | } |
| 213 | |
| 214 | /* |
| 215 | * Native function definitions |
| 216 | */ |
| 217 | private native long nativeOpen(); |
| 218 | private native void nativeClose(long module); |
| 219 | private native int[] nativeGetSupportedCameras(long module); |
| 220 | private native long nativeGetDevice(long module, int cameraType); |
| 221 | private native int nativeGetCapabilities(long device); |
| 222 | private native Rect nativeGetCameraCrop(long device); |
| 223 | private native void nativeSetCameraCrop(long device, Rect rect); |
| 224 | private native Rect nativeGetCameraPosition(long device); |
| 225 | private native void nativeSetCameraPosition(long device, Rect rect); |
| 226 | private native CarCameraState nativeGetCameraState(long device); |
| 227 | private native void nativeSetCameraState(long device, CarCameraState state); |
| 228 | } |