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