• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021-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 { backBar } from "../common/components/backBar";
17import router from '@ohos.router';
18import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
19import { groups } from "../common/model/permissionGroup";
20import Constants from '../common/utils/constant';
21
22var TAG = 'PermissionManager_MainAbility:'
23
24const allowedStatus = 0; // Status: Allowed
25const bannedStatus = 1; // Status: Banned
26const PRECISE_LOCATION_PERMISSION = 'ohos.permission.LOCATION'
27class permissionObj {
28  groupName: string;
29  permission: string[];
30  group: string;
31  constructor(groupName: string, permission: string[], group: string) {
32    this.groupName = groupName;
33    this.permission = permission;
34    this.group = group
35  }
36}
37
38class ResourceObj {
39  bundleName: string
40  api: number
41  tokenId: number
42  iconId: string
43  labelId: string
44  permissions: Array<any>
45  groupId: Array<any>
46  constructor(bundleName: string, api: number, tokenId: number, iconId: string, labelId: string, permissions: Array<any>, groupId: Array<any>) {
47    this.bundleName = bundleName
48    this.api = api
49    this.tokenId = tokenId
50    this.iconId = iconId
51    this.labelId = labelId
52    this.permissions = permissions
53    this.groupId = groupId
54  }
55}
56
57@Entry
58@Component
59struct appNamePlusPage {
60  @State allowedListItem: permissionObj[] = []; // Array of allowed permissions
61  @State bannedListItem: permissionObj[] = []; // array of forbidden permissions
62  @State routerData: ResourceObj = globalThis.applicationInfo; // Routing jump data
63
64  @Builder ListItemLayout(item, index, status) {
65    ListItem() {
66      Row() {
67        Column() {
68          Flex({ justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) {
69            Row() {
70              Text(item.groupName)
71                .fontSize(Constants.TEXT_MIDDLE_FONT_SIZE)
72                .fontColor($r('app.color.text_color'))
73                .fontWeight(FontWeight.Medium)
74                .flexGrow(Constants.FLEX_GROW)
75              Image($r('app.media.ic_public_arrow_right'))
76                .objectFit(ImageFit.Contain)
77                .height(Constants.IMAGE_HEIGHT)
78                .width(Constants.IMAGE_WIDTH)
79            }
80            .width(Constants.FULL_WIDTH)
81            .height(Constants.LISTITEM_ROW_HEIGHT)
82          }
83          if (!index) {
84            Row() {
85              Column()
86                .backgroundColor($r('app.color.text_decoration_color'))
87                .width(Constants.FULL_WIDTH)
88                .height(Constants.TEXT_DECORATION_HEIGHT)
89            }
90          }
91        }.onClick(() => {
92          globalThis.currentPermissionGroup = item.group
93          if (status === 'allow') {
94            if (item.group == "OTHER") {
95              router.pushUrl({
96                url: 'pages/other-permissions',
97                params: {
98                  routerData: this.routerData.bundleName,
99                  backTitle: item.groupName,
100                  permission: item.permission,
101                  status: allowedStatus,
102                  tokenId: this.routerData.tokenId
103                }
104              });
105            } else {
106              router.pushUrl({
107                url: 'pages/application-tertiary',
108                params: {
109                  routerData: this.routerData.bundleName,
110                  backTitle: item.groupName,
111                  permission: item.permission,
112                  status: allowedStatus
113                }
114              });
115            }
116          }
117          else if (status === 'banned') {
118            if (item.group == "OTHER") {
119              router.pushUrl({
120                url: 'pages/other-permissions',
121                params: {
122                  routerData: this.routerData.bundleName,
123                  backTitle: item.groupName,
124                  permission: item.permission,
125                  status: bannedStatus,
126                  tokenId: this.routerData.tokenId
127                }
128              });
129            } else {
130              router.pushUrl({
131                url: 'pages/application-tertiary',
132                params: {
133                  routerData: this.routerData.bundleName,
134                  backTitle: item.groupName,
135                  permission: item.permission,
136                  status: bannedStatus
137                }
138              });
139            }
140          }
141        })
142      }
143    }.padding({ left: Constants.DEFAULT_PADDING_START, right: Constants.DEFAULT_PADDING_END })
144  }
145
146  /**
147   * Initialize permission status information and group permission information
148   */
149  async initialPermissions() {
150    var reqPermissions = this.routerData.permissions;
151    var reqGroupIds = this.routerData.groupId;
152
153    if (!reqGroupIds.length) {
154      this.allowedListItem = [];
155      this.bannedListItem = [];
156      return;
157    }
158
159    for (let i = 0; i < reqGroupIds.length; i++) {
160      let id = reqGroupIds[i];
161      let groupName = groups[id].groupName;
162      let group = groups[id].name;
163      let groupReqPermissons = [];
164      for (let j = 0; j < reqPermissions.length; j++) {
165        let permission = reqPermissions[j];
166        if (groups[id].permissions.indexOf(permission) != -1) {
167          groupReqPermissons.push(permission)
168        }
169      }
170      let isGranted = true;
171      for (let i = 0; i < groupReqPermissons.length; i++) {
172        let permission = groupReqPermissons[i]
173        if(this.routerData.api >= Constants.API_VERSION_SUPPORT_STAGE && permission == PRECISE_LOCATION_PERMISSION) {
174          continue
175        }
176        let res = await abilityAccessCtrl.createAtManager().verifyAccessToken(
177          this.routerData.tokenId, permission);
178        if (res != 0) {
179          isGranted = false;
180        }
181      }
182
183      if (isGranted) {
184        this.allowedListItem.push(new permissionObj(groupName, groupReqPermissons, group));
185      } else {
186        this.bannedListItem.push(new permissionObj(groupName, groupReqPermissons, group));
187      }
188    }
189  }
190
191  /**
192   * Lifecycle function, triggered once when this page is displayed
193   */
194  onPageShow() {
195    console.log(TAG + 'onPageShow application-secondary')
196    this.initialPermissions();
197  }
198
199  /**
200   * Lifecycle function, triggered once when this page disappears
201   */
202  onPageHide() {
203    console.log(TAG + 'onPageHide application-secondary')
204    setTimeout(()=> {
205      this.allowedListItem = [];
206      this.bannedListItem = [];
207    }, Constants.DELAY_TIME)
208  }
209
210  build() {
211    Column(){
212      GridContainer({ gutter: Constants.GUTTER, margin: Constants.GRID_MARGIN }) {
213        Row({}) {
214          Row()
215            .useSizeType({
216              xs: { span: Constants.LEFT_XS_SPAN, offset: Constants.LEFT_XS_OFFSET },
217              sm: { span: Constants.LEFT_SM_SPAN, offset: Constants.LEFT_SM_OFFSET },
218              md: { span: Constants.LEFT_MD_SPAN, offset: Constants.LEFT_MD_OFFSET },
219              lg: { span: Constants.LEFT_LG_SPAN, offset: Constants.LEFT_LG_OFFSET }
220            })
221            .height(Constants.FULL_HEIGHT)
222          Row() {
223            Column() {
224              Row() {
225                backBar({ title: JSON.stringify(this.routerData.labelId), recordable: false })
226              }
227              Row() {
228                Column() {
229                  if (!this.allowedListItem.length && !this.bannedListItem.length) {
230                    Row() {
231                      List() {
232                        ListItem() {
233                          Row() {
234                            Column() {
235                              Flex({ justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) {
236                                Row() {
237                                  Column() {
238                                    Row() {
239                                      Flex({ justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) {
240                                        Text($r('app.string.no_permission'))
241                                          .fontSize(Constants.TEXT_MIDDLE_FONT_SIZE)
242                                          .fontColor($r('app.color.text_color'))
243                                      }.margin({ top: Constants.FLEX_MARGIN_TOP, bottom: Constants.FLEX_MARGIN_BOTTOM })
244                                    }.height(Constants.FULL_HEIGHT)
245                                  }.flexGrow(Constants.FLEX_GROW)
246                                   .constraintSize({minHeight: Constants.CONSTRAINTSIZE_MINHEIGHT })
247                                }
248                                .width(Constants.FULL_WIDTH)
249                                .height(Constants.LISTITEM_ROW_HEIGHT)
250                              }
251                            }
252                          }
253                        }.padding({ left: Constants.LISTITEM_PADDING_LEFT, right: Constants.LISTITEM_PADDING_RIGHT })
254                      }
255                      .backgroundColor($r('app.color.default_background_color'))
256                      .borderRadius(Constants.BORDER_RADIUS)
257                      .padding({ top: Constants.LIST_PADDING_TOP, bottom: Constants.LIST_PADDING_BOTTOM })
258                    }.margin({ top: Constants.ROW_MARGIN_TOP })
259                     .padding({ left: Constants.SECONDARY_LIST_PADDING_LEFT, right: Constants.SECONDARY_LIST_PADDING_RIGHT })
260                  } else {
261                    Scroll() {
262                      List() {
263                        if (this.allowedListItem.length) {
264                          ListItem() {
265                            Row() {
266                              Text($r('app.string.allowed'))
267                                .fontSize(Constants.TEXT_SMAL_FONT_SIZE)
268                                .fontColor($r('app.color.label_color_light'))
269                                .fontWeight(FontWeight.Medium)
270                                .lineHeight(Constants.SUBTITLE_LINE_HEIGHT)
271                            }.constraintSize({ minHeight: Constants.SUBTITLE_MIN_HEIGHT })
272                            .width(Constants.FULL_WIDTH)
273                            .padding({ top: Constants.SUBTITLE_PADDING_TOP, bottom: Constants.SUBTITLE_PADDING_BOTTOM,
274                              left: Constants.SECONDARY_TEXT_MARGIN_LEFT})
275                          }
276
277                          ListItem() {
278                            Row() {
279                              List() {
280                                ForEach(this.allowedListItem.slice(Constants.SLICE_START, this.allowedListItem.length - 1),
281                                  (item) => {
282                                    this.ListItemLayout(item, Constants.SLICE_START_INDEX, 'allow')
283                                  }, item => item.toString())
284                                ForEach(this.allowedListItem.slice(Constants.SLICE_END), (item) => {
285                                  this.ListItemLayout(item, Constants.SLICE_END_INDEX, 'allow')
286                                }, item => item.toString())
287                              }
288                              .backgroundColor($r('app.color.default_background_color'))
289                              .borderRadius(Constants.BORDER_RADIUS)
290                              .padding({ top: Constants.LIST_PADDING_TOP, bottom: Constants.LIST_PADDING_BOTTOM })
291                            }.margin({ top: Constants.ROW_MARGIN_TOP })
292                            .padding({
293                              left: Constants.SECONDARY_LIST_PADDING_LEFT,
294                              right: Constants.SECONDARY_LIST_PADDING_RIGHT
295                            })
296                          }
297                        }
298                        if (this.bannedListItem.length) {
299                          ListItem() {
300                            Row() {
301                              Text($r('app.string.banned'))
302                                .fontSize(Constants.TEXT_SMAL_FONT_SIZE)
303                                .fontColor($r('app.color.label_color_light'))
304                                .fontWeight(FontWeight.Medium)
305                                .lineHeight(Constants.SUBTITLE_LINE_HEIGHT)
306                            }.constraintSize({ minHeight: Constants.SUBTITLE_MIN_HEIGHT })
307                            .width(Constants.FULL_WIDTH)
308                            .padding({ top: Constants.SUBTITLE_PADDING_TOP, bottom: Constants.SUBTITLE_PADDING_BOTTOM,
309                              left: Constants.SECONDARY_TEXT_MARGIN_LEFT})
310                          }
311
312                          ListItem() {
313                            Row() {
314                              List() {
315                                ForEach(this.bannedListItem.slice(Constants.SLICE_START, this.bannedListItem.length - 1),
316                                  (item) => {
317                                    this.ListItemLayout(item, Constants.SLICE_START_INDEX, 'banned')
318                                  }, item => item.toString())
319                                ForEach(this.bannedListItem.slice(Constants.SLICE_END), (item) => {
320                                  this.ListItemLayout(item, Constants.SLICE_END_INDEX, 'banned')
321                                }, item => item.toString())
322                              }
323                              .backgroundColor($r('app.color.default_background_color'))
324                              .borderRadius(Constants.BORDER_RADIUS)
325                              .padding({ top: Constants.LIST_PADDING_TOP, bottom: Constants.LIST_PADDING_BOTTOM })
326                            }.margin({ top: Constants.ROW_MARGIN_TOP })
327                            .padding({
328                              left: Constants.SECONDARY_LIST_PADDING_LEFT,
329                              right: Constants.SECONDARY_LIST_PADDING_RIGHT
330                            })
331                          }
332                        }
333                      }
334                    }.scrollBar(BarState.Off)
335                  }
336                }
337                .width(Constants.FULL_WIDTH)
338                .height(Constants.FULL_HEIGHT)
339              }
340              .layoutWeight(Constants.LAYOUT_WEIGHT)
341            }
342          }
343          .useSizeType({
344            xs: { span: Constants.MIDDLE_XS_SPAN, offset: Constants.MIDDLE_XS_OFFSET },
345            sm: { span: Constants.MIDDLE_SM_SPAN, offset: Constants.MIDDLE_SM_OFFSET },
346            md: { span: Constants.MIDDLE_MD_SPAN, offset: Constants.MIDDLE_MD_OFFSET },
347            lg: { span: Constants.MIDDLE_LG_SPAN, offset: Constants.MIDDLE_LG_OFFSET }
348          })
349          .height(Constants.FULL_HEIGHT)
350          Row()
351            .useSizeType({
352              xs: { span: Constants.RIGHT_XS_SPAN, offset: Constants.RIGHT_XS_OFFSET },
353              sm: { span: Constants.RIGHT_SM_SPAN, offset: Constants.RIGHT_SM_OFFSET },
354              md: { span: Constants.RIGHT_MD_SPAN, offset: Constants.RIGHT_MD_OFFSET },
355              lg: { span: Constants.RIGHT_LG_SPAN, offset: Constants.RIGHT_LG_OFFSET }
356            })
357            .height(Constants.FULL_HEIGHT)
358        }
359        .height(Constants.FULL_HEIGHT)
360        .width(Constants.FULL_WIDTH)
361        .backgroundColor($r("sys.color.ohos_id_color_sub_background"))
362      }
363    }
364  }
365}
366