• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 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
16import edmEnterpriseDeviceManager from '@ohos.enterprise.adminManager';
17import router from '@ohos.router';
18import appDetailData from '../common/appManagement/appDetailData'
19import baseData from '../common/baseData';
20import entryComponent from './component/entryComponent'
21import headComponent from './component/headComponent';
22import logger from '../common/logger'
23import permissionListComponent from './component/permissionListComponent';
24import utils from '../common/utils'
25
26let appInfo;
27let elementNameVal;
28let isAdminTypeVal;
29let isActiveState;
30let enterInfo;
31const TAG = 'ApplicationInfo';
32
33@Entry
34@Component
35struct ApplicationInfo {
36  @StorageLink('applicationInfo') applicationInfo: any = {};
37  @State isShowActive: boolean = false;
38  @State isEnableButton: boolean = false;
39  @StorageLink('activeType') isAdminType: number = 0;
40  @State deviceAdminActive: string = '';
41  @State deviceAdminDisActive: string = '';
42  @State enterpriseName: string = '';
43  @State enterpriseDescription: string = '';
44
45  build() {
46    Column() {
47      Column() {
48        GridContainer({
49          columns: utils.isLargeDevice(),
50          sizeType: SizeType.Auto,
51          gutter: '12vp',
52          margin: '12vp'
53        }) {
54          Column() {
55            headComponent({ headName: $r('app.string.adminprovisioning') });
56
57            Row() {
58              entryComponent({
59                appIcon: this.applicationInfo.appIcon,
60                appTitle: this.applicationInfo.appTitle,
61              })
62            }
63
64            Column() {
65              Text(this.enterpriseName)
66                .height($r('app.float.wh_value_21'))
67                .fontColor($r('sys.color.ohos_id_color_text_primary'))
68                .fontSize($r('app.float.font_16'))
69                .fontWeight(FontWeight.Medium)
70                .textAlign(TextAlign.Start)
71                .maxLines(3)
72                .textOverflow({ overflow: TextOverflow.Ellipsis })
73                .margin({
74                  left: $r('app.float.wh_value_36'),
75                  top: $r('app.float.wh_value_12'),
76                });
77
78              Text(this.enterpriseDescription)
79                .height($r('app.float.wh_value_19'))
80                .fontColor($r('sys.color.ohos_id_color_text_secondary'))
81                .fontSize($r('app.float.font_14'))
82                .fontWeight(FontWeight.Regular)
83                .textAlign(TextAlign.Start)
84                .maxLines(3)
85                .textOverflow({ overflow: TextOverflow.Ellipsis })
86                .margin({
87                  left: $r('app.float.wh_value_36'),
88                  top: $r('app.float.wh_value_2'),
89                });
90            }
91            .width('100%')
92            .alignItems(HorizontalAlign.Start)
93            .align(Alignment.Start)
94
95            Column() {
96              Text(this.isShowActive ? this.deviceAdminActive : this.deviceAdminDisActive)
97                .fontColor($r('sys.color.ohos_id_color_text_primary'))
98                .fontSize($r('app.float.font_16'))
99                .fontWeight(FontWeight.Medium)
100                .textAlign(TextAlign.Start)
101                .maxLines(3)
102                .textOverflow({ overflow: TextOverflow.Ellipsis })
103                .margin({
104                  left: $r('app.float.wh_value_36'),
105                  top: $r('app.float.wh_value_24'),
106                });
107
108              List() {
109                ForEach(appInfo.appPermissionList, (item) => {
110                  ListItem() {
111                    permissionListComponent({
112                      permissionName: item.permissionLabel,
113                      permissionDescription: item.permissionDescription,
114                    });
115                  }
116                }, item => JSON.stringify(item));
117              }
118            }
119            .alignItems(HorizontalAlign.Start)
120            .align(Alignment.Start)
121          }
122          .useSizeType({
123            xs: { span: 8, offset: 0 }, sm: { span: 8, offset: 0 },
124            md: { span: 8, offset: 0 }, lg: { span: 8, offset: 2 }
125          })
126        }
127      }
128      .layoutWeight(1)
129
130      Column() {
131        GridContainer({
132          columns: utils.isLargeDevice(),
133          sizeType: SizeType.Auto,
134          gutter: '12vp',
135          margin: '12vp'
136        }) {
137          Row() {
138            Row()
139              .useSizeType({
140                xs: { span: 1, offset: 0 }, sm: { span: 1, offset: 0 },
141                md: { span: 1, offset: 0 }, lg: { span: 3, offset: 0 }
142              })
143
144            Button() {
145              Text(this.isShowActive ? $r('app.string.deActivate') : $r('app.string.activation'))
146                .opacity(this.isEnableButton ? 1 : 0.38)
147                .fontSize($r('sys.float.ohos_id_text_size_button1'))
148                .fontColor($r('sys.color.ohos_id_color_text_primary_activated'))
149            }
150            .enabled(this.isEnableButton)
151            .backgroundColor($r('sys.color.ohos_id_color_button_normal'))
152            .height($r('app.float.button_height'))
153            .margin({
154              right: $r('app.float.wh_value_6'),
155              bottom: $r('app.float.wh_value_24')
156            })
157            .onClick(() => {
158              logger.info(TAG, 'button active:' + this.isShowActive);
159              this.activateAdmin(this.isAdminType);
160            })
161            .useSizeType({
162              xs: { span: 3, offset: 1 }, sm: { span: 3, offset: 1 },
163              md: { span: 3, offset: 1 }, lg: { span: 3, offset: 3 }
164            })
165
166            Button() {
167              Text($r('app.string.cancel'))
168                .fontSize($r('sys.float.ohos_id_text_size_button1'))
169                .fontColor($r('sys.color.ohos_id_color_text_primary_activated'))
170            }
171            .backgroundColor($r('sys.color.ohos_id_color_button_normal'))
172            .height($r('app.float.button_height'))
173            .margin({
174              left: $r('app.float.wh_value_6'),
175              bottom: $r('app.float.wh_value_24')
176            })
177            .onClick(() => {
178              logger.info(TAG, 'button cancel in isShowActive:' + this.isShowActive);
179              appDetailData.terminateAbilityPage();
180            })
181            .useSizeType({
182              xs: { span: 3, offset: 4 }, sm: { span: 3, offset: 4 },
183              md: { span: 3, offset: 4 }, lg: { span: 3, offset: 6 }
184            })
185
186            Row()
187              .useSizeType({
188                xs: { span: 1, offset: 7 }, sm: { span: 1, offset: 7 },
189                md: { span: 1, offset: 7 }, lg: { span: 3, offset: 9 }
190              })
191          }
192          .justifyContent(FlexAlign.Center)
193        }
194      }.width('100%')
195    }
196    .height('100%')
197    .width('100%')
198    .backgroundColor($r('sys.color.ohos_id_color_sub_background'))
199  }
200
201  aboutToAppear(): void {
202    appInfo = {
203      appIcon: '',
204      appTitle: '',
205      appBundleName: '',
206      appPermissionList: [],
207    };
208
209    elementNameVal = {
210      abilityName: '',
211      bundleName: '',
212    };
213
214    enterInfo = {
215      name: '',
216      description: '',
217    };
218
219    isAdminTypeVal = { admintype: 0 };
220    logger.info(TAG, 'aboutToAppear in');
221    this.getCheckAbilityList(appInfo, elementNameVal, isAdminTypeVal);
222    logger.info(TAG, 'aboutToAppear out');
223  }
224
225  aboutToDisappear(): void {
226    logger.info(TAG, 'aboutToDisappear');
227  }
228
229  onPageShow(): void {
230    logger.info(TAG, 'onPageShow in');
231    this.isAdminActive();
232    logger.info(TAG, 'onPageShow isActiveState =' + isActiveState);
233    if (!utils.isValid(isActiveState)) {
234      return;
235    }
236    this.isShowActive = isActiveState;
237    logger.info(TAG, 'onPageShow out');
238  }
239
240  onPageHide() {
241    logger.info(TAG, 'onPageHide');
242  }
243
244  onBackPress() {
245    logger.info(TAG, 'onBackPress');
246    router.back();
247  }
248
249  async isAdminActive() {
250    let wantTemp = {
251      bundleName: elementNameVal.bundleName,
252      abilityName: elementNameVal.abilityName,
253    };
254    let retAppState;
255    let retSuperState;
256    logger.info(TAG, 'isAdminActive elementNameVal.bundleName=' + elementNameVal.bundleName
257      + ' | elementNameVal.abilityName=' + elementNameVal.abilityName);
258    if (elementNameVal.bundleName === baseData.EMPTY_STR || elementNameVal.abilityName === baseData.EMPTY_STR) {
259      logger.info(TAG, 'isAdminActive elementNameVal is null');
260      return;
261    }
262
263    retAppState = await edmEnterpriseDeviceManager.isAdminEnabled(wantTemp);
264    retSuperState = await edmEnterpriseDeviceManager.isSuperAdmin(elementNameVal.bundleName);
265    if (!retAppState) {
266      this.isShowActive = false;
267      this.isEnableButton = true;
268    } else {
269      if (retSuperState) {
270        this.isEnableButton = false;
271      } else {
272        this.isEnableButton = true;
273      }
274      this.isShowActive = true;
275    }
276    logger.info(TAG, 'isAdminActive retAppState:' + retAppState
277      + ' | retSuperState:' + retSuperState);
278    isActiveState = this.isShowActive;
279  }
280
281  async activateAdmin(adminType) {
282    logger.info(TAG, 'activateAdmin isShowActive:' + this.isShowActive);
283    let wantTemp = {
284      bundleName: elementNameVal.bundleName,
285      abilityName: elementNameVal.abilityName,
286    };
287    await this.isAdminActive();
288    let ret = true;
289    if (!this.isShowActive) {
290      if (adminType === edmEnterpriseDeviceManager.AdminType.ADMIN_TYPE_NORMAL) {
291        await edmEnterpriseDeviceManager.enableAdmin(wantTemp,
292          { name: enterInfo.name, description: enterInfo.description },
293          edmEnterpriseDeviceManager.AdminType.ADMIN_TYPE_NORMAL)
294          .catch((error) => {
295            ret = false;
296            logger.info(TAG, 'errorCode : ' + error.code + 'errorMessage : ' + error.message);
297          });
298      } else {
299        logger.warn(TAG, 'not support AdminType.ADMIN_TYPE_SUPER enable Admin')
300      }
301      logger.info(TAG, 'activateAdmin Activate admin end');
302    } else {
303      if (this.isEnableButton) {
304        await edmEnterpriseDeviceManager.disableAdmin(wantTemp)
305          .catch((error) => {
306            ret = false;
307            logger.info(TAG, 'errorCode : ' + error.code + 'errorMessage : ' + error.message);
308          });
309      } else {
310        logger.info(TAG, 'activateAdmin not support super admin active');
311      }
312    }
313    logger.info(TAG, 'activateAdmin adminType:' + adminType + ' | ret:' + ret);
314    if (ret) {
315      appDetailData.terminateAbilityPage();
316    }
317  }
318
319  async getSelfResourceVal(resource) {
320    let resMgr = await globalThis.adminProvisioningContext.resourceManager;
321    logger.info(TAG, 'getSelfResourceVal in:');
322    let value = await resMgr.getString(resource.id);
323    logger.info(TAG, 'getSelfResourceVal finish value:' + value);
324    if (!utils.isValid(value) || value === baseData.EMPTY_STR) {
325      return baseData.EMPTY_STR;
326    }
327    return value;
328  }
329
330  async getAbilityWantVal(appInfo, elementNameVal, adminType) {
331    logger.info(TAG, 'getAbilityWantVal in:');
332    let data = await globalThis.adminProvisioningWant;
333
334    if (!utils.checkObjPropertyValid(data, 'parameters.elementName.abilityName')
335      || !utils.checkObjPropertyValid(data, 'parameters.enterprise.name')
336      || !utils.isValid(data.parameters.elementName.bundleName)
337      || !utils.isValid(data.parameters.activeType)
338      || !utils.isValid(data.parameters.enterprise.description)) {
339      logger.warn(TAG, 'data.parameters = ' + JSON.stringify(data.parameters));
340      return;
341    }
342
343    elementNameVal.abilityName = data.parameters.elementName.abilityName;
344    elementNameVal.bundleName = data.parameters.elementName.bundleName;
345    appInfo.appBundleName = data.parameters.elementName.bundleName;
346    adminType.admintype = data.parameters.activeType;
347    AppStorage.SetOrCreate('activeType', adminType.admintype);
348
349    enterInfo.name = data.parameters.enterprise.name.substring(0, baseData.MAX_LEN);
350    enterInfo.description = data.parameters.enterprise.description.substring(0, baseData.MAX_LEN);
351    logger.info(TAG, 'getAbilityWantVal out isAdminTypeVal.admintype:'
352      + isAdminTypeVal.admintype + ' enter.name=' + enterInfo.name + ' enter.des=' + enterInfo.description);
353  }
354
355  async getCheckAbilityList(appInfo, elementNameVal, adminType) {
356    let deviceActiveOne = '';
357    let deviceActiveTwo = '';
358    let deviceDeactivate = '';
359    let enterName = '';
360    let enterDescription = '';
361    let getName = '';
362    let getDescription = '';
363    logger.info(TAG, 'getCheckAbilityList in:');
364    await this.getAbilityWantVal(appInfo, elementNameVal, adminType);
365
366    let ret = await appDetailData.checkAppItem(elementNameVal);
367    if (!ret.val) {
368      logger.info(TAG, 'aboutToAppear not exist bundleName:' + appInfo.appBundleName);
369      appDetailData.terminateAbilityPage();
370      return;
371    }
372
373    await this.isAdminActive();
374
375    if (appInfo.appBundleName) {
376      await appDetailData.getBundleInfoItem(appInfo.appBundleName, appInfo);
377    }
378
379    deviceActiveOne = await this.getSelfResourceVal($r('app.string.device_active_1'));
380    deviceActiveTwo = await this.getSelfResourceVal($r('app.string.device_active_2'));
381    deviceDeactivate = await this.getSelfResourceVal($r('app.string.device_deactivate'));
382    enterName = await this.getSelfResourceVal($r('app.string.enterprise_name'));
383    enterDescription = await this.getSelfResourceVal($r('app.string.enterprise_description'));
384    this.deviceAdminActive = deviceActiveOne + `${appInfo.appTitle}` + deviceActiveTwo;
385    this.deviceAdminDisActive = deviceDeactivate + `${appInfo.appTitle}` + deviceActiveTwo;
386
387    let want = {
388      bundleName: elementNameVal.bundleName,
389      abilityName: elementNameVal.abilityName,
390    };
391    let getEnterInfo = {
392      name: '',
393      description: '',
394    };
395    if (this.isShowActive) {
396      logger.info(TAG, 'getCheckAbilityList get getEnterInfo');
397      getEnterInfo = await edmEnterpriseDeviceManager.getEnterpriseInfo(want);
398    }
399
400    logger.info(TAG, 'getCheckAbilityList start enterpriseName=' + getEnterInfo.name
401      + ' enterpriseDescription=' + getEnterInfo.description);
402    if (utils.isValid(getEnterInfo.name)) {
403      getName = getEnterInfo.name.substring(0, baseData.MAX_LEN);
404    }
405    if (utils.isValid(getEnterInfo.description)) {
406      getDescription = getEnterInfo.description.substring(0, baseData.MAX_LEN);
407    }
408
409    if (this.isShowActive) {
410      this.enterpriseName = enterName + getName;
411      this.enterpriseDescription = enterDescription + getDescription;
412    } else {
413      this.enterpriseName = enterName + enterInfo.name;
414      this.enterpriseDescription = enterDescription + enterInfo.description;
415    }
416    logger.info(TAG, 'getCheckAbilityList enterpriseName=' + this.enterpriseName
417      + ' enterpriseDescription=' + this.enterpriseDescription);
418  }
419}