Skip to content

Commit 0f69b46

Browse files
author
Chien-Yu Chen
committed
Camera: Reconnect after camera service crashes
Schedule a reconnection after camera service crashes if any client registered a camera availability callback or torch status callback. Bug: 407755 Change-Id: Iabe0bf713863898ca468cff59d4c97c66a802630
1 parent 1cb0de1 commit 0f69b46

1 file changed

Lines changed: 47 additions & 8 deletions

File tree

core/java/android/hardware/camera2/CameraManager.java

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,8 @@ private static final class CameraManagerGlobal extends ICameraServiceListener.St
723723
private static final String TAG = "CameraManagerGlobal";
724724
private final boolean DEBUG = false;
725725

726+
private final int CAMERA_SERVICE_RECONNECT_DELAY_MS = 1000;
727+
726728
// Singleton instance
727729
private static final CameraManagerGlobal gCameraManager =
728730
new CameraManagerGlobal();
@@ -1157,6 +1159,45 @@ public void onTorchStatusChanged(int status, String cameraId) throws RemoteExcep
11571159
}
11581160
}
11591161

1162+
/**
1163+
* Try to connect to camera service after some delay if any client registered camera
1164+
* availability callback or torch status callback.
1165+
*/
1166+
private void scheduleCameraServiceReconnectionLocked() {
1167+
final Handler handler;
1168+
1169+
if (mCallbackMap.size() > 0) {
1170+
handler = mCallbackMap.valueAt(0);
1171+
} else if (mTorchCallbackMap.size() > 0) {
1172+
handler = mTorchCallbackMap.valueAt(0);
1173+
} else {
1174+
// Not necessary to reconnect camera service if no client registers a callback.
1175+
return;
1176+
}
1177+
1178+
if (DEBUG) {
1179+
Log.v(TAG, "Reconnecting Camera Service in " + CAMERA_SERVICE_RECONNECT_DELAY_MS +
1180+
" ms");
1181+
}
1182+
1183+
handler.postDelayed(
1184+
new Runnable() {
1185+
@Override
1186+
public void run() {
1187+
ICameraService cameraService = getCameraService();
1188+
if (cameraService == null) {
1189+
synchronized(mLock) {
1190+
if (DEBUG) {
1191+
Log.v(TAG, "Reconnecting Camera Service failed.");
1192+
}
1193+
scheduleCameraServiceReconnectionLocked();
1194+
}
1195+
}
1196+
}
1197+
},
1198+
CAMERA_SERVICE_RECONNECT_DELAY_MS);
1199+
}
1200+
11601201
/**
11611202
* Listener for camera service death.
11621203
*
@@ -1171,21 +1212,19 @@ public void binderDied() {
11711212

11721213
mCameraService = null;
11731214

1174-
// Tell listeners that the cameras and torch modes are _available_, because any
1175-
// existing clients will have gotten disconnected. This is optimistic under the
1176-
// assumption that the service will be back shortly.
1177-
//
1178-
// Without this, a camera service crash while a camera is open will never signal
1179-
// to listeners that previously in-use cameras are now available.
1215+
// Tell listeners that the cameras and torch modes are unavailable and schedule a
1216+
// reconnection to camera service. When camera service is reconnected, the camera
1217+
// and torch statuses will be updated.
11801218
for (int i = 0; i < mDeviceStatus.size(); i++) {
11811219
String cameraId = mDeviceStatus.keyAt(i);
1182-
onStatusChangedLocked(STATUS_PRESENT, cameraId);
1220+
onStatusChangedLocked(STATUS_NOT_PRESENT, cameraId);
11831221
}
11841222
for (int i = 0; i < mTorchStatus.size(); i++) {
11851223
String cameraId = mTorchStatus.keyAt(i);
1186-
onTorchStatusChangedLocked(TORCH_STATUS_AVAILABLE_OFF, cameraId);
1224+
onTorchStatusChangedLocked(TORCH_STATUS_NOT_AVAILABLE, cameraId);
11871225
}
11881226

1227+
scheduleCameraServiceReconnectionLocked();
11891228
}
11901229
}
11911230

0 commit comments

Comments
 (0)