您的位置:首页 > 移动开发 > Android开发

android 7.1 默认动态壁纸

2018-01-22 19:34 627 查看
最近客户提了个需求:升级后默认使用动态壁纸。

但是根据网络资料大量修改动态壁纸的都是修改frameworks/base/core/res/res/values/config.xml文件中

default_wallpaper_component就好了。

我尝试改了一下,升级后第一次开机变现为纯黑色壁纸,并不是动态壁纸。第二次开机以后就会变成默认的静态壁纸了。

我需要修改的System是基于RK的安卓7.1开发的,不知道是不是有哪里不一样。

这里需要修改的关键类是\frameworks\base\services\core\java\com\android\server\wallpaper\WallpaperManagerService.java
关于WallpaperManager的分析在这里就不写了,有兴趣的可以自行到网上在,有很多。

导致这个现象的原因是
WallpaperManagerService被实例化的时候会调用loadSettingsLocked()方法。loadSettingsLocke()会读取/data/system/users/0/wallpaper_info.xml(安卓7.1)文件来初始化一些属性,其中包括当前用户壁纸的ComponentName。但是由于第一次开机时wallpaper_info.xml并不存在,所以当前用户壁纸的ComponentName为空。
当WallpaperManagerService调用switchWallpaper(WallpaperData,IRemoteCallback)时,会在里面调用bindWallpaperComponent(ComponentName,boolean,boolean,WallpaperData,IRemoteCallback)去尝试连接壁纸的服务。因为ComponentName是空的,所以就去获取了默认的ComponentName(就是在xml改的那个)。然后连接上了就返回true,连接不上就返回false。
但是,获取的默认ComponentName只在bindWallpaperComponent(...)中有效,对于switchWallpaper(...)来说ComponentName仍然是空的。在接下来获取壁纸服务信息失败后,就把壁纸信息全清空还顺带存储起来了(过分)。
switchWallpaper(...)的代码如下:

void switchWallpaper(WallpaperData wallpaper, IRemoteCallback reply) {
synchronized (mLock) {
mWaitingForUnlock = false;
final ComponentName cname = wallpaper.wallpaperComponent != null ?
wallpaper.wallpaperComponent : wallpaper.nextWallpaperComponent;
if (!bindWallpaperComponentLocked(cname, true, false, wallpaper, reply)) {
// We failed to bind the desired wallpaper, but that might
// happen if the wallpaper isn't direct-boot aware
ServiceInfo si = null;
try {
si = mIPackageManager.getServiceInfo(cname,
PackageManager.MATCH_DIRECT_BOOT_UNAWARE, wallpaper.userId);
} catch (RemoteException ignored) {
}

if (si == null) {
Slog.w(TAG, "Failure starting previous wallpaper; clearing");
clearWallpaperLocked(false, FLAG_SYSTEM, wallpaper.userId, reply);
} else {
Slog.w(TAG, "Wallpaper isn't direct boot aware; using fallback until unlocked");
// We might end up persisting the current wallpaper data
// while locked, so pretend like the component was actually
// bound into place
wallpaper.wallpaperComponent = wallpaper.nextWallpaperComponent;
final WallpaperData fallback = new WallpaperData(wallpaper.userId,
WALLPAPER_LOCK_ORIG, WALLPAPER_LOCK_CROP);
ensureSaneWallpaperData(fallback);
bindWallpaperComponentLocked(mImageWallpaper, true, false, fallback, reply);
mWaitingForUnlock = true;
}
}
}
}


解决思路:
既然是因为switchWallpaper(...<
a889
span style="color:rgb(79,79,79);line-height:28px;">)中的ComponentName为空引起的问题,那么使得ComponentName不为空就可以了。在获取壁纸服务信息之前再加一个判断,如果ComponentName为空的话就去获取默认值,基本和bindWallpaperComponent(...)里面的处理一样。在获取服务信息成功后,系统会记录下来“有这个服务,不过这个服务不能在直接启动阶段中连接上,等用户解锁后再连接看看好了”。所以之前修改完ComponentName后要注意往mWallpaperMaps中put一下,不然等到用户解锁后系统在mWallpaperMaps中获取到的ComponentName还是空的。
修改后switchWallpaper(...)的代码如下:
void switchWallpaper(WallpaperData wallpaper, IRemoteCallback reply) {
synchronized (mLock) {
mWaitingForUnlock = false;
ComponentName cname = wallpaper.wallpaperComponent != null ?
wallpaper.wallpaperComponent : wallpaper.nextWallpaperComponent;
if (!bindWallpaperComponentLocked(cname, true, false, wallpaper, reply)) {
if (cname == null) {
// 将nextWallpaperComponent赋值再写入xml,不然下次读取nextWallpaperComponent为空时会使用静态壁纸。
cname =  WallpaperManager.getDefaultWallpaperComponent(mContext);
if (cname == null) {
// Fall back to static image wallpaper
cname = mImageWallpaper;
}
wallpaper.nextWallpaperComponent = cname;
mWallpaperMap.put(mCurrentUserId, wallpaper);
}
// We failed to bind the desired wallpaper, but that might
// happen if the wallpaper isn't direct-boot aware
ServiceInfo si = null;
try {
si = mIPackageManager.getServiceInfo(cname,
PackageManager.MATCH_DIRECT_BOOT_UNAWARE, wallpaper.userId);
} catch (RemoteException ignored) {
}

if (si == null) {
Slog.w(TAG, "Failure starting previous wallpaper; clearing");
clearWallpaperLocked(false, FLAG_SYSTEM, wallpaper.userId, reply);
} else {
Slog.w(TAG, "Wallpaper isn't direct boot aware; using fallback until unlocked");
// We might end up persisting the current wallpaper data
// while locked, so pretend like the component was actually
// bound into place
wallpaper.wallpaperComponent = wallpaper.nextWallpaperComponent;
final WallpaperData fallback = new WallpaperData(wallpaper.userId,
WALLPAPER_LOCK_ORIG, WALLPAPER_LOCK_CROP);
ensureSaneWallpaperData(fallback);
bindWallpaperComponentLocked(mImageWallpaper, true, false, fallback, reply);
mWaitingForUnlock = true;
}
}
}
}


以上个人对代码的理解可能有误,欢迎提出。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: