Refactor wallpaper setting feature, extracting to it's own class.

This commit is contained in:
Hermes Junior 2020-06-15 23:14:00 +02:00 committed by OHermesJunior
parent a0d6c220c5
commit 0d818c02e9
4 changed files with 112 additions and 163 deletions

View File

@ -1,25 +1,19 @@
package ml.docilealligator.infinityforreddit.Activity;
import android.Manifest;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.Html;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast;
@ -45,7 +39,6 @@ import com.bumptech.glide.request.transition.Transition;
import com.thefuntasty.hauler.HaulerView;
import java.io.File;
import java.io.IOException;
import javax.inject.Inject;
import javax.inject.Named;
@ -68,6 +61,7 @@ import ml.docilealligator.infinityforreddit.MediaDownloaderImpl;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.SetAsWallpaperCallback;
import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils;
import ml.docilealligator.infinityforreddit.WallpaperSetter;
import pl.droidsonroids.gif.GifImageView;
public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWallpaperCallback {
@ -91,6 +85,7 @@ 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;
@ -136,6 +131,7 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa
mHaulerView.setOnDragDismissedListener(dragDirection -> finish());
mediaDownloader = new MediaDownloaderImpl();
wallpaperSetter = new WallpaperSetter();
glide = Glide.with(this);
@ -253,7 +249,7 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa
SetAsWallpaperBottomSheetFragment setAsWallpaperBottomSheetFragment = new SetAsWallpaperBottomSheetFragment();
setAsWallpaperBottomSheetFragment.show(getSupportFragmentManager(), setAsWallpaperBottomSheetFragment.getTag());
} else {
setAsWallpaper(2);
wallpaperSetter.set(mImageUrl, WallpaperSetter.BOTH_SCREENS, this);
}
}
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
public void setToHomeScreen(int viewPagerPosition) {
setAsWallpaper(0);
wallpaperSetter.set(mImageUrl, WallpaperSetter.HOME_SCREEN, this);
}
@Override
public void setToLockScreen(int viewPagerPosition) {
setAsWallpaper(1);
wallpaperSetter.set(mImageUrl, WallpaperSetter.LOCK_SCREEN, this);
}
@Override
public void setToBoth(int viewPagerPosition) {
setAsWallpaper(2);
wallpaperSetter.set(mImageUrl, WallpaperSetter.BOTH_SCREENS, this);
}
}

View File

@ -1,27 +1,17 @@
package ml.docilealligator.infinityforreddit.Activity;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.media.ThumbnailUtils;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
@ -29,16 +19,12 @@ import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
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 org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import javax.inject.Inject;
@ -62,6 +48,7 @@ import ml.docilealligator.infinityforreddit.SetAsWallpaperCallback;
import ml.docilealligator.infinityforreddit.Utils.APIUtils;
import ml.docilealligator.infinityforreddit.Utils.JSONUtils;
import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils;
import ml.docilealligator.infinityforreddit.WallpaperSetter;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
@ -92,6 +79,7 @@ public class ViewImgurMediaActivity extends AppCompatActivity implements SetAsWa
@Inject
@Named("default")
SharedPreferences sharedPreferences;
private WallpaperSetter wallpaperSetter;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -128,6 +116,8 @@ public class ViewImgurMediaActivity extends AppCompatActivity implements SetAsWa
actionBar.setHomeAsUpIndicator(upArrow);
actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.transparentActionBarAndExoPlayerControllerColor)));
wallpaperSetter = new WallpaperSetter();
setTitle(" ");
String imgurId = getIntent().getStringExtra(EXTRA_IMGUR_ID);
@ -149,9 +139,7 @@ public class ViewImgurMediaActivity extends AppCompatActivity implements SetAsWa
setupViewPager();
}
errorLinearLayout.setOnClickListener(view -> {
fetchImgurMedia(imgurId);
});
errorLinearLayout.setOnClickListener(view -> fetchImgurMedia(imgurId));
}
private void fetchImgurMedia(String imgurId) {
@ -317,89 +305,24 @@ public class ViewImgurMediaActivity extends AppCompatActivity implements SetAsWa
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
public void setToHomeScreen(int viewPagerPosition) {
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
public void setToLockScreen(int viewPagerPosition) {
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
public void setToBoth(int viewPagerPosition) {
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 {
void success(ArrayList<ImgurMedia> images);
void failed();
}
@ -501,6 +425,7 @@ public class ViewImgurMediaActivity extends AppCompatActivity implements SetAsWa
interface ParseImgurImageAsyncTaskListener {
void success(ImgurMedia image);
void failed();
}
@ -541,4 +466,4 @@ public class ViewImgurMediaActivity extends AppCompatActivity implements SetAsWa
}
}
}
}
}

View File

@ -48,6 +48,7 @@ import ml.docilealligator.infinityforreddit.ImgurMedia;
import ml.docilealligator.infinityforreddit.MediaDownloader;
import ml.docilealligator.infinityforreddit.MediaDownloaderImpl;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.SetAsWallpaperCallback;
public class ViewImgurImageFragment extends Fragment {
@ -188,7 +189,7 @@ public class ViewImgurImageFragment extends Fragment {
setAsWallpaperBottomSheetFragment.setArguments(bundle);
setAsWallpaperBottomSheetFragment.show(activity.getSupportFragmentManager(), setAsWallpaperBottomSheetFragment.getTag());
} else {
activity.setAsWallpaper(imgurMedia.getLink(), 2);
((SetAsWallpaperCallback) activity).setToBoth(activity.getCurrentPagePosition());
}
return true;
}

View File

@ -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) {
}
});
}
}