diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageOrGifActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageOrGifActivity.java index ee4ecb8d..68631472 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageOrGifActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageOrGifActivity.java @@ -86,7 +86,6 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa @Named("default") SharedPreferences mSharedPreferences; private MediaDownloader mediaDownloader; - private WallpaperSetter wallpaperSetter; private boolean isActionBarHidden = false; private boolean isDownloading = false; private RequestManager glide; @@ -132,7 +131,6 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa mHaulerView.setOnDragDismissedListener(dragDirection -> finish()); mediaDownloader = new MediaDownloaderImpl(); - wallpaperSetter = new WallpaperSetter(); glide = Glide.with(this); @@ -256,7 +254,18 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa SetAsWallpaperBottomSheetFragment setAsWallpaperBottomSheetFragment = new SetAsWallpaperBottomSheetFragment(); setAsWallpaperBottomSheetFragment.show(getSupportFragmentManager(), setAsWallpaperBottomSheetFragment.getTag()); } 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; @@ -359,16 +368,49 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa @Override 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 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 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(); + } + }); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImgurMediaActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImgurMediaActivity.java index bf56ffac..e272ec51 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImgurMediaActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImgurMediaActivity.java @@ -10,6 +10,7 @@ import android.view.MenuItem; import android.view.View; import android.widget.LinearLayout; import android.widget.ProgressBar; +import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBar; @@ -313,21 +314,54 @@ public class ViewImgurMediaActivity extends AppCompatActivity implements SetAsWa @Override public void setToHomeScreen(int viewPagerPosition) { 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 public void setToLockScreen(int viewPagerPosition) { 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 public void setToBoth(int viewPagerPosition) { 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(); + } + }); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/SetAsWallpaperAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/SetAsWallpaperAsyncTask.java index 9e3aa8f5..b868e5d6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/SetAsWallpaperAsyncTask.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/SetAsWallpaperAsyncTask.java @@ -11,27 +11,24 @@ import android.view.WindowManager; import java.io.IOException; +import ml.docilealligator.infinityforreddit.WallpaperSetter; + public class SetAsWallpaperAsyncTask extends AsyncTask { private Bitmap bitmap; private int setTo; private WallpaperManager manager; private WindowManager windowManager; - private SetAsWallpaperAsyncTaskListener setAsWallpaperAsyncTaskListener; + private WallpaperSetter.SetWallpaperListener setWallpaperListener; private boolean success = true; public SetAsWallpaperAsyncTask(Bitmap bitmap, int setTo, WallpaperManager manager, WindowManager windowManager, - SetAsWallpaperAsyncTaskListener setAsWallpaperAsyncTaskListener) { + WallpaperSetter.SetWallpaperListener setWallpaperListener) { this.bitmap = bitmap; this.setTo = setTo; this.manager = manager; this.windowManager = windowManager; - this.setAsWallpaperAsyncTaskListener = setAsWallpaperAsyncTaskListener; - } - - public interface SetAsWallpaperAsyncTaskListener { - void success(); - void failed(); + this.setWallpaperListener = setWallpaperListener; } @Override @@ -61,17 +58,17 @@ public class SetAsWallpaperAsyncTask extends AsyncTask { } try { switch (setTo) { - case 0: + case WallpaperSetter.HOME_SCREEN: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { manager.setBitmap(bitmap, rect, true, WallpaperManager.FLAG_SYSTEM); } break; - case 1: + case WallpaperSetter.LOCK_SCREEN: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { manager.setBitmap(bitmap, rect, true, WallpaperManager.FLAG_LOCK); } break; - case 2: + case WallpaperSetter.BOTH_SCREENS: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { manager.setBitmap(bitmap, rect, true, WallpaperManager.FLAG_SYSTEM | WallpaperManager.FLAG_LOCK); } else { @@ -90,9 +87,9 @@ public class SetAsWallpaperAsyncTask extends AsyncTask { protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); if (success) { - setAsWallpaperAsyncTaskListener.success(); + setWallpaperListener.success(); } else { - setAsWallpaperAsyncTaskListener.failed(); + setWallpaperListener.failed(); } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/WallpaperSetter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/WallpaperSetter.java index 58884997..f1407bab 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/WallpaperSetter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/WallpaperSetter.java @@ -3,11 +3,7 @@ package ml.docilealligator.infinityforreddit; import android.app.WallpaperManager; import android.content.Context; import android.graphics.Bitmap; -import android.graphics.Rect; import android.graphics.drawable.Drawable; -import android.media.ThumbnailUtils; -import android.os.Build; -import android.util.DisplayMetrics; import android.view.WindowManager; 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.transition.Transition; -import java.io.IOException; +import ml.docilealligator.infinityforreddit.AsyncTask.SetAsWallpaperAsyncTask; public class WallpaperSetter { public static final int HOME_SCREEN = 0; public static final int LOCK_SCREEN = 1; public static final int BOTH_SCREENS = 2; - public void set(String url, int setTo, Context ctx) { - Toast.makeText(ctx, R.string.save_image_first, Toast.LENGTH_SHORT).show(); - Glide.with(ctx).asBitmap().load(url).into(new CustomTarget() { + public static void set(String url, int setTo, Context context, SetWallpaperListener setWallpaperListener) { + Toast.makeText(context, R.string.save_image_first, Toast.LENGTH_SHORT).show(); + WallpaperManager wallpaperManager = WallpaperManager.getInstance(context); + WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); + + Glide.with(context).asBitmap().load(url).into(new CustomTarget() { @Override public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition transition) { - WallpaperManager manager = WallpaperManager.getInstance(ctx); - - 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(); - } + new SetAsWallpaperAsyncTask(resource, setTo, wallpaperManager, windowManager, setWallpaperListener).execute(); } @Override @@ -89,4 +38,9 @@ public class WallpaperSetter { } }); } + + public interface SetWallpaperListener { + void success(); + void failed(); + } }