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.wallpaper.picker.preview.data.util
18 
19 import android.app.Activity
20 import androidx.activity.result.ActivityResultLauncher
21 import androidx.activity.result.IntentSenderRequest
22 import com.android.wallpaper.picker.data.WallpaperModel.LiveWallpaperModel
23 import com.android.wallpaper.picker.data.WallpaperModel.StaticWallpaperModel
24 import kotlinx.coroutines.flow.Flow
25 
26 /**
27  * Handles the download process of a downloadable wallpaper. This downloader should be aware of the
28  * Activity's lifecycle.
29  */
30 interface LiveWallpaperDownloader {
31 
32     val isDownloaderReady: Flow<Boolean>
33 
34     interface LiveWallpaperDownloadListener {
onDownloadSuccessnull35         fun onDownloadSuccess(wallpaperModel: LiveWallpaperModel)
36 
37         fun onDownloadFailed()
38     }
39 
40     /**
41      * Initializes the downloadable service. This needs to be called when [Activity.onCreate] and
42      * before calling [downloadWallpaper].
43      */
44     fun initiateDownloadableService(
45         activity: Activity,
46         wallpaperData: StaticWallpaperModel,
47         intentSenderLauncher: ActivityResultLauncher<IntentSenderRequest>,
48     )
49 
50     /**
51      * Clean up the underlying downloadable service. This needs to be called when
52      * [Activity.onDestroy].
53      */
54     fun cleanup()
55 
56     fun downloadWallpaper(listener: LiveWallpaperDownloadListener)
57 
58     /** @return True if there is a confirm cancel download dialog from the download service. */
59     fun cancelDownloadWallpaper(): Boolean
60 }
61