use of android.app.WallpaperManager in project PhoneProfiles by henrichg.
the class ActivateProfileHelper method executeForWallpaper.
private static void executeForWallpaper(Context context, final Profile profile) {
if (profile._deviceWallpaperChange == 1) {
final Context appContext = context.getApplicationContext();
PPApplication.startHandlerThreadWallpaper();
final Handler handler = new Handler(PPApplication.handlerThreadWallpaper.getLooper());
handler.post(new Runnable() {
@Override
public void run() {
PowerManager powerManager = (PowerManager) appContext.getSystemService(POWER_SERVICE);
PowerManager.WakeLock wakeLock = null;
if (powerManager != null) {
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivateProfileHelper.executeForWallpaper");
wakeLock.acquire(10 * 60 * 1000);
}
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) appContext.getSystemService(Context.WINDOW_SERVICE);
if (wm != null) {
Display display = wm.getDefaultDisplay();
// if (android.os.Build.VERSION.SDK_INT >= 17)
display.getRealMetrics(displayMetrics);
/*else
display.getMetrics(displayMetrics);*/
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
if (appContext.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
// noinspection SuspiciousNameCombination
height = displayMetrics.widthPixels;
// noinspection SuspiciousNameCombination
width = displayMetrics.heightPixels;
}
// for lock screen no double width
if ((android.os.Build.VERSION.SDK_INT < 24) || (profile._deviceWallpaperFor != 2))
// best wallpaper width is twice screen width
width = width << 1;
Bitmap decodedSampleBitmap = BitmapManipulator.resampleBitmapUri(profile._deviceWallpaper, width, height, appContext);
if (decodedSampleBitmap != null) {
// set wallpaper
WallpaperManager wallpaperManager = WallpaperManager.getInstance(appContext);
try {
if (android.os.Build.VERSION.SDK_INT >= 24) {
int flags = WallpaperManager.FLAG_SYSTEM | WallpaperManager.FLAG_LOCK;
Rect visibleCropHint = null;
if (profile._deviceWallpaperFor == 1)
flags = WallpaperManager.FLAG_SYSTEM;
if (profile._deviceWallpaperFor == 2) {
flags = WallpaperManager.FLAG_LOCK;
int left = 0;
int right = decodedSampleBitmap.getWidth();
if (decodedSampleBitmap.getWidth() > width) {
left = (decodedSampleBitmap.getWidth() / 2) - (width / 2);
right = (decodedSampleBitmap.getWidth() / 2) + (width / 2);
}
visibleCropHint = new Rect(left, 0, right, decodedSampleBitmap.getHeight());
}
// noinspection WrongConstant
wallpaperManager.setBitmap(decodedSampleBitmap, visibleCropHint, true, flags);
} else
wallpaperManager.setBitmap(decodedSampleBitmap);
} catch (IOException e) {
Log.e("ActivateProfileHelper.executeForWallpaper", Log.getStackTraceString(e));
}
}
}
if ((wakeLock != null) && wakeLock.isHeld())
wakeLock.release();
}
});
}
}
use of android.app.WallpaperManager in project android_packages_apps_SetupWizard by LineageOS.
the class FinishActivity method setupRevealImage.
private void setupRevealImage() {
final Point p = new Point();
getWindowManager().getDefaultDisplay().getRealSize(p);
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
wallpaperManager.forgetLoadedWallpaper();
final Bitmap wallpaper = wallpaperManager.getBitmap();
Bitmap cropped = null;
if (wallpaper != null) {
cropped = Bitmap.createBitmap(wallpaper, 0, 0, Math.min(p.x, wallpaper.getWidth()), Math.min(p.y, wallpaper.getHeight()));
}
if (cropped != null) {
mReveal.setScaleType(ImageView.ScaleType.CENTER_CROP);
mReveal.setImageBitmap(cropped);
} else {
mReveal.setBackground(wallpaperManager.getBuiltInDrawable(p.x, p.y, false, 0, 0));
}
animateOut();
}
use of android.app.WallpaperManager in project ABPlayer by winkstu.
the class CropImage method onSaveClicked.
private void onSaveClicked() {
// bitmap doesn't have to be read into memory
if (mSaving)
return;
if (mCrop == null) {
return;
}
mSaving = true;
Rect r = mCrop.getCropRect();
int width = r.width();
int height = r.height();
// If we are circle cropping, we want alpha channel, which is the
// third param here.
Bitmap croppedImage = Bitmap.createBitmap(width, height, mCircleCrop ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
{
Canvas canvas = new Canvas(croppedImage);
Rect dstRect = new Rect(0, 0, width, height);
canvas.drawBitmap(mBitmap, r, dstRect, null);
}
if (mCircleCrop) {
// OK, so what's all this about?
// Bitmaps are inherently rectangular but we want to return
// something that's basically a circle. So we fill in the
// area around the circle with alpha. Note the all important
// PortDuff.Mode.CLEAR.
Canvas c = new Canvas(croppedImage);
Path p = new Path();
p.addCircle(width / 2F, height / 2F, width / 2F, Path.Direction.CW);
c.clipPath(p, Region.Op.DIFFERENCE);
c.drawColor(0x00000000, PorterDuff.Mode.CLEAR);
}
/* If the output is required to a specific size then scale or fill */
if (mOutputX != 0 && mOutputY != 0) {
if (mScale) {
/* Scale the image to the required dimensions */
Bitmap old = croppedImage;
croppedImage = BitmapUtils.transform(new Matrix(), croppedImage, mOutputX, mOutputY, mScaleUp);
if (old != croppedImage) {
old.recycle();
}
} else {
/* Don't scale the image crop it to the size requested.
* Create an new image with the cropped image in the center and
* the extra space filled.
*/
// Don't scale the image but instead fill it so it's the
// required dimension
Bitmap b = Bitmap.createBitmap(mOutputX, mOutputY, Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(b);
Rect srcRect = mCrop.getCropRect();
Rect dstRect = new Rect(0, 0, mOutputX, mOutputY);
int dx = (srcRect.width() - dstRect.width()) / 2;
int dy = (srcRect.height() - dstRect.height()) / 2;
/* If the srcRect is too big, use the center part of it. */
srcRect.inset(Math.max(0, dx), Math.max(0, dy));
/* If the dstRect is too big, use the center part of it. */
dstRect.inset(Math.max(0, -dx), Math.max(0, -dy));
/* Draw the cropped bitmap in the center */
canvas.drawBitmap(mBitmap, srcRect, dstRect, null);
/* Set the cropped bitmap as the new bitmap */
croppedImage.recycle();
croppedImage = b;
}
}
// Return the cropped image directly or save it to the specified URI.
Bundle myExtras = getIntent().getExtras();
if (myExtras != null && (myExtras.getParcelable("data") != null || myExtras.getBoolean("return-data"))) {
Bundle extras = new Bundle();
extras.putParcelable("data", croppedImage);
setResult(RESULT_OK, (new Intent()).setAction("inline-data").putExtras(extras));
finish();
} else if (myExtras != null && myExtras.getBoolean("set-wallpaper")) {
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage(getString(R.string.cropimage_wallpaper_is_setting));
dialog.setIndeterminate(true);
dialog.setCancelable(false);
new AsyncTask<Bitmap, Void, Boolean>() {
protected void onPreExecute() {
dialog.show();
}
protected void onPostExecute(Boolean result) {
reset();
if (result)
showToast(R.string.cropimage_wallpaper_success);
else
showToast(R.string.cropimage_wallpaper_failed);
CropImage.this.finish();
}
protected void onCancelled() {
reset();
}
@Override
protected Boolean doInBackground(Bitmap... image) {
WallpaperManager myWallpaperManager = WallpaperManager.getInstance(CropImage.this);
if (image != null && image[0] != null) {
try {
myWallpaperManager.setBitmap(image[0]);
} catch (IOException e) {
return false;
}
} else {
return false;
}
return true;
}
private void reset() {
dialog.dismiss();
}
private void showToast(int resID) {
Toast.makeText(CropImage.this, getString(resID), Toast.LENGTH_SHORT).show();
}
}.execute(croppedImage);
} else {
final Bitmap b = croppedImage;
BitmapUtils.startBackgroundJob(this, null, getString(R.string.cropimage_image_saving), new Runnable() {
public void run() {
saveOutput(b);
}
}, mHandler);
}
}
use of android.app.WallpaperManager in project AisenWeiBo by wangdan.
the class WallpaperDownloadTask method setWallpaper.
// 设置壁纸
public static void setWallpaper(Context context, File file, final OnProgressCallback callback) throws TaskException {
long time = System.currentTimeMillis();
try {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(GlobalContext.getInstance());
try {
DisplayMetrics dm = new DisplayMetrics();
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
int navigationBarHeight = SystemUtils.getNavigationBarHeight(context);
int wallpaperWidth = width;
int wallpaperHeight = height;
Options opts = new Options();
opts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), opts);
boolean decode = false;
if (opts.outHeight > height + navigationBarHeight) {
decode = true;
}
Logger.d(TAG, "image height = %d, screen height = %d", opts.outHeight, height + navigationBarHeight);
if (decode) {
// docode的时间会稍微长一点,idol3测试在1S内,所以先显示一个99%的进度
// if (progress <= 0) {
// progress = 999l;
// total = 1000l;
// publishProgress(progress, total);
// }
opts.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeStream(new FileInputStream(file));
Matrix matrix = new Matrix();
float scale = wallpaperHeight * 1.0f / bitmap.getHeight();
matrix.setScale(scale, scale);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
wallpaperManager.setStream(in);
if (callback != null) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
callback.onSetWallpaper(true);
}
});
}
Logger.d(TAG, "设置处理后的壁纸耗时 : " + (System.currentTimeMillis() - time));
return;
}
} catch (Throwable e) {
Logger.printExc(WallpaperDownloadTask.class, e);
}
wallpaperManager.setStream(new FileInputStream(file));
Logger.d(TAG, "设置原壁纸耗时 : " + (System.currentTimeMillis() - time));
if (callback != null) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
callback.onSetWallpaper(true);
}
});
}
} catch (Exception e) {
Logger.printExc(WallpaperDownloadTask.class, e);
if (callback != null) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
callback.onSetWallpaper(false);
}
});
}
throw new TaskException("", context.getResources().getString(R.string.txt_set_wallpaper_fail));
}
}
use of android.app.WallpaperManager in project AndroidStudy by tinggengyan.
the class WallpaperManagerProxy method setLockWallpaper.
/**
* 从参数所提供的路径,读取图片并设置为锁屏界面
*
* @param context 上下文
* @param path 图片的路径
* @return boolean 成功执行返回true
*/
public static boolean setLockWallpaper(Context context, String path) {
boolean result = false;
WallpaperManager wm = WallpaperManager.getInstance(context);
try {
InputStream is = new FileInputStream(path);
sSetLockWallpaper = getMethod(sSetLockWallpaper, sClass, "setStreamToLockWallpaper", InputStream.class);
if (wm != null) {
result = invoke(sSetLockWallpaper, wm, is);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return result;
}
Aggregations