Android 判断壁纸是否为默认壁纸或是用户设置壁纸

sancaiodm Android 2023-03-12 605 0

此类有两个值得学习借鉴的地方:
[1] 日常开发中经常无法引用到Context对象,为引用Context不得不修改大量系统原有方法api  一层一层将把Context对象导入到自己想用的方法中,

[2] 判断当前壁纸是否为系统默认壁纸

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


评论