1 /*
2  * Copyright (C) 2018 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.server;
18 
19 import android.annotation.RequiresPermission;
20 import android.app.ActivityThread;
21 import android.app.WallpaperInfo;
22 import android.app.WallpaperManager;
23 import android.content.BroadcastReceiver;
24 import android.content.ComponentName;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.os.AsyncTask;
28 import android.os.ParcelFileDescriptor;
29 import android.util.Slog;
30 
31 /**
32  * Receiver responsible for updating the wallpaper when the device
33  * configuration has changed.
34  *
35  * @hide
36  */
37 public class WallpaperUpdateReceiver extends BroadcastReceiver {
38 
39     private static final String TAG = "WallpaperUpdateReceiver";
40     private static final boolean DEBUG = false;
41 
42     @Override
onReceive(final Context context, final Intent intent)43     public void onReceive(final Context context, final Intent intent) {
44         if (DEBUG) Slog.d(TAG, "onReceive: " + intent);
45 
46         if (intent != null && Intent.ACTION_DEVICE_CUSTOMIZATION_READY.equals(intent.getAction())) {
47             AsyncTask.execute(this::updateWallpaper);
48         }
49     }
50 
updateWallpaper()51     private void updateWallpaper() {
52         try {
53             ActivityThread currentActivityThread = ActivityThread.currentActivityThread();
54             Context uiContext = currentActivityThread.getSystemUiContext();
55             WallpaperManager wallpaperManager = WallpaperManager.getInstance(uiContext);
56             if (isUserSetWallpaper(wallpaperManager, uiContext)) {
57                 Slog.i(TAG, "User has set wallpaper, skip to resetting");
58                 return;
59             }
60             if (DEBUG) Slog.d(TAG, "Set customized default_wallpaper.");
61             // Check if it is not a live wallpaper set
62             if (wallpaperManager.getWallpaperInfo() == null) {
63                 wallpaperManager.clearWallpaper();
64             }
65         } catch (Exception e) {
66             Slog.w(TAG, "Failed to customize system wallpaper." + e);
67         }
68     }
69 
70     /**
71      * A function to validate if users have set customized (live)wallpaper
72      * <p>
73      * return true if users have customized their wallpaper
74      **/
75     @RequiresPermission(android.Manifest.permission.READ_WALLPAPER_INTERNAL)
isUserSetWallpaper(WallpaperManager wm, Context context)76     private boolean isUserSetWallpaper(WallpaperManager wm, Context context) {
77         WallpaperInfo info = wm.getWallpaperInfo();
78         if (info == null) {
79             //Image Wallpaper
80             ParcelFileDescriptor sysWallpaper =
81                     wm.getWallpaperFile(WallpaperManager.FLAG_SYSTEM);
82             ParcelFileDescriptor lockWallpaper =
83                     wm.getWallpaperFile(WallpaperManager.FLAG_LOCK);
84             if (sysWallpaper != null || lockWallpaper != null) {
85                 return true;
86             }
87         } else {
88             //live wallpaper
89             ComponentName currCN = info.getComponent();
90             ComponentName defaultCN = WallpaperManager.getCmfDefaultWallpaperComponent(context);
91             if (!currCN.equals(defaultCN)) {
92                 return true;
93             }
94         }
95         return false;
96     }
97 }
98