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.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.Context 23 import android.content.Intent 24 import android.content.pm.ApplicationInfo 25 import android.text.TextUtils 26 import android.util.Log 27 import com.android.wallpaper.picker.LivePreviewFragment 28 29 /** The utility class for live wallpaper the can be deleted */ 30 object DeletableUtils { 31 private const val TAG = "DeletableUtils" 32 33 /** If a wallpaper can be deleted. */ 34 @JvmStatic canBeDeletednull35 fun canBeDeleted(context: Context, wallpaperInfo: WallpaperInfo): Boolean { 36 return !TextUtils.isEmpty(getDeleteAction(context, wallpaperInfo)) 37 } 38 39 /** Delete a wallpaper. */ 40 @JvmStatic deleteLiveWallpapernull41 fun deleteLiveWallpaper( 42 context: Context, 43 wallpaperInfo: com.android.wallpaper.model.WallpaperInfo 44 ) { 45 val deleteIntent = getDeleteActionIntent(context, wallpaperInfo.wallpaperComponent) 46 if (deleteIntent != null) { 47 context.startService(deleteIntent) 48 } 49 } 50 getDeleteActionIntentnull51 private fun getDeleteActionIntent(context: Context, wallpaperInfo: WallpaperInfo): Intent? { 52 val deleteAction = getDeleteAction(context, wallpaperInfo) 53 if (TextUtils.isEmpty(deleteAction)) { 54 return null 55 } 56 val deleteActionIntent = Intent(deleteAction) 57 deleteActionIntent.setPackage(wallpaperInfo.packageName) 58 deleteActionIntent.putExtra(LivePreviewFragment.EXTRA_LIVE_WALLPAPER_INFO, wallpaperInfo) 59 return deleteActionIntent 60 } 61 getDeleteActionnull62 private fun getDeleteAction(context: Context, wallpaperInfo: WallpaperInfo): String? { 63 val currentInfo = WallpaperManager.getInstance(context).getWallpaperInfo(FLAG_SYSTEM) 64 val currentLockInfo = WallpaperManager.getInstance(context).getWallpaperInfo(FLAG_LOCK) 65 val serviceInfo = wallpaperInfo.serviceInfo 66 val appInfo = serviceInfo.applicationInfo 67 val isPackagePreInstalled = 68 appInfo != null && appInfo.flags and ApplicationInfo.FLAG_SYSTEM != 0 69 // This wallpaper is not installed before 70 if (!isPackagePreInstalled) { 71 Log.d(TAG, "This wallpaper is not pre-installed: " + serviceInfo.name) 72 return null 73 } 74 75 // A currently set Live wallpaper should not be deleted. 76 val currentService = currentInfo?.serviceInfo 77 if (currentService != null && TextUtils.equals(serviceInfo.name, currentService.name)) { 78 return null 79 } 80 val currentLockService = currentLockInfo?.serviceInfo 81 if ( 82 currentLockService != null && 83 TextUtils.equals(serviceInfo.name, currentLockService.name) 84 ) { 85 return null 86 } 87 val metaData = serviceInfo.metaData 88 return metaData?.getString(LivePreviewFragment.KEY_ACTION_DELETE_LIVE_WALLPAPER) 89 } 90 } 91