下面列出了怎么用android.app.WallpaperColors的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* We can easily change theme by modified colors hint. This function will check
* current theme mode and return the WallpaperColors fit current theme mode.
* If color need modified, it will return a copied WallpaperColors which
* its ColorsHint is modified to fit current theme mode.
*
* @param colors a wallpaper primary colors representation
*/
private WallpaperColors getThemeColorsLocked(WallpaperColors colors) {
if (colors == null) {
Slog.w(TAG, "Cannot get theme colors because WallpaperColors is null.");
return null;
}
int colorHints = colors.getColorHints();
boolean supportDarkTheme = (colorHints & WallpaperColors.HINT_SUPPORTS_DARK_THEME) != 0;
if (mThemeMode == Settings.Secure.THEME_MODE_WALLPAPER ||
(mThemeMode == Settings.Secure.THEME_MODE_LIGHT && !supportDarkTheme) ||
(mThemeMode == Settings.Secure.THEME_MODE_DARK && supportDarkTheme)) {
return colors;
}
WallpaperColors themeColors = new WallpaperColors(colors.getPrimaryColor(),
colors.getSecondaryColor(), colors.getTertiaryColor());
if (mThemeMode == Settings.Secure.THEME_MODE_LIGHT) {
colorHints &= ~WallpaperColors.HINT_SUPPORTS_DARK_THEME;
} else if (mThemeMode == Settings.Secure.THEME_MODE_DARK) {
colorHints |= WallpaperColors.HINT_SUPPORTS_DARK_THEME;
}
themeColors.setColorHints(colorHints);
return themeColors;
}
/**
* Called by a live wallpaper if its colors have changed.
* @param primaryColors representation of wallpaper primary colors
*/
@Override
public void onWallpaperColorsChanged(WallpaperColors primaryColors) {
int which;
synchronized (mLock) {
// Do not broadcast changes on ImageWallpaper since it's handled
// internally by this class.
if (mImageWallpaper.equals(mWallpaper.wallpaperComponent)) {
return;
}
mWallpaper.primaryColors = primaryColors;
// Live wallpapers always are system wallpapers.
which = FLAG_SYSTEM;
// It's also the lock screen wallpaper when we don't have a bitmap in there
WallpaperData lockedWallpaper = mLockWallpaperMap.get(mWallpaper.userId);
if (lockedWallpaper == null) {
which |= FLAG_LOCK;
}
}
if (which != 0) {
notifyWallpaperColorsChanged(mWallpaper, which);
}
}
/**
* Check whether to call notifyWallpaperColorsChanged. Assumed that the theme mode
* was wallpaper theme mode and dark wallpaper was set, therefoe, the theme was dark.
* Then theme mode changing to dark theme mode, however, theme should not update since
* theme was dark already.
*/
private boolean needUpdateLocked(WallpaperColors colors, int themeMode) {
if (colors == null) {
return false;
}
if (themeMode == mThemeMode) {
return false;
}
boolean result = true;
boolean supportDarkTheme =
(colors.getColorHints() & WallpaperColors.HINT_SUPPORTS_DARK_THEME) != 0;
switch (themeMode) {
case Settings.Secure.THEME_MODE_WALLPAPER:
if (mThemeMode == Settings.Secure.THEME_MODE_LIGHT) {
result = supportDarkTheme;
} else {
result = !supportDarkTheme;
}
break;
case Settings.Secure.THEME_MODE_LIGHT:
if (mThemeMode == Settings.Secure.THEME_MODE_WALLPAPER) {
result = supportDarkTheme;
}
break;
case Settings.Secure.THEME_MODE_DARK:
if (mThemeMode == Settings.Secure.THEME_MODE_WALLPAPER) {
result = !supportDarkTheme;
}
break;
default:
Slog.w(TAG, "unkonwn theme mode " + themeMode);
return false;
}
mThemeMode = themeMode;
return result;
}
@Override
public WallpaperColors getWallpaperColors(int which, int userId) throws RemoteException {
if (which != FLAG_LOCK && which != FLAG_SYSTEM) {
throw new IllegalArgumentException("which should be either FLAG_LOCK or FLAG_SYSTEM");
}
userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(),
userId, false, true, "getWallpaperColors", null);
WallpaperData wallpaperData = null;
boolean shouldExtract;
synchronized (mLock) {
if (which == FLAG_LOCK) {
wallpaperData = mLockWallpaperMap.get(userId);
}
// Try to get the system wallpaper anyway since it might
// also be the lock screen wallpaper
if (wallpaperData == null) {
wallpaperData = mWallpaperMap.get(userId);
}
if (wallpaperData == null) {
return null;
}
shouldExtract = wallpaperData.primaryColors == null;
}
if (shouldExtract) {
extractColors(wallpaperData);
}
synchronized (mLock) {
return getThemeColorsLocked(wallpaperData.primaryColors);
}
}
/**
* Notifies the engine that wallpaper colors changed significantly.
* This will trigger a {@link #onComputeColors()} call.
*/
public void notifyColorsChanged() {
final long now = mClockFunction.get();
if (now - mLastColorInvalidation < NOTIFY_COLORS_RATE_LIMIT_MS) {
Log.w(TAG, "This call has been deferred. You should only call "
+ "notifyColorsChanged() once every "
+ (NOTIFY_COLORS_RATE_LIMIT_MS / 1000f) + " seconds.");
if (!mHandler.hasCallbacks(mNotifyColorsChanged)) {
mHandler.postDelayed(mNotifyColorsChanged, NOTIFY_COLORS_RATE_LIMIT_MS);
}
return;
}
mLastColorInvalidation = now;
mHandler.removeCallbacks(mNotifyColorsChanged);
try {
final WallpaperColors newColors = onComputeColors();
if (mConnection != null) {
mConnection.onWallpaperColorsChanged(newColors);
} else {
Log.w(TAG, "Can't notify system because wallpaper connection "
+ "was not established.");
}
} catch (RemoteException e) {
Log.w(TAG, "Can't notify system because wallpaper connection was lost.", e);
}
}
@TargetApi(Build.VERSION_CODES.O_MR1)
@Override public WallpaperColors onComputeColors ()
{
Color color = Color.valueOf(Color.BLACK);
return new WallpaperColors(color, color, color);
}
private void parseWallpaperAttributes(XmlPullParser parser, WallpaperData wallpaper,
boolean keepDimensionHints) {
final String idString = parser.getAttributeValue(null, "id");
if (idString != null) {
final int id = wallpaper.wallpaperId = Integer.parseInt(idString);
if (id > mWallpaperId) {
mWallpaperId = id;
}
} else {
wallpaper.wallpaperId = makeWallpaperIdLocked();
}
if (!keepDimensionHints) {
wallpaper.width = Integer.parseInt(parser.getAttributeValue(null, "width"));
wallpaper.height = Integer.parseInt(parser
.getAttributeValue(null, "height"));
}
wallpaper.cropHint.left = getAttributeInt(parser, "cropLeft", 0);
wallpaper.cropHint.top = getAttributeInt(parser, "cropTop", 0);
wallpaper.cropHint.right = getAttributeInt(parser, "cropRight", 0);
wallpaper.cropHint.bottom = getAttributeInt(parser, "cropBottom", 0);
wallpaper.padding.left = getAttributeInt(parser, "paddingLeft", 0);
wallpaper.padding.top = getAttributeInt(parser, "paddingTop", 0);
wallpaper.padding.right = getAttributeInt(parser, "paddingRight", 0);
wallpaper.padding.bottom = getAttributeInt(parser, "paddingBottom", 0);
int colorsCount = getAttributeInt(parser, "colorsCount", 0);
if (colorsCount > 0) {
Color primary = null, secondary = null, tertiary = null;
for (int i = 0; i < colorsCount; i++) {
Color color = Color.valueOf(getAttributeInt(parser, "colorValue" + i, 0));
if (i == 0) {
primary = color;
} else if (i == 1) {
secondary = color;
} else if (i == 2) {
tertiary = color;
} else {
break;
}
}
int colorHints = getAttributeInt(parser, "colorHints", 0);
wallpaper.primaryColors = new WallpaperColors(primary, secondary, tertiary, colorHints);
}
wallpaper.name = parser.getAttributeValue(null, "name");
wallpaper.allowBackup = "true".equals(parser.getAttributeValue(null, "backup"));
}
/**
* Called by the system when it needs to know what colors the wallpaper is using.
* You might return null if no color information is available at the moment.
* In that case you might want to call {@link #notifyColorsChanged()} when
* color information becomes available.
* <p>
* The simplest way of creating a {@link android.app.WallpaperColors} object is by using
* {@link android.app.WallpaperColors#fromBitmap(Bitmap)} or
* {@link android.app.WallpaperColors#fromDrawable(Drawable)}, but you can also specify
* your main colors by constructing a {@link android.app.WallpaperColors} object manually.
*
* @return Wallpaper colors.
*/
public @Nullable WallpaperColors onComputeColors() {
return null;
}