/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.systemui;
import android.app.WallpaperManager;
import android.content.ComponentCallbacks2;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region.Op;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Trace;
import android.service.wallpaper.WallpaperService;
import android.util.Log;
import android.view.Display;
import android.view.DisplayInfo;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.WindowManager;
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Default built-in wallpaper that simply shows a static image.
*/
@SuppressWarnings({"UnusedDeclaration"})
public class ImageWallpaper extends WallpaperService {
private static final String TAG = "ImageWallpaper";
private static final String GL_LOG_TAG = "ImageWallpaperGL";
private static final boolean DEBUG = false;
private static final String PROPERTY_KERNEL_QEMU = "ro.kernel.qemu";
private static final long DELAY_FORGET_WALLPAPER = 5000;
private WallpaperManager mWallpaperManager;
private DrawableEngine mEngine;
@Override
public void onCreate() {
super.onCreate();
mWallpaperManager = getSystemService(WallpaperManager.class);
}
@Override
public void onTrimMemory(int level) {
if (mEngine != null) {
mEngine.trimMemory(level);
}
}
@Override
public Engine onCreateEngine() {
mEngine = new DrawableEngine();
return mEngine;
}
class DrawableEngine extends Engine {
private final Runnable mUnloadWallpaperCallback = () -> {
unloadWallpaper(false /* forgetSize */);
};
Bitmap mBackground;
int mBackgroundWidth = -1, mBackgroundHeight = -1;
int mLastSurfaceWidth = -1, mLastSurfaceHeight = -1;
int mLastRotation = -1;
float mXOffset = 0f;
float mYOffset = 0f;
float mScale = 1f;
private Display mDefaultDisplay;
private final DisplayInfo mTmpDisplayInfo = new DisplayInfo();
boolean mVisible = true;
boolean mOffsetsChanged;
int mLastXTranslation;
int mLastYTranslation;
private int mRotationAtLastSurfaceSizeUpdate = -1;
private int mDisplayWidthAtLastSurfaceSizeUpdate = -1;
private int mDisplayHeightAtLastSurfaceSizeUpdate = -1;
private int mLastRequestedWidth = -1;
private int mLastRequestedHeight = -1;
private AsyncTask<Void, Void, Bitmap> mLoader;
private boolean mNeedsDrawAfterLoadingWallpaper;
private boolean mSurfaceValid;
private boolean mSurfaceRedrawNeeded;
DrawableEngine() {
super();
setFixedSizeAllowed(true);
}
void trimMemory(int level) {
if (level >= ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW
&& level <= ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL
&& mBackground != null) {
if (DEBUG) {
Log.d(TAG, "trimMemory");
}
unloadWallpaper(true /* forgetSize */);
}
}
@Override
public void onCreate(SurfaceHolder surfaceHolder) {
if (DEBUG) {
Log.d(TAG, "onCreate");
}
super.onCreate(surfaceHolder);
//noinspection ConstantConditions
mDefaultDisplay = getSystemService(WindowManager.class).getDefaultDisplay();
setOffsetNotificationsEnabled(false);
updateSurfaceSize(surfaceHolder, getDefaultDisplayInfo(), false /* forDraw */);
}
@Override
public void onDestroy() {
super.onDestroy();
mBackground = null;
unloadWallpaper(true /* forgetSize */);
}
boolean updateSurfaceSize(SurfaceHolder surfaceHolder, DisplayInfo displayInfo,
boolean forDraw) {
boolean hasWallpaper = true;
// Load background image dimensions, if we haven't saved them yet
if (mBackgroundWidth <= 0 || mBackgroundHeight <= 0) {
// Need to load the image to get dimensions
loadWallpaper(forDraw);
if (DEBUG) {
Log.d(TAG, "Reloading, redoing updateSurfaceSize later.");
}
hasWallpaper = false;
}
// Force the wallpaper to cover the screen in both dimensions
int surfaceWidth = Math.max(displayInfo.logicalWidth, mBackgroundWidth);
int surfaceHeight = Math.max(displayInfo.logicalHeight, mBackgroundHeight);
android.util.Log.i("jyj","logicalWidth="+displayInfo.logicalWidth+"--logicalHeight="+displayInfo.logicalHeight);
android.util.Log.i("jyj","mBackgroundWidth="+mBackgroundWidth+"--mBackgroundHeight="+mBackgroundHeight);
android.util.Log.i("jyj","surfaceWidth111="+surfaceWidth+"--surfaceHeight111="+surfaceHeight);
// Used a fixed size surface, because we are special. We can do
// this because we know the current design of window animations doesn't
// cause this to break.
//surfaceHolder.setFixedSize(surfaceWidth, surfaceHeight);
if(mBackgroundWidth<=0){
mBackgroundWidth =1300;
}
if(mBackgroundHeight<=0){
mBackgroundHeight = 1280;
}
surfaceWidth = mBackgroundWidth;
surfaceHeight = mBackgroundHeight;
surfaceHolder.setFixedSize(surfaceWidth, surfaceHeight);
mLastRequestedWidth = surfaceWidth;
mLastRequestedHeight = surfaceHeight;
return hasWallpaper;
}
@Override
public void onVisibilityChanged(boolean visible) {
if (DEBUG) {
Log.d(TAG, "onVisibilityChanged: mVisible, visible=" + mVisible + ", " + visible);
}
if (mVisible != visible) {
if (DEBUG) {
Log.d(TAG, "Visibility changed to visible=" + visible);
}
mVisible = visible;
if (visible) {
drawFrame();
}
}
}
@Override
public void onOffsetsChanged(float xOffset, float yOffset,
float xOffsetStep, float yOffsetStep,
int xPixels, int yPixels) {
if (DEBUG) {
Log.d(TAG, "onOffsetsChanged: xOffset=" + xOffset + ", yOffset=" + yOffset
+ ", xOffsetStep=" + xOffsetStep + ", yOffsetStep=" + yOffsetStep
+ ", xPixels=" + xPixels + ", yPixels=" + yPixels);
}
if (mXOffset != xOffset || mYOffset != yOffset) {
if (DEBUG) {
Log.d(TAG, "Offsets changed to (" + xOffset + "," + yOffset + ").");
}
mXOffset = xOffset;
mYOffset = yOffset;
mOffsetsChanged = true;
}
drawFrame();
}
@Override
public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if (DEBUG) {
Log.d(TAG, "onSurfaceChanged: width=" + width + ", heigh