mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2024-11-10 12:47:26 +01:00
Refactor wallpaper setting feature, extracting to it's own class.
This commit is contained in:
parent
a0d6c220c5
commit
0d818c02e9
@ -1,25 +1,19 @@
|
|||||||
package ml.docilealligator.infinityforreddit.Activity;
|
package ml.docilealligator.infinityforreddit.Activity;
|
||||||
|
|
||||||
import android.Manifest;
|
import android.Manifest;
|
||||||
import android.app.WallpaperManager;
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.Rect;
|
|
||||||
import android.graphics.drawable.ColorDrawable;
|
import android.graphics.drawable.ColorDrawable;
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
import android.media.ThumbnailUtils;
|
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.text.Html;
|
import android.text.Html;
|
||||||
import android.util.DisplayMetrics;
|
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.WindowManager;
|
|
||||||
import android.widget.LinearLayout;
|
import android.widget.LinearLayout;
|
||||||
import android.widget.ProgressBar;
|
import android.widget.ProgressBar;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
@ -45,7 +39,6 @@ import com.bumptech.glide.request.transition.Transition;
|
|||||||
import com.thefuntasty.hauler.HaulerView;
|
import com.thefuntasty.hauler.HaulerView;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.inject.Named;
|
import javax.inject.Named;
|
||||||
@ -68,6 +61,7 @@ import ml.docilealligator.infinityforreddit.MediaDownloaderImpl;
|
|||||||
import ml.docilealligator.infinityforreddit.R;
|
import ml.docilealligator.infinityforreddit.R;
|
||||||
import ml.docilealligator.infinityforreddit.SetAsWallpaperCallback;
|
import ml.docilealligator.infinityforreddit.SetAsWallpaperCallback;
|
||||||
import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils;
|
import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils;
|
||||||
|
import ml.docilealligator.infinityforreddit.WallpaperSetter;
|
||||||
import pl.droidsonroids.gif.GifImageView;
|
import pl.droidsonroids.gif.GifImageView;
|
||||||
|
|
||||||
public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWallpaperCallback {
|
public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWallpaperCallback {
|
||||||
@ -91,6 +85,7 @@ 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;
|
||||||
@ -136,6 +131,7 @@ 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);
|
||||||
|
|
||||||
@ -253,7 +249,7 @@ 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 {
|
||||||
setAsWallpaper(2);
|
wallpaperSetter.set(mImageUrl, WallpaperSetter.BOTH_SCREENS, this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -354,83 +350,18 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setAsWallpaper(int setTo) {
|
|
||||||
Toast.makeText(ViewImageOrGifActivity.this, R.string.save_image_first, Toast.LENGTH_SHORT).show();
|
|
||||||
glide.asBitmap().load(mImageUrl).into(new CustomTarget<Bitmap>() {
|
|
||||||
@Override
|
|
||||||
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
|
|
||||||
WallpaperManager manager = WallpaperManager.getInstance(ViewImageOrGifActivity.this);
|
|
||||||
|
|
||||||
DisplayMetrics metrics = new DisplayMetrics();
|
|
||||||
WindowManager windowManager = (WindowManager) 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 0:
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
||||||
manager.setBitmap(resource, rect, true, WallpaperManager.FLAG_SYSTEM);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
||||||
manager.setBitmap(resource, rect, true, WallpaperManager.FLAG_LOCK);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
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(ViewImageOrGifActivity.this, R.string.wallpaper_set, Toast.LENGTH_SHORT).show();
|
|
||||||
} catch (IOException e) {
|
|
||||||
Toast.makeText(ViewImageOrGifActivity.this, R.string.error_set_wallpaper, Toast.LENGTH_SHORT).show();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onLoadCleared(@Nullable Drawable placeholder) {
|
|
||||||
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setToHomeScreen(int viewPagerPosition) {
|
public void setToHomeScreen(int viewPagerPosition) {
|
||||||
setAsWallpaper(0);
|
wallpaperSetter.set(mImageUrl, WallpaperSetter.HOME_SCREEN, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setToLockScreen(int viewPagerPosition) {
|
public void setToLockScreen(int viewPagerPosition) {
|
||||||
setAsWallpaper(1);
|
wallpaperSetter.set(mImageUrl, WallpaperSetter.LOCK_SCREEN, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setToBoth(int viewPagerPosition) {
|
public void setToBoth(int viewPagerPosition) {
|
||||||
setAsWallpaper(2);
|
wallpaperSetter.set(mImageUrl, WallpaperSetter.BOTH_SCREENS, this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,27 +1,17 @@
|
|||||||
package ml.docilealligator.infinityforreddit.Activity;
|
package ml.docilealligator.infinityforreddit.Activity;
|
||||||
|
|
||||||
import android.app.WallpaperManager;
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.graphics.Bitmap;
|
|
||||||
import android.graphics.Rect;
|
|
||||||
import android.graphics.drawable.ColorDrawable;
|
import android.graphics.drawable.ColorDrawable;
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
import android.media.ThumbnailUtils;
|
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.os.Build;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.util.DisplayMetrics;
|
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.WindowManager;
|
|
||||||
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.annotation.Nullable;
|
|
||||||
import androidx.appcompat.app.ActionBar;
|
import androidx.appcompat.app.ActionBar;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
@ -29,16 +19,12 @@ import androidx.fragment.app.FragmentManager;
|
|||||||
import androidx.fragment.app.FragmentStatePagerAdapter;
|
import androidx.fragment.app.FragmentStatePagerAdapter;
|
||||||
import androidx.viewpager.widget.ViewPager;
|
import androidx.viewpager.widget.ViewPager;
|
||||||
|
|
||||||
import com.bumptech.glide.Glide;
|
|
||||||
import com.bumptech.glide.request.target.CustomTarget;
|
|
||||||
import com.bumptech.glide.request.transition.Transition;
|
|
||||||
import com.thefuntasty.hauler.HaulerView;
|
import com.thefuntasty.hauler.HaulerView;
|
||||||
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
@ -62,6 +48,7 @@ import ml.docilealligator.infinityforreddit.SetAsWallpaperCallback;
|
|||||||
import ml.docilealligator.infinityforreddit.Utils.APIUtils;
|
import ml.docilealligator.infinityforreddit.Utils.APIUtils;
|
||||||
import ml.docilealligator.infinityforreddit.Utils.JSONUtils;
|
import ml.docilealligator.infinityforreddit.Utils.JSONUtils;
|
||||||
import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils;
|
import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils;
|
||||||
|
import ml.docilealligator.infinityforreddit.WallpaperSetter;
|
||||||
import retrofit2.Call;
|
import retrofit2.Call;
|
||||||
import retrofit2.Callback;
|
import retrofit2.Callback;
|
||||||
import retrofit2.Response;
|
import retrofit2.Response;
|
||||||
@ -92,6 +79,7 @@ public class ViewImgurMediaActivity extends AppCompatActivity implements SetAsWa
|
|||||||
@Inject
|
@Inject
|
||||||
@Named("default")
|
@Named("default")
|
||||||
SharedPreferences sharedPreferences;
|
SharedPreferences sharedPreferences;
|
||||||
|
private WallpaperSetter wallpaperSetter;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
@ -128,6 +116,8 @@ public class ViewImgurMediaActivity extends AppCompatActivity implements SetAsWa
|
|||||||
actionBar.setHomeAsUpIndicator(upArrow);
|
actionBar.setHomeAsUpIndicator(upArrow);
|
||||||
actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.transparentActionBarAndExoPlayerControllerColor)));
|
actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.transparentActionBarAndExoPlayerControllerColor)));
|
||||||
|
|
||||||
|
wallpaperSetter = new WallpaperSetter();
|
||||||
|
|
||||||
setTitle(" ");
|
setTitle(" ");
|
||||||
|
|
||||||
String imgurId = getIntent().getStringExtra(EXTRA_IMGUR_ID);
|
String imgurId = getIntent().getStringExtra(EXTRA_IMGUR_ID);
|
||||||
@ -149,9 +139,7 @@ public class ViewImgurMediaActivity extends AppCompatActivity implements SetAsWa
|
|||||||
setupViewPager();
|
setupViewPager();
|
||||||
}
|
}
|
||||||
|
|
||||||
errorLinearLayout.setOnClickListener(view -> {
|
errorLinearLayout.setOnClickListener(view -> fetchImgurMedia(imgurId));
|
||||||
fetchImgurMedia(imgurId);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void fetchImgurMedia(String imgurId) {
|
private void fetchImgurMedia(String imgurId) {
|
||||||
@ -317,89 +305,24 @@ public class ViewImgurMediaActivity extends AppCompatActivity implements SetAsWa
|
|||||||
outState.putParcelableArrayList(IMGUR_IMAGES_STATE, images);
|
outState.putParcelableArrayList(IMGUR_IMAGES_STATE, images);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAsWallpaper(String link, int setTo) {
|
|
||||||
Toast.makeText(ViewImgurMediaActivity.this, R.string.save_image_first, Toast.LENGTH_SHORT).show();
|
|
||||||
Glide.with(this).asBitmap().load(link).into(new CustomTarget<Bitmap>() {
|
|
||||||
@Override
|
|
||||||
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
|
|
||||||
WallpaperManager manager = WallpaperManager.getInstance(ViewImgurMediaActivity.this);
|
|
||||||
|
|
||||||
DisplayMetrics metrics = new DisplayMetrics();
|
|
||||||
WindowManager windowManager = (WindowManager) 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 0:
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
||||||
manager.setBitmap(resource, rect, true, WallpaperManager.FLAG_SYSTEM);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
||||||
manager.setBitmap(resource, rect, true, WallpaperManager.FLAG_LOCK);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
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(ViewImgurMediaActivity.this, R.string.wallpaper_set, Toast.LENGTH_SHORT).show();
|
|
||||||
} catch (IOException e) {
|
|
||||||
Toast.makeText(ViewImgurMediaActivity.this, R.string.error_set_wallpaper, Toast.LENGTH_SHORT).show();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onLoadCleared(@Nullable Drawable placeholder) {
|
|
||||||
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@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()) {
|
||||||
setAsWallpaper(images.get(viewPagerPosition).getLink(), 0);
|
wallpaperSetter.set(images.get(viewPagerPosition).getLink(), WallpaperSetter.HOME_SCREEN, this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@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()) {
|
||||||
setAsWallpaper(images.get(viewPagerPosition).getLink(), 1);
|
wallpaperSetter.set(images.get(viewPagerPosition).getLink(), WallpaperSetter.LOCK_SCREEN, this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@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()) {
|
||||||
setAsWallpaper(images.get(viewPagerPosition).getLink(), 2);
|
wallpaperSetter.set(images.get(viewPagerPosition).getLink(), WallpaperSetter.BOTH_SCREENS, this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -447,6 +370,7 @@ public class ViewImgurMediaActivity extends AppCompatActivity implements SetAsWa
|
|||||||
|
|
||||||
interface ParseImgurImagesAsyncTaskListener {
|
interface ParseImgurImagesAsyncTaskListener {
|
||||||
void success(ArrayList<ImgurMedia> images);
|
void success(ArrayList<ImgurMedia> images);
|
||||||
|
|
||||||
void failed();
|
void failed();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -501,6 +425,7 @@ public class ViewImgurMediaActivity extends AppCompatActivity implements SetAsWa
|
|||||||
|
|
||||||
interface ParseImgurImageAsyncTaskListener {
|
interface ParseImgurImageAsyncTaskListener {
|
||||||
void success(ImgurMedia image);
|
void success(ImgurMedia image);
|
||||||
|
|
||||||
void failed();
|
void failed();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -541,4 +466,4 @@ public class ViewImgurMediaActivity extends AppCompatActivity implements SetAsWa
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -48,6 +48,7 @@ import ml.docilealligator.infinityforreddit.ImgurMedia;
|
|||||||
import ml.docilealligator.infinityforreddit.MediaDownloader;
|
import ml.docilealligator.infinityforreddit.MediaDownloader;
|
||||||
import ml.docilealligator.infinityforreddit.MediaDownloaderImpl;
|
import ml.docilealligator.infinityforreddit.MediaDownloaderImpl;
|
||||||
import ml.docilealligator.infinityforreddit.R;
|
import ml.docilealligator.infinityforreddit.R;
|
||||||
|
import ml.docilealligator.infinityforreddit.SetAsWallpaperCallback;
|
||||||
|
|
||||||
public class ViewImgurImageFragment extends Fragment {
|
public class ViewImgurImageFragment extends Fragment {
|
||||||
|
|
||||||
@ -188,7 +189,7 @@ public class ViewImgurImageFragment extends Fragment {
|
|||||||
setAsWallpaperBottomSheetFragment.setArguments(bundle);
|
setAsWallpaperBottomSheetFragment.setArguments(bundle);
|
||||||
setAsWallpaperBottomSheetFragment.show(activity.getSupportFragmentManager(), setAsWallpaperBottomSheetFragment.getTag());
|
setAsWallpaperBottomSheetFragment.show(activity.getSupportFragmentManager(), setAsWallpaperBottomSheetFragment.getTag());
|
||||||
} else {
|
} else {
|
||||||
activity.setAsWallpaper(imgurMedia.getLink(), 2);
|
((SetAsWallpaperCallback) activity).setToBoth(activity.getCurrentPagePosition());
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,92 @@
|
|||||||
|
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;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
|
|
||||||
|
import com.bumptech.glide.Glide;
|
||||||
|
import com.bumptech.glide.request.target.CustomTarget;
|
||||||
|
import com.bumptech.glide.request.transition.Transition;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
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<Bitmap>() {
|
||||||
|
@Override
|
||||||
|
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onLoadCleared(@Nullable Drawable placeholder) {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user