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 image from '@ohos.multimedia.image'; 17import WallpaperMar from '@ohos.wallpaper' 18import {Log} from '@ohos/common' 19 20const TAG = 'ScreenLock-WallpaperViewModel' 21 22export default class WallpaperViewModel { 23 private wallpaperData: image.PixelMap = undefined 24 25 ViewModelInit(): void { 26 Log.showDebug(TAG, "ViewModelInit"); 27 this.getScreenLockWallpaper() 28 } 29 30 ViewModelDestroy(): void { 31 Log.showDebug(TAG, "ViewModelDestroy"); 32 this.freeScreenLockWallpaper(); 33 } 34 35 getWallpaperData() { 36 return this.wallpaperData; 37 } 38 39 private getScreenLockWallpaper() { 40 Log.showInfo(TAG, 'getScreenLockWallpaper'); 41 WallpaperMar.getPixelMap(WallpaperMar.WallpaperType.WALLPAPER_LOCKSCREEN, (error, data) => { 42 if (error != undefined && error != null) { 43 Log.showError(TAG, 'getScreenLockWallpaper error:' + JSON.stringify(error)); 44 this.getScreenLockWallpaper() 45 } else { 46 Log.showDebug(TAG, 'getScreenLockWallpaper data:' + JSON.stringify(data)); 47 this.wallpaperData = data 48 } 49 }) 50 } 51 52 private freeScreenLockWallpaper() { 53 Log.showInfo(TAG, 'free ScreenLockWallpaper'); 54 if (typeof this.wallpaperData === 'undefined' || this.wallpaperData == null) { 55 return; 56 } 57 this.wallpaperData.release().then(() => { 58 Log.showDebug(TAG, 'release succeed'); 59 }).catch((err) => { 60 Log.showDebug(TAG, `release failed ${err}`); 61 }) 62 } 63} 64