• 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 display from '@ohos.display'
17import router from '@ohos.router';
18import Logger from '../common/Logger'
19import { Warn } from '../common/Warn'
20
21const TAG: string = '[ScreenDetector]'
22const VIRTUAL_SCREEN_MAX: number = 5
23const VIRTUAL_SCREEN_MIN: number = 0
24
25var deviceScreens = new Object
26
27@Component
28export struct Detector {
29  @State allScreens: display.Display[] = []
30  private screenIds: number[] = []
31  private errorMsg: Resource = undefined
32  private limit: string = ""
33  private virtualScrNum: number = 0
34  private realScrNum: number = 0
35  errorDialog: CustomDialogController = new CustomDialogController({
36    builder: Warn({
37      errMsg: this.errorMsg,
38      limit: this.limit
39    }),
40    autoCancel: true,
41    alignment: DialogAlignment.Default
42  })
43  async refreshScreen() {
44    await this.getAllScreens()
45    Logger.info(TAG, 'Screen refresh. The Screen number is ' + this.allScreens.length)
46  }
47  registerListener() {
48    display.on('add', (id) => {
49      this.refreshScreen()
50    })
51    display.on('remove', (id) => {
52      this.refreshScreen()
53    })
54  }
55  async getAllScreens() {
56    this.allScreens = await display.getAllDisplay()
57    this.realScrNum = this.allScreens.length
58    let defaultScreen = await display.getDefaultDisplay()
59    for (let i = 0; i < this.virtualScrNum; i++) {
60      this.allScreens.push(defaultScreen);
61    }
62    Logger.info(TAG, `All Screens = ${JSON.stringify(this.allScreens)}`)
63  }
64  aboutToAppear() {
65    this.registerListener()
66    this.getAllScreens()
67    this.virtualScrNum = 0
68  }
69  build() {
70    Column() {
71        Flex({ wrap: FlexWrap.Wrap }) {
72          ForEach(this.allScreens, (item, index) => {
73            Row() {
74              if (index < this.realScrNum) {
75                Text('R' + JSON.stringify(index))
76              } else {
77                Text('V' + JSON.stringify(index - this.realScrNum))
78              }
79            }
80            .margin({ right: '10%', bottom: '5%' })
81            .justifyContent(FlexAlign.Center)
82            .backgroundColor('#5dbbff')
83            .height('12%').width('18%')
84            .onClick(async () => {
85              router.push({
86                url: 'pages/ScreenInfo',
87                params: {
88                  scrInfo: item
89                }
90              });
91            }).id('test_screen' + index)
92          })
93        }.height('50%').width('100%').padding(10)
94
95        Flex({ alignItems: ItemAlign.End, justifyContent: FlexAlign.End }) {
96          Button($r('app.string.add_screen'))
97            .fontSize(18)
98            .margin({ top: 10, left: 4, right: 4, bottom: 4 })
99            .onClick(async () => {
100              if (this.virtualScrNum < VIRTUAL_SCREEN_MAX) {
101                let defaultScreen = await display.getDefaultDisplay()
102                this.allScreens.push(defaultScreen)
103                this.virtualScrNum = this.virtualScrNum + 1
104                Logger.info(TAG, `add a virtual screen success!`)
105              } else {
106                this.errorMsg = $r('app.string.err_max_screen')
107                this.limit = JSON.stringify(VIRTUAL_SCREEN_MAX)
108                this.errorDialog.open()
109              }
110            }).id('test_add')
111
112          Button($r('app.string.remove_screen'))
113            .fontSize(18)
114            .margin({ top: 10, left: 4, right: 4, bottom: 4 })
115            .onClick(async () => {
116              if (this.virtualScrNum > VIRTUAL_SCREEN_MIN) {
117                this.allScreens.pop();
118                this.virtualScrNum = this.virtualScrNum - 1
119                Logger.info(TAG, `remove a virtual screen success!`)
120              } else {
121                this.errorMsg = $r('app.string.err_min_screen')
122                this.limit = JSON.stringify(VIRTUAL_SCREEN_MIN)
123                this.errorDialog.open()
124              }
125            }).id('test_cut')
126        }.height('50%')
127    }.height('90%')
128  }
129}