配置默认壁纸:
1)代码分析
frameworks/base/core/java/android/app/WallpaperManager.java
private Bitmap getDefaultWallpaper(Context context, @SetWallpaperFlags int which) {
InputStream is = openDefaultWallpaper(context, which);
if (is != null) {
try {
BitmapFactory.Options options = new BitmapFactory.Options();
return BitmapFactory.decodeStream(is, null, options);
} catch (OutOfMemoryError e) {
Log.w(TAG, "Can't decode stream", e);
} finally {
IoUtils.closeQuietly(is);
}
}
return null;
}
可以看到,具体的默认壁纸设置在openDefaultWallpaper()方法:
/**
* Open stream representing the default static image wallpaper.
*
* If the device defines no default wallpaper of the requested kind,
* {@code null} is returned.
*
* @hide
*/
public static InputStream openDefaultWallpaper(Context context, @SetWallpaperFlags int which) {
// +++
//++Panther wallpaper
final String customWallpaper = "system/flex/wallpaper/default_wallpaper.png";
final String customWallpaperBlue = "system/flex/wallpaper/default_wallpaper_blue.png";
//--Panther wallpaper
//++Deadpool wallpaper,chenhaibing.wt add,2019/01/19
final String customDeadpoolWallpaper = "system/flex/wallpaper/default_deadpool_wallpaper.png";
final String customDeadpoolWallpaperBlue = "system/flex/wallpaper/default_deadpool_wallpaper_blue.png";
//--Deadpool wallpaper,chenhaibing.wt add,2019/01/19
//++Deadpool wallpaper,chenhaibing.wt modify&add,2019/01/19
if ( (!TextUtils.isEmpty(customWallpaper)) && (!isFactoryMode()) && isUserBlackWallpaper() && isPantherDevice()) {
final File wallpaperFile = new File(customWallpaper);
if (wallpaperFile.exists()) {
try {
Log.v("chentest WallpaperManager", "return wallpaperFile");
return new FileInputStream(wallpaperFile);
} catch (IOException e) {
Log.v(TAG, "File not found customWallpaper: " + customWallpaper);
}
}
}
if ( (!TextUtils.isEmpty(customWallpaperBlue)) && (!isFactoryMode()) && (!isUserBlackWallpaper()) && isPantherDevice()) {
final File wallpaperFile = new File(customWallpaperBlue);
if (wallpaperFile.exists()) {
try {
Log.v("chentest WallpaperManager", "return customWallpaperBlue");
return new FileInputStream(wallpaperFile);
} catch (IOException e) {
Log.v(TAG, "File not found customWallpaperBlue: " + customWallpaperBlue);
}
}
}
if ( (!TextUtils.isEmpty(customDeadpoolWallpaper)) && (!isFactoryMode()) && isUserBlackWallpaper() && isDeadpoolDevice()) {
final File wallpaperFile = new File(customDeadpoolWallpaper);
if (wallpaperFile.exists()) {
try {
Log.v("chentest WallpaperManager", "return customDeadpoolWallpaper");
return new FileInputStream(wallpaperFile);
} catch (IOException e) {
Log.v(TAG, "File not found customDeadpoolWallpaper: " + customDeadpoolWallpaper);
}
}
}
if ( (!TextUtils.isEmpty(customDeadpoolWallpaperBlue)) && (!isFactoryMode()) && (!isUserBlackWallpaper()) && isDeadpoolDevice()) {
final File wallpaperFile = new File(customDeadpoolWallpaperBlue);
if (wallpaperFile.exists()) {
try {
Log.v("chentest WallpaperManager", "return customDeadpoolWallpaperBlue");
return new FileInputStream(wallpaperFile);
} catch (IOException e) {
Log.v(TAG, "File not found customDeadpoolWallpaperBlue: " + customDeadpoolWallpaperBlue);
}
}
}
//--Deadpool wallpaper,chenhaibing.wt modify&add,2019/01/19
// ---
final String whichProp;
final int defaultResId;
if (which == FLAG_LOCK) {
/* Factory-default lock wallpapers are not yet supported
whichProp = PROP_LOCK_WALLPAPER;
defaultResId = com.android.internal.R.drawable.default_lock_wallpaper;
*/
return null;
//+bug 337344, liyichong.wt, 20180201, add, for ato version's wallpaper
} else if (isFactoryMode()) {
whichProp = PROP_WALLPAPER;
defaultResId = com.android.internal.R.drawable.home_wallpaper_ato;
//-bug 337344, liyichong.wt, 20180201, add, for ato version's wallpaper
} else {
whichProp = PROP_WALLPAPER;
defaultResId = com.android.internal.R.drawable.default_wallpaper;
}
final String path = SystemProperties.get(whichProp);
if (!TextUtils.isEmpty(path)) {
final File file = new File(path);
if (file.exists()) {
try {
return new FileInputStream(file);
} catch (IOException e) {
// Ignored, fall back to platform default below
}
}
}
try {
return context.getResources().openRawResource(defaultResId);
} catch (NotFoundException e) {
// no default defined for this device; this is not a failure
}
return null;
}
注:可以看到, 此处代码写的太烂了, 需要优化, 自我批评一下, 只顾着实现需求.
此处从system/flex/wallpaper/中获取壁纸资源,根据项目(Panther/DeadPool)、型号(Black/Blue)、工厂版本(即ATO版本)设置默认的壁纸:
判断项目:
//++Deadpool wallpaper,chenhaibing.wt add,2019/01/19
public static boolean isPantherDevice(){
String isPanther = SystemProperties.get("ro.wt.panther", "0");
Log.v("chentest WallpaperManager", "ro.wt.panther=" + isPanther);
if ("1".equals(isPanther)) {
Log.v("chentest WallpaperManager", "return true");
return true;
} else {
Log.v("chentest WallpaperManager", "return false");
return false;
}
}
public static boolean isDeadpoolDevice(){
String isDeadpool = SystemProperties.get("ro.wt.deadpool", "0");
Log.v("chentest WallpaperManager", "ro.wt.deadpool=" + isDeadpool);
if ("1".equals(isDeadpool)){
Log.v("chentest WallpaperManager", "return true");
return true;
} else {
Log.v("chentest WallpaperManager", "return false");
return false;
}
}
//--Deadpool wallpaper,chenhaibing.wt add,2019/01/19
判断型号颜色(colorID):
public static boolean isUserBlackWallpaper() {
String str = SystemProperties.get("ro.boot.colorid", "0x4");
//bug 435119,chenhaibing.wt add,2019/03/25
if ("0x4".equals(str) | "4".equals(str)) return true;
if ("0x1".equals(str) | "1".equals(str)) return false;
if ("0x8".equals(str) | "8".equals(str)) return false;
return true;
}
判断工厂版本:
/**
* ro.factory_mode=1 indicates factory version
* ro.factory_mode=0 indicates non-factory version
* @hide
*/
public static boolean isFactoryMode() {
try {
return "1".equals(SystemProperties.get("ro.factory_mode")) ? true : false;
} catch (Exception e) {
Log.e(TAG, "get factory mode property fail!");
}
return false;
}
2)预置默认壁纸资源
在vendor/Flex/wallpaper下预置默认壁纸资源:
在wallpaper.mk文件中设置make编译时规则:
LOCAL_PATH := vendor/Flex/wallpaper
TARGET_PATH := system/flex/wallpaper
PRODUCT_COPY_FILES += \
$(LOCAL_PATH)/default_wallpaper.png:$(TARGET_PATH)/default_wallpaper.png \
$(LOCAL_PATH)/default_wallpaper_blue.png:$(TARGET_PATH)/default_wallpaper_blue.png \
$(LOCAL_PATH)/default_deadpool_wallpaper.png:$(TARGET_PATH)/default_deadpool_wallpaper.png \
$(LOCAL_PATH)/default_deadpool_wallpaper_blue.png:$(TARGET_PATH)/default_deadpool_wallpaper_blue.png
需要注意的是,在上级目录Flex中记得包含Wallpaper:
Flex.mk
$(warning $(LOCAL_PATH))
DEVICE_PACKAGE_OVERLAYS += vendor/Flex/overlays
$(call inherit-product-if-exists, vendor/Flex/xml/workspace.mk)
$(call inherit-product-if-exists, vendor/Flex/wallpaper/wallpaper.mk)
$(call inherit-product-if-exists, vendor/Flex/media/media.mk)
PRODUCT_PACKAGES += \
NokiaSupportStub \
GoogleAssistant \
Wallpaper \
PicMix
这样,默认壁纸在编译时就会复制到system/flex/wallpaper以供openDefaultWallpaper()方法获取。