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 app from '@system.app';
17import mediaquery from '@ohos.mediaquery'
18import { Log } from '@ohos/common';
19import { Trace } from '@ohos/common';
20import { CommonConstants } from '@ohos/common';
21import { EventConstants } from '@ohos/common';
22import { windowManager } from '@ohos/common';
23import { localEventManager } from '@ohos/common';
24import { SettingsModel, LayoutViewModel } from '@ohos/common';
25import { RemoteWindowWrapper } from '@ohos/common/component';
26import { SmartDock } from '@ohos/smartdock/component';
27import { PageDesktopLayout } from '@ohos/pagedesktop/component';
28import { BigFolderConstants, BigFolderStyleConfig } from '@ohos/bigfolder';
29import { FolderOpenComponent } from '@ohos/bigfolder/component';
30import PadStage from '../common/PadStage';
31import StyleConstants from '../common/constants/StyleConstants';
32import { FormStyleConfig } from '@ohos/form';
33import AppCenterGridStyleConfig from '@ohos/appcenter/src/main/ets/default/common/AppCenterGridStyleConfig';
34import PadPageDesktopGridStyleConfig from '../common/PadPageDesktopGridStyleConfig';
35import { SmartDockStyleConfig } from '@ohos/smartdock';
36import display from '@ohos.display';
37
38const RAW_IMAGE_CACHE_SIZE = 20000000;
39const TAG = 'EntryView';
40
41interface LocalEventListener {
42  onReceiveEvent: (event: string, params: string) => void;
43}
44
45@Entry
46@Component
47struct EntryView {
48  @StorageLink('screenWidth') screenWidth: number = 0;
49  @StorageLink('screenHeight') @Watch('updateScreenInfo') screenHeight: number = 0;
50  @StorageLink('deviceType') deviceType: string = CommonConstants.PAD_DEVICE_TYPE;
51  @State workSpaceWidth: number = 0;
52  @State workSpaceHeight: number = 0;
53  @State dockHeight: number = 0;
54  private mStage: PadStage = new PadStage();
55  private navigationBarStatus: string | undefined;
56  private mOrientationListener = mediaquery.matchMediaSync('(orientation: landscape)');
57  @State mAppScaleX: number = 1.0;
58  @State mAppScaleY: number = 1.0;
59  @State mAppAlpha: number = 1.0;
60  @StorageLink('IsSetImageRawDataCacheSize') isSetImageRawDataCacheSize?: boolean = undefined;
61  async onPortrait(mediaQueryResult: mediaquery.MediaQueryResult) {
62    if (mediaQueryResult.matches) {
63      Log.showInfo(TAG, 'screen change to landscape');
64      AppStorage.setOrCreate('isPortrait', false);
65    } else {
66      Log.showInfo(TAG, 'screen change to portrait');
67      AppStorage.setOrCreate('isPortrait', true);
68    }
69    let dis: display.Display | null = null;
70    try {
71      dis = display.getDefaultDisplaySync();
72      Log.showInfo(TAG, `change to display: ${JSON.stringify(dis)}`);
73      AppStorage.setOrCreate('screenWidth', px2vp(dis.width));
74      AppStorage.setOrCreate('screenHeight', px2vp(dis.height));
75      Log.showDebug(TAG, `screenWidth and screenHeight: ${AppStorage.get('screenWidth')},${AppStorage.get('screenHeight')}`);
76    } catch (err) {
77      Log.showError(TAG, `display.getDefaultDisplaySync err: ${JSON.stringify(err)}`)
78    }
79  }
80
81  aboutToAppear(): void {
82    Log.showInfo(TAG, 'aboutToAppear');
83    this.mStage.onCreate();
84    this.navigationBarStatus = SettingsModel.getInstance().getValue();
85    this.getWindowSize();
86    this.updateScreenSize();
87    this.mOrientationListener.on('change', this.onPortrait);
88    this.registerPageDesktopNavigatorStatusChangeEvent(this.mLocalEventListener);
89  }
90
91  registerPageDesktopNavigatorStatusChangeEvent(listener: LocalEventListener): void {
92    localEventManager.registerEventListener(listener, [
93      EventConstants.EVENT_NAVIGATOR_BAR_STATUS_CHANGE,
94      EventConstants.EVENT_ANIMATION_START_APPLICATION,
95      EventConstants.EVENT_ANIMATION_CLOSE_APPLICATION
96    ]);
97  }
98
99  private readonly mLocalEventListener: LocalEventListener = {
100    onReceiveEvent: (event: string, params: string) => {
101      Log.showInfo(TAG, `receive event: ${event}, params: ${params}`);
102      if (event === EventConstants.EVENT_NAVIGATOR_BAR_STATUS_CHANGE) {
103        this.navigationBarStatus = params;
104        this.updateScreenInfo();
105      } else if (event === EventConstants.EVENT_ANIMATION_START_APPLICATION) {
106        this.startAppAnimation();
107      } else if (event === EventConstants.EVENT_ANIMATION_CLOSE_APPLICATION) {
108        this.closeAppAnimation();
109      }
110    }
111  };
112
113  private startAppAnimation(): void {
114    animateTo({
115      duration: 500,
116      curve: Curve.Friction,
117      onFinish: () => {},
118    }, () => {
119      this.mAppScaleX = 0.95;
120      this.mAppScaleY = 0.95;
121    })
122  }
123
124  private closeAppAnimation(): void {
125    animateTo({
126      duration: 370,
127      delay: 0,
128      curve: Curve.Friction,
129    }, () => {
130      this.mAppScaleX = 1.0;
131      this.mAppScaleY = 1.0;
132    })
133  }
134
135  private updateScreenInfo(): void {
136    Log.showInfo(TAG, 'updateScreenInfo');
137    if (this.screenWidth !== 0 && this.screenHeight !== 0) {
138      LayoutViewModel.getInstance().initScreen(this.navigationBarStatus);
139      SmartDockStyleConfig.getInstance();
140      PadPageDesktopGridStyleConfig.getInstance();
141      AppCenterGridStyleConfig.getInstance();
142      BigFolderStyleConfig.getInstance();
143      FormStyleConfig.getInstance();
144      this.updateScreenSize();
145    }
146  }
147
148  onPageShow(): void {
149    Log.showInfo(TAG, 'onPageShow');
150    if (typeof this.isSetImageRawDataCacheSize === 'undefined') {
151      Log.showInfo(TAG, 'onPageShow setImageRawDataCacheSize');
152
153      // If cannot compile this, comment next line or add following code into Class App in "@system.app.d.ts":
154      // static setImageRawDataCacheSize(value: number): void;
155      app.setImageRawDataCacheSize(RAW_IMAGE_CACHE_SIZE);
156      app.setImageCacheCount(100);
157      this.isSetImageRawDataCacheSize = true;
158    }
159  }
160
161  private getWindowSize(): void {
162    try {
163      this.screenWidth = px2vp(windowManager.getWindowWidth());
164      this.screenHeight = px2vp(windowManager.getWindowHeight());
165      AppStorage.setOrCreate('screenWidth', this.screenWidth);
166      AppStorage.setOrCreate('screenHeight', this.screenHeight);
167    } catch (error) {
168      Log.showError(TAG, `getWindowWidth or getWindowHeight error: ${error}`);
169    }
170  }
171
172  private updateScreenSize(): void {
173    this.workSpaceWidth = this.screenWidth;
174    this.workSpaceHeight = LayoutViewModel.getInstance().getWorkSpaceHeight() as number;
175    this.dockHeight = LayoutViewModel.getInstance().getDockHeight() as number;
176    AppStorage.setOrCreate('workSpaceWidth', this.workSpaceWidth);
177    AppStorage.setOrCreate('workSpaceHeight', this.workSpaceHeight);
178    AppStorage.setOrCreate('dockHeight', this.dockHeight);
179    Log.showDebug(TAG, `updateScreenSize product: ${this.deviceType}, screenWidth: ${this.screenWidth}, screenHeight: ${this.screenHeight}, workSpaceWidth: ${this.workSpaceWidth}, workSpaceHeight: ${this.workSpaceHeight}, dockHeight: ${this.dockHeight}`);
180  }
181
182  aboutToDisappear(): void {
183    this.mOrientationListener.off('change', this.onPortrait);
184    this.mStage.onDestroy();
185  }
186
187  onBackPress(): boolean {
188    Log.showInfo(TAG, 'onBackPress');
189    ContextMenu.close();
190    AppStorage.setOrCreate('overlayMode', CommonConstants.OVERLAY_TYPE_HIDE);
191    AppStorage.setOrCreate('openFolderStatus', BigFolderConstants.OPEN_FOLDER_STATUS_CLOSE);
192    AppStorage.setOrCreate('selectDesktopAppItem', '');
193    this.closeRecentDockPopup();
194    return true;
195  }
196
197  private closeRecentDockPopup(): void {
198    let num: number = AppStorage.get('sysUiRecentOnClickEvent') as number;
199    AppStorage.setOrCreate('sysUiRecentOnClickEvent', ++num);
200  }
201
202  private buildLog(): boolean {
203    Log.showInfo(TAG, 'buildLog');
204    return true;
205  }
206
207  build() {
208    Stack() {
209      if (this.buildLog()) {}
210      Column() {
211        Column() {
212          PageDesktopLayout();
213        }
214        .alignItems(HorizontalAlign.Center)
215        .justifyContent(FlexAlign.Center)
216        .height(this.workSpaceHeight)
217        .onAreaChange((oldValue: Area, newValue: Area) => {
218          Log.showDebug(TAG, `onAreaChange navigationBarStatus: ${this.navigationBarStatus}`);
219          if (JSON.stringify(oldValue) == JSON.stringify(newValue)) {
220            return;
221          }
222          if (this.navigationBarStatus == '1') {
223            setTimeout(() => {
224              SettingsModel.getInstance().setValue(this.navigationBarStatus);
225            }, 30);
226          }
227        })
228
229        Column() {
230          SmartDock({
231            showAppCenter: () => {
232              Trace.start(Trace.CORE_METHOD_START_APP_CENTER);
233              windowManager.createWindowWithName(windowManager.APP_CENTER_WINDOW_NAME, windowManager.DESKTOP_RANK);
234            }
235          });
236        }
237        .height(this.dockHeight)
238      }
239      .width('100%')
240      .height('100%')
241      .scale({ x: this.mAppScaleX, y: this.mAppScaleY })
242      .opacity(this.mAppAlpha)
243
244      if ((AppStorage.get<number>('openFolderStatus') as number) !== BigFolderConstants.OPEN_FOLDER_STATUS_CLOSE) {
245        FolderOpenComponent();
246      }
247      RemoteWindowWrapper();
248    }
249    .backgroundImage(StyleConstants.DEFAULT_BACKGROUND_IMAGE)
250    .backgroundImageSize(ImageSize.Cover)
251    .backgroundImagePosition(Alignment.Center)
252    .width('100%')
253    .height('100%')
254  }
255}
256