• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.content.Context
21 import android.content.Intent
22 import android.content.pm.ApplicationInfo
23 import android.text.TextUtils
24 import android.util.Log
25 import com.android.wallpaper.picker.LivePreviewFragment
26 
27 /** The utility class for live wallpaper the can be deleted */
28 object DeletableUtils {
29     private const val TAG = "DeletableUtils"
30 
31     /** If a wallpaper can be deleted. */
32     @JvmStatic
canBeDeletednull33     fun canBeDeleted(context: Context, wallpaperInfo: WallpaperInfo): Boolean {
34         return !TextUtils.isEmpty(getDeleteAction(context, wallpaperInfo))
35     }
36 
37     /** Delete a wallpaper. */
38     @JvmStatic
deleteLiveWallpapernull39     fun deleteLiveWallpaper(
40         context: Context,
41         wallpaperInfo: com.android.wallpaper.model.WallpaperInfo
42     ) {
43         val deleteIntent = getDeleteActionIntent(context, wallpaperInfo.wallpaperComponent)
44         if (deleteIntent != null) {
45             context.startService(deleteIntent)
46         }
47     }
48 
getDeleteActionIntentnull49     private fun getDeleteActionIntent(context: Context, wallpaperInfo: WallpaperInfo): Intent? {
50         val deleteAction = getDeleteAction(context, wallpaperInfo)
51         if (TextUtils.isEmpty(deleteAction)) {
52             return null
53         }
54         val deleteActionIntent = Intent(deleteAction)
55         deleteActionIntent.setPackage(wallpaperInfo.packageName)
56         deleteActionIntent.putExtra(LivePreviewFragment.EXTRA_LIVE_WALLPAPER_INFO, wallpaperInfo)
57         return deleteActionIntent
58     }
59 
getDeleteActionnull60     private fun getDeleteAction(context: Context, wallpaperInfo: WallpaperInfo): String? {
61         val currentInfo = WallpaperManager.getInstance(context).wallpaperInfo
62         val serviceInfo = wallpaperInfo.serviceInfo
63         val appInfo = serviceInfo.applicationInfo
64         val isPackagePreInstalled =
65             appInfo != null && appInfo.flags and ApplicationInfo.FLAG_SYSTEM != 0
66         // This wallpaper is not installed before
67         if (!isPackagePreInstalled) {
68             Log.d(TAG, "This wallpaper is not pre-installed: " + serviceInfo.name)
69             return null
70         }
71         val currentService = currentInfo?.serviceInfo
72         // A currently set Live wallpaper should not be deleted.
73         if (currentService != null && TextUtils.equals(serviceInfo.name, currentService.name)) {
74             return null
75         }
76         val metaData = serviceInfo.metaData
77         return metaData?.getString(LivePreviewFragment.KEY_ACTION_DELETE_LIVE_WALLPAPER)
78     }
79 }
80