1/**
2 * Copyright (c) 2024-2024 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 Logger from '../common/utils/Logger';
17import common from '@ohos.app.ability.common';
18import BundleInfoModel from '../model/bundleInfo/BundleInfoModel';
19import bundleManager from '@ohos.bundle.bundleManager';
20import { StringUtil } from '../common/utils/StringUtil';
21import Constants from '../common/constants/ComConstant';
22import { BusinessError } from '@ohos.base';
23import TitleBar from '../common/components/TitleBarComponent';
24import PrivacyProtectionListView from '../view/privacy/PrivacyProtectionListView';
25import router from '@ohos.router';
26import ResourceUtil from '../common/utils/ResourceUtil';
27import HiSysEventUtil from '../common/utils/HiSysEventUtil';
28import HiSysEventConstant from '../common/constants/HiSysEventConstant';
29import { AutoMenuClickIntent, AutoMenuRefreshIntent } from '../main/auto_menu/AutoMenuIntent';
30import { AutoMenuViewModel } from '../main/auto_menu/AutoMenuViewModel';
31import MenuInfo from '../common/bean/MenuInfo';
32
33const TAG: string = 'Index';
34const autoMenuViewModel: AutoMenuViewModel = new AutoMenuViewModel();
35let storage = LocalStorage.getShared();
36
37@Entry(storage)
38@Component
39struct Index {
40  @State startTime: number = 0;
41  private context = getContext(this) as common.UIAbilityContext;
42  @State isSplitMode: boolean = false;
43  scroller: Scroller = new Scroller();
44  @State windowWidth: number = 0;
45
46  @Styles
47  PressedStyles() {
48    .backgroundColor($r('sys.color.ohos_id_color_click_effect'))
49  }
50
51  @Styles
52  normalStyles() {
53    .backgroundColor($r('sys.color.ohos_id_color_list_card_bg'))
54  }
55
56  onPageShow() {
57    this.startTime = new Date().getTime();
58    autoMenuViewModel.processIntent(new AutoMenuRefreshIntent(this.context));
59    this.getBundleInfo();
60  }
61
62  //  Get all package information
63  getBundleInfo() {
64    BundleInfoModel.getAllBundleInfoByFunctionAccess()
65      .then(async (data: bundleManager.BundleInfo[]) => {
66        //   filter blank icon and text label resources
67        let initAppInfoList: bundleManager.ApplicationInfo[] = [];
68        for (let i = 0; i < data.length; i++) {
69          let info = data[i]
70          if (StringUtil.isNotEmpty(info.appInfo.icon)) {
71            initAppInfoList = initAppInfoList.concat(info.appInfo);
72          }
73        }
74        if (initAppInfoList?.length) {
75          AppStorage.setOrCreate(Constants.INIT_APP_INFO_LIST, initAppInfoList)
76        }
77      })
78  }
79
80  build() {
81    Column() {
82      TitleBar({
83        title: JSON.stringify($r('app.string.EntryAbility_label')),
84        isSplitMode: this.isSplitMode,
85      }).padding({
86        left: $r('sys.float.ohos_id_default_padding_start'),
87        right: $r('sys.float.ohos_id_default_padding_end')
88      })
89
90      Scroll(this.scroller) {
91        Column() {
92          //   Function access' list 'component
93          PrivacyProtectionListView({
94            menuViewState: autoMenuViewModel.getViewState(),
95            itemClickEvent: ((menuInfo: MenuInfo) => {
96              autoMenuViewModel.processIntent(new AutoMenuClickIntent(menuInfo));
97            })
98          })
99          //  Location information component
100          Row() {
101            Flex({ justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Center }) {
102              Text($r('app.string.locationServicesTab'))
103                .fontColor($r('sys.color.ohos_id_color_text_primary'))
104                .textAlign(TextAlign.Start)
105                .fontFamily('HarmonyHeiTi')
106                .fontWeight(FontWeight.Medium)
107                .fontSize($r('sys.float.ohos_id_text_size_body1'))
108              Row() {
109                Image($r('app.media.ic_settings_arrow'))
110                  .width($r('app.float.width_height_xs'))
111                  .height($r('app.float.width_height_m'))
112                  .align(Alignment.End)
113                  .fillColor($r('sys.color.ohos_id_color_fourth'))
114                  .draggable(false)
115              }
116              .padding({
117                top: $r('app.float.function_access_inside_list_padding'),
118                bottom: $r('app.float.function_access_inside_list_padding'),
119                left: $r('app.float.function_access_list_padding_top'),
120                right: $r('app.float.function_access_list_padding_top')
121              })
122              .margin({
123                left: $r('sys.float.ohos_id_text_margin_horizontal'),
124              })
125            }
126            .stateStyles({
127              pressed: this.PressedStyles,
128              normal: this.normalStyles
129            })
130            .padding({
131              top: $r('app.float.book_mark_padding'),
132              bottom: $r('app.float.book_mark_padding'),
133              left: $r('app.float.function_access_inside_list_padding'),
134              right: $r('app.float.function_access_inside_list_padding')
135            })
136            .width(Constants.WIDTH_HEIGHT_FULL_SCREEN)
137            .hoverEffect(HoverEffect.Highlight)
138            .borderRadius(ResourceUtil.getFloatNumber($r('sys.float.ohos_id_corner_radius_default_l')) - 4)
139            .onClick(async () => {
140              // Positioning service management
141              HiSysEventUtil.reportLocationClick('LOCATION')
142
143              router.pushUrl({
144                url: 'pages/locationServices'
145              }, router.RouterMode.Standard).then(() => {
146                Logger.info(TAG, 'succeeded in jumping to the locationServices page.')
147              }).catch((err: BusinessError) => {
148                Logger.error(TAG, `Failed to jump to the locationServices page.code is ${err.code}.message is ${err.message}`);
149              })
150            })
151          }
152          .width(Constants.WIDTH_HEIGHT_FULL_SCREEN)
153          .borderRadius($r('sys.float.ohos_id_corner_radius_default_l'))
154          .padding($r('app.float.function_access_list_padding'))
155          .backgroundColor($r('sys.color.ohos_id_color_list_card_bg'))
156        }
157      }
158      .height(Constants.WIDTH_HEIGHT_FULL_SCREEN)
159      .scrollable(ScrollDirection.Vertical)
160      .edgeEffect(EdgeEffect.Spring)
161      .margin({
162        bottom: $r('app.float.index_margin_bottom')
163      })
164      .padding({
165        left: $r('sys.float.ohos_id_default_padding_start'),
166        right: $r('sys.float.ohos_id_default_padding_end')
167      })
168      .align(Alignment.Top)
169    }
170    .height(Constants.WIDTH_HEIGHT_FULL_SCREEN)
171    .width(Constants.WIDTH_HEIGHT_FULL_SCREEN)
172    .backgroundColor($r('sys.color.ohos_id_color_sub_background'))
173
174  }
175}