10e98b08fSopenharmony_ci/*
20e98b08fSopenharmony_ci * Copyright (c) 2020 Huawei Device Co., Ltd.
30e98b08fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
40e98b08fSopenharmony_ci * you may not use this file except in compliance with the License.
50e98b08fSopenharmony_ci * You may obtain a copy of the License at
60e98b08fSopenharmony_ci *
70e98b08fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
80e98b08fSopenharmony_ci *
90e98b08fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
100e98b08fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
110e98b08fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
120e98b08fSopenharmony_ci * See the License for the specific language governing permissions and
130e98b08fSopenharmony_ci * limitations under the License.
140e98b08fSopenharmony_ci */
150e98b08fSopenharmony_ci
160e98b08fSopenharmony_ci#include "nativeapi_deviceinfo.h"
170e98b08fSopenharmony_ci#include <string>
180e98b08fSopenharmony_ci#include <new>
190e98b08fSopenharmony_ci#include "global.h"
200e98b08fSopenharmony_ci#include "js_async_work.h"
210e98b08fSopenharmony_ci#include "nativeapi_common.h"
220e98b08fSopenharmony_ci#include "nativeapi_config.h"
230e98b08fSopenharmony_ci#include "parameter.h"
240e98b08fSopenharmony_ci#include "common/screen.h"
250e98b08fSopenharmony_ci
260e98b08fSopenharmony_cinamespace OHOS {
270e98b08fSopenharmony_cinamespace ACELite {
280e98b08fSopenharmony_cinamespace {
290e98b08fSopenharmony_ciJSIValue ExecuteAsyncWork(const JSIValue thisVal, const JSIValue* args,
300e98b08fSopenharmony_ci    uint8_t argsNum, AsyncWorkHandler ExecuteFunc, bool flag = false)
310e98b08fSopenharmony_ci{
320e98b08fSopenharmony_ci    JSIValue undefValue = JSI::CreateUndefined();
330e98b08fSopenharmony_ci    if (!NativeapiCommon::IsValidJSIValue(args, argsNum)) {
340e98b08fSopenharmony_ci        return undefValue;
350e98b08fSopenharmony_ci    }
360e98b08fSopenharmony_ci    FuncParams* params = new(std::nothrow) FuncParams();
370e98b08fSopenharmony_ci    if (params == nullptr) {
380e98b08fSopenharmony_ci        return undefValue;
390e98b08fSopenharmony_ci    }
400e98b08fSopenharmony_ci    params->thisVal = JSI::AcquireValue(thisVal);
410e98b08fSopenharmony_ci    params->args = JSI::AcquireValue(args[0]);
420e98b08fSopenharmony_ci    params->flag = flag;
430e98b08fSopenharmony_ci    JsAsyncWork::DispatchAsyncWork(ExecuteFunc, reinterpret_cast<void *>(params));
440e98b08fSopenharmony_ci    return undefValue;
450e98b08fSopenharmony_ci}
460e98b08fSopenharmony_ci
470e98b08fSopenharmony_civoid ExecuteGetInfo(void* data)
480e98b08fSopenharmony_ci{
490e98b08fSopenharmony_ci    FuncParams* params = reinterpret_cast<FuncParams *>(data);
500e98b08fSopenharmony_ci    if (params == nullptr) {
510e98b08fSopenharmony_ci        return;
520e98b08fSopenharmony_ci    }
530e98b08fSopenharmony_ci    JSIValue args = params->args;
540e98b08fSopenharmony_ci    JSIValue thisVal = params->thisVal;
550e98b08fSopenharmony_ci    JSIValue result = JSI::CreateObject();
560e98b08fSopenharmony_ci    if (!NativeapiDeviceInfo::GetProductInfo(result)) {
570e98b08fSopenharmony_ci        NativeapiCommon::FailCallBack(thisVal, args, ERROR_CODE_GENERAL);
580e98b08fSopenharmony_ci    } else {
590e98b08fSopenharmony_ci        NativeapiCommon::SuccessCallBack(thisVal, args, result);
600e98b08fSopenharmony_ci    }
610e98b08fSopenharmony_ci    JSI::ReleaseValueList(args, thisVal, result, ARGS_END);
620e98b08fSopenharmony_ci    delete params;
630e98b08fSopenharmony_ci    params = nullptr;
640e98b08fSopenharmony_ci}
650e98b08fSopenharmony_ci}
660e98b08fSopenharmony_ci
670e98b08fSopenharmony_civoid InitDeviceModule(JSIValue exports)
680e98b08fSopenharmony_ci{
690e98b08fSopenharmony_ci    JSI::SetModuleAPI(exports, "getInfo", NativeapiDeviceInfo::GetDeviceInfo);
700e98b08fSopenharmony_ci}
710e98b08fSopenharmony_ci
720e98b08fSopenharmony_cibool NativeapiDeviceInfo::GetAPILevel(JSIValue result)
730e98b08fSopenharmony_ci{
740e98b08fSopenharmony_ci    int apiLevel = GetSdkApiVersion();
750e98b08fSopenharmony_ci    if (apiLevel < 1) {
760e98b08fSopenharmony_ci        return false;
770e98b08fSopenharmony_ci    }
780e98b08fSopenharmony_ci    JSI::SetStringProperty(result, "apiVersion", std::to_string(apiLevel).c_str());
790e98b08fSopenharmony_ci    return true;
800e98b08fSopenharmony_ci}
810e98b08fSopenharmony_ci
820e98b08fSopenharmony_ciJSIValue NativeapiDeviceInfo::GetDeviceInfo(const JSIValue thisVal, const JSIValue* args, uint8_t argsNum)
830e98b08fSopenharmony_ci{
840e98b08fSopenharmony_ci    return ExecuteAsyncWork(thisVal, args, argsNum, ExecuteGetInfo);
850e98b08fSopenharmony_ci}
860e98b08fSopenharmony_ci
870e98b08fSopenharmony_cibool NativeapiDeviceInfo::GetDeviceType(JSIValue result)
880e98b08fSopenharmony_ci{
890e98b08fSopenharmony_ci    const char* deviceType = ::GetDeviceType();
900e98b08fSopenharmony_ci    if (deviceType == nullptr) {
910e98b08fSopenharmony_ci        return false;
920e98b08fSopenharmony_ci    }
930e98b08fSopenharmony_ci    JSI::SetStringProperty(result, "deviceType", deviceType);
940e98b08fSopenharmony_ci    return true;
950e98b08fSopenharmony_ci}
960e98b08fSopenharmony_ci
970e98b08fSopenharmony_cibool NativeapiDeviceInfo::GetLanguage(JSIValue result)
980e98b08fSopenharmony_ci{
990e98b08fSopenharmony_ci    // because of MAX_LANGUAGE_LENGTH is little,we use array instead of pointer
1000e98b08fSopenharmony_ci    char langStr[MAX_LANGUAGE_LENGTH + 1] = {0};
1010e98b08fSopenharmony_ci    if (GLOBAL_GetLanguage(langStr, MAX_LANGUAGE_LENGTH) != 0) {
1020e98b08fSopenharmony_ci        JSI::SetStringProperty(result, "language", "");
1030e98b08fSopenharmony_ci    } else {
1040e98b08fSopenharmony_ci        JSI::SetStringProperty(result, "language", langStr);
1050e98b08fSopenharmony_ci    }
1060e98b08fSopenharmony_ci    return true;
1070e98b08fSopenharmony_ci}
1080e98b08fSopenharmony_ci
1090e98b08fSopenharmony_cibool NativeapiDeviceInfo::GetProductInfo(JSIValue result)
1100e98b08fSopenharmony_ci{
1110e98b08fSopenharmony_ci    bool isSuccess = true;
1120e98b08fSopenharmony_ci    const char* brand =  GetBrand();
1130e98b08fSopenharmony_ci    const char* manufacture = GetManufacture();
1140e98b08fSopenharmony_ci    const char* model = GetProductModel();
1150e98b08fSopenharmony_ci    if (brand == nullptr || manufacture == nullptr || model == nullptr) {
1160e98b08fSopenharmony_ci        isSuccess = false;
1170e98b08fSopenharmony_ci    } else {
1180e98b08fSopenharmony_ci        JSI::SetStringProperty(result, "brand", brand);
1190e98b08fSopenharmony_ci        JSI::SetStringProperty(result, "manufacturer", manufacture);
1200e98b08fSopenharmony_ci        JSI::SetStringProperty(result, "model", model);
1210e98b08fSopenharmony_ci        JSI::SetStringProperty(result, "product", model);
1220e98b08fSopenharmony_ci    }
1230e98b08fSopenharmony_ci    if (isSuccess) {
1240e98b08fSopenharmony_ci        if (!NativeapiDeviceInfo::GetDeviceType(result) ||
1250e98b08fSopenharmony_ci            !NativeapiDeviceInfo::GetLanguage(result) ||
1260e98b08fSopenharmony_ci            !NativeapiDeviceInfo::GetAPILevel(result) ||
1270e98b08fSopenharmony_ci            !NativeapiDeviceInfo::GetRegion(result)) {
1280e98b08fSopenharmony_ci            isSuccess = false;
1290e98b08fSopenharmony_ci        }
1300e98b08fSopenharmony_ci    }
1310e98b08fSopenharmony_ci
1320e98b08fSopenharmony_ci    Screen &screen = Screen::GetInstance();
1330e98b08fSopenharmony_ci    JSI::SetNumberProperty(result, "windowWidth", static_cast<double>(screen.GetWidth()));
1340e98b08fSopenharmony_ci    JSI::SetNumberProperty(result, "windowHeight", static_cast<double>(screen.GetHeight()));
1350e98b08fSopenharmony_ci    // set default value
1360e98b08fSopenharmony_ci    const uint8_t defaultScreenDensity = 195;
1370e98b08fSopenharmony_ci    const char * const defaultScreenShape = "rect";
1380e98b08fSopenharmony_ci    JSI::SetNumberProperty(result, "screenDensity", static_cast<double>(defaultScreenDensity));
1390e98b08fSopenharmony_ci    JSI::SetStringProperty(result, "screenShape", defaultScreenShape);
1400e98b08fSopenharmony_ci    return isSuccess;
1410e98b08fSopenharmony_ci}
1420e98b08fSopenharmony_ci
1430e98b08fSopenharmony_cibool NativeapiDeviceInfo::GetRegion(JSIValue result)
1440e98b08fSopenharmony_ci{
1450e98b08fSopenharmony_ci    // because of MAX_REGION_LENGTH is little,we use array instead of pointer
1460e98b08fSopenharmony_ci    char region[MAX_REGION_LENGTH + 1] = {0};
1470e98b08fSopenharmony_ci    if (GLOBAL_GetRegion(region, MAX_REGION_LENGTH) != 0) {
1480e98b08fSopenharmony_ci        JSI::SetStringProperty(result, "region", "");
1490e98b08fSopenharmony_ci    } else {
1500e98b08fSopenharmony_ci        JSI::SetStringProperty(result, "region", region);
1510e98b08fSopenharmony_ci    }
1520e98b08fSopenharmony_ci    return true;
1530e98b08fSopenharmony_ci}
1540e98b08fSopenharmony_ci} // ACELite
1550e98b08fSopenharmony_ci} // OHOS
156