1 /*
2 * Copyright (C) 2025 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 #include <iostream>
16 #include <array>
17 #include "ani_class_name.h"
18 #include "media_log.h"
19 #include "moving_photo_ani.h"
20 #include "medialibrary_ani_utils.h"
21
22 using namespace OHOS::Media;
23
MovingPhotoInit(ani_env * env)24 ani_status MovingPhotoAni::MovingPhotoInit(ani_env *env)
25 {
26 static const char *className = ANI_CLASS_MOVING_PHOTO.c_str();
27 ani_class cls;
28 ani_status status = env->FindClass(className, &cls);
29 if (status != ANI_OK) {
30 MEDIA_ERR_LOG("Failed to find class: %{public}s", className);
31 return status;
32 }
33 std::array methods = {
34 ani_native_function {"requestContent1", nullptr, reinterpret_cast<void *>(MovingPhotoAni::RequestContent1)},
35 ani_native_function {"requestContent2", nullptr, reinterpret_cast<void *>(MovingPhotoAni::RequestContent2)},
36 ani_native_function {"requestContent3", nullptr, reinterpret_cast<void *>(MovingPhotoAni::RequestContent3)},
37 ani_native_function {"getUri", nullptr, reinterpret_cast<void *>(MovingPhotoAni::GetUri)},
38 };
39
40 status = env->Class_BindNativeMethods(cls, methods.data(), methods.size());
41 if (status != ANI_OK) {
42 MEDIA_ERR_LOG("Failed to bind native methods to: %{public}s", className);
43 return status;
44 }
45
46 ANI_INFO_LOG("MovingPhotoInit ok");
47 return ANI_OK;
48 }
49
Constructor(ani_env * env,ani_class clazz,ani_object context)50 ani_object MovingPhotoAni::Constructor([[maybe_unused]] ani_env *env, [[maybe_unused]] ani_class clazz,
51 [[maybe_unused]] ani_object context)
52 {
53 auto nativeMovingPhotoHandle = std::make_unique<MovingPhotoAni>();
54
55 static const char *className = ANI_CLASS_MOVING_PHOTO.c_str();
56 ani_class cls;
57 if (ANI_OK != env->FindClass(className, &cls)) {
58 MEDIA_ERR_LOG("Failed to find class: %{public}s", className);
59 return nullptr;
60 }
61
62 ani_method ctor;
63 if (ANI_OK != env->Class_FindMethod(cls, "<ctor>", "J:V", &ctor)) {
64 MEDIA_ERR_LOG("Failed to find method: %{public}s", "ctor");
65 return nullptr;
66 }
67
68 ani_object movingPhoto_object;
69 if (ANI_OK != env->Object_New(cls, ctor, &movingPhoto_object,
70 reinterpret_cast<ani_long>(nativeMovingPhotoHandle.get()))) {
71 MEDIA_ERR_LOG("New MovingPhoto Fail");
72 return nullptr;
73 }
74
75 nativeMovingPhotoHandle.release();
76 return movingPhoto_object;
77 }
78
Unwrap(ani_env * env,ani_object object)79 MovingPhotoAni* MovingPhotoAni::Unwrap(ani_env *env, ani_object object)
80 {
81 ani_long movingPhoto;
82 if (ANI_OK != env->Object_GetFieldByName_Long(object, "nativeMovingPhoto", &movingPhoto)) {
83 MEDIA_ERR_LOG("get nativeMovingPhoto Fail");
84 return nullptr;
85 }
86 return reinterpret_cast<MovingPhotoAni*>(movingPhoto);
87 }
88
GetUriInner()89 std::string MovingPhotoAni::GetUriInner()
90 {
91 return photoUri_;
92 }
93
RequestContent1(ani_env * env,ani_object object,ani_string imageFileUri,ani_string videoFileUri)94 void MovingPhotoAni::RequestContent1(ani_env *env, ani_object object, ani_string imageFileUri, ani_string videoFileUri)
95 {
96 auto movingPhotoAni = Unwrap(env, object);
97 if (movingPhotoAni == nullptr) {
98 ANI_ERR_LOG("movingPhotoAni is nullptr");
99 return;
100 }
101
102 std::string imageFileUriStr;
103 MediaLibraryAniUtils::GetString(env, imageFileUri, imageFileUriStr);
104 std::string videoFileUriStr;
105 MediaLibraryAniUtils::GetString(env, videoFileUri, videoFileUriStr);
106 return;
107 }
108
RequestContent2(ani_env * env,ani_object object,ani_enum_item resourceType,ani_string fileUri)109 void MovingPhotoAni::RequestContent2(ani_env *env, ani_object object, ani_enum_item resourceType, ani_string fileUri)
110 {
111 return;
112 }
113
RequestContent3(ani_env * env,ani_object object,ani_enum_item resourceType)114 void MovingPhotoAni::RequestContent3(ani_env *env, ani_object object, ani_enum_item resourceType)
115 {
116 return;
117 }
118
GetUri(ani_env * env,ani_object object)119 ani_string MovingPhotoAni::GetUri(ani_env *env, ani_object object)
120 {
121 ani_string result = nullptr;
122 auto movingPhotoAni = Unwrap(env, object);
123 if (movingPhotoAni == nullptr) {
124 ANI_ERR_LOG("movingPhotoAni is nullptr");
125 return result;
126 }
127
128 const std::string& uri = movingPhotoAni->GetUriInner();
129 const char *utf8String = uri.c_str();
130 const ani_size stringLength = uri.length();
131 env->String_NewUTF8(utf8String, stringLength, &result);
132 return result;
133 }