1 /*
2  * Copyright (C) 2009 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.livepicker;
18 
19 import android.app.WallpaperInfo;
20 import android.app.WallpaperManager;
21 import android.content.ComponentName;
22 import android.content.Intent;
23 import android.content.pm.ApplicationInfo;
24 import android.content.pm.PackageManager;
25 import android.content.pm.ResolveInfo;
26 import android.content.pm.ServiceInfo;
27 import android.os.Bundle;
28 import android.os.Parcelable;
29 import android.service.wallpaper.WallpaperService;
30 import android.text.TextUtils;
31 import android.util.Log;
32 
33 import androidx.annotation.NonNull;
34 import androidx.annotation.Nullable;
35 
36 import org.xmlpull.v1.XmlPullParserException;
37 
38 import java.io.IOException;
39 import java.util.List;
40 
41 public class LiveWallpaperChange extends LiveWallpaperPreview {
42     private static final String TAG = "CHANGE_LIVE_WALLPAPER";
43     private static final String KEY_ACTION_DELETE_LIVE_WALLPAPER = "action_delete_live_wallpaper";
44 
45     @Override
init()46     protected void init() {
47         Parcelable obj = getIntent().getParcelableExtra(
48                 WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT);
49         if (obj == null || !(obj instanceof ComponentName)) {
50             Log.w(TAG, "No LIVE_WALLPAPER_COMPONENT extra supplied");
51             finish();
52             return;
53         }
54 
55         ComponentName comp = (ComponentName)obj;
56         WallpaperInfo currentWallpaper = WallpaperManager.getInstance(this).getWallpaperInfo();
57 
58         // Get the information about this component.  Implemented this way
59         // to not allow us to direct the caller to a service that is not a
60         // live wallpaper.
61         Intent queryIntent = new Intent(WallpaperService.SERVICE_INTERFACE);
62         queryIntent.setPackage(comp.getPackageName());
63         List<ResolveInfo> list = getPackageManager().queryIntentServices(
64                 queryIntent, PackageManager.GET_META_DATA);
65         if (list != null) {
66             for (int i=0; i<list.size(); i++) {
67                 ResolveInfo ri = list.get(i);
68                 if (ri.serviceInfo.name.equals(comp.getClassName())) {
69                     WallpaperInfo info;
70                     try {
71                         info = new WallpaperInfo(this, ri);
72                     } catch (XmlPullParserException|IOException e) {
73                         Log.w(TAG, "Bad wallpaper " + ri.serviceInfo, e);
74                         finish();
75                         return;
76                     }
77                     initUI(info, getDeleteAction(ri.serviceInfo,
78                         (currentWallpaper == null) ? null : currentWallpaper.getServiceInfo()));
79                     return;
80                 }
81             }
82         }
83 
84         Log.w(TAG, "Not a live wallpaper: " + comp);
85         finish();
86     }
87 
88     @Nullable
getDeleteAction(@onNull ServiceInfo serviceInfo, @Nullable ServiceInfo currentService)89     private String getDeleteAction(@NonNull ServiceInfo serviceInfo,
90         @Nullable ServiceInfo currentService) {
91         if (!isPackagePreInstalled(serviceInfo.applicationInfo)) {
92             Log.d(TAG, "This wallpaper is not pre-installed: " + serviceInfo.name);
93             return null;
94         }
95 
96         // A currently set Live wallpaper should not be deleted.
97         if (currentService != null && TextUtils.equals(serviceInfo.name, currentService.name)) {
98             return null;
99         }
100 
101         final Bundle metaData = serviceInfo.metaData;
102         if (metaData != null) {
103             return metaData.getString(KEY_ACTION_DELETE_LIVE_WALLPAPER);
104         }
105         return null;
106     }
107 
isPackagePreInstalled(ApplicationInfo info)108     private boolean isPackagePreInstalled(ApplicationInfo info) {
109         if (info != null && (info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
110             return true;
111         }
112         return false;
113     }
114 }
115