xref: /aosp_15_r20/cts/common/device-side/bedstead/nene/src/main/java/com/android/bedstead/nene/wallpaper/Wallpaper.kt (revision b7c941bb3fa97aba169d73cee0bed2de8ac964bf)
1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
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 
17 package com.android.bedstead.nene.wallpaper;
18 
19 import android.app.WallpaperManager
20 import android.graphics.Bitmap
21 import com.android.bedstead.nene.TestApis
22 import com.android.bedstead.nene.annotations.Experimental
23 import com.android.bedstead.nene.exceptions.NeneException
24 import com.android.bedstead.permissions.CommonPermissions.SET_WALLPAPER
25 import com.android.bedstead.nene.utils.BitmapUtils
26 import java.io.InputStream
27 
28 /** Test APIs related to wallpaper. */
29 @Experimental
30 object Wallpaper {
31 
32     private val sContext = TestApis.context().instrumentedContext()
33     private val wallpaperManager = sContext.getSystemService(WallpaperManager::class.java)!!
34 
35     /**
36      * Get the {@code Bitmap} value of the current wallpaper.
37      */
getBitmapnull38     fun getBitmap(): Bitmap? {
39         try {
40             return BitmapUtils.getWallpaperBitmap(sContext)
41         } catch (e: Exception) {
42             throw NeneException("Unable to get current wallpaper", e);
43         }
44     }
45 
46     /**
47      * See {@link WallpaperManager#setBitmap(Bitmap)}.
48      */
setBitmapnull49     fun setBitmap(bitmap: Bitmap) {
50         TestApis.permissions().withPermission(SET_WALLPAPER).use {
51             try {
52                 wallpaperManager.setBitmap(bitmap)
53             } catch (e: Exception) {
54                 throw NeneException("Unable to set wallpaper to the provided bitmap", e);
55             }
56         }
57     }
58 
59     /**
60      * See {@link WallpaperManager#setStream(InputStream)}.
61      */
setStreamnull62     fun setStream(inputStream: InputStream) {
63         TestApis.permissions().withPermission(SET_WALLPAPER).use {
64             try {
65                 wallpaperManager.setStream(inputStream);
66             } catch (e: Exception) {
67                 throw NeneException("Unable to set wallpaper to the provided stream", e);
68             }
69         }
70     }
71 
72 }
73