| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.hardware.camera2; |
| 18 |
|
| 19 |
import android.annotation.CallbackExecutor; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.annotation.RequiresPermission; |
| 23 |
import android.annotation.SystemApi; |
| 24 |
import android.annotation.SystemService; |
| 25 |
import android.annotation.TestApi; |
| 26 |
import android.app.compat.CompatChanges; |
| 27 |
import android.compat.annotation.ChangeId; |
| 28 |
import android.compat.annotation.EnabledSince; |
| 29 |
import android.compat.annotation.Overridable; |
| 30 |
import android.content.Context; |
| 31 |
import android.content.pm.PackageManager; |
| 32 |
import android.graphics.Point; |
| 33 |
import android.hardware.CameraStatus; |
| 34 |
import android.hardware.ICameraService; |
| 35 |
import android.hardware.ICameraServiceListener; |
| 36 |
import android.hardware.camera2.impl.CameraDeviceImpl; |
| 37 |
import android.hardware.camera2.impl.CameraInjectionSessionImpl; |
| 38 |
import android.hardware.camera2.impl.CameraMetadataNative; |
| 39 |
import android.hardware.camera2.params.ExtensionSessionConfiguration; |
| 40 |
import android.hardware.camera2.params.SessionConfiguration; |
| 41 |
import android.hardware.camera2.params.StreamConfiguration; |
| 42 |
import android.hardware.camera2.utils.CameraIdAndSessionConfiguration; |
| 43 |
import android.hardware.camera2.utils.ConcurrentCameraIdCombination; |
| 44 |
import android.hardware.devicestate.DeviceStateManager; |
| 45 |
import android.hardware.display.DisplayManager; |
| 46 |
import android.os.Binder; |
| 47 |
import android.os.DeadObjectException; |
| 48 |
import android.os.Handler; |
| 49 |
import android.os.HandlerExecutor; |
| 50 |
import android.os.HandlerThread; |
| 51 |
import android.os.IBinder; |
| 52 |
import android.os.RemoteException; |
| 53 |
import android.os.ServiceManager; |
| 54 |
import android.os.ServiceSpecificException; |
| 55 |
import android.os.SystemProperties; |
| 56 |
import android.util.ArrayMap; |
| 57 |
import android.util.ArraySet; |
| 58 |
import android.util.Log; |
| 59 |
import android.util.Size; |
| 60 |
import android.view.Display; |
| 61 |
|
| 62 |
import com.android.internal.annotations.GuardedBy; |
| 63 |
import com.android.internal.util.ArrayUtils; |
| 64 |
|
| 65 |
import java.lang.ref.WeakReference; |
| 66 |
import java.util.ArrayList; |
| 67 |
import java.util.Arrays; |
| 68 |
import java.util.Comparator; |
| 69 |
import java.util.HashMap; |
| 70 |
import java.util.Map; |
| 71 |
import java.util.Set; |
| 72 |
import java.util.concurrent.Executor; |
| 73 |
import java.util.concurrent.Executors; |
| 74 |
import java.util.concurrent.RejectedExecutionException; |
| 75 |
import java.util.concurrent.ScheduledExecutorService; |
| 76 |
import java.util.concurrent.TimeUnit; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
@SystemService(Context.CAMERA_SERVICE) |
| 87 |
public final class CameraManager { |
| 88 |
|
| 89 |
private static final String TAG = "CameraManager"; |
| 90 |
private final boolean DEBUG = false; |
| 91 |
|
| 92 |
private static final int USE_CALLING_UID = -1; |
| 93 |
|
| 94 |
@SuppressWarnings("unused") |
| 95 |
private static final int API_VERSION_1 = 1; |
| 96 |
private static final int API_VERSION_2 = 2; |
| 97 |
|
| 98 |
private static final int CAMERA_TYPE_BACKWARD_COMPATIBLE = 0; |
| 99 |
private static final int CAMERA_TYPE_ALL = 1; |
| 100 |
|
| 101 |
private ArrayList<String> mDeviceIdList; |
| 102 |
|
| 103 |
private final Context mContext; |
| 104 |
private final Object mLock = new Object(); |
| 105 |
|
| 106 |
private static final String CAMERA_OPEN_CLOSE_LISTENER_PERMISSION = |
| 107 |
"android.permission.CAMERA_OPEN_CLOSE_LISTENER"; |
| 108 |
private final boolean mHasOpenCloseListenerPermission; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
@ChangeId |
| 116 |
@Overridable |
| 117 |
@EnabledSince(targetSdkVersion = android.os.Build.VERSION_CODES.BASE) |
| 118 |
@TestApi |
| 119 |
public static final long OVERRIDE_FRONT_CAMERA_APP_COMPAT = 250678880L; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public static final String LANDSCAPE_TO_PORTRAIT_PROP = |
| 126 |
"camera.enable_landscape_to_portrait"; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public CameraManager(Context context) { |
| 132 |
synchronized(mLock) { |
| 133 |
mContext = context; |
| 134 |
mHasOpenCloseListenerPermission = |
| 135 |
mContext.checkSelfPermission(CAMERA_OPEN_CLOSE_LISTENER_PERMISSION) == |
| 136 |
PackageManager.PERMISSION_GRANTED; |
| 137 |
} |
| 138 |
|
| 139 |
mHandlerThread = new HandlerThread(TAG); |
| 140 |
mHandlerThread.start(); |
| 141 |
mHandler = new Handler(mHandlerThread.getLooper()); |
| 142 |
mFoldStateListener = new FoldStateListener(context); |
| 143 |
try { |
| 144 |
context.getSystemService(DeviceStateManager.class) |
| 145 |
.registerCallback(new HandlerExecutor(mHandler), mFoldStateListener); |
| 146 |
} catch (IllegalStateException e) { |
| 147 |
Log.v(TAG, "Failed to register device state listener!"); |
| 148 |
Log.v(TAG, "Device state dependent characteristics updates will not be functional!"); |
| 149 |
mHandlerThread.quitSafely(); |
| 150 |
mHandler = null; |
| 151 |
mFoldStateListener = null; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
private HandlerThread mHandlerThread; |
| 156 |
private Handler mHandler; |
| 157 |
private FoldStateListener mFoldStateListener; |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public interface DeviceStateListener { |
| 163 |
void onDeviceStateChanged(boolean folded); |
| 164 |
} |
| 165 |
|
| 166 |
private static final class FoldStateListener implements DeviceStateManager.DeviceStateCallback { |
| 167 |
private final int[] mFoldedDeviceStates; |
| 168 |
|
| 169 |
private ArrayList<WeakReference<DeviceStateListener>> mDeviceStateListeners = |
| 170 |
new ArrayList<>(); |
| 171 |
private boolean mFoldedDeviceState; |
| 172 |
|
| 173 |
public FoldStateListener(Context context) { |
| 174 |
mFoldedDeviceStates = context.getResources().getIntArray( |
| 175 |
com.android.internal.R.array.config_foldedDeviceStates); |
| 176 |
} |
| 177 |
|
| 178 |
private synchronized void handleStateChange(int state) { |
| 179 |
boolean folded = ArrayUtils.contains(mFoldedDeviceStates, state); |
| 180 |
|
| 181 |
mFoldedDeviceState = folded; |
| 182 |
ArrayList<WeakReference<DeviceStateListener>> invalidListeners = new ArrayList<>(); |
| 183 |
for (WeakReference<DeviceStateListener> listener : mDeviceStateListeners) { |
| 184 |
DeviceStateListener callback = listener.get(); |
| 185 |
if (callback != null) { |
| 186 |
callback.onDeviceStateChanged(folded); |
| 187 |
} else { |
| 188 |
invalidListeners.add(listener); |
| 189 |
} |
| 190 |
} |
| 191 |
if (!invalidListeners.isEmpty()) { |
| 192 |
mDeviceStateListeners.removeAll(invalidListeners); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
public synchronized void addDeviceStateListener(DeviceStateListener listener) { |
| 197 |
listener.onDeviceStateChanged(mFoldedDeviceState); |
| 198 |
mDeviceStateListeners.add(new WeakReference<>(listener)); |
| 199 |
} |
| 200 |
|
| 201 |
@Override |
| 202 |
public final void onBaseStateChanged(int state) { |
| 203 |
handleStateChange(state); |
| 204 |
} |
| 205 |
|
| 206 |
@Override |
| 207 |
public final void onStateChanged(int state) { |
| 208 |
handleStateChange(state); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
public void registerDeviceStateListener(@NonNull CameraCharacteristics chars) { |
| 220 |
synchronized (mLock) { |
| 221 |
DeviceStateListener listener = chars.getDeviceStateListener(); |
| 222 |
if (mFoldStateListener != null) { |
| 223 |
mFoldStateListener.addDeviceStateListener(listener); |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
@NonNull |
| 242 |
public String[] getCameraIdList() throws CameraAccessException { |
| 243 |
return CameraManagerGlobal.get().getCameraIdList(); |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
@TestApi |
| 257 |
public String[] getCameraIdListNoLazy() throws CameraAccessException { |
| 258 |
return CameraManagerGlobal.get().getCameraIdListNoLazy(); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
@NonNull |
| 311 |
public Set<Set<String>> getConcurrentCameraIds() throws CameraAccessException { |
| 312 |
return CameraManagerGlobal.get().getConcurrentCameraIds(); |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
@RequiresPermission(android.Manifest.permission.CAMERA) |
| 347 |
public boolean isConcurrentSessionConfigurationSupported( |
| 348 |
@NonNull Map<String, SessionConfiguration> cameraIdAndSessionConfig) |
| 349 |
throws CameraAccessException { |
| 350 |
return CameraManagerGlobal.get().isConcurrentSessionConfigurationSupported( |
| 351 |
cameraIdAndSessionConfig, mContext.getApplicationInfo().targetSdkVersion); |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
public void registerAvailabilityCallback(@NonNull AvailabilityCallback callback, |
| 382 |
@Nullable Handler handler) { |
| 383 |
CameraManagerGlobal.get().registerAvailabilityCallback(callback, |
| 384 |
CameraDeviceImpl.checkAndWrapHandler(handler), mHasOpenCloseListenerPermission); |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
public void registerAvailabilityCallback(@NonNull @CallbackExecutor Executor executor, |
| 401 |
@NonNull AvailabilityCallback callback) { |
| 402 |
if (executor == null) { |
| 403 |
throw new IllegalArgumentException("executor was null"); |
| 404 |
} |
| 405 |
CameraManagerGlobal.get().registerAvailabilityCallback(callback, executor, |
| 406 |
mHasOpenCloseListenerPermission); |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
public void unregisterAvailabilityCallback(@NonNull AvailabilityCallback callback) { |
| 418 |
CameraManagerGlobal.get().unregisterAvailabilityCallback(callback); |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
public void registerTorchCallback(@NonNull TorchCallback callback, @Nullable Handler handler) { |
| 444 |
CameraManagerGlobal.get().registerTorchCallback(callback, |
| 445 |
CameraDeviceImpl.checkAndWrapHandler(handler)); |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
public void registerTorchCallback(@NonNull @CallbackExecutor Executor executor, |
| 462 |
@NonNull TorchCallback callback) { |
| 463 |
if (executor == null) { |
| 464 |
throw new IllegalArgumentException("executor was null"); |
| 465 |
} |
| 466 |
CameraManagerGlobal.get().registerTorchCallback(callback, executor); |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
public void unregisterTorchCallback(@NonNull TorchCallback callback) { |
| 478 |
CameraManagerGlobal.get().unregisterTorchCallback(callback); |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
private Size getDisplaySize() { |
| 483 |
Size ret = new Size(0, 0); |
| 484 |
|
| 485 |
try { |
| 486 |
DisplayManager displayManager = |
| 487 |
(DisplayManager) mContext.getSystemService(Context.DISPLAY_SERVICE); |
| 488 |
Display display = displayManager.getDisplay(Display.DEFAULT_DISPLAY); |
| 489 |
if (display != null) { |
| 490 |
Point sz = new Point(); |
| 491 |
display.getRealSize(sz); |
| 492 |
int width = sz.x; |
| 493 |
int height = sz.y; |
| 494 |
|
| 495 |
if (height > width) { |
| 496 |
height = width; |
| 497 |
width = sz.y; |
| 498 |
} |
| 499 |
|
| 500 |
ret = new Size(width, height); |
| 501 |
} else { |
| 502 |
Log.e(TAG, "Invalid default display!"); |
| 503 |
} |
| 504 |
} catch (Exception e) { |
| 505 |
Log.e(TAG, "getDisplaySize Failed. " + e); |
| 506 |
} |
| 507 |
|
| 508 |
return ret; |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
private Map<String, StreamConfiguration[]> getPhysicalCameraMultiResolutionConfigs( |
| 523 |
String cameraId, CameraMetadataNative info, ICameraService cameraService) |
| 524 |
throws CameraAccessException { |
| 525 |
HashMap<String, StreamConfiguration[]> multiResolutionStreamConfigurations = |
| 526 |
new HashMap<String, StreamConfiguration[]>(); |
| 527 |
|
| 528 |
Boolean multiResolutionStreamSupported = info.get( |
| 529 |
CameraCharacteristics.SCALER_MULTI_RESOLUTION_STREAM_SUPPORTED); |
| 530 |
if (multiResolutionStreamSupported == null || !multiResolutionStreamSupported) { |
| 531 |
return multiResolutionStreamConfigurations; |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
Set<String> physicalCameraIds = info.getPhysicalCameraIds(); |
| 539 |
if (physicalCameraIds.size() == 0 && info.isUltraHighResolutionSensor()) { |
| 540 |
StreamConfiguration[] configs = info.get(CameraCharacteristics. |
| 541 |
SCALER_PHYSICAL_CAMERA_MULTI_RESOLUTION_STREAM_CONFIGURATIONS); |
| 542 |
if (configs != null) { |
| 543 |
multiResolutionStreamConfigurations.put(cameraId, configs); |
| 544 |
} |
| 545 |
return multiResolutionStreamConfigurations; |
| 546 |
} |
| 547 |
try { |
| 548 |
for (String physicalCameraId : physicalCameraIds) { |
| 549 |
CameraMetadataNative physicalCameraInfo = |
| 550 |
cameraService.getCameraCharacteristics(physicalCameraId, |
| 551 |
mContext.getApplicationInfo().targetSdkVersion, |
| 552 |
false); |
| 553 |
StreamConfiguration[] configs = physicalCameraInfo.get( |
| 554 |
CameraCharacteristics. |
| 555 |
SCALER_PHYSICAL_CAMERA_MULTI_RESOLUTION_STREAM_CONFIGURATIONS); |
| 556 |
if (configs != null) { |
| 557 |
multiResolutionStreamConfigurations.put(physicalCameraId, configs); |
| 558 |
} |
| 559 |
} |
| 560 |
} catch (RemoteException e) { |
| 561 |
ServiceSpecificException sse = new ServiceSpecificException( |
| 562 |
ICameraService.ERROR_DISCONNECTED, |
| 563 |
"Camera service is currently unavailable"); |
| 564 |
throwAsPublicException(sse); |
| 565 |
} |
| 566 |
|
| 567 |
return multiResolutionStreamConfigurations; |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
@NonNull |
| 596 |
public CameraCharacteristics getCameraCharacteristics(@NonNull String cameraId) |
| 597 |
throws CameraAccessException { |
| 598 |
CameraCharacteristics characteristics = null; |
| 599 |
if (CameraManagerGlobal.sCameraServiceDisabled) { |
| 600 |
throw new IllegalArgumentException("No cameras available on device"); |
| 601 |
} |
| 602 |
synchronized (mLock) { |
| 603 |
ICameraService cameraService = CameraManagerGlobal.get().getCameraService(); |
| 604 |
if (cameraService == null) { |
| 605 |
throw new CameraAccessException(CameraAccessException.CAMERA_DISCONNECTED, |
| 606 |
"Camera service is currently unavailable"); |
| 607 |
} |
| 608 |
try { |
| 609 |
Size displaySize = getDisplaySize(); |
| 610 |
|
| 611 |
boolean overrideToPortrait = shouldOverrideToPortrait(); |
| 612 |
CameraMetadataNative info = cameraService.getCameraCharacteristics(cameraId, |
| 613 |
mContext.getApplicationInfo().targetSdkVersion, overrideToPortrait); |
| 614 |
try { |
| 615 |
info.setCameraId(Integer.parseInt(cameraId)); |
| 616 |
} catch (NumberFormatException e) { |
| 617 |
Log.v(TAG, "Failed to parse camera Id " + cameraId + " to integer"); |
| 618 |
} |
| 619 |
|
| 620 |
boolean hasConcurrentStreams = |
| 621 |
CameraManagerGlobal.get().cameraIdHasConcurrentStreamsLocked(cameraId); |
| 622 |
info.setHasMandatoryConcurrentStreams(hasConcurrentStreams); |
| 623 |
info.setDisplaySize(displaySize); |
| 624 |
|
| 625 |
Map<String, StreamConfiguration[]> multiResolutionSizeMap = |
| 626 |
getPhysicalCameraMultiResolutionConfigs(cameraId, info, cameraService); |
| 627 |
if (multiResolutionSizeMap.size() > 0) { |
| 628 |
info.setMultiResolutionStreamConfigurationMap(multiResolutionSizeMap); |
| 629 |
} |
| 630 |
|
| 631 |
characteristics = new CameraCharacteristics(info); |
| 632 |
} catch (ServiceSpecificException e) { |
| 633 |
throwAsPublicException(e); |
| 634 |
} catch (RemoteException e) { |
| 635 |
|
| 636 |
throw new CameraAccessException(CameraAccessException.CAMERA_DISCONNECTED, |
| 637 |
"Camera service is currently unavailable", e); |
| 638 |
} |
| 639 |
} |
| 640 |
registerDeviceStateListener(characteristics); |
| 641 |
return characteristics; |
| 642 |
} |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
@NonNull |
| 660 |
public CameraExtensionCharacteristics getCameraExtensionCharacteristics( |
| 661 |
@NonNull String cameraId) throws CameraAccessException { |
| 662 |
CameraCharacteristics chars = getCameraCharacteristics(cameraId); |
| 663 |
return new CameraExtensionCharacteristics(mContext, cameraId, chars); |
| 664 |
} |
| 665 |
|
| 666 |
private Map<String, CameraCharacteristics> getPhysicalIdToCharsMap( |
| 667 |
CameraCharacteristics chars) throws CameraAccessException { |
| 668 |
HashMap<String, CameraCharacteristics> physicalIdsToChars = |
| 669 |
new HashMap<String, CameraCharacteristics>(); |
| 670 |
Set<String> physicalCameraIds = chars.getPhysicalCameraIds(); |
| 671 |
for (String physicalCameraId : physicalCameraIds) { |
| 672 |
CameraCharacteristics physicalChars = getCameraCharacteristics(physicalCameraId); |
| 673 |
physicalIdsToChars.put(physicalCameraId, physicalChars); |
| 674 |
} |
| 675 |
return physicalIdsToChars; |
| 676 |
} |
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
private CameraDevice openCameraDeviceUserAsync(String cameraId, |
| 702 |
CameraDevice.StateCallback callback, Executor executor, final int uid, |
| 703 |
final int oomScoreOffset) throws CameraAccessException { |
| 704 |
CameraCharacteristics characteristics = getCameraCharacteristics(cameraId); |
| 705 |
CameraDevice device = null; |
| 706 |
Map<String, CameraCharacteristics> physicalIdsToChars = |
| 707 |
getPhysicalIdToCharsMap(characteristics); |
| 708 |
synchronized (mLock) { |
| 709 |
|
| 710 |
ICameraDeviceUser cameraUser = null; |
| 711 |
android.hardware.camera2.impl.CameraDeviceImpl deviceImpl = |
| 712 |
new android.hardware.camera2.impl.CameraDeviceImpl( |
| 713 |
cameraId, |
| 714 |
callback, |
| 715 |
executor, |
| 716 |
characteristics, |
| 717 |
physicalIdsToChars, |
| 718 |
mContext.getApplicationInfo().targetSdkVersion, |
| 719 |
mContext); |
| 720 |
|
| 721 |
ICameraDeviceCallbacks callbacks = deviceImpl.getCallbacks(); |
| 722 |
|
| 723 |
try { |
| 724 |
ICameraService cameraService = CameraManagerGlobal.get().getCameraService(); |
| 725 |
if (cameraService == null) { |
| 726 |
throw new ServiceSpecificException( |
| 727 |
ICameraService.ERROR_DISCONNECTED, |
| 728 |
"Camera service is currently unavailable"); |
| 729 |
} |
| 730 |
|
| 731 |
boolean overrideToPortrait = shouldOverrideToPortrait(); |
| 732 |
cameraUser = cameraService.connectDevice(callbacks, cameraId, |
| 733 |
mContext.getOpPackageName(), mContext.getAttributionTag(), uid, |
| 734 |
oomScoreOffset, mContext.getApplicationInfo().targetSdkVersion, |
| 735 |
overrideToPortrait); |
| 736 |
} catch (ServiceSpecificException e) { |
| 737 |
if (e.errorCode == ICameraService.ERROR_DEPRECATED_HAL) { |
| 738 |
throw new AssertionError("Should've gone down the shim path"); |
| 739 |
} else if (e.errorCode == ICameraService.ERROR_CAMERA_IN_USE || |
| 740 |
e.errorCode == ICameraService.ERROR_MAX_CAMERAS_IN_USE || |
| 741 |
e.errorCode == ICameraService.ERROR_DISABLED || |
| 742 |
e.errorCode == ICameraService.ERROR_DISCONNECTED || |
| 743 |
e.errorCode == ICameraService.ERROR_INVALID_OPERATION) { |
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
deviceImpl.setRemoteFailure(e); |
| 748 |
|
| 749 |
if (e.errorCode == ICameraService.ERROR_DISABLED || |
| 750 |
e.errorCode == ICameraService.ERROR_DISCONNECTED || |
| 751 |
e.errorCode == ICameraService.ERROR_CAMERA_IN_USE) { |
| 752 |
|
| 753 |
throwAsPublicException(e); |
| 754 |
} |
| 755 |
} else { |
| 756 |
|
| 757 |
throwAsPublicException(e); |
| 758 |
} |
| 759 |
} catch (RemoteException e) { |
| 760 |
|
| 761 |
ServiceSpecificException sse = new ServiceSpecificException( |
| 762 |
ICameraService.ERROR_DISCONNECTED, |
| 763 |
"Camera service is currently unavailable"); |
| 764 |
deviceImpl.setRemoteFailure(sse); |
| 765 |
throwAsPublicException(sse); |
| 766 |
} |
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
deviceImpl.setRemoteDevice(cameraUser); |
| 774 |
device = deviceImpl; |
| 775 |
} |
| 776 |
|
| 777 |
return device; |
| 778 |
} |
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
@RequiresPermission(android.Manifest.permission.CAMERA) |
| 858 |
public void openCamera(@NonNull String cameraId, |
| 859 |
@NonNull final CameraDevice.StateCallback callback, @Nullable Handler handler) |
| 860 |
throws CameraAccessException { |
| 861 |
|
| 862 |
openCameraForUid(cameraId, callback, CameraDeviceImpl.checkAndWrapHandler(handler), |
| 863 |
USE_CALLING_UID); |
| 864 |
} |
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
@RequiresPermission(android.Manifest.permission.CAMERA) |
| 895 |
public void openCamera(@NonNull String cameraId, |
| 896 |
@NonNull @CallbackExecutor Executor executor, |
| 897 |
@NonNull final CameraDevice.StateCallback callback) |
| 898 |
throws CameraAccessException { |
| 899 |
if (executor == null) { |
| 900 |
throw new IllegalArgumentException("executor was null"); |
| 901 |
} |
| 902 |
openCameraForUid(cameraId, callback, executor, USE_CALLING_UID); |
| 903 |
} |
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
@SystemApi |
| 955 |
@TestApi |
| 956 |
@RequiresPermission(allOf = { |
| 957 |
android.Manifest.permission.SYSTEM_CAMERA, |
| 958 |
android.Manifest.permission.CAMERA, |
| 959 |
}) |
| 960 |
public void openCamera(@NonNull String cameraId, int oomScoreOffset, |
| 961 |
@NonNull @CallbackExecutor Executor executor, |
| 962 |
@NonNull final CameraDevice.StateCallback callback) throws CameraAccessException { |
| 963 |
if (executor == null) { |
| 964 |
throw new IllegalArgumentException("executor was null"); |
| 965 |
} |
| 966 |
if (oomScoreOffset < 0) { |
| 967 |
throw new IllegalArgumentException( |
| 968 |
"oomScoreOffset < 0, cannot increase priority of camera client"); |
| 969 |
} |
| 970 |
openCameraForUid(cameraId, callback, executor, USE_CALLING_UID, oomScoreOffset); |
| 971 |
} |
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
public void openCameraForUid(@NonNull String cameraId, |
| 991 |
@NonNull final CameraDevice.StateCallback callback, @NonNull Executor executor, |
| 992 |
int clientUid, int oomScoreOffset) throws CameraAccessException { |
| 993 |
|
| 994 |
if (cameraId == null) { |
| 995 |
throw new IllegalArgumentException("cameraId was null"); |
| 996 |
} else if (callback == null) { |
| 997 |
throw new IllegalArgumentException("callback was null"); |
| 998 |
} |
| 999 |
if (CameraManagerGlobal.sCameraServiceDisabled) { |
| 1000 |
throw new IllegalArgumentException("No cameras available on device"); |
| 1001 |
} |
| 1002 |
|
| 1003 |
openCameraDeviceUserAsync(cameraId, callback, executor, clientUid, oomScoreOffset); |
| 1004 |
} |
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
public void openCameraForUid(@NonNull String cameraId, |
| 1022 |
@NonNull final CameraDevice.StateCallback callback, @NonNull Executor executor, |
| 1023 |
int clientUid) throws CameraAccessException { |
| 1024 |
openCameraForUid(cameraId, callback, executor, clientUid, 0); |
| 1025 |
} |
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
public void setTorchMode(@NonNull String cameraId, boolean enabled) |
| 1067 |
throws CameraAccessException { |
| 1068 |
if (CameraManagerGlobal.sCameraServiceDisabled) { |
| 1069 |
throw new IllegalArgumentException("No cameras available on device"); |
| 1070 |
} |
| 1071 |
CameraManagerGlobal.get().setTorchMode(cameraId, enabled); |
| 1072 |
} |
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
public void turnOnTorchWithStrengthLevel(@NonNull String cameraId, int torchStrength) |
| 1130 |
throws CameraAccessException { |
| 1131 |
if (CameraManagerGlobal.sCameraServiceDisabled) { |
| 1132 |
throw new IllegalArgumentException("No camera available on device"); |
| 1133 |
} |
| 1134 |
CameraManagerGlobal.get().turnOnTorchWithStrengthLevel(cameraId, torchStrength); |
| 1135 |
} |
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
public int getTorchStrengthLevel(@NonNull String cameraId) |
| 1156 |
throws CameraAccessException { |
| 1157 |
if (CameraManagerGlobal.sCameraServiceDisabled) { |
| 1158 |
throw new IllegalArgumentException("No camera available on device."); |
| 1159 |
} |
| 1160 |
return CameraManagerGlobal.get().getTorchStrengthLevel(cameraId); |
| 1161 |
} |
| 1162 |
|
| 1163 |
private static boolean shouldOverrideToPortrait() { |
| 1164 |
return CompatChanges.isChangeEnabled(OVERRIDE_FRONT_CAMERA_APP_COMPAT) |
| 1165 |
&& CameraManagerGlobal.sLandscapeToPortrait; |
| 1166 |
} |
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
public static abstract class AvailabilityCallback { |
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
public void onCameraAvailable(@NonNull String cameraId) { |
| 1192 |
|
| 1193 |
} |
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
public void onCameraUnavailable(@NonNull String cameraId) { |
| 1207 |
|
| 1208 |
} |
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
public void onCameraAccessPrioritiesChanged() { |
| 1232 |
|
| 1233 |
} |
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
public void onPhysicalCameraAvailable(@NonNull String cameraId, |
| 1268 |
@NonNull String physicalCameraId) { |
| 1269 |
|
| 1270 |
} |
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
public void onPhysicalCameraUnavailable(@NonNull String cameraId, |
| 1304 |
@NonNull String physicalCameraId) { |
| 1305 |
|
| 1306 |
} |
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
@SystemApi |
| 1321 |
@TestApi |
| 1322 |
@RequiresPermission(android.Manifest.permission.CAMERA_OPEN_CLOSE_LISTENER) |
| 1323 |
public void onCameraOpened(@NonNull String cameraId, @NonNull String packageId) { |
| 1324 |
|
| 1325 |
} |
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
@SystemApi |
| 1337 |
@TestApi |
| 1338 |
@RequiresPermission(android.Manifest.permission.CAMERA_OPEN_CLOSE_LISTENER) |
| 1339 |
public void onCameraClosed(@NonNull String cameraId) { |
| 1340 |
|
| 1341 |
} |
| 1342 |
} |
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
public static abstract class TorchCallback { |
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
public void onTorchModeUnavailable(@NonNull String cameraId) { |
| 1380 |
|
| 1381 |
} |
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
public void onTorchModeChanged(@NonNull String cameraId, boolean enabled) { |
| 1397 |
|
| 1398 |
} |
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
public void onTorchStrengthLevelChanged(@NonNull String cameraId, int newStrengthLevel) { |
| 1415 |
|
| 1416 |
} |
| 1417 |
} |
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 1425 |
public static void throwAsPublicException(Throwable t) throws CameraAccessException { |
| 1426 |
if (t instanceof ServiceSpecificException) { |
| 1427 |
ServiceSpecificException e = (ServiceSpecificException) t; |
| 1428 |
int reason = CameraAccessException.CAMERA_ERROR; |
| 1429 |
switch(e.errorCode) { |
| 1430 |
case ICameraService.ERROR_DISCONNECTED: |
| 1431 |
reason = CameraAccessException.CAMERA_DISCONNECTED; |
| 1432 |
break; |
| 1433 |
case ICameraService.ERROR_DISABLED: |
| 1434 |
reason = CameraAccessException.CAMERA_DISABLED; |
| 1435 |
break; |
| 1436 |
case ICameraService.ERROR_CAMERA_IN_USE: |
| 1437 |
reason = CameraAccessException.CAMERA_IN_USE; |
| 1438 |
break; |
| 1439 |
case ICameraService.ERROR_MAX_CAMERAS_IN_USE: |
| 1440 |
reason = CameraAccessException.MAX_CAMERAS_IN_USE; |
| 1441 |
break; |
| 1442 |
case ICameraService.ERROR_DEPRECATED_HAL: |
| 1443 |
reason = CameraAccessException.CAMERA_DEPRECATED_HAL; |
| 1444 |
break; |
| 1445 |
case ICameraService.ERROR_ILLEGAL_ARGUMENT: |
| 1446 |
case ICameraService.ERROR_ALREADY_EXISTS: |
| 1447 |
throw new IllegalArgumentException(e.getMessage(), e); |
| 1448 |
case ICameraService.ERROR_PERMISSION_DENIED: |
| 1449 |
throw new SecurityException(e.getMessage(), e); |
| 1450 |
case ICameraService.ERROR_TIMED_OUT: |
| 1451 |
case ICameraService.ERROR_INVALID_OPERATION: |
| 1452 |
default: |
| 1453 |
reason = CameraAccessException.CAMERA_ERROR; |
| 1454 |
} |
| 1455 |
throw new CameraAccessException(reason, e.getMessage(), e); |
| 1456 |
} else if (t instanceof DeadObjectException) { |
| 1457 |
throw new CameraAccessException(CameraAccessException.CAMERA_DISCONNECTED, |
| 1458 |
"Camera service has died unexpectedly", |
| 1459 |
t); |
| 1460 |
} else if (t instanceof RemoteException) { |
| 1461 |
throw new UnsupportedOperationException("An unknown RemoteException was thrown" + |
| 1462 |
" which should never happen.", t); |
| 1463 |
} else if (t instanceof RuntimeException) { |
| 1464 |
RuntimeException e = (RuntimeException) t; |
| 1465 |
throw e; |
| 1466 |
} |
| 1467 |
} |
| 1468 |
|
| 1469 |
|
| 1470 |
|
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
public static boolean isHiddenPhysicalCamera(String cameraId) { |
| 1482 |
try { |
| 1483 |
ICameraService cameraService = CameraManagerGlobal.get().getCameraService(); |
| 1484 |
|
| 1485 |
if (cameraService == null) return false; |
| 1486 |
|
| 1487 |
return cameraService.isHiddenPhysicalCamera(cameraId); |
| 1488 |
} catch (RemoteException e) { |
| 1489 |
|
| 1490 |
} |
| 1491 |
return false; |
| 1492 |
} |
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
|
| 1510 |
|
| 1511 |
|
| 1512 |
|
| 1513 |
|
| 1514 |
|
| 1515 |
|
| 1516 |
|
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
@RequiresPermission(android.Manifest.permission.CAMERA_INJECT_EXTERNAL_CAMERA) |
| 1521 |
public void injectCamera(@NonNull String packageName, @NonNull String internalCamId, |
| 1522 |
@NonNull String externalCamId, @NonNull @CallbackExecutor Executor executor, |
| 1523 |
@NonNull CameraInjectionSession.InjectionStatusCallback callback) |
| 1524 |
throws CameraAccessException, SecurityException, |
| 1525 |
IllegalArgumentException { |
| 1526 |
if (CameraManagerGlobal.sCameraServiceDisabled) { |
| 1527 |
throw new IllegalArgumentException("No cameras available on device"); |
| 1528 |
} |
| 1529 |
ICameraService cameraService = CameraManagerGlobal.get().getCameraService(); |
| 1530 |
if (cameraService == null) { |
| 1531 |
throw new CameraAccessException(CameraAccessException.CAMERA_DISCONNECTED, |
| 1532 |
"Camera service is currently unavailable"); |
| 1533 |
} |
| 1534 |
synchronized (mLock) { |
| 1535 |
try { |
| 1536 |
CameraInjectionSessionImpl injectionSessionImpl = |
| 1537 |
new CameraInjectionSessionImpl(callback, executor); |
| 1538 |
ICameraInjectionCallback cameraInjectionCallback = |
| 1539 |
injectionSessionImpl.getCallback(); |
| 1540 |
ICameraInjectionSession injectionSession = cameraService.injectCamera(packageName, |
| 1541 |
internalCamId, externalCamId, cameraInjectionCallback); |
| 1542 |
injectionSessionImpl.setRemoteInjectionSession(injectionSession); |
| 1543 |
} catch (ServiceSpecificException e) { |
| 1544 |
throwAsPublicException(e); |
| 1545 |
} catch (RemoteException e) { |
| 1546 |
|
| 1547 |
ServiceSpecificException sse = new ServiceSpecificException( |
| 1548 |
ICameraService.ERROR_DISCONNECTED, |
| 1549 |
"Camera service is currently unavailable"); |
| 1550 |
throwAsPublicException(sse); |
| 1551 |
} |
| 1552 |
} |
| 1553 |
} |
| 1554 |
|
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
private static final class CameraManagerGlobal extends ICameraServiceListener.Stub |
| 1560 |
implements IBinder.DeathRecipient { |
| 1561 |
|
| 1562 |
private static final String TAG = "CameraManagerGlobal"; |
| 1563 |
private final boolean DEBUG = false; |
| 1564 |
|
| 1565 |
private final int CAMERA_SERVICE_RECONNECT_DELAY_MS = 1000; |
| 1566 |
|
| 1567 |
|
| 1568 |
private static final CameraManagerGlobal gCameraManager = |
| 1569 |
new CameraManagerGlobal(); |
| 1570 |
|
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
private static final String CAMERA_SERVICE_BINDER_NAME = "media.camera"; |
| 1575 |
|
| 1576 |
private final ScheduledExecutorService mScheduler = Executors.newScheduledThreadPool(1); |
| 1577 |
|
| 1578 |
private final ArrayMap<String, Integer> mDeviceStatus = new ArrayMap<String, Integer>(); |
| 1579 |
|
| 1580 |
private final ArrayMap<String, ArrayList<String>> mUnavailablePhysicalDevices = |
| 1581 |
new ArrayMap<String, ArrayList<String>>(); |
| 1582 |
|
| 1583 |
private final ArrayMap<String, String> mOpenedDevices = new ArrayMap<String, String>(); |
| 1584 |
|
| 1585 |
private final Set<Set<String>> mConcurrentCameraIdCombinations = |
| 1586 |
new ArraySet<Set<String>>(); |
| 1587 |
|
| 1588 |
|
| 1589 |
private final ArrayMap<AvailabilityCallback, Executor> mCallbackMap = |
| 1590 |
new ArrayMap<AvailabilityCallback, Executor>(); |
| 1591 |
|
| 1592 |
|
| 1593 |
private Binder mTorchClientBinder = new Binder(); |
| 1594 |
|
| 1595 |
|
| 1596 |
private final ArrayMap<String, Integer> mTorchStatus = new ArrayMap<String, Integer>(); |
| 1597 |
|
| 1598 |
|
| 1599 |
private final ArrayMap<TorchCallback, Executor> mTorchCallbackMap = |
| 1600 |
new ArrayMap<TorchCallback, Executor>(); |
| 1601 |
|
| 1602 |
private final Object mLock = new Object(); |
| 1603 |
|
| 1604 |
|
| 1605 |
private ICameraService mCameraService; |
| 1606 |
private boolean mHasOpenCloseListenerPermission = false; |
| 1607 |
|
| 1608 |
|
| 1609 |
private CameraManagerGlobal() { } |
| 1610 |
|
| 1611 |
public static final boolean sCameraServiceDisabled = |
| 1612 |
SystemProperties.getBoolean("config.disable_cameraservice", false); |
| 1613 |
|
| 1614 |
public static final boolean sLandscapeToPortrait = |
| 1615 |
SystemProperties.getBoolean(LANDSCAPE_TO_PORTRAIT_PROP, false); |
| 1616 |
|
| 1617 |
public static CameraManagerGlobal get() { |
| 1618 |
return gCameraManager; |
| 1619 |
} |
| 1620 |
|
| 1621 |
@Override |
| 1622 |
public IBinder asBinder() { |
| 1623 |
return this; |
| 1624 |
} |
| 1625 |
|
| 1626 |
|
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
public ICameraService getCameraService() { |
| 1634 |
synchronized(mLock) { |
| 1635 |
connectCameraServiceLocked(); |
| 1636 |
if (mCameraService == null && !sCameraServiceDisabled) { |
| 1637 |
Log.e(TAG, "Camera service is unavailable"); |
| 1638 |
} |
| 1639 |
return mCameraService; |
| 1640 |
} |
| 1641 |
} |
| 1642 |
|
| 1643 |
|
| 1644 |
|
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
private void connectCameraServiceLocked() { |
| 1650 |
|
| 1651 |
if (mCameraService != null || sCameraServiceDisabled) return; |
| 1652 |
|
| 1653 |
Log.i(TAG, "Connecting to camera service"); |
| 1654 |
|
| 1655 |
IBinder cameraServiceBinder = ServiceManager.getService(CAMERA_SERVICE_BINDER_NAME); |
| 1656 |
if (cameraServiceBinder == null) { |
| 1657 |
|
| 1658 |
return; |
| 1659 |
} |
| 1660 |
try { |
| 1661 |
cameraServiceBinder.linkToDeath(this, 0); |
| 1662 |
} catch (RemoteException e) { |
| 1663 |
|
| 1664 |
return; |
| 1665 |
} |
| 1666 |
|
| 1667 |
ICameraService cameraService = ICameraService.Stub.asInterface(cameraServiceBinder); |
| 1668 |
|
| 1669 |
try { |
| 1670 |
CameraMetadataNative.setupGlobalVendorTagDescriptor(); |
| 1671 |
} catch (ServiceSpecificException e) { |
| 1672 |
handleRecoverableSetupErrors(e); |
| 1673 |
} |
| 1674 |
|
| 1675 |
try { |
| 1676 |
CameraStatus[] cameraStatuses = cameraService.addListener(this); |
| 1677 |
for (CameraStatus c : cameraStatuses) { |
| 1678 |
onStatusChangedLocked(c.status, c.cameraId); |
| 1679 |
|
| 1680 |
if (c.unavailablePhysicalCameras != null) { |
| 1681 |
for (String unavailPhysicalCamera : c.unavailablePhysicalCameras) { |
| 1682 |
onPhysicalCameraStatusChangedLocked( |
| 1683 |
ICameraServiceListener.STATUS_NOT_PRESENT, |
| 1684 |
c.cameraId, unavailPhysicalCamera); |
| 1685 |
} |
| 1686 |
} |
| 1687 |
|
| 1688 |
if (mHasOpenCloseListenerPermission && |
| 1689 |
c.status == ICameraServiceListener.STATUS_NOT_AVAILABLE && |
| 1690 |
!c.clientPackage.isEmpty()) { |
| 1691 |
onCameraOpenedLocked(c.cameraId, c.clientPackage); |
| 1692 |
} |
| 1693 |
} |
| 1694 |
mCameraService = cameraService; |
| 1695 |
} catch(ServiceSpecificException e) { |
| 1696 |
|
| 1697 |
throw new IllegalStateException("Failed to register a camera service listener", e); |
| 1698 |
} catch (RemoteException e) { |
| 1699 |
|
| 1700 |
} |
| 1701 |
|
| 1702 |
try { |
| 1703 |
ConcurrentCameraIdCombination[] cameraIdCombinations = |
| 1704 |
cameraService.getConcurrentCameraIds(); |
| 1705 |
for (ConcurrentCameraIdCombination comb : cameraIdCombinations) { |
| 1706 |
mConcurrentCameraIdCombinations.add(comb.getConcurrentCameraIdCombination()); |
| 1707 |
} |
| 1708 |
} catch (ServiceSpecificException e) { |
| 1709 |
|
| 1710 |
throw new IllegalStateException("Failed to get concurrent camera id combinations", |
| 1711 |
e); |
| 1712 |
} catch (RemoteException e) { |
| 1713 |
|
| 1714 |
} |
| 1715 |
} |
| 1716 |
|
| 1717 |
private String[] extractCameraIdListLocked() { |
| 1718 |
String[] cameraIds = null; |
| 1719 |
int idCount = 0; |
| 1720 |
for (int i = 0; i < mDeviceStatus.size(); i++) { |
| 1721 |
int status = mDeviceStatus.valueAt(i); |
| 1722 |
if (status == ICameraServiceListener.STATUS_NOT_PRESENT |
| 1723 |
|| status == ICameraServiceListener.STATUS_ENUMERATING) continue; |
| 1724 |
idCount++; |
| 1725 |
} |
| 1726 |
cameraIds = new String[idCount]; |
| 1727 |
idCount = 0; |
| 1728 |
for (int i = 0; i < mDeviceStatus.size(); i++) { |
| 1729 |
int status = mDeviceStatus.valueAt(i); |
| 1730 |
if (status == ICameraServiceListener.STATUS_NOT_PRESENT |
| 1731 |
|| status == ICameraServiceListener.STATUS_ENUMERATING) continue; |
| 1732 |
cameraIds[idCount] = mDeviceStatus.keyAt(i); |
| 1733 |
idCount++; |
| 1734 |
} |
| 1735 |
return cameraIds; |
| 1736 |
} |
| 1737 |
|
| 1738 |
private Set<Set<String>> extractConcurrentCameraIdListLocked() { |
| 1739 |
Set<Set<String>> concurrentCameraIds = new ArraySet<Set<String>>(); |
| 1740 |
for (Set<String> cameraIds : mConcurrentCameraIdCombinations) { |
| 1741 |
Set<String> extractedCameraIds = new ArraySet<String>(); |
| 1742 |
for (String cameraId : cameraIds) { |
| 1743 |
|
| 1744 |
|
| 1745 |
|
| 1746 |
Integer status = mDeviceStatus.get(cameraId); |
| 1747 |
if (status == null) { |
| 1748 |
|
| 1749 |
continue; |
| 1750 |
} |
| 1751 |
if (status == ICameraServiceListener.STATUS_ENUMERATING |
| 1752 |
|| status == ICameraServiceListener.STATUS_NOT_PRESENT) { |
| 1753 |
continue; |
| 1754 |
} |
| 1755 |
extractedCameraIds.add(cameraId); |
| 1756 |
} |
| 1757 |
concurrentCameraIds.add(extractedCameraIds); |
| 1758 |
} |
| 1759 |
return concurrentCameraIds; |
| 1760 |
} |
| 1761 |
|
| 1762 |
private static void sortCameraIds(String[] cameraIds) { |
| 1763 |
|
| 1764 |
|
| 1765 |
Arrays.sort(cameraIds, new Comparator<String>() { |
| 1766 |
@Override |
| 1767 |
public int compare(String s1, String s2) { |
| 1768 |
int s1Int = 0, s2Int = 0; |
| 1769 |
try { |
| 1770 |
s1Int = Integer.parseInt(s1); |
| 1771 |
} catch (NumberFormatException e) { |
| 1772 |
s1Int = -1; |
| 1773 |
} |
| 1774 |
|
| 1775 |
try { |
| 1776 |
s2Int = Integer.parseInt(s2); |
| 1777 |
} catch (NumberFormatException e) { |
| 1778 |
s2Int = -1; |
| 1779 |
} |
| 1780 |
|
| 1781 |
|
| 1782 |
if (s1Int >= 0 && s2Int >= 0) { |
| 1783 |
return s1Int - s2Int; |
| 1784 |
} else if (s1Int >= 0) { |
| 1785 |
return -1; |
| 1786 |
} else if (s2Int >= 0) { |
| 1787 |
return 1; |
| 1788 |
} else { |
| 1789 |
|
| 1790 |
return s1.compareTo(s2); |
| 1791 |
} |
| 1792 |
}}); |
| 1793 |
|
| 1794 |
} |
| 1795 |
|
| 1796 |
public static boolean cameraStatusesContains(CameraStatus[] cameraStatuses, String id) { |
| 1797 |
for (CameraStatus c : cameraStatuses) { |
| 1798 |
if (c.cameraId.equals(id)) { |
| 1799 |
return true; |
| 1800 |
} |
| 1801 |
} |
| 1802 |
return false; |
| 1803 |
} |
| 1804 |
|
| 1805 |
public String[] getCameraIdListNoLazy() { |
| 1806 |
if (sCameraServiceDisabled) { |
| 1807 |
return new String[] {}; |
| 1808 |
} |
| 1809 |
|
| 1810 |
CameraStatus[] cameraStatuses; |
| 1811 |
ICameraServiceListener.Stub testListener = new ICameraServiceListener.Stub() { |
| 1812 |
@Override |
| 1813 |
public void onStatusChanged(int status, String id) throws RemoteException { |
| 1814 |
} |
| 1815 |
@Override |
| 1816 |
public void onPhysicalCameraStatusChanged(int status, |
| 1817 |
String id, String physicalId) throws RemoteException { |
| 1818 |
} |
| 1819 |
@Override |
| 1820 |
public void onTorchStatusChanged(int status, String id) throws RemoteException { |
| 1821 |
} |
| 1822 |
@Override |
| 1823 |
public void onTorchStrengthLevelChanged(String id, int newStrengthLevel) |
| 1824 |
throws RemoteException { |
| 1825 |
} |
| 1826 |
@Override |
| 1827 |
public void onCameraAccessPrioritiesChanged() { |
| 1828 |
} |
| 1829 |
@Override |
| 1830 |
public void onCameraOpened(String id, String clientPackageId) { |
| 1831 |
} |
| 1832 |
@Override |
| 1833 |
public void onCameraClosed(String id) { |
| 1834 |
}}; |
| 1835 |
|
| 1836 |
String[] cameraIds = null; |
| 1837 |
synchronized (mLock) { |
| 1838 |
connectCameraServiceLocked(); |
| 1839 |
try { |
| 1840 |
|
| 1841 |
|
| 1842 |
|
| 1843 |
|
| 1844 |
|
| 1845 |
|
| 1846 |
|
| 1847 |
|
| 1848 |
|
| 1849 |
|
| 1850 |
|
| 1851 |
|
| 1852 |
cameraStatuses = mCameraService.addListener(testListener); |
| 1853 |
mCameraService.removeListener(testListener); |
| 1854 |
for (CameraStatus c : cameraStatuses) { |
| 1855 |
onStatusChangedLocked(c.status, c.cameraId); |
| 1856 |
} |
| 1857 |
Set<String> deviceCameraIds = mDeviceStatus.keySet(); |
| 1858 |
ArrayList<String> deviceIdsToRemove = new ArrayList<String>(); |
| 1859 |
for (String deviceCameraId : deviceCameraIds) { |
| 1860 |
|
| 1861 |
|
| 1862 |
|
| 1863 |
|
| 1864 |
if (!cameraStatusesContains(cameraStatuses, deviceCameraId)) { |
| 1865 |
deviceIdsToRemove.add(deviceCameraId); |
| 1866 |
} |
| 1867 |
} |
| 1868 |
for (String id : deviceIdsToRemove) { |
| 1869 |
onStatusChangedLocked(ICameraServiceListener.STATUS_NOT_PRESENT, id); |
| 1870 |
mTorchStatus.remove(id); |
| 1871 |
} |
| 1872 |
} catch (ServiceSpecificException e) { |
| 1873 |
|
| 1874 |
throw new IllegalStateException("Failed to register a camera service listener", |
| 1875 |
e); |
| 1876 |
} catch (RemoteException e) { |
| 1877 |
|
| 1878 |
} |
| 1879 |
cameraIds = extractCameraIdListLocked(); |
| 1880 |
} |
| 1881 |
sortCameraIds(cameraIds); |
| 1882 |
return cameraIds; |
| 1883 |
} |
| 1884 |
|
| 1885 |
|
| 1886 |
|
| 1887 |
|
| 1888 |
|
| 1889 |
public String[] getCameraIdList() { |
| 1890 |
String[] cameraIds = null; |
| 1891 |
synchronized (mLock) { |
| 1892 |
|
| 1893 |
connectCameraServiceLocked(); |
| 1894 |
cameraIds = extractCameraIdListLocked(); |
| 1895 |
} |
| 1896 |
sortCameraIds(cameraIds); |
| 1897 |
return cameraIds; |
| 1898 |
} |
| 1899 |
|
| 1900 |
public @NonNull Set<Set<String>> getConcurrentCameraIds() { |
| 1901 |
Set<Set<String>> concurrentStreamingCameraIds = null; |
| 1902 |
synchronized (mLock) { |
| 1903 |
|
| 1904 |
connectCameraServiceLocked(); |
| 1905 |
concurrentStreamingCameraIds = extractConcurrentCameraIdListLocked(); |
| 1906 |
} |
| 1907 |
|
| 1908 |
return concurrentStreamingCameraIds; |
| 1909 |
} |
| 1910 |
|
| 1911 |
public boolean isConcurrentSessionConfigurationSupported( |
| 1912 |
@NonNull Map<String, SessionConfiguration> cameraIdsAndSessionConfigurations, |
| 1913 |
int targetSdkVersion) throws CameraAccessException { |
| 1914 |
|
| 1915 |
if (cameraIdsAndSessionConfigurations == null) { |
| 1916 |
throw new IllegalArgumentException("cameraIdsAndSessionConfigurations was null"); |
| 1917 |
} |
| 1918 |
|
| 1919 |
int size = cameraIdsAndSessionConfigurations.size(); |
| 1920 |
if (size == 0) { |
| 1921 |
throw new IllegalArgumentException("camera id and session combination is empty"); |
| 1922 |
} |
| 1923 |
|
| 1924 |
synchronized (mLock) { |
| 1925 |
|
| 1926 |
|
| 1927 |
boolean subsetFound = false; |
| 1928 |
for (Set<String> combination : mConcurrentCameraIdCombinations) { |
| 1929 |
if (combination.containsAll(cameraIdsAndSessionConfigurations.keySet())) { |
| 1930 |
subsetFound = true; |
| 1931 |
} |
| 1932 |
} |
| 1933 |
if (!subsetFound) { |
| 1934 |
Log.v(TAG, "isConcurrentSessionConfigurationSupported called with a subset of" |
| 1935 |
+ "camera ids not returned by getConcurrentCameraIds"); |
| 1936 |
return false; |
| 1937 |
} |
| 1938 |
CameraIdAndSessionConfiguration [] cameraIdsAndConfigs = |
| 1939 |
new CameraIdAndSessionConfiguration[size]; |
| 1940 |
int i = 0; |
| 1941 |
for (Map.Entry<String, SessionConfiguration> pair : |
| 1942 |
cameraIdsAndSessionConfigurations.entrySet()) { |
| 1943 |
cameraIdsAndConfigs[i] = |
| 1944 |
new CameraIdAndSessionConfiguration(pair.getKey(), pair.getValue()); |
| 1945 |
i++; |
| 1946 |
} |
| 1947 |
try { |
| 1948 |
return mCameraService.isConcurrentSessionConfigurationSupported( |
| 1949 |
cameraIdsAndConfigs, targetSdkVersion); |
| 1950 |
} catch (ServiceSpecificException e) { |
| 1951 |
throwAsPublicException(e); |
| 1952 |
} catch (RemoteException e) { |
| 1953 |
|
| 1954 |
throw new CameraAccessException(CameraAccessException.CAMERA_DISCONNECTED, |
| 1955 |
"Camera service is currently unavailable", e); |
| 1956 |
} |
| 1957 |
} |
| 1958 |
|
| 1959 |
return false; |
| 1960 |
} |
| 1961 |
|
| 1962 |
|
| 1963 |
|
| 1964 |
|
| 1965 |
|
| 1966 |
|
| 1967 |
|
| 1968 |
|
| 1969 |
public boolean cameraIdHasConcurrentStreamsLocked(String cameraId) { |
| 1970 |
if (!mDeviceStatus.containsKey(cameraId)) { |
| 1971 |
|
| 1972 |
if (DEBUG) { |
| 1973 |
Log.v(TAG, " physical camera id " + cameraId + " is hidden." + |
| 1974 |
" Available logical camera ids : " + mDeviceStatus.toString()); |
| 1975 |
} |
| 1976 |
return false; |
| 1977 |
} |
| 1978 |
for (Set<String> comb : mConcurrentCameraIdCombinations) { |
| 1979 |
if (comb.contains(cameraId)) { |
| 1980 |
return true; |
| 1981 |
} |
| 1982 |
} |
| 1983 |
return false; |
| 1984 |
} |
| 1985 |
|
| 1986 |
public void setTorchMode(String cameraId, boolean enabled) throws CameraAccessException { |
| 1987 |
synchronized(mLock) { |
| 1988 |
|
| 1989 |
if (cameraId == null) { |
| 1990 |
throw new IllegalArgumentException("cameraId was null"); |
| 1991 |
} |
| 1992 |
|
| 1993 |
ICameraService cameraService = getCameraService(); |
| 1994 |
if (cameraService == null) { |
| 1995 |
throw new CameraAccessException(CameraAccessException.CAMERA_DISCONNECTED, |
| 1996 |
"Camera service is currently unavailable"); |
| 1997 |
} |
| 1998 |
|
| 1999 |
try { |
| 2000 |
cameraService.setTorchMode(cameraId, enabled, mTorchClientBinder); |
| 2001 |
} catch(ServiceSpecificException e) { |
| 2002 |
throwAsPublicException(e); |
| 2003 |
} catch (RemoteException e) { |
| 2004 |
throw new CameraAccessException(CameraAccessException.CAMERA_DISCONNECTED, |
| 2005 |
"Camera service is currently unavailable"); |
| 2006 |
} |
| 2007 |
} |
| 2008 |
} |
| 2009 |
|
| 2010 |
public void turnOnTorchWithStrengthLevel(String cameraId, int torchStrength) throws |
| 2011 |
CameraAccessException { |
| 2012 |
synchronized(mLock) { |
| 2013 |
|
| 2014 |
if (cameraId == null) { |
| 2015 |
throw new IllegalArgumentException("cameraId was null"); |
| 2016 |
} |
| 2017 |
|
| 2018 |
ICameraService cameraService = getCameraService(); |
| 2019 |
if (cameraService == null) { |
| 2020 |
throw new CameraAccessException(CameraAccessException.CAMERA_DISCONNECTED, |
| 2021 |
"Camera service is currently unavailable."); |
| 2022 |
} |
| 2023 |
|
| 2024 |
try { |
| 2025 |
cameraService.turnOnTorchWithStrengthLevel(cameraId, torchStrength, |
| 2026 |
mTorchClientBinder); |
| 2027 |
} catch(ServiceSpecificException e) { |
| 2028 |
throwAsPublicException(e); |
| 2029 |
} catch (RemoteException e) { |
| 2030 |
throw new CameraAccessException(CameraAccessException.CAMERA_DISCONNECTED, |
| 2031 |
"Camera service is currently unavailable."); |
| 2032 |
} |
| 2033 |
} |
| 2034 |
} |
| 2035 |
|
| 2036 |
public int getTorchStrengthLevel(String cameraId) throws CameraAccessException { |
| 2037 |
int torchStrength = 0; |
| 2038 |
synchronized(mLock) { |
| 2039 |
if (cameraId == null) { |
| 2040 |
throw new IllegalArgumentException("cameraId was null"); |
| 2041 |
} |
| 2042 |
|
| 2043 |
ICameraService cameraService = getCameraService(); |
| 2044 |
if (cameraService == null) { |
| 2045 |
throw new CameraAccessException(CameraAccessException.CAMERA_DISCONNECTED, |
| 2046 |
"Camera service is currently unavailable."); |
| 2047 |
} |
| 2048 |
|
| 2049 |
try { |
| 2050 |
torchStrength = cameraService.getTorchStrengthLevel(cameraId); |
| 2051 |
} catch(ServiceSpecificException e) { |
| 2052 |
throwAsPublicException(e); |
| 2053 |
} catch (RemoteException e) { |
| 2054 |
throw new CameraAccessException(CameraAccessException.CAMERA_DISCONNECTED, |
| 2055 |
"Camera service is currently unavailable."); |
| 2056 |
} |
| 2057 |
} |
| 2058 |
return torchStrength; |
| 2059 |
} |
| 2060 |
|
| 2061 |
private void handleRecoverableSetupErrors(ServiceSpecificException e) { |
| 2062 |
switch (e.errorCode) { |
| 2063 |
case ICameraService.ERROR_DISCONNECTED: |
| 2064 |
Log.w(TAG, e.getMessage()); |
| 2065 |
break; |
| 2066 |
default: |
| 2067 |
throw new IllegalStateException(e); |
| 2068 |
} |
| 2069 |
} |
| 2070 |
|
| 2071 |
private boolean isAvailable(int status) { |
| 2072 |
switch (status) { |
| 2073 |
case ICameraServiceListener.STATUS_PRESENT: |
| 2074 |
return true; |
| 2075 |
default: |
| 2076 |
return false; |
| 2077 |
} |
| 2078 |
} |
| 2079 |
|
| 2080 |
private boolean validStatus(int status) { |
| 2081 |
switch (status) { |
| 2082 |
case ICameraServiceListener.STATUS_NOT_PRESENT: |
| 2083 |
case ICameraServiceListener.STATUS_PRESENT: |
| 2084 |
case ICameraServiceListener.STATUS_ENUMERATING: |
| 2085 |
case ICameraServiceListener.STATUS_NOT_AVAILABLE: |
| 2086 |
return true; |
| 2087 |
default: |
| 2088 |
return false; |
| 2089 |
} |
| 2090 |
} |
| 2091 |
|
| 2092 |
private boolean validTorchStatus(int status) { |
| 2093 |
switch (status) { |
| 2094 |
case ICameraServiceListener.TORCH_STATUS_NOT_AVAILABLE: |
| 2095 |
case ICameraServiceListener.TORCH_STATUS_AVAILABLE_ON: |
| 2096 |
case ICameraServiceListener.TORCH_STATUS_AVAILABLE_OFF: |
| 2097 |
return true; |
| 2098 |
default: |
| 2099 |
return false; |
| 2100 |
} |
| 2101 |
} |
| 2102 |
|
| 2103 |
private void postSingleAccessPriorityChangeUpdate(final AvailabilityCallback callback, |
| 2104 |
final Executor executor) { |
| 2105 |
final long ident = Binder.clearCallingIdentity(); |
| 2106 |
try { |
| 2107 |
executor.execute( |
| 2108 |
new Runnable() { |
| 2109 |
@Override |
| 2110 |
public void run() { |
| 2111 |
callback.onCameraAccessPrioritiesChanged(); |
| 2112 |
} |
| 2113 |
}); |
| 2114 |
} finally { |
| 2115 |
Binder.restoreCallingIdentity(ident); |
| 2116 |
} |
| 2117 |
} |
| 2118 |
|
| 2119 |
private void postSingleCameraOpenedUpdate(final AvailabilityCallback callback, |
| 2120 |
final Executor executor, final String id, final String packageId) { |
| 2121 |
final long ident = Binder.clearCallingIdentity(); |
| 2122 |
try { |
| 2123 |
executor.execute( |
| 2124 |
new Runnable() { |
| 2125 |
@Override |
| 2126 |
public void run() { |
| 2127 |
callback.onCameraOpened(id, packageId); |
| 2128 |
} |
| 2129 |
}); |
| 2130 |
} finally { |
| 2131 |
Binder.restoreCallingIdentity(ident); |
| 2132 |
} |
| 2133 |
} |
| 2134 |
|
| 2135 |
private void postSingleCameraClosedUpdate(final AvailabilityCallback callback, |
| 2136 |
final Executor executor, final String id) { |
| 2137 |
final long ident = Binder.clearCallingIdentity(); |
| 2138 |
try { |
| 2139 |
executor.execute( |
| 2140 |
new Runnable() { |
| 2141 |
@Override |
| 2142 |
public void run() { |
| 2143 |
callback.onCameraClosed(id); |
| 2144 |
} |
| 2145 |
}); |
| 2146 |
} finally { |
| 2147 |
Binder.restoreCallingIdentity(ident); |
| 2148 |
} |
| 2149 |
} |
| 2150 |
|
| 2151 |
private void postSingleUpdate(final AvailabilityCallback callback, final Executor executor, |
| 2152 |
final String id, final String physicalId, final int status) { |
| 2153 |
if (isAvailable(status)) { |
| 2154 |
final long ident = Binder.clearCallingIdentity(); |
| 2155 |
try { |
| 2156 |
executor.execute( |
| 2157 |
new Runnable() { |
| 2158 |
@Override |
| 2159 |
public void run() { |
| 2160 |
if (physicalId == null) { |
| 2161 |
callback.onCameraAvailable(id); |
| 2162 |
} else { |
| 2163 |
callback.onPhysicalCameraAvailable(id, physicalId); |
| 2164 |
} |
| 2165 |
} |
| 2166 |
}); |
| 2167 |
} finally { |
| 2168 |
Binder.restoreCallingIdentity(ident); |
| 2169 |
} |
| 2170 |
} else { |
| 2171 |
final long ident = Binder.clearCallingIdentity(); |
| 2172 |
try { |
| 2173 |
executor.execute( |
| 2174 |
new Runnable() { |
| 2175 |
@Override |
| 2176 |
public void run() { |
| 2177 |
if (physicalId == null) { |
| 2178 |
callback.onCameraUnavailable(id); |
| 2179 |
} else { |
| 2180 |
callback.onPhysicalCameraUnavailable(id, physicalId); |
| 2181 |
} |
| 2182 |
} |
| 2183 |
}); |
| 2184 |
} finally { |
| 2185 |
Binder.restoreCallingIdentity(ident); |
| 2186 |
} |
| 2187 |
} |
| 2188 |
} |
| 2189 |
|
| 2190 |
private void postSingleTorchUpdate(final TorchCallback callback, final Executor executor, |
| 2191 |
final String id, final int status) { |
| 2192 |
switch(status) { |
| 2193 |
case ICameraServiceListener.TORCH_STATUS_AVAILABLE_ON: |
| 2194 |
case ICameraServiceListener.TORCH_STATUS_AVAILABLE_OFF: { |
| 2195 |
final long ident = Binder.clearCallingIdentity(); |
| 2196 |
try { |
| 2197 |
executor.execute(() -> { |
| 2198 |
callback.onTorchModeChanged(id, status == |
| 2199 |
ICameraServiceListener.TORCH_STATUS_AVAILABLE_ON); |
| 2200 |
}); |
| 2201 |
} finally { |
| 2202 |
Binder.restoreCallingIdentity(ident); |
| 2203 |
} |
| 2204 |
} |
| 2205 |
break; |
| 2206 |
default: { |
| 2207 |
final long ident = Binder.clearCallingIdentity(); |
| 2208 |
try { |
| 2209 |
executor.execute(() -> { |
| 2210 |
callback.onTorchModeUnavailable(id); |
| 2211 |
}); |
| 2212 |
} finally { |
| 2213 |
Binder.restoreCallingIdentity(ident); |
| 2214 |
} |
| 2215 |
} |
| 2216 |
break; |
| 2217 |
} |
| 2218 |
} |
| 2219 |
|
| 2220 |
private void postSingleTorchStrengthLevelUpdate(final TorchCallback callback, |
| 2221 |
final Executor executor, final String id, final int newStrengthLevel) { |
| 2222 |
final long ident = Binder.clearCallingIdentity(); |
| 2223 |
try { |
| 2224 |
executor.execute(() -> { |
| 2225 |
callback.onTorchStrengthLevelChanged(id, newStrengthLevel); |
| 2226 |
}); |
| 2227 |
} finally { |
| 2228 |
Binder.restoreCallingIdentity(ident); |
| 2229 |
} |
| 2230 |
} |
| 2231 |
|
| 2232 |
|
| 2233 |
|
| 2234 |
|
| 2235 |
|
| 2236 |
private void updateCallbackLocked(AvailabilityCallback callback, Executor executor) { |
| 2237 |
for (int i = 0; i < mDeviceStatus.size(); i++) { |
| 2238 |
String id = mDeviceStatus.keyAt(i); |
| 2239 |
Integer status = mDeviceStatus.valueAt(i); |
| 2240 |
postSingleUpdate(callback, executor, id, null , status); |
| 2241 |
|
| 2242 |
|
| 2243 |
if (isAvailable(status) && mUnavailablePhysicalDevices.containsKey(id)) { |
| 2244 |
ArrayList<String> unavailableIds = mUnavailablePhysicalDevices.get(id); |
| 2245 |
for (String unavailableId : unavailableIds) { |
| 2246 |
postSingleUpdate(callback, executor, id, unavailableId, |
| 2247 |
ICameraServiceListener.STATUS_NOT_PRESENT); |
| 2248 |
} |
| 2249 |
} |
| 2250 |
|
| 2251 |
} |
| 2252 |
for (int i = 0; i < mOpenedDevices.size(); i++) { |
| 2253 |
String id = mOpenedDevices.keyAt(i); |
| 2254 |
String clientPackageId = mOpenedDevices.valueAt(i); |
| 2255 |
postSingleCameraOpenedUpdate(callback, executor, id, clientPackageId); |
| 2256 |
} |
| 2257 |
} |
| 2258 |
|
| 2259 |
private void onStatusChangedLocked(int status, String id) { |
| 2260 |
if (DEBUG) { |
| 2261 |
Log.v(TAG, |
| 2262 |
String.format("Camera id %s has status changed to 0x%x", id, status)); |
| 2263 |
} |
| 2264 |
|
| 2265 |
if (!validStatus(status)) { |
| 2266 |
Log.e(TAG, String.format("Ignoring invalid device %s status 0x%x", id, |
| 2267 |
status)); |
| 2268 |
return; |
| 2269 |
} |
| 2270 |
|
| 2271 |
Integer oldStatus; |
| 2272 |
if (status == ICameraServiceListener.STATUS_NOT_PRESENT) { |
| 2273 |
oldStatus = mDeviceStatus.remove(id); |
| 2274 |
mUnavailablePhysicalDevices.remove(id); |
| 2275 |
} else { |
| 2276 |
oldStatus = mDeviceStatus.put(id, status); |
| 2277 |
if (oldStatus == null) { |
| 2278 |
mUnavailablePhysicalDevices.put(id, new ArrayList<String>()); |
| 2279 |
} |
| 2280 |
} |
| 2281 |
|
| 2282 |
if (oldStatus != null && oldStatus == status) { |
| 2283 |
if (DEBUG) { |
| 2284 |
Log.v(TAG, String.format( |
| 2285 |
"Device status changed to 0x%x, which is what it already was", |
| 2286 |
status)); |
| 2287 |
} |
| 2288 |
return; |
| 2289 |
} |
| 2290 |
|
| 2291 |
|
| 2292 |
|
| 2293 |
|
| 2294 |
|
| 2295 |
|
| 2296 |
|
| 2297 |
|
| 2298 |
|
| 2299 |
|
| 2300 |
|
| 2301 |
|
| 2302 |
|
| 2303 |
|
| 2304 |
if (oldStatus != null && isAvailable(status) == isAvailable(oldStatus)) { |
| 2305 |
if (DEBUG) { |
| 2306 |
Log.v(TAG, |
| 2307 |
String.format( |
| 2308 |
"Device status was previously available (%b), " + |
| 2309 |
" and is now again available (%b)" + |
| 2310 |
"so no new client visible update will be sent", |
| 2311 |
isAvailable(oldStatus), isAvailable(status))); |
| 2312 |
} |
| 2313 |
return; |
| 2314 |
} |
| 2315 |
|
| 2316 |
final int callbackCount = mCallbackMap.size(); |
| 2317 |
for (int i = 0; i < callbackCount; i++) { |
| 2318 |
Executor executor = mCallbackMap.valueAt(i); |
| 2319 |
final AvailabilityCallback callback = mCallbackMap.keyAt(i); |
| 2320 |
|
| 2321 |
postSingleUpdate(callback, executor, id, null , status); |
| 2322 |
} |
| 2323 |
} |
| 2324 |
|
| 2325 |
private void onPhysicalCameraStatusChangedLocked(int status, |
| 2326 |
String id, String physicalId) { |
| 2327 |
if (DEBUG) { |
| 2328 |
Log.v(TAG, |
| 2329 |
String.format("Camera id %s physical camera id %s has status " |
| 2330 |
+ "changed to 0x%x", id, physicalId, status)); |
| 2331 |
} |
| 2332 |
|
| 2333 |
if (!validStatus(status)) { |
| 2334 |
Log.e(TAG, String.format( |
| 2335 |
"Ignoring invalid device %s physical device %s status 0x%x", id, |
| 2336 |
physicalId, status)); |
| 2337 |
return; |
| 2338 |
} |
| 2339 |
|
| 2340 |
|
| 2341 |
if (!mDeviceStatus.containsKey(id) || !isAvailable(mDeviceStatus.get(id)) |
| 2342 |
|| !mUnavailablePhysicalDevices.containsKey(id)) { |
| 2343 |
Log.e(TAG, String.format("Camera %s is not available. Ignore physical camera " |
| 2344 |
+ "status change", id)); |
| 2345 |
return; |
| 2346 |
} |
| 2347 |
|
| 2348 |
ArrayList<String> unavailablePhysicalDevices = mUnavailablePhysicalDevices.get(id); |
| 2349 |
if (!isAvailable(status) |
| 2350 |
&& !unavailablePhysicalDevices.contains(physicalId)) { |
| 2351 |
unavailablePhysicalDevices.add(physicalId); |
| 2352 |
} else if (isAvailable(status) |
| 2353 |
&& unavailablePhysicalDevices.contains(physicalId)) { |
| 2354 |
unavailablePhysicalDevices.remove(physicalId); |
| 2355 |
} else { |
| 2356 |
if (DEBUG) { |
| 2357 |
Log.v(TAG, |
| 2358 |
String.format( |
| 2359 |
"Physical camera device status was previously available (%b), " |
| 2360 |
+ " and is now again available (%b)" |
| 2361 |
+ "so no new client visible update will be sent", |
| 2362 |
!unavailablePhysicalDevices.contains(physicalId), |
| 2363 |
isAvailable(status))); |
| 2364 |
} |
| 2365 |
return; |
| 2366 |
} |
| 2367 |
|
| 2368 |
final int callbackCount = mCallbackMap.size(); |
| 2369 |
for (int i = 0; i < callbackCount; i++) { |
| 2370 |
Executor executor = mCallbackMap.valueAt(i); |
| 2371 |
final AvailabilityCallback callback = mCallbackMap.keyAt(i); |
| 2372 |
|
| 2373 |
postSingleUpdate(callback, executor, id, physicalId, status); |
| 2374 |
} |
| 2375 |
} |
| 2376 |
|
| 2377 |
private void updateTorchCallbackLocked(TorchCallback callback, Executor executor) { |
| 2378 |
for (int i = 0; i < mTorchStatus.size(); i++) { |
| 2379 |
String id = mTorchStatus.keyAt(i); |
| 2380 |
Integer status = mTorchStatus.valueAt(i); |
| 2381 |
postSingleTorchUpdate(callback, executor, id, status); |
| 2382 |
} |
| 2383 |
} |
| 2384 |
|
| 2385 |
private void onTorchStatusChangedLocked(int status, String id) { |
| 2386 |
if (DEBUG) { |
| 2387 |
Log.v(TAG, |
| 2388 |
String.format("Camera id %s has torch status changed to 0x%x", id, status)); |
| 2389 |
} |
| 2390 |
|
| 2391 |
if (!validTorchStatus(status)) { |
| 2392 |
Log.e(TAG, String.format("Ignoring invalid device %s torch status 0x%x", id, |
| 2393 |
status)); |
| 2394 |
return; |
| 2395 |
} |
| 2396 |
|
| 2397 |
Integer oldStatus = mTorchStatus.put(id, status); |
| 2398 |
if (oldStatus != null && oldStatus == status) { |
| 2399 |
if (DEBUG) { |
| 2400 |
Log.v(TAG, String.format( |
| 2401 |
"Torch status changed to 0x%x, which is what it already was", |
| 2402 |
status)); |
| 2403 |
} |
| 2404 |
return; |
| 2405 |
} |
| 2406 |
|
| 2407 |
final int callbackCount = mTorchCallbackMap.size(); |
| 2408 |
for (int i = 0; i < callbackCount; i++) { |
| 2409 |
final Executor executor = mTorchCallbackMap.valueAt(i); |
| 2410 |
final TorchCallback callback = mTorchCallbackMap.keyAt(i); |
| 2411 |
postSingleTorchUpdate(callback, executor, id, status); |
| 2412 |
} |
| 2413 |
} |
| 2414 |
|
| 2415 |
private void onTorchStrengthLevelChangedLocked(String cameraId, int newStrengthLevel) { |
| 2416 |
if (DEBUG) { |
| 2417 |
|
| 2418 |
Log.v(TAG, |
| 2419 |
String.format("Camera id %s has torch strength level changed to %d", |
| 2420 |
cameraId, newStrengthLevel)); |
| 2421 |
} |
| 2422 |
|
| 2423 |
final int callbackCount = mTorchCallbackMap.size(); |
| 2424 |
for (int i = 0; i < callbackCount; i++) { |
| 2425 |
final Executor executor = mTorchCallbackMap.valueAt(i); |
| 2426 |
final TorchCallback callback = mTorchCallbackMap.keyAt(i); |
| 2427 |
postSingleTorchStrengthLevelUpdate(callback, executor, cameraId, newStrengthLevel); |
| 2428 |
} |
| 2429 |
} |
| 2430 |
|
| 2431 |
|
| 2432 |
|
| 2433 |
|
| 2434 |
|
| 2435 |
|
| 2436 |
|
| 2437 |
|
| 2438 |
|
| 2439 |
|
| 2440 |
public void registerAvailabilityCallback(AvailabilityCallback callback, Executor executor, |
| 2441 |
boolean hasOpenCloseListenerPermission) { |
| 2442 |
synchronized (mLock) { |
| 2443 |
|
| 2444 |
|
| 2445 |
mHasOpenCloseListenerPermission = hasOpenCloseListenerPermission; |
| 2446 |
connectCameraServiceLocked(); |
| 2447 |
|
| 2448 |
Executor oldExecutor = mCallbackMap.put(callback, executor); |
| 2449 |
|
| 2450 |
if (oldExecutor == null) { |
| 2451 |
updateCallbackLocked(callback, executor); |
| 2452 |
} |
| 2453 |
|
| 2454 |
|
| 2455 |
if (mCameraService == null) { |
| 2456 |
scheduleCameraServiceReconnectionLocked(); |
| 2457 |
} |
| 2458 |
} |
| 2459 |
} |
| 2460 |
|
| 2461 |
|
| 2462 |
|
| 2463 |
|
| 2464 |
|
| 2465 |
|
| 2466 |
|
| 2467 |
public void unregisterAvailabilityCallback(AvailabilityCallback callback) { |
| 2468 |
synchronized (mLock) { |
| 2469 |
mCallbackMap.remove(callback); |
| 2470 |
} |
| 2471 |
} |
| 2472 |
|
| 2473 |
public void registerTorchCallback(TorchCallback callback, Executor executor) { |
| 2474 |
synchronized(mLock) { |
| 2475 |
connectCameraServiceLocked(); |
| 2476 |
|
| 2477 |
Executor oldExecutor = mTorchCallbackMap.put(callback, executor); |
| 2478 |
|
| 2479 |
if (oldExecutor == null) { |
| 2480 |
updateTorchCallbackLocked(callback, executor); |
| 2481 |
} |
| 2482 |
|
| 2483 |
|
| 2484 |
if (mCameraService == null) { |
| 2485 |
scheduleCameraServiceReconnectionLocked(); |
| 2486 |
} |
| 2487 |
} |
| 2488 |
} |
| 2489 |
|
| 2490 |
public void unregisterTorchCallback(TorchCallback callback) { |
| 2491 |
synchronized(mLock) { |
| 2492 |
mTorchCallbackMap.remove(callback); |
| 2493 |
} |
| 2494 |
} |
| 2495 |
|
| 2496 |
|
| 2497 |
|
| 2498 |
|
| 2499 |
@Override |
| 2500 |
public void onStatusChanged(int status, String cameraId) throws RemoteException { |
| 2501 |
synchronized(mLock) { |
| 2502 |
onStatusChangedLocked(status, cameraId); |
| 2503 |
} |
| 2504 |
} |
| 2505 |
|
| 2506 |
@Override |
| 2507 |
public void onPhysicalCameraStatusChanged(int status, String cameraId, |
| 2508 |
String physicalCameraId) throws RemoteException { |
| 2509 |
synchronized (mLock) { |
| 2510 |
onPhysicalCameraStatusChangedLocked(status, cameraId, physicalCameraId); |
| 2511 |
} |
| 2512 |
} |
| 2513 |
|
| 2514 |
@Override |
| 2515 |
public void onTorchStatusChanged(int status, String cameraId) throws RemoteException { |
| 2516 |
synchronized (mLock) { |
| 2517 |
onTorchStatusChangedLocked(status, cameraId); |
| 2518 |
} |
| 2519 |
} |
| 2520 |
|
| 2521 |
@Override |
| 2522 |
public void onTorchStrengthLevelChanged(String cameraId, int newStrengthLevel) |
| 2523 |
throws RemoteException { |
| 2524 |
synchronized (mLock) { |
| 2525 |
onTorchStrengthLevelChangedLocked(cameraId, newStrengthLevel); |
| 2526 |
} |
| 2527 |
} |
| 2528 |
|
| 2529 |
@Override |
| 2530 |
public void onCameraAccessPrioritiesChanged() { |
| 2531 |
synchronized (mLock) { |
| 2532 |
final int callbackCount = mCallbackMap.size(); |
| 2533 |
for (int i = 0; i < callbackCount; i++) { |
| 2534 |
Executor executor = mCallbackMap.valueAt(i); |
| 2535 |
final AvailabilityCallback callback = mCallbackMap.keyAt(i); |
| 2536 |
|
| 2537 |
postSingleAccessPriorityChangeUpdate(callback, executor); |
| 2538 |
} |
| 2539 |
} |
| 2540 |
} |
| 2541 |
|
| 2542 |
@Override |
| 2543 |
public void onCameraOpened(String cameraId, String clientPackageId) { |
| 2544 |
synchronized (mLock) { |
| 2545 |
onCameraOpenedLocked(cameraId, clientPackageId); |
| 2546 |
} |
| 2547 |
} |
| 2548 |
|
| 2549 |
private void onCameraOpenedLocked(String cameraId, String clientPackageId) { |
| 2550 |
String oldApk = mOpenedDevices.put(cameraId, clientPackageId); |
| 2551 |
|
| 2552 |
if (oldApk != null) { |
| 2553 |
if (oldApk.equals(clientPackageId)) { |
| 2554 |
Log.w(TAG, |
| 2555 |
"onCameraOpened was previously called for " + oldApk |
| 2556 |
+ " and is now again called for the same package name, " |
| 2557 |
+ "so no new client visible update will be sent"); |
| 2558 |
return; |
| 2559 |
} else { |
| 2560 |
Log.w(TAG, |
| 2561 |
"onCameraOpened was previously called for " + oldApk |
| 2562 |
+ " and is now called for " + clientPackageId |
| 2563 |
+ " without onCameraClosed being called first"); |
| 2564 |
} |
| 2565 |
} |
| 2566 |
|
| 2567 |
final int callbackCount = mCallbackMap.size(); |
| 2568 |
for (int i = 0; i < callbackCount; i++) { |
| 2569 |
Executor executor = mCallbackMap.valueAt(i); |
| 2570 |
final AvailabilityCallback callback = mCallbackMap.keyAt(i); |
| 2571 |
|
| 2572 |
postSingleCameraOpenedUpdate(callback, executor, cameraId, clientPackageId); |
| 2573 |
} |
| 2574 |
} |
| 2575 |
|
| 2576 |
@Override |
| 2577 |
public void onCameraClosed(String cameraId) { |
| 2578 |
synchronized (mLock) { |
| 2579 |
onCameraClosedLocked(cameraId); |
| 2580 |
} |
| 2581 |
} |
| 2582 |
|
| 2583 |
private void onCameraClosedLocked(String cameraId) { |
| 2584 |
mOpenedDevices.remove(cameraId); |
| 2585 |
|
| 2586 |
final int callbackCount = mCallbackMap.size(); |
| 2587 |
for (int i = 0; i < callbackCount; i++) { |
| 2588 |
Executor executor = mCallbackMap.valueAt(i); |
| 2589 |
final AvailabilityCallback callback = mCallbackMap.keyAt(i); |
| 2590 |
|
| 2591 |
postSingleCameraClosedUpdate(callback, executor, cameraId); |
| 2592 |
} |
| 2593 |
} |
| 2594 |
|
| 2595 |
|
| 2596 |
|
| 2597 |
|
| 2598 |
|
| 2599 |
private void scheduleCameraServiceReconnectionLocked() { |
| 2600 |
if (mCallbackMap.isEmpty() && mTorchCallbackMap.isEmpty()) { |
| 2601 |
|
| 2602 |
return; |
| 2603 |
} |
| 2604 |
|
| 2605 |
if (DEBUG) { |
| 2606 |
Log.v(TAG, "Reconnecting Camera Service in " + CAMERA_SERVICE_RECONNECT_DELAY_MS + |
| 2607 |
" ms"); |
| 2608 |
} |
| 2609 |
|
| 2610 |
try { |
| 2611 |
mScheduler.schedule(() -> { |
| 2612 |
ICameraService cameraService = getCameraService(); |
| 2613 |
if (cameraService == null) { |
| 2614 |
synchronized(mLock) { |
| 2615 |
if (DEBUG) { |
| 2616 |
Log.v(TAG, "Reconnecting Camera Service failed."); |
| 2617 |
} |
| 2618 |
scheduleCameraServiceReconnectionLocked(); |
| 2619 |
} |
| 2620 |
} |
| 2621 |
}, CAMERA_SERVICE_RECONNECT_DELAY_MS, TimeUnit.MILLISECONDS); |
| 2622 |
} catch (RejectedExecutionException e) { |
| 2623 |
Log.e(TAG, "Failed to schedule camera service re-connect: " + e); |
| 2624 |
} |
| 2625 |
} |
| 2626 |
|
| 2627 |
|
| 2628 |
|
| 2629 |
|
| 2630 |
|
| 2631 |
|
| 2632 |
|
| 2633 |
|
| 2634 |
public void binderDied() { |
| 2635 |
synchronized(mLock) { |
| 2636 |
|
| 2637 |
if (mCameraService == null) return; |
| 2638 |
|
| 2639 |
mCameraService = null; |
| 2640 |
|
| 2641 |
|
| 2642 |
|
| 2643 |
|
| 2644 |
|
| 2645 |
|
| 2646 |
for (int i = mDeviceStatus.size() - 1; i >= 0; i--) { |
| 2647 |
String cameraId = mDeviceStatus.keyAt(i); |
| 2648 |
onStatusChangedLocked(ICameraServiceListener.STATUS_NOT_PRESENT, cameraId); |
| 2649 |
|
| 2650 |
if (mHasOpenCloseListenerPermission) { |
| 2651 |
onCameraClosedLocked(cameraId); |
| 2652 |
} |
| 2653 |
} |
| 2654 |
for (int i = 0; i < mTorchStatus.size(); i++) { |
| 2655 |
String cameraId = mTorchStatus.keyAt(i); |
| 2656 |
onTorchStatusChangedLocked(ICameraServiceListener.TORCH_STATUS_NOT_AVAILABLE, |
| 2657 |
cameraId); |
| 2658 |
} |
| 2659 |
|
| 2660 |
mConcurrentCameraIdCombinations.clear(); |
| 2661 |
|
| 2662 |
scheduleCameraServiceReconnectionLocked(); |
| 2663 |
} |
| 2664 |
} |
| 2665 |
|
| 2666 |
} |
| 2667 |
|
| 2668 |
} |