Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | #define LOG_TAG "CameraOfflineClient" |
| 18 | #define ATRACE_TAG ATRACE_TAG_CAMERA |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | |
| 21 | #include "CameraOfflineSessionClient.h" |
| 22 | #include <utils/Trace.h> |
| 23 | |
| 24 | namespace android { |
| 25 | |
| 26 | using binder::Status; |
| 27 | |
| 28 | status_t CameraOfflineSessionClient::initialize(sp<CameraProviderManager>, const String8&) { |
| 29 | return OK; |
| 30 | } |
| 31 | |
| 32 | status_t CameraOfflineSessionClient::dump(int /*fd*/, const Vector<String16>& /*args*/) { |
| 33 | return OK; |
| 34 | } |
| 35 | |
| 36 | status_t CameraOfflineSessionClient::dumpClient(int /*fd*/, const Vector<String16>& /*args*/) { |
| 37 | return OK; |
| 38 | } |
| 39 | |
| 40 | binder::Status CameraOfflineSessionClient::disconnect() { |
| 41 | binder::Status res = Status::ok(); |
| 42 | if (mDisconnected) { |
| 43 | return res; |
| 44 | } |
| 45 | mDisconnected = true; |
| 46 | |
| 47 | sCameraService->removeByClient(this); |
| 48 | sCameraService->logDisconnectedOffline(mCameraIdStr, mClientPid, String8(mClientPackageName)); |
| 49 | |
| 50 | sp<IBinder> remote = getRemote(); |
| 51 | if (remote != nullptr) { |
| 52 | remote->unlinkToDeath(sCameraService); |
| 53 | } |
| 54 | |
| 55 | finishCameraOps(); |
| 56 | ALOGI("%s: Disconnected client for offline camera %s for PID %d", __FUNCTION__, |
| 57 | mCameraIdStr.string(), mClientPid); |
| 58 | |
| 59 | // client shouldn't be able to call into us anymore |
| 60 | mClientPid = 0; |
| 61 | |
| 62 | return res; |
| 63 | } |
| 64 | |
| 65 | void CameraOfflineSessionClient::notifyError(int32_t errorCode, |
| 66 | const CaptureResultExtras& resultExtras) { |
| 67 | // Thread safe. Don't bother locking. |
| 68 | sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback(); |
| 69 | // TODO: handle composite streams |
| 70 | if ((remoteCb != 0)) { |
| 71 | remoteCb->onDeviceError(errorCode, resultExtras); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | status_t CameraOfflineSessionClient::startCameraOps() { |
| 76 | ATRACE_CALL(); |
| 77 | { |
| 78 | ALOGV("%s: Start camera ops, package name = %s, client UID = %d", |
| 79 | __FUNCTION__, String8(mClientPackageName).string(), mClientUid); |
| 80 | } |
| 81 | |
| 82 | if (mAppOpsManager != nullptr) { |
| 83 | // Notify app ops that the camera is not available |
| 84 | mOpsCallback = new OpsCallback(this); |
| 85 | int32_t res; |
| 86 | // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION |
| 87 | mAppOpsManager->startWatchingMode(AppOpsManager::OP_CAMERA, |
| 88 | mClientPackageName, mOpsCallback); |
| 89 | // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION |
| 90 | res = mAppOpsManager->startOpNoThrow(AppOpsManager::OP_CAMERA, |
| 91 | mClientUid, mClientPackageName, /*startIfModeDefault*/ false); |
| 92 | |
| 93 | if (res == AppOpsManager::MODE_ERRORED) { |
| 94 | ALOGI("Offline Camera %s: Access for \"%s\" has been revoked", |
| 95 | mCameraIdStr.string(), String8(mClientPackageName).string()); |
| 96 | return PERMISSION_DENIED; |
| 97 | } |
| 98 | |
| 99 | if (res == AppOpsManager::MODE_IGNORED) { |
| 100 | ALOGI("Offline Camera %s: Access for \"%s\" has been restricted", |
| 101 | mCameraIdStr.string(), String8(mClientPackageName).string()); |
| 102 | // Return the same error as for device policy manager rejection |
| 103 | return -EACCES; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | mOpsActive = true; |
| 108 | |
| 109 | // Transition device state to OPEN |
| 110 | sCameraService->mUidPolicy->registerMonitorUid(mClientUid); |
| 111 | |
| 112 | return OK; |
| 113 | } |
| 114 | |
| 115 | status_t CameraOfflineSessionClient::finishCameraOps() { |
| 116 | ATRACE_CALL(); |
| 117 | |
| 118 | // Check if startCameraOps succeeded, and if so, finish the camera op |
| 119 | if (mOpsActive) { |
| 120 | // Notify app ops that the camera is available again |
| 121 | if (mAppOpsManager != nullptr) { |
| 122 | // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION |
| 123 | mAppOpsManager->finishOp(AppOpsManager::OP_CAMERA, mClientUid, |
| 124 | mClientPackageName); |
| 125 | mOpsActive = false; |
| 126 | } |
| 127 | } |
| 128 | // Always stop watching, even if no camera op is active |
| 129 | if (mOpsCallback != nullptr && mAppOpsManager != nullptr) { |
| 130 | mAppOpsManager->stopWatchingMode(mOpsCallback); |
| 131 | } |
| 132 | mOpsCallback.clear(); |
| 133 | |
| 134 | sCameraService->mUidPolicy->unregisterMonitorUid(mClientUid); |
| 135 | |
| 136 | return OK; |
| 137 | } |
| 138 | |
| 139 | // ---------------------------------------------------------------------------- |
| 140 | }; // namespace android |