1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "frameworks/bridge/common/dom/dom_camera.h"
17
18 #include "frameworks/bridge/common/dom/dom_type.h"
19 #include "frameworks/bridge/common/utils/utils.h"
20
21 namespace OHOS::Ace::Framework {
22 namespace {
23
24 const char CALLBACK_SUCCESS[] = "success";
25 const char CALLBACK_FAIL[] = "fail";
26 const char CALLBACK_COMPLETE[] = "complete";
27 const char QUALITY[] = "quality";
28 const char DEVICE_POSITION_FRONT[] = "front";
29 const char FLUSH_ON[] = "on";
30 const char FLUSH_OFF[] = "off";
31 const char FLUSH_TORCH[] = "torch";
32 const char FLUSH_AUTO[] = "auto";
33 const char START_STR[] = "[\"";
34
35 } // namespace
36
DOMCamera(NodeId nodeId,const std::string & nodeName)37 DOMCamera::DOMCamera(NodeId nodeId, const std::string& nodeName) : DOMNode(nodeId, nodeName)
38 {
39 cameraComponent_ = AceType::MakeRefPtr<CameraComponent>();
40 #if defined(PREVIEW)
41 if (IsRightToLeft()) {
42 cameraComponent_->SetTextDirection(TextDirection::RTL);
43 }
44 #endif
45 }
46
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)47 bool DOMCamera::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
48 {
49 // Operator map for attr
50 static const std::unordered_map<std::string, void (*)(const RefPtr<CameraComponent>&, const std::string&)>
51 attrOperators = {
52 { DOM_CAMERA_FLASH,
53 [](const RefPtr<CameraComponent>& cameraComponent, const std::string& val) {
54 cameraComponent->SetFlash(GetFlashType(val));
55 } },
56 { DOM_CAMERA_DEVICE_POSITION,
57 [](const RefPtr<CameraComponent>& cameraComponent, const std::string& val) {
58 cameraComponent->SetDevicePosition(GetDevicePosition(val));
59 } },
60 };
61 auto operatorIter = attrOperators.find(attr.first);
62 if (operatorIter != attrOperators.end()) {
63 operatorIter->second(cameraComponent_, attr.second);
64 return true;
65 }
66 return false;
67 }
68
AddSpecializedEvent(int32_t pageId,const std::string & event)69 bool DOMCamera::AddSpecializedEvent(int32_t pageId, const std::string& event)
70 {
71 // Operator map for event
72 static const std::unordered_map<std::string, void (*)(const RefPtr<CameraComponent>&, const EventMarker&)>
73 eventOperators = {
74 { DOM_ERROR,
75 [](const RefPtr<CameraComponent>& cameraComponent, const EventMarker& event) {
76 cameraComponent->SetErrorEventId(event);
77 } },
78 };
79 auto operatorIter = eventOperators.find(event);
80 if (operatorIter != eventOperators.end()) {
81 operatorIter->second(cameraComponent_, EventMarker(GetNodeIdForEvent(), event, pageId));
82 return true;
83 }
84 return false;
85 }
86
CallSpecializedMethod(const std::string & method,const std::string & args)87 void DOMCamera::CallSpecializedMethod(const std::string& method, const std::string& args)
88 {
89 LOGI("DOMCamera: method: %{public}s args: %{public}s", method.c_str(), args.c_str());
90 if (method == DOM_TAKE_PHOTO) {
91 auto controller = cameraComponent_->GetCameraController();
92 if (controller) {
93 controller->TakePhoto(GetParamFromJson(args));
94 }
95 }
96
97 if (method == DOM_CAMERA_START_RECORD) {
98 auto controller = cameraComponent_->GetCameraController();
99 if (controller) {
100 controller->StartRecord();
101 }
102 }
103
104 if (method == DOM_CAMERA_CLOSE_RECORDER) {
105 auto controller = cameraComponent_->GetCameraController();
106 if (controller) {
107 controller->CloseRecorder(GetRecorderParam(args));
108 }
109 }
110 }
111
GetParamFromJson(const std::string & args) const112 TakePhotoParams DOMCamera::GetParamFromJson(const std::string& args) const
113 {
114 std::unique_ptr<JsonValue> argsValue = JsonUtil::ParseJsonString(args);
115
116 TakePhotoParams takePhotoParams;
117 if (!argsValue || !argsValue->IsArray() || argsValue->GetArraySize() == 0) {
118 return takePhotoParams;
119 }
120 std::unique_ptr<JsonValue> firstItem = argsValue->GetArrayItem(0);
121 std::string quality = firstItem->GetString(QUALITY);
122 if (!quality.empty()) {
123 takePhotoParams.quality = quality;
124 }
125
126 std::string successId = firstItem->GetString(CALLBACK_SUCCESS);
127 if (!successId.empty()) {
128 takePhotoParams.success = successId;
129 }
130
131 std::string failId = firstItem->GetString(CALLBACK_FAIL);
132 if (!failId.empty()) {
133 takePhotoParams.fail = failId;
134 }
135
136 std::string completeId = firstItem->GetString(CALLBACK_COMPLETE);
137 if (!completeId.empty()) {
138 takePhotoParams.complete = completeId;
139 }
140
141 return takePhotoParams;
142 }
143
GetRecorderParam(const std::string & args) const144 std::string DOMCamera::GetRecorderParam(const std::string& args) const
145 {
146 size_t len = args.length();
147 size_t pos = args.find(START_STR);
148 size_t strLen = sizeof(START_STR) - 1;
149 int32_t result = 0;
150
151 if (pos == std::string::npos || (pos + strLen) >= len) {
152 return std::string();
153 }
154 std::stringstream ss;
155 ss << args.substr(pos + strLen);
156 ss >> result;
157
158 return std::to_string(result);
159 }
160
GetFlashType(const std::string & val)161 FlashType DOMCamera::GetFlashType(const std::string& val)
162 {
163 static const std::unordered_map<std::string, FlashType> flashTypes = {
164 { FLUSH_ON, FlashType::ON },
165 { FLUSH_OFF, FlashType::OFF },
166 { FLUSH_TORCH, FlashType::TORCH },
167 { FLUSH_AUTO, FlashType::AUTO },
168 };
169 auto operatorIter = flashTypes.find(val);
170 if (operatorIter != flashTypes.end()) {
171 return operatorIter->second;
172 }
173 return FlashType::AUTO;
174 }
175
GetDevicePosition(const std::string & val)176 DevicePosition DOMCamera::GetDevicePosition(const std::string& val)
177 {
178 if (val == DEVICE_POSITION_FRONT) {
179 return DevicePosition::CAMERA_FACING_FRONT;
180 } else {
181 return DevicePosition::CAMERA_FACING_BACK;
182 }
183 }
184
185 } // namespace OHOS::Ace::Framework