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