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 package com.android.wallpaper.picker.preview.ui.util
17 
18 import android.app.WallpaperInfo
19 import android.app.WallpaperManager
20 import android.app.WallpaperManager.FLAG_LOCK
21 import android.app.WallpaperManager.FLAG_SYSTEM
22 import android.content.Intent
23 import android.content.pm.ApplicationInfo
24 import android.text.TextUtils
25 import com.android.wallpaper.picker.LivePreviewFragment
26 import javax.inject.Inject
27 import javax.inject.Singleton
28 
29 /** The utility class for live wallpaper that can be deleted */
30 @Singleton
31 class LiveWallpaperDeleteUtil @Inject constructor(private val wallpaperManager: WallpaperManager) {
32 
getDeleteActionIntentnull33     fun getDeleteActionIntent(wallpaperInfo: WallpaperInfo): Intent? {
34         val deleteAction = getDeleteAction(wallpaperInfo)
35         if (TextUtils.isEmpty(deleteAction)) {
36             return null
37         }
38         val deleteActionIntent = Intent(deleteAction)
39         deleteActionIntent.setPackage(wallpaperInfo.packageName)
40         deleteActionIntent.putExtra(LivePreviewFragment.EXTRA_LIVE_WALLPAPER_INFO, wallpaperInfo)
41         return deleteActionIntent
42     }
43 
getDeleteActionnull44     private fun getDeleteAction(wallpaperInfo: WallpaperInfo): String? {
45         val currentInfo = wallpaperManager.getWallpaperInfo(FLAG_SYSTEM)
46         val currentLockInfo = wallpaperManager.getWallpaperInfo(FLAG_LOCK)
47         val serviceInfo = wallpaperInfo.serviceInfo
48         val appInfo = serviceInfo.applicationInfo
49         val isPackagePreInstalled =
50             appInfo != null && appInfo.flags and ApplicationInfo.FLAG_SYSTEM != 0
51         if (!isPackagePreInstalled) {
52             // This wallpaper is not installed before
53             return null
54         }
55 
56         // A currently set Live wallpaper should not be deleted.
57         val currentService = currentInfo?.serviceInfo
58         if (currentService != null && TextUtils.equals(serviceInfo.name, currentService.name)) {
59             return null
60         }
61         val currentLockService = currentLockInfo?.serviceInfo
62         if (
63             currentLockService != null &&
64                 TextUtils.equals(serviceInfo.name, currentLockService.name)
65         ) {
66             return null
67         }
68         val metaData = serviceInfo.metaData
69         return metaData?.getString(LivePreviewFragment.KEY_ACTION_DELETE_LIVE_WALLPAPER)
70     }
71 }
72