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 <cstdio>
17 #include <securec.h>
18
19 #include "app_manage.h"
20
21 namespace OHOS {
22 ViewGroupPage* AppManage::viewPage_[MAX_VIEWGROUP] = { nullptr };
23 funcClick AppManage::installFuncclick_ = { nullptr };
24 funcLongPress AppManage::installFunclPress_ = { nullptr };
25 int AppManage::size_ = 0;
26
~AppManage()27 AppManage::~AppManage()
28 {
29 UnregisterCallback();
30 }
31
GetAailityInfosByBundleName(const char * bundleName,AppInfo * pApp)32 bool AppManage::GetAailityInfosByBundleName(const char* bundleName, AppInfo* pApp)
33 {
34 if (bundleName == nullptr || pApp == nullptr) {
35 return false;
36 }
37 uint8_t ret = -1;
38 BundleInfo* pBundleInfos = nullptr;
39 int count = 0;
40 ret = GetBundleInfos(1, &pBundleInfos, &count);
41 if (ret == 0) {
42 BundleInfo* pBundleInfo = pBundleInfos;
43 for (int i = 0; i < count; i++, pBundleInfo++) {
44 if (memcmp(bundleName, pBundleInfo->bundleName, strlen(pBundleInfo->bundleName)) == 0) {
45 (void)memcpy_s(
46 pApp->appName_, sizeof(pApp->appName_), pBundleInfo->bundleName, strlen(pBundleInfo->bundleName));
47 pApp->appName_[strlen(pBundleInfo->bundleName)] = 0;
48 if (pBundleInfo->abilityInfos[0].name) {
49 (void)memcpy_s(pApp->abilityName_, sizeof(pApp->abilityName_), pBundleInfo->abilityInfos[0].name,
50 strlen(pBundleInfo->abilityInfos[0].name));
51 pApp->abilityName_[strlen(pBundleInfo->abilityInfos[0].name)] = 0;
52 }
53 if (pBundleInfo->bigIconPath) {
54 (void)memcpy_s(pApp->appIconDir_, sizeof(pApp->appIconDir_), pBundleInfo->bigIconPath,
55 strlen(pBundleInfo->bigIconPath));
56 pApp->appIconDir_[strlen(pBundleInfo->bigIconPath)] = 0;
57 }
58 return true;
59 }
60 }
61 }
62 return false;
63 }
64
GetAppInstallInfo(const char * bundleName)65 bool AppManage::GetAppInstallInfo(const char* bundleName)
66 {
67 if (bundleName == nullptr) {
68 return false;
69 }
70 AppInfo* pApp = new AppInfo();
71 pApp->funcclick_ = installFuncclick_;
72 pApp->funclPress_ = installFunclPress_;
73 if (GetAailityInfosByBundleName(bundleName, pApp) == true) {
74 int i = 0;
75 for (; i < size_; i++) {
76 if (viewPage_[i]->FindApp(pApp)) {
77 break;
78 }
79 }
80 if (i == size_) {
81 for (i = 0; i < size_; i++) {
82 if (viewPage_[i]->AddApp(pApp)) {
83 return true;
84 }
85 }
86 }
87 }
88 delete pApp;
89 pApp = nullptr;
90 return false;
91 }
92
MyBundleStateCallback(const uint8_t installType,const uint8_t resultCode,const void * resultMessage,const char * bundleName,void * data)93 void AppManage::MyBundleStateCallback(
94 const uint8_t installType, const uint8_t resultCode, const void* resultMessage, const char* bundleName, void* data)
95 {
96 if (installType == 0) { // install update
97 if (resultCode == 0 && bundleName != nullptr) {
98 char tmpName[TMP_BUF_SIZE] = {0};
99 if (memcpy_s(tmpName, sizeof(tmpName), bundleName, strlen(bundleName)) == LAUNCHER_SUCCESS) {
100 tmpName[strlen(bundleName)] = 0;
101 GetAppInstallInfo(tmpName);
102 }
103 }
104 }
105 }
106
MyBundleOwnCallback(const uint8_t resultCode,const void * resultMessage)107 void AppManage::MyBundleOwnCallback(const uint8_t resultCode, const void* resultMessage)
108 {
109 // uninstall callback
110 }
111
LauncherApp(BundleInfo ** info,int & count)112 bool AppManage::LauncherApp(BundleInfo** info, int& count)
113 {
114 callBackParam_.bundleName = nullptr;
115 callBackParam_.data = nullptr;
116 callBackParam_.callBack = MyBundleStateCallback;
117 RegisterCallback(&callBackParam_);
118
119 BundleInfo* pBundleInfos = nullptr;
120 uint8_t ret = GetBundleInfos(1, &pBundleInfos, &count);
121 if (ret == 0) {
122 *info = pBundleInfos;
123 return true;
124 } else {
125 *info = nullptr;
126 return false;
127 }
128 }
129
InstallApp(AppInfo * app)130 bool AppManage::InstallApp(AppInfo* app)
131 {
132 return true;
133 }
134
UnInstallApp(AppInfo * app)135 bool AppManage::UnInstallApp(AppInfo* app)
136 {
137 return Uninstall(app->appName_, nullptr, MyBundleOwnCallback);
138 }
139
StartApp(AppInfo * app)140 bool AppManage::StartApp(AppInfo* app)
141 {
142 Want want1 = { nullptr };
143 ElementName element = { nullptr };
144 SetElementBundleName(&element, app->appName_);
145 SetElementAbilityName(&element, app->abilityName_);
146 SetWantElement(&want1, element);
147 SetWantData(&want1, "WantData", strlen("WantData") + 1);
148 StartAbility(&want1);
149 ClearElement(&element);
150 ClearWant(&want1);
151 return true;
152 }
153
SetViewGroup(funcClick click,funcLongPress press,ViewGroupPage * viewPage[MAX_VIEWGROUP],int size)154 void AppManage::SetViewGroup(funcClick click, funcLongPress press, ViewGroupPage* viewPage[MAX_VIEWGROUP], int size)
155 {
156 if (click == nullptr || press == nullptr || viewPage == nullptr) {
157 return;
158 }
159 for (int i = 0; i < size; i++) {
160 viewPage_[i] = viewPage[i];
161 }
162 size_ = size;
163 installFuncclick_ = click;
164 installFunclPress_ = press;
165 }
166 } // namespace OHOS
167