1// @ts-nocheck
2/**
3 * Copyright (c) 2022 Huawei Device Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import windowManager from '@ohos.window'
18import Extension from '@ohos.WallpaperExtension'
19import wallPaper from '@ohos.wallpaper'
20
21const MODULE_TAG = 'ExtWallpaper : ';
22
23export default class WallpaperExtAbility extends Extension {
24    onCreated(want) {
25        console.info(MODULE_TAG + 'ability on created start');
26        windowManager.create(this.context, "wallpaper", 2000).then((win) => {
27            win.loadContent("pages/index").then(() => {
28                console.info(MODULE_TAG, "wallpaper window loadContent in then!")
29                win.show().then(() => {
30                    win.setFullScreen(true).then(() => {
31                        console.info(MODULE_TAG, "set full");
32                    });
33                    console.info(MODULE_TAG, "then begin window show in then!");
34                })
35            })
36        }, (error) => {
37            Log.showError(TAG, name + " window createFailed, error.code = " + error.code)
38        })
39        //        super.setUiContent("pages/index");
40        this.initWallpaperImage();
41        console.info(MODULE_TAG + 'ability on created end');
42    }
43
44    onWallpaperChanged(wallpaperType) {
45        console.info(MODULE_TAG + 'ability on wallpaper changed start, type is : ' + wallpaperType);
46        if (wallPaper) {
47            this.sendPixelMapData();
48        }
49        console.info(MODULE_TAG + 'ability on wallpaper changed end');
50    }
51
52    onDestroy() {
53        console.info(MODULE_TAG + 'ability on destroy');
54    }
55
56    initWallpaperImage() {
57        console.info(MODULE_TAG + 'ability init wallpaper image start');
58        if (!wallPaper) {
59            console.info(MODULE_TAG + 'ability init wallpaper image failed as wallpaper is null');
60            return;
61        }
62        this.sendPixelMapData();
63        console.info(MODULE_TAG + 'ability init wallpaper image end');
64    }
65
66    sendPixelMapData() {
67        console.info(MODULE_TAG + 'ability send pixel map data start');
68        wallPaper.getPixelMap(0, (err, data) => {
69            console.info(MODULE_TAG + 'ability get pixel map, err: ' + JSON.stringify(err) +
70            " data: " + JSON.stringify(data));
71            AppStorage.SetOrCreate('slPixelData', data);
72            console.info(MODULE_TAG + 'ability set or create end');
73        });
74        console.info(MODULE_TAG + 'ability send pixel map data end');
75    }
76};
77