mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2024-11-07 11:17:25 +01:00
Set wallpaper in an AsyncTask.
This commit is contained in:
parent
f0f4cf42ff
commit
66f4db6b40
@ -86,7 +86,6 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa
|
|||||||
@Named("default")
|
@Named("default")
|
||||||
SharedPreferences mSharedPreferences;
|
SharedPreferences mSharedPreferences;
|
||||||
private MediaDownloader mediaDownloader;
|
private MediaDownloader mediaDownloader;
|
||||||
private WallpaperSetter wallpaperSetter;
|
|
||||||
private boolean isActionBarHidden = false;
|
private boolean isActionBarHidden = false;
|
||||||
private boolean isDownloading = false;
|
private boolean isDownloading = false;
|
||||||
private RequestManager glide;
|
private RequestManager glide;
|
||||||
@ -132,7 +131,6 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa
|
|||||||
mHaulerView.setOnDragDismissedListener(dragDirection -> finish());
|
mHaulerView.setOnDragDismissedListener(dragDirection -> finish());
|
||||||
|
|
||||||
mediaDownloader = new MediaDownloaderImpl();
|
mediaDownloader = new MediaDownloaderImpl();
|
||||||
wallpaperSetter = new WallpaperSetter();
|
|
||||||
|
|
||||||
glide = Glide.with(this);
|
glide = Glide.with(this);
|
||||||
|
|
||||||
@ -256,7 +254,18 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa
|
|||||||
SetAsWallpaperBottomSheetFragment setAsWallpaperBottomSheetFragment = new SetAsWallpaperBottomSheetFragment();
|
SetAsWallpaperBottomSheetFragment setAsWallpaperBottomSheetFragment = new SetAsWallpaperBottomSheetFragment();
|
||||||
setAsWallpaperBottomSheetFragment.show(getSupportFragmentManager(), setAsWallpaperBottomSheetFragment.getTag());
|
setAsWallpaperBottomSheetFragment.show(getSupportFragmentManager(), setAsWallpaperBottomSheetFragment.getTag());
|
||||||
} else {
|
} else {
|
||||||
wallpaperSetter.set(mImageUrl, WallpaperSetter.BOTH_SCREENS, this);
|
WallpaperSetter.set(mImageUrl, WallpaperSetter.BOTH_SCREENS, this,
|
||||||
|
new WallpaperSetter.SetWallpaperListener() {
|
||||||
|
@Override
|
||||||
|
public void success() {
|
||||||
|
Toast.makeText(ViewImageOrGifActivity.this, R.string.wallpaper_set, Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void failed() {
|
||||||
|
Toast.makeText(ViewImageOrGifActivity.this, R.string.error_set_wallpaper, Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -359,16 +368,49 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setToHomeScreen(int viewPagerPosition) {
|
public void setToHomeScreen(int viewPagerPosition) {
|
||||||
wallpaperSetter.set(mImageUrl, WallpaperSetter.HOME_SCREEN, this);
|
WallpaperSetter.set(mImageUrl, WallpaperSetter.HOME_SCREEN, this,
|
||||||
|
new WallpaperSetter.SetWallpaperListener() {
|
||||||
|
@Override
|
||||||
|
public void success() {
|
||||||
|
Toast.makeText(ViewImageOrGifActivity.this, R.string.wallpaper_set, Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void failed() {
|
||||||
|
Toast.makeText(ViewImageOrGifActivity.this, R.string.error_set_wallpaper, Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setToLockScreen(int viewPagerPosition) {
|
public void setToLockScreen(int viewPagerPosition) {
|
||||||
wallpaperSetter.set(mImageUrl, WallpaperSetter.LOCK_SCREEN, this);
|
WallpaperSetter.set(mImageUrl, WallpaperSetter.LOCK_SCREEN, this,
|
||||||
|
new WallpaperSetter.SetWallpaperListener() {
|
||||||
|
@Override
|
||||||
|
public void success() {
|
||||||
|
Toast.makeText(ViewImageOrGifActivity.this, R.string.wallpaper_set, Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void failed() {
|
||||||
|
Toast.makeText(ViewImageOrGifActivity.this, R.string.error_set_wallpaper, Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setToBoth(int viewPagerPosition) {
|
public void setToBoth(int viewPagerPosition) {
|
||||||
wallpaperSetter.set(mImageUrl, WallpaperSetter.BOTH_SCREENS, this);
|
WallpaperSetter.set(mImageUrl, WallpaperSetter.BOTH_SCREENS, this,
|
||||||
|
new WallpaperSetter.SetWallpaperListener() {
|
||||||
|
@Override
|
||||||
|
public void success() {
|
||||||
|
Toast.makeText(ViewImageOrGifActivity.this, R.string.wallpaper_set, Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void failed() {
|
||||||
|
Toast.makeText(ViewImageOrGifActivity.this, R.string.error_set_wallpaper, Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,7 @@ import android.view.MenuItem;
|
|||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.LinearLayout;
|
import android.widget.LinearLayout;
|
||||||
import android.widget.ProgressBar;
|
import android.widget.ProgressBar;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.appcompat.app.ActionBar;
|
import androidx.appcompat.app.ActionBar;
|
||||||
@ -313,21 +314,54 @@ public class ViewImgurMediaActivity extends AppCompatActivity implements SetAsWa
|
|||||||
@Override
|
@Override
|
||||||
public void setToHomeScreen(int viewPagerPosition) {
|
public void setToHomeScreen(int viewPagerPosition) {
|
||||||
if (images != null && viewPagerPosition >= 0 && viewPagerPosition < images.size()) {
|
if (images != null && viewPagerPosition >= 0 && viewPagerPosition < images.size()) {
|
||||||
wallpaperSetter.set(images.get(viewPagerPosition).getLink(), WallpaperSetter.HOME_SCREEN, this);
|
WallpaperSetter.set(images.get(viewPagerPosition).getLink(), WallpaperSetter.HOME_SCREEN, this,
|
||||||
|
new WallpaperSetter.SetWallpaperListener() {
|
||||||
|
@Override
|
||||||
|
public void success() {
|
||||||
|
Toast.makeText(ViewImgurMediaActivity.this, R.string.wallpaper_set, Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void failed() {
|
||||||
|
Toast.makeText(ViewImgurMediaActivity.this, R.string.error_set_wallpaper, Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setToLockScreen(int viewPagerPosition) {
|
public void setToLockScreen(int viewPagerPosition) {
|
||||||
if (images != null && viewPagerPosition >= 0 && viewPagerPosition < images.size()) {
|
if (images != null && viewPagerPosition >= 0 && viewPagerPosition < images.size()) {
|
||||||
wallpaperSetter.set(images.get(viewPagerPosition).getLink(), WallpaperSetter.LOCK_SCREEN, this);
|
WallpaperSetter.set(images.get(viewPagerPosition).getLink(), WallpaperSetter.LOCK_SCREEN, this,
|
||||||
|
new WallpaperSetter.SetWallpaperListener() {
|
||||||
|
@Override
|
||||||
|
public void success() {
|
||||||
|
Toast.makeText(ViewImgurMediaActivity.this, R.string.wallpaper_set, Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void failed() {
|
||||||
|
Toast.makeText(ViewImgurMediaActivity.this, R.string.error_set_wallpaper, Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setToBoth(int viewPagerPosition) {
|
public void setToBoth(int viewPagerPosition) {
|
||||||
if (images != null && viewPagerPosition >= 0 && viewPagerPosition < images.size()) {
|
if (images != null && viewPagerPosition >= 0 && viewPagerPosition < images.size()) {
|
||||||
wallpaperSetter.set(images.get(viewPagerPosition).getLink(), WallpaperSetter.BOTH_SCREENS, this);
|
WallpaperSetter.set(images.get(viewPagerPosition).getLink(), WallpaperSetter.BOTH_SCREENS, this,
|
||||||
|
new WallpaperSetter.SetWallpaperListener() {
|
||||||
|
@Override
|
||||||
|
public void success() {
|
||||||
|
Toast.makeText(ViewImgurMediaActivity.this, R.string.wallpaper_set, Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void failed() {
|
||||||
|
Toast.makeText(ViewImgurMediaActivity.this, R.string.error_set_wallpaper, Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,27 +11,24 @@ import android.view.WindowManager;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import ml.docilealligator.infinityforreddit.WallpaperSetter;
|
||||||
|
|
||||||
public class SetAsWallpaperAsyncTask extends AsyncTask<Void, Void, Void> {
|
public class SetAsWallpaperAsyncTask extends AsyncTask<Void, Void, Void> {
|
||||||
|
|
||||||
private Bitmap bitmap;
|
private Bitmap bitmap;
|
||||||
private int setTo;
|
private int setTo;
|
||||||
private WallpaperManager manager;
|
private WallpaperManager manager;
|
||||||
private WindowManager windowManager;
|
private WindowManager windowManager;
|
||||||
private SetAsWallpaperAsyncTaskListener setAsWallpaperAsyncTaskListener;
|
private WallpaperSetter.SetWallpaperListener setWallpaperListener;
|
||||||
private boolean success = true;
|
private boolean success = true;
|
||||||
|
|
||||||
public SetAsWallpaperAsyncTask(Bitmap bitmap, int setTo, WallpaperManager manager, WindowManager windowManager,
|
public SetAsWallpaperAsyncTask(Bitmap bitmap, int setTo, WallpaperManager manager, WindowManager windowManager,
|
||||||
SetAsWallpaperAsyncTaskListener setAsWallpaperAsyncTaskListener) {
|
WallpaperSetter.SetWallpaperListener setWallpaperListener) {
|
||||||
this.bitmap = bitmap;
|
this.bitmap = bitmap;
|
||||||
this.setTo = setTo;
|
this.setTo = setTo;
|
||||||
this.manager = manager;
|
this.manager = manager;
|
||||||
this.windowManager = windowManager;
|
this.windowManager = windowManager;
|
||||||
this.setAsWallpaperAsyncTaskListener = setAsWallpaperAsyncTaskListener;
|
this.setWallpaperListener = setWallpaperListener;
|
||||||
}
|
|
||||||
|
|
||||||
public interface SetAsWallpaperAsyncTaskListener {
|
|
||||||
void success();
|
|
||||||
void failed();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -61,17 +58,17 @@ public class SetAsWallpaperAsyncTask extends AsyncTask<Void, Void, Void> {
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
switch (setTo) {
|
switch (setTo) {
|
||||||
case 0:
|
case WallpaperSetter.HOME_SCREEN:
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||||
manager.setBitmap(bitmap, rect, true, WallpaperManager.FLAG_SYSTEM);
|
manager.setBitmap(bitmap, rect, true, WallpaperManager.FLAG_SYSTEM);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 1:
|
case WallpaperSetter.LOCK_SCREEN:
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||||
manager.setBitmap(bitmap, rect, true, WallpaperManager.FLAG_LOCK);
|
manager.setBitmap(bitmap, rect, true, WallpaperManager.FLAG_LOCK);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 2:
|
case WallpaperSetter.BOTH_SCREENS:
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||||
manager.setBitmap(bitmap, rect, true, WallpaperManager.FLAG_SYSTEM | WallpaperManager.FLAG_LOCK);
|
manager.setBitmap(bitmap, rect, true, WallpaperManager.FLAG_SYSTEM | WallpaperManager.FLAG_LOCK);
|
||||||
} else {
|
} else {
|
||||||
@ -90,9 +87,9 @@ public class SetAsWallpaperAsyncTask extends AsyncTask<Void, Void, Void> {
|
|||||||
protected void onPostExecute(Void aVoid) {
|
protected void onPostExecute(Void aVoid) {
|
||||||
super.onPostExecute(aVoid);
|
super.onPostExecute(aVoid);
|
||||||
if (success) {
|
if (success) {
|
||||||
setAsWallpaperAsyncTaskListener.success();
|
setWallpaperListener.success();
|
||||||
} else {
|
} else {
|
||||||
setAsWallpaperAsyncTaskListener.failed();
|
setWallpaperListener.failed();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,11 +3,7 @@ package ml.docilealligator.infinityforreddit;
|
|||||||
import android.app.WallpaperManager;
|
import android.app.WallpaperManager;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.Rect;
|
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
import android.media.ThumbnailUtils;
|
|
||||||
import android.os.Build;
|
|
||||||
import android.util.DisplayMetrics;
|
|
||||||
import android.view.WindowManager;
|
import android.view.WindowManager;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
@ -18,69 +14,22 @@ import com.bumptech.glide.Glide;
|
|||||||
import com.bumptech.glide.request.target.CustomTarget;
|
import com.bumptech.glide.request.target.CustomTarget;
|
||||||
import com.bumptech.glide.request.transition.Transition;
|
import com.bumptech.glide.request.transition.Transition;
|
||||||
|
|
||||||
import java.io.IOException;
|
import ml.docilealligator.infinityforreddit.AsyncTask.SetAsWallpaperAsyncTask;
|
||||||
|
|
||||||
public class WallpaperSetter {
|
public class WallpaperSetter {
|
||||||
public static final int HOME_SCREEN = 0;
|
public static final int HOME_SCREEN = 0;
|
||||||
public static final int LOCK_SCREEN = 1;
|
public static final int LOCK_SCREEN = 1;
|
||||||
public static final int BOTH_SCREENS = 2;
|
public static final int BOTH_SCREENS = 2;
|
||||||
|
|
||||||
public void set(String url, int setTo, Context ctx) {
|
public static void set(String url, int setTo, Context context, SetWallpaperListener setWallpaperListener) {
|
||||||
Toast.makeText(ctx, R.string.save_image_first, Toast.LENGTH_SHORT).show();
|
Toast.makeText(context, R.string.save_image_first, Toast.LENGTH_SHORT).show();
|
||||||
Glide.with(ctx).asBitmap().load(url).into(new CustomTarget<Bitmap>() {
|
WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
|
||||||
|
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
|
||||||
|
|
||||||
|
Glide.with(context).asBitmap().load(url).into(new CustomTarget<Bitmap>() {
|
||||||
@Override
|
@Override
|
||||||
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
|
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
|
||||||
WallpaperManager manager = WallpaperManager.getInstance(ctx);
|
new SetAsWallpaperAsyncTask(resource, setTo, wallpaperManager, windowManager, setWallpaperListener).execute();
|
||||||
|
|
||||||
DisplayMetrics metrics = new DisplayMetrics();
|
|
||||||
WindowManager windowManager = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
|
|
||||||
|
|
||||||
Rect rect = null;
|
|
||||||
|
|
||||||
if (windowManager != null) {
|
|
||||||
windowManager.getDefaultDisplay().getMetrics(metrics);
|
|
||||||
int height = metrics.heightPixels;
|
|
||||||
int width = metrics.widthPixels;
|
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
|
|
||||||
resource = ThumbnailUtils.extractThumbnail(resource, width, height);
|
|
||||||
}
|
|
||||||
|
|
||||||
float imageAR = (float) resource.getWidth() / (float) resource.getHeight();
|
|
||||||
float screenAR = (float) width / (float) height;
|
|
||||||
|
|
||||||
if (imageAR > screenAR) {
|
|
||||||
int desiredWidth = (int) (resource.getHeight() * screenAR);
|
|
||||||
rect = new Rect((resource.getWidth() - desiredWidth) / 2, 0, resource.getWidth(), resource.getHeight());
|
|
||||||
} else {
|
|
||||||
int desiredHeight = (int) (resource.getWidth() / screenAR);
|
|
||||||
rect = new Rect(0, (resource.getHeight() - desiredHeight) / 2, resource.getWidth(), (resource.getHeight() + desiredHeight) / 2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
switch (setTo) {
|
|
||||||
case HOME_SCREEN:
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
||||||
manager.setBitmap(resource, rect, true, WallpaperManager.FLAG_SYSTEM);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case LOCK_SCREEN:
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
||||||
manager.setBitmap(resource, rect, true, WallpaperManager.FLAG_LOCK);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case BOTH_SCREENS:
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
||||||
manager.setBitmap(resource, rect, true, WallpaperManager.FLAG_SYSTEM | WallpaperManager.FLAG_LOCK);
|
|
||||||
} else {
|
|
||||||
manager.setBitmap(resource);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
Toast.makeText(ctx, R.string.wallpaper_set, Toast.LENGTH_SHORT).show();
|
|
||||||
} catch (IOException e) {
|
|
||||||
Toast.makeText(ctx, R.string.error_set_wallpaper, Toast.LENGTH_SHORT).show();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -89,4 +38,9 @@ public class WallpaperSetter {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface SetWallpaperListener {
|
||||||
|
void success();
|
||||||
|
void failed();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user