• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import extension from '@ohos.app.ability.ServiceExtensionAbility'
2import window from '@ohos.window';
3import display from '@ohos.display';
4import bundle from '@ohos.bundle';
5import router from '@system.router';
6import deviceInfo from '@ohos.deviceInfo';
7
8let TAG = "[DeviceManagerUI:PinCode]==>"
9
10export default class ServiceExtAbility extends extension {
11    private lineNums = 0
12
13    onCreate(want) {
14        globalThis.extensionContext = this.context
15        globalThis.windowNum = 0
16        this.getShareStyle()
17    }
18
19    onRequest(want, startId) {
20        console.log(TAG + "onRequest execute" + JSON.stringify(want.parameters))
21        // 每次startAbility拉起页面的时候, 都传递want
22        globalThis.abilityWant = want
23        console.log(TAG + "onRequest execute" + JSON.stringify(globalThis.abilityWant.parameters))
24
25        // 每次调用startAbility时可以在这里创建窗口
26        display.getDefaultDisplay().then(dis => {
27            // 获取像素密度系数
28            let density = dis.densityPixels
29            let homeHeight = 48
30            let dialogRect = {
31                left: (dis.width - (globalThis.style["shareWidth"] * density)) / 2,
32                top: (dis.height - (globalThis.style["shareHeight"] * density)) / 2 -
33                (globalThis.style["shareHeight"] * density) / 6,
34                width: globalThis.style["shareWidth"] * density,
35                height: globalThis.style["shareHeight"] * density * 0.8
36            }
37            this.createWindow("picker Dialog:" + startId, window.WindowType.TYPE_FLOAT, dialogRect)
38        })
39    }
40
41
42    getShareStyle() {
43        globalThis.style = {}
44        if (deviceInfo.deviceType == "phone" || deviceInfo.deviceType == "default") {
45            // 页面的默认类型是手机
46            this.lineNums = 8
47            globalThis.style["shareWidth"] = 360
48            globalThis.style["shareHeight"] = 355
49            globalThis.style["shareCardWidth"] = 336
50            globalThis.style["shareCardHeight"] = 97
51            globalThis.style["shareCardRadius"] = 16
52            globalThis.style["shareContentMargin"] = { top: 16, right: 43.5, bottom: 6, left: 16 }
53            globalThis.style["shareCardTextLeft"] = 12
54            globalThis.style["shareCardTextTop"] = 6
55            globalThis.style["shareBackTop"] = 0
56            globalThis.style["shareCardTextWidth"] = 236
57            globalThis.style["shareCardSubTop"] = 3
58            globalThis.style["shareCardBtnLeft"] = 238
59            globalThis.style["swiperHeight"] = 200
60            globalThis.style["swiperSelectMarginTop"] = 8
61        } else {
62            // pad类型
63            this.lineNums = 6
64            globalThis.style["shareWidth"] = 519
65            globalThis.style["shareHeight"] = 256
66            globalThis.style["shareCardWidth"] = 495
67            globalThis.style["shareCardHeight"] = 87
68            globalThis.style["shareCardRadius"] = 24
69            globalThis.style["shareContentMargin"] = { top: 16, right: 24, bottom: 8, left: 16 }
70            globalThis.style["shareCardTextLeft"] = 16
71            globalThis.style["shareCardTextTop"] = 3
72            globalThis.style["shareBackTop"] = 6
73            globalThis.style["shareCardTextWidth"] = 380
74            globalThis.style["shareCardSubTop"] = 2
75            globalThis.style["shareCardBtnLeft"] = 368
76            globalThis.style["swiperHeight"] = 115
77            globalThis.style["swiperSelectMarginTop"] = 16
78        }
79    }
80
81    onDestroy() {
82        console.log(TAG + "ServiceExtAbility destroyed")
83    }
84
85
86    private async createWindow(name: string, windowType: number, rect) {
87        console.log(TAG + "createWindow execute")
88        try {
89            const win = await window.create(this.context, name, windowType)
90            globalThis.extensionWin = win
91            await win.moveTo(rect.left, rect.top)
92            await win.resetSize(rect.width, rect.height)
93            await win.loadContent('pages/PinDialog')
94            await win.show()
95            globalThis.windowNum ++
96            console.log(TAG + "window create successfully")
97        } catch {
98            console.info(TAG + "window create failed")
99        }
100    }
101
102
103    private async getSupportSharingApps() {
104        // 注意:每次获取应用时都清空一次shareHapList
105        globalThis.shareHapList = []
106        try {
107            const apps = await bundle.queryAbilityByWant({action: "ohos.want.action.select"}, bundle.AbilityType.PAGE)
108            let shareHap = []
109            for (let i = 0; i < apps.length; i++) {
110                let hap = {}
111                hap['ability'] = apps[i].name
112                hap['bundle'] = apps[i].bundleName
113                const label = await bundle.getAbilityLabel(apps[i].bundleName, apps[i].name)
114                const icon = await bundle.getAbilityIcon(apps[i].bundleName, apps[i].name)
115                hap['name'] = label
116                hap['icon'] = icon
117                shareHap.push(hap)
118
119                // 每一页最多App数
120                if (i % this.lineNums == (this.lineNums - 1) || i == apps.length - 1) {
121                    globalThis.shareHapList.push(shareHap)
122                    shareHap = []
123                }
124            }
125        } catch {
126            console.log(TAG + "support sharing apps failed")
127        }
128    }
129};