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 { Log } from '@ohos/common';
17import { CommonConstants } from '@ohos/common';
18import { EventConstants } from '@ohos/common';
19import { windowManager } from '@ohos/common';
20import { localEventManager } from '@ohos/common';
21import { SettingsModel } from '@ohos/common';
22import { LayoutViewModel } from '@ohos/common';
23import { SmartDock } from '@ohos/smartdock/component';
24import { PageDesktopLayout } from '@ohos/pagedesktop/component';
25import { FolderOpenComponent } from '@ohos/bigfolder/component';
26import { BigFolderConstants, BigFolderStyleConfig } from '@ohos/bigfolder';
27import PhoneStage from '../common/PhoneStage';
28import StyleConstants from '../common/constants/StyleConstants';
29import { SmartDockStyleConfig } from '@ohos/smartdock';
30import PhonePageDesktopGridStyleConfig from '../common/PhonePageDesktopGridStyleConfig';
31import { FormStyleConfig } from '@ohos/form';
32import systemParameter from '@ohos.systemparameter';
33
34const TAG = 'EntryView';
35
36interface LocalEventListener {
37  onReceiveEvent: (event: string, params: string) => void;
38}
39
40@Entry
41@Component
42struct EntryView {
43  @StorageLink('screenWidth') screenWidth: number = 0;
44  @StorageLink('screenHeight') @Watch('updateScreenInfo') screenHeight: number = 0;
45  @StorageLink('deviceType') deviceType: string = CommonConstants.DEFAULT_DEVICE_TYPE;
46  @StorageLink('loaded') loaded: boolean = false;
47  @State workSpaceWidth: number = 0;
48  @State workSpaceHeight: number = 0;
49  @State dockHeight: number = 0;
50  private mStage: PhoneStage = new PhoneStage();
51  private navigationBarStatus: string | undefined;
52
53  onPageShow(): void {
54    Log.showInfo(TAG, 'onPageShow');
55    let firstActivate:boolean | undefined = AppStorage.get('firstActivate');
56    if (firstActivate) {
57      this.voteBootEvent();
58    }
59  }
60
61  onPageHide(): void {
62    Log.showInfo(TAG, 'onPageHide');
63  }
64
65  aboutToAppear(): void {
66    Log.showInfo(TAG, 'aboutToAppear');
67    this.mStage.onCreate();
68    this.navigationBarStatus = SettingsModel.getInstance().getValue();
69    this.getWindowSize();
70    this.updateScreenSize();
71
72    this.registerPageDesktopNavigatorStatusChangeEvent(this.mLocalEventListener);
73
74  }
75
76  registerPageDesktopNavigatorStatusChangeEvent(listener: LocalEventListener): void {
77    localEventManager.registerEventListener(listener, [EventConstants.EVENT_NAVIGATOR_BAR_STATUS_CHANGE]);
78  }
79
80  private voteBootEvent(): void {
81    Log.showInfo(TAG, 'voteBootEvent for launcher begin');
82    try {
83      AppStorage.setOrCreate('firstActivate', false);
84      AppStorage.setOrCreate('loaded', true);
85      systemParameter.setSync('bootevent.launcher.ready', 'true');
86    } catch (err) {
87      Log.showError(TAG, `set voteBootEvent err, ${JSON.stringify(err)}`);
88    }
89    Log.showInfo(TAG, 'voteBootEvent for launcher end');
90  }
91
92  private readonly mLocalEventListener: LocalEventListener = {
93    onReceiveEvent: (event: string, params: string) => {
94      Log.showDebug(TAG, `receive event: ${event}, params: ${params}`);
95      if (event === EventConstants.EVENT_NAVIGATOR_BAR_STATUS_CHANGE) {
96        this.navigationBarStatus = params;
97        this.updateScreenInfo();
98      }
99    }
100  };
101
102  aboutToDisappear(): void {
103    this.mStage.onDestroy();
104    Log.showInfo(TAG, 'aboutToDisappear');
105  }
106
107  onBackPress(): boolean {
108    Log.showInfo(TAG, 'onBackPress');
109    ContextMenu.close();
110    AppStorage.setOrCreate('overlayMode', CommonConstants.OVERLAY_TYPE_HIDE);
111    AppStorage.setOrCreate('openFolderStatus', BigFolderConstants.OPEN_FOLDER_STATUS_CLOSE);
112    return true;
113  }
114
115  private updateScreenInfo(): void {
116    Log.showDebug(TAG, 'updateScreenInfo');
117    if (this.screenWidth != 0 && this.screenHeight != 0) {
118      LayoutViewModel.getInstance().initScreen(this.navigationBarStatus);
119      SmartDockStyleConfig.getInstance();
120      PhonePageDesktopGridStyleConfig.getInstance();
121      BigFolderStyleConfig.getInstance();
122      FormStyleConfig.getInstance();
123      this.updateScreenSize();
124    }
125  }
126
127  private getWindowSize(): void {
128    try {
129      this.screenWidth = px2vp(windowManager.getWindowWidth());
130      this.screenHeight = px2vp(windowManager.getWindowHeight());
131      AppStorage.setOrCreate('screenWidth', this.screenWidth);
132      AppStorage.setOrCreate('screenHeight', this.screenHeight);
133    } catch (error) {
134      Log.showError(TAG, `getWindowWidth or getWindowHeight error: ${error}`);
135    }
136  }
137
138  private updateScreenSize(): void {
139    this.workSpaceWidth = this.screenWidth;
140    this.workSpaceHeight = LayoutViewModel.getInstance().getWorkSpaceHeight() as number;
141    this.dockHeight = LayoutViewModel.getInstance().getDockHeight() as number;
142    AppStorage.setOrCreate('workSpaceWidth', this.workSpaceWidth);
143    AppStorage.setOrCreate('workSpaceHeight', this.workSpaceHeight);
144    AppStorage.setOrCreate('dockHeight', this.dockHeight);
145    Log.showDebug(TAG, `updateScreenSize product: ${this.deviceType}, screenWidth: ${this.screenWidth}, screenHeight: ${this.screenHeight},
146      workSpaceWidth: ${this.workSpaceWidth}, workSpaceHeight: ${this.workSpaceHeight}, dockHeight: ${this.dockHeight}`);
147  }
148
149  build() {
150    Stack() {
151      if (this.loaded) {
152        Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Start }) {
153          Column() {
154            PageDesktopLayout();
155          }
156          .height(this.workSpaceHeight)
157          .onAreaChange((oldValue: Area, newValue: Area) => {
158            Log.showDebug(TAG, `onAreaChange navigationBarStatus: ${this.navigationBarStatus}`);
159            if (JSON.stringify(oldValue) == JSON.stringify(newValue)) {
160              return;
161            }
162            if (this.navigationBarStatus == '1') {
163              setTimeout(() => {
164                SettingsModel.getInstance().setValue(this.navigationBarStatus);
165              }, 50)
166            }
167          })
168
169          Column() {
170            SmartDock();
171          }
172          .height(this.dockHeight)
173        }
174
175        FolderOpenComponent();
176      }
177    }
178    .backgroundImage(StyleConstants.DEFAULT_BACKGROUND_IMAGE)
179    .backgroundImageSize(ImageSize.Cover)
180    .backgroundImagePosition(Alignment.Center)
181    .width('100%')
182    .height('100%')
183  }
184}
185