Skip to content

Commit

Permalink
Fixed #85
Browse files Browse the repository at this point in the history
  • Loading branch information
danimahardhika authored Sep 24, 2017
1 parent 2623917 commit b4c9d01
Showing 1 changed file with 27 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageSize;

import java.lang.ref.WeakReference;
import java.util.Locale;
import java.util.concurrent.Executor;

Expand All @@ -53,15 +54,15 @@

public class WallpaperApplyTask extends AsyncTask<Void, Void, Boolean> implements WallpaperPropertiesLoaderTask.Callback{

private final Context mContext;
private final WeakReference<Context> mContext;
private Apply mApply;
private RectF mRectF;
private Executor mExecutor;
private Wallpaper mWallpaper;
private MaterialDialog mDialog;

private WallpaperApplyTask(Context context) {
mContext = context;
mContext = new WeakReference<>(context);
}

public WallpaperApplyTask to(Apply apply) {
Expand All @@ -87,12 +88,12 @@ public AsyncTask start(@NonNull Executor executor) {
if (mDialog == null) {
int color = mWallpaper.getColor();
if (color == 0) {
color = ColorHelper.getAttributeColor(mContext, R.attr.colorAccent);
color = ColorHelper.getAttributeColor(mContext.get(), R.attr.colorAccent);
}

final MaterialDialog.Builder builder = new MaterialDialog.Builder(mContext);
final MaterialDialog.Builder builder = new MaterialDialog.Builder(mContext.get());
builder.widgetColor(color)
.typeface(TypefaceHelper.getMedium(mContext), TypefaceHelper.getRegular(mContext))
.typeface(TypefaceHelper.getMedium(mContext.get()), TypefaceHelper.getRegular(mContext.get()))
.progress(true, 0)
.cancelable(false)
.progressIndeterminateStyle(true)
Expand All @@ -116,7 +117,7 @@ public AsyncTask start(@NonNull Executor executor) {
}

if (mWallpaper.getDimensions() == null) {
return WallpaperPropertiesLoaderTask.prepare(mContext)
return WallpaperPropertiesLoaderTask.prepare(mContext.get())
.wallpaper(mWallpaper)
.callback(this)
.start(AsyncTask.THREAD_POOL_EXECUTOR);
Expand All @@ -136,7 +137,7 @@ public void onPropertiesReceived(Wallpaper wallpaper) {
mDialog.dismiss();
LogUtil.e("WallpaperApply cancelled, unable to retrieve wallpaper dimensions");

Toast.makeText(mContext, R.string.wallpaper_apply_failed,
Toast.makeText(mContext.get(), R.string.wallpaper_apply_failed,
Toast.LENGTH_LONG).show();
return;
}
Expand All @@ -149,18 +150,18 @@ protected Boolean doInBackground(Void... voids) {
while (!isCancelled()) {
try {
Thread.sleep(1);
ImageSize imageSize = WallpaperHelper.getTargetSize(mContext);
ImageSize imageSize = WallpaperHelper.getTargetSize(mContext.get());

LogUtil.d("original rectF: " +mRectF);

if (mRectF != null && Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
Point point = WindowHelper.getScreenSize(mContext);
int height = point.y - WindowHelper.getStatusBarHeight(mContext) - WindowHelper.getNavigationBarHeight(mContext);
Point point = WindowHelper.getScreenSize(mContext.get());
int height = point.y - WindowHelper.getStatusBarHeight(mContext.get()) - WindowHelper.getNavigationBarHeight(mContext.get());
float heightFactor = (float) imageSize.getHeight() / (float) height;
mRectF = WallpaperHelper.getScaledRectF(mRectF, heightFactor, 1f);
}

if (mRectF == null && Preferences.get(mContext).isCropWallpaper()) {
if (mRectF == null && Preferences.get(mContext.get()).isCropWallpaper()) {
/*
* Create a center crop rectF if wallpaper applied from grid, not opening the preview first
*/
Expand Down Expand Up @@ -239,13 +240,12 @@ protected Boolean doInBackground(Void... voids) {
publishProgress();

Bitmap bitmap = loadedBitmap;
WallpaperManager manager = WallpaperManager.getInstance(mContext);
if (Preferences.get(mContext).isCropWallpaper() && adjustedRectF != null) {
if (Preferences.get(mContext.get()).isCropWallpaper() && adjustedRectF != null) {
LogUtil.d("rectF: " +adjustedRectF);
/*
* Cropping bitmap
*/
ImageSize targetSize = WallpaperHelper.getTargetSize(mContext);
ImageSize targetSize = WallpaperHelper.getTargetSize(mContext.get());

int targetWidth = Double.valueOf(
((double) loadedBitmap.getHeight() / (double) targetSize.getHeight())
Expand Down Expand Up @@ -274,22 +274,25 @@ protected Boolean doInBackground(Void... voids) {
/*
* Final bitmap generated
*/
loadedBitmap.recycle();
LogUtil.d(String.format(Locale.getDefault(), "generated bitmap: %d x %d ",
bitmap.getWidth(), bitmap.getHeight()));

if (mApply == Apply.HOMESCREEN) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
manager.setBitmap(bitmap, null, true, WallpaperManager.FLAG_SYSTEM);
WallpaperManager.getInstance(mContext.get().getApplicationContext()).setBitmap(
bitmap, null, true, WallpaperManager.FLAG_SYSTEM);
return true;
}

manager.setBitmap(bitmap);
WallpaperManager.getInstance(mContext.get().getApplicationContext()).setBitmap(bitmap);
return true;
}

if (mApply == Apply.LOCKSCREEN) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
manager.setBitmap(bitmap, null, true, WallpaperManager.FLAG_LOCK);
WallpaperManager.getInstance(mContext.get().getApplicationContext()).setBitmap(
bitmap, null, true, WallpaperManager.FLAG_LOCK);
return true;
}
}
Expand Down Expand Up @@ -333,18 +336,18 @@ protected void onProgressUpdate(Void... values) {
@Override
protected void onCancelled(Boolean aBoolean) {
super.onCancelled(aBoolean);
Toast.makeText(mContext, R.string.wallpaper_apply_cancelled,
Toast.makeText(mContext.get(), R.string.wallpaper_apply_cancelled,
Toast.LENGTH_LONG).show();
}

@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
if (mContext == null) {
if (mContext.get() == null) {
return;
}

if (((AppCompatActivity) mContext).isFinishing()) {
if (((AppCompatActivity) mContext.get()).isFinishing()) {
return;
}

Expand All @@ -353,16 +356,16 @@ protected void onPostExecute(Boolean aBoolean) {
}

if (aBoolean) {
CafeBar.builder(mContext)
CafeBar.builder(mContext.get())
.theme(CafeBarTheme.Custom(ColorHelper.getAttributeColor(
mContext, R.attr.card_background)))
.contentTypeface(TypefaceHelper.getRegular(mContext))
mContext.get(), R.attr.card_background)))
.contentTypeface(TypefaceHelper.getRegular(mContext.get()))
.floating(true)
.fitSystemWindow()
.content(R.string.wallpaper_applied)
.show();
} else {
Toast.makeText(mContext, R.string.wallpaper_apply_failed,
Toast.makeText(mContext.get(), R.string.wallpaper_apply_failed,
Toast.LENGTH_LONG).show();
}
}
Expand Down

0 comments on commit b4c9d01

Please sign in to comment.