• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 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 "nativeapi_deviceinfo.h"
17 #include <string>
18 #include "global.h"
19 #include "js_async_work.h"
20 #include "nativeapi_common.h"
21 #include "nativeapi_config.h"
22 #include "parameter.h"
23 #include "common/screen.h"
24 
25 namespace OHOS {
26 namespace ACELite {
27 namespace {
ExecuteAsyncWork(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum,AsyncWorkHandler ExecuteFunc,bool flag=false)28 JSIValue ExecuteAsyncWork(const JSIValue thisVal, const JSIValue* args,
29     uint8_t argsNum, AsyncWorkHandler ExecuteFunc, bool flag = false)
30 {
31     JSIValue undefValue = JSI::CreateUndefined();
32     if (!NativeapiCommon::IsValidJSIValue(args, argsNum)) {
33         return undefValue;
34     }
35     FuncParams* params = new FuncParams();
36     if (params == nullptr) {
37         return undefValue;
38     }
39     params->thisVal = JSI::AcquireValue(thisVal);
40     params->args = JSI::AcquireValue(args[0]);
41     params->flag = flag;
42     JsAsyncWork::DispatchAsyncWork(ExecuteFunc, reinterpret_cast<void *>(params));
43     return undefValue;
44 }
45 
ExecuteGetInfo(void * data)46 void ExecuteGetInfo(void* data)
47 {
48     FuncParams* params = reinterpret_cast<FuncParams *>(data);
49     if (params == nullptr) {
50         return;
51     }
52     JSIValue args = params->args;
53     JSIValue thisVal = params->thisVal;
54     JSIValue result = JSI::CreateObject();
55     if (!NativeapiDeviceInfo::GetProductInfo(result)) {
56         NativeapiCommon::FailCallBack(thisVal, args, ERROR_CODE_GENERAL);
57     } else {
58         NativeapiCommon::SuccessCallBack(thisVal, args, result);
59     }
60     JSI::ReleaseValueList(args, thisVal, result, ARGS_END);
61     delete params;
62     params = nullptr;
63 }
64 }
65 
InitDeviceModule(JSIValue exports)66 void InitDeviceModule(JSIValue exports)
67 {
68     JSI::SetModuleAPI(exports, "getInfo", NativeapiDeviceInfo::GetDeviceInfo);
69 }
70 
GetAPILevel(JSIValue result)71 bool NativeapiDeviceInfo::GetAPILevel(JSIValue result)
72 {
73     int apiLevel = GetSdkApiVersion();
74     if (apiLevel < 1) {
75         return false;
76     }
77     JSI::SetStringProperty(result, "apiVersion", std::to_string(apiLevel).c_str());
78     return true;
79 }
80 
GetDeviceInfo(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum)81 JSIValue NativeapiDeviceInfo::GetDeviceInfo(const JSIValue thisVal, const JSIValue* args, uint8_t argsNum)
82 {
83     return ExecuteAsyncWork(thisVal, args, argsNum, ExecuteGetInfo);
84 }
85 
GetDeviceType(JSIValue result)86 bool NativeapiDeviceInfo::GetDeviceType(JSIValue result)
87 {
88     const char* deviceType = ::GetDeviceType();
89     if (deviceType == nullptr) {
90         return false;
91     }
92     JSI::SetStringProperty(result, "deviceType", deviceType);
93     return true;
94 }
95 
GetLanguage(JSIValue result)96 bool NativeapiDeviceInfo::GetLanguage(JSIValue result)
97 {
98     // because of MAX_LANGUAGE_LENGTH is little,we use array instead of pointer
99     char langStr[MAX_LANGUAGE_LENGTH + 1] = {0};
100     if (GLOBAL_GetLanguage(langStr, MAX_LANGUAGE_LENGTH) != 0) {
101         JSI::SetStringProperty(result, "language", "");
102     } else {
103         JSI::SetStringProperty(result, "language", langStr);
104     }
105     return true;
106 }
107 
GetProductInfo(JSIValue result)108 bool NativeapiDeviceInfo::GetProductInfo(JSIValue result)
109 {
110     bool isSuccess = true;
111     const char* brand =  GetBrand();
112     const char* manufacture = GetManufacture();
113     const char* model = GetProductModel();
114     if (brand == nullptr || manufacture == nullptr || model == nullptr) {
115         isSuccess = false;
116     } else {
117         JSI::SetStringProperty(result, "brand", brand);
118         JSI::SetStringProperty(result, "manufacturer", manufacture);
119         JSI::SetStringProperty(result, "model", model);
120         JSI::SetStringProperty(result, "product", model);
121     }
122     if (isSuccess) {
123         if (!NativeapiDeviceInfo::GetDeviceType(result) ||
124             !NativeapiDeviceInfo::GetLanguage(result) ||
125             !NativeapiDeviceInfo::GetAPILevel(result) ||
126             !NativeapiDeviceInfo::GetRegion(result)) {
127             isSuccess = false;
128         }
129     }
130 
131     Screen &screen = Screen::GetInstance();
132     JSI::SetNumberProperty(result, "windowWidth", (double)screen.GetWidth());
133     JSI::SetNumberProperty(result, "windowHeight", (double)screen.GetHeight());
134     // set default value
135     const uint8_t defaultScreenDensity = 195;
136     const char * const defaultScreenShape = "rect";
137     JSI::SetNumberProperty(result, "screenDensity", (double)defaultScreenDensity);
138     JSI::SetStringProperty(result, "screenShape", defaultScreenShape);
139     return isSuccess;
140 }
141 
GetRegion(JSIValue result)142 bool NativeapiDeviceInfo::GetRegion(JSIValue result)
143 {
144     // because of MAX_REGION_LENGTH is little,we use array instead of pointer
145     char region[MAX_REGION_LENGTH + 1] = {0};
146     if (GLOBAL_GetRegion(region, MAX_REGION_LENGTH) != 0) {
147         JSI::SetStringProperty(result, "region", "");
148     } else {
149         JSI::SetStringProperty(result, "region", region);
150     }
151     return true;
152 }
153 } // ACELite
154 } // OHOS
155