diff options
Diffstat (limited to '')
6 files changed, 302 insertions, 8 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewGIFActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewGIFActivity.java index cafbaed4..a1802a90 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewGIFActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewGIFActivity.java @@ -218,7 +218,7 @@ public class ViewGIFActivity extends AppCompatActivity { return true; case R.id.action_share_view_gif_activity: - Toast.makeText(ViewGIFActivity.this, R.string.save_gif_before_sharing, Toast.LENGTH_SHORT).show(); + Toast.makeText(ViewGIFActivity.this, R.string.save_gif_first, Toast.LENGTH_SHORT).show(); glide.asGif().load(mImageUrl).listener(new RequestListener<GifDrawable>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageActivity.java index ea3bde7f..76acf253 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageActivity.java @@ -2,21 +2,26 @@ package ml.docilealligator.infinityforreddit.Activity; import android.Manifest; import android.app.DownloadManager; +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.os.Environment; 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; @@ -41,6 +46,7 @@ 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; @@ -48,6 +54,7 @@ import javax.inject.Named; import butterknife.BindView; import butterknife.ButterKnife; import ml.docilealligator.infinityforreddit.AsyncTask.SaveImageToFileAsyncTask; +import ml.docilealligator.infinityforreddit.BottomSheetFragment.SetAsWallpaperBottomSheetFragment; import ml.docilealligator.infinityforreddit.BuildConfig; import ml.docilealligator.infinityforreddit.Font.ContentFontFamily; import ml.docilealligator.infinityforreddit.Font.ContentFontStyle; @@ -57,9 +64,10 @@ import ml.docilealligator.infinityforreddit.Font.TitleFontFamily; import ml.docilealligator.infinityforreddit.Font.TitleFontStyle; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.SetAsWallpaperCallback; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; -public class ViewImageActivity extends AppCompatActivity { +public class ViewImageActivity extends AppCompatActivity implements SetAsWallpaperCallback { public static final String IMAGE_URL_KEY = "IUK"; public static final String FILE_NAME_KEY = "FNK"; @@ -224,7 +232,7 @@ public class ViewImageActivity extends AppCompatActivity { @Override public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) { if (getExternalCacheDir() != null) { - Toast.makeText(ViewImageActivity.this, R.string.save_image_before_sharing, Toast.LENGTH_SHORT).show(); + Toast.makeText(ViewImageActivity.this, R.string.save_image_first, Toast.LENGTH_SHORT).show(); new SaveImageToFileAsyncTask(resource, getExternalCacheDir().getPath(), mImageFileName, new SaveImageToFileAsyncTask.SaveImageToFileAsyncTaskListener() { @Override @@ -257,6 +265,15 @@ public class ViewImageActivity extends AppCompatActivity { } }); return true; + + case R.id.action_set_wallpaper_view_image_activity: + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + SetAsWallpaperBottomSheetFragment setAsWallpaperBottomSheetFragment = new SetAsWallpaperBottomSheetFragment(); + setAsWallpaperBottomSheetFragment.show(getSupportFragmentManager(), setAsWallpaperBottomSheetFragment.getTag()); + } else { + setAsWallpaper(2); + } + return true; } return false; @@ -317,4 +334,84 @@ public class ViewImageActivity extends AppCompatActivity { manager.enqueue(request); Toast.makeText(this, R.string.download_started, Toast.LENGTH_SHORT).show(); } + + private void setAsWallpaper(int setTo) { + Toast.makeText(ViewImageActivity.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(ViewImageActivity.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(ViewImageActivity.this, R.string.wallpaper_set, Toast.LENGTH_SHORT).show(); + } catch (IOException e) { + Toast.makeText(ViewImageActivity.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); + } + + @Override + public void setToLockScreen(int viewPagerPosition) { + setAsWallpaper(1); + } + + @Override + public void setToBoth(int viewPagerPosition) { + setAsWallpaper(2); + } } 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 f38315db..c4b0e38d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImgurMediaActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImgurMediaActivity.java @@ -1,17 +1,27 @@ 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; @@ -19,12 +29,16 @@ 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; @@ -44,6 +58,7 @@ import ml.docilealligator.infinityforreddit.Fragment.ViewImgurVideoFragment; import ml.docilealligator.infinityforreddit.ImgurMedia; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.SetAsWallpaperCallback; import ml.docilealligator.infinityforreddit.Utils.APIUtils; import ml.docilealligator.infinityforreddit.Utils.JSONUtils; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; @@ -52,7 +67,7 @@ import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; -public class ViewImgurMediaActivity extends AppCompatActivity { +public class ViewImgurMediaActivity extends AppCompatActivity implements SetAsWallpaperCallback { public static final String EXTRA_IMGUR_TYPE = "EIT"; public static final String EXTRA_IMGUR_ID = "EII"; @@ -300,6 +315,90 @@ public class ViewImgurMediaActivity extends AppCompatActivity { 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) { + setAsWallpaper(images.get(viewPagerPosition).getLink(), 0); + } + + @Override + public void setToLockScreen(int viewPagerPosition) { + setAsWallpaper(images.get(viewPagerPosition).getLink(), 1); + } + + @Override + public void setToBoth(int viewPagerPosition) { + setAsWallpaper(images.get(viewPagerPosition).getLink(), 2); + } + + public int getCurrentPagePosition() { + return viewPager.getCurrentItem(); + } + private class SectionsPagerAdapter extends FragmentStatePagerAdapter { SectionsPagerAdapter(@NonNull FragmentManager fm) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/BottomSheetFragment/SetAsWallpaperBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/BottomSheetFragment/SetAsWallpaperBottomSheetFragment.java new file mode 100644 index 00000000..db6e80e9 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/BottomSheetFragment/SetAsWallpaperBottomSheetFragment.java @@ -0,0 +1,79 @@ +package ml.docilealligator.infinityforreddit.BottomSheetFragment; + +import android.app.Activity; +import android.content.Context; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import androidx.annotation.NonNull; + +import com.deishelon.roundedbottomsheet.RoundedBottomSheetDialogFragment; + +import butterknife.BindView; +import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.SetAsWallpaperCallback; + +public class SetAsWallpaperBottomSheetFragment extends RoundedBottomSheetDialogFragment { + + public static final String EXTRA_VIEW_PAGER_POSITION = "EVPP"; + + @BindView(R.id.home_screen_text_view_set_as_wallpaper_bottom_sheet_fragment) + TextView homeScreenTextvView; + @BindView(R.id.lock_screen_text_view_set_as_wallpaper_bottom_sheet_fragment) + TextView lockScreenTextView; + @BindView(R.id.both_text_view_set_as_wallpaper_bottom_sheet_fragment) + TextView bothTextView; + private Activity mActivity; + + public SetAsWallpaperBottomSheetFragment() { + // Required empty public constructor + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View rootView = inflater.inflate(R.layout.fragment_set_as_wallpaper_bottom_sheet, container, false); + ButterKnife.bind(this, rootView); + + Bundle bundle = getArguments(); + int viewPagerPosition = bundle == null ? -1 : bundle.getInt(EXTRA_VIEW_PAGER_POSITION); + + bothTextView.setOnClickListener(view -> { + if (mActivity instanceof SetAsWallpaperCallback) { + ((SetAsWallpaperCallback) mActivity).setToBoth(viewPagerPosition); + } + dismiss(); + }); + + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { + homeScreenTextvView.setVisibility(View.VISIBLE); + lockScreenTextView.setVisibility(View.VISIBLE); + + homeScreenTextvView.setOnClickListener(view -> { + if (mActivity instanceof SetAsWallpaperCallback) { + ((SetAsWallpaperCallback) mActivity).setToHomeScreen(viewPagerPosition); + } + dismiss(); + }); + + lockScreenTextView.setOnClickListener(view -> { + if (mActivity instanceof SetAsWallpaperCallback) { + ((SetAsWallpaperCallback) mActivity).setToLockScreen(viewPagerPosition); + } + dismiss(); + }); + } + + return rootView; + } + + @Override + public void onAttach(@NonNull Context context) { + super.onAttach(context); + this.mActivity = (Activity) context; + } +}
\ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurImageFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurImageFragment.java index ae047287..0319c904 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurImageFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurImageFragment.java @@ -1,7 +1,6 @@ package ml.docilealligator.infinityforreddit.Fragment; import android.Manifest; -import android.app.Activity; import android.app.DownloadManager; import android.content.Context; import android.content.Intent; @@ -43,7 +42,9 @@ import java.io.File; import butterknife.BindView; import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.Activity.ViewImgurMediaActivity; import ml.docilealligator.infinityforreddit.AsyncTask.SaveImageToFileAsyncTask; +import ml.docilealligator.infinityforreddit.BottomSheetFragment.SetAsWallpaperBottomSheetFragment; import ml.docilealligator.infinityforreddit.BuildConfig; import ml.docilealligator.infinityforreddit.ImgurMedia; import ml.docilealligator.infinityforreddit.R; @@ -60,7 +61,7 @@ public class ViewImgurImageFragment extends Fragment { @BindView(R.id.load_image_error_linear_layout_view_imgur_image_fragment) LinearLayout errorLinearLayout; - private Activity activity; + private ViewImgurMediaActivity activity; private RequestManager glide; private ImgurMedia imgurMedia; private boolean isDownloading = false; @@ -143,7 +144,7 @@ public class ViewImgurImageFragment extends Fragment { @Override public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) { if (activity.getExternalCacheDir() != null) { - Toast.makeText(activity, R.string.save_image_before_sharing, Toast.LENGTH_SHORT).show(); + Toast.makeText(activity, R.string.save_image_first, Toast.LENGTH_SHORT).show(); new SaveImageToFileAsyncTask(resource, activity.getExternalCacheDir().getPath(), imgurMedia.getFileName(), new SaveImageToFileAsyncTask.SaveImageToFileAsyncTaskListener() { @@ -177,6 +178,17 @@ public class ViewImgurImageFragment extends Fragment { } }); return true; + case R.id.action_set_wallpaper_view_imgur_image_fragments: + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + SetAsWallpaperBottomSheetFragment setAsWallpaperBottomSheetFragment = new SetAsWallpaperBottomSheetFragment(); + Bundle bundle = new Bundle(); + bundle.putInt(SetAsWallpaperBottomSheetFragment.EXTRA_VIEW_PAGER_POSITION, activity.getCurrentPagePosition()); + setAsWallpaperBottomSheetFragment.setArguments(bundle); + setAsWallpaperBottomSheetFragment.show(activity.getSupportFragmentManager(), setAsWallpaperBottomSheetFragment.getTag()); + } else { + activity.setAsWallpaper(imgurMedia.getLink(), 2); + } + return true; } return false; @@ -241,6 +253,6 @@ public class ViewImgurImageFragment extends Fragment { @Override public void onAttach(@NonNull Context context) { super.onAttach(context); - activity = (Activity) context; + activity = (ViewImgurMediaActivity) context; } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SetAsWallpaperCallback.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SetAsWallpaperCallback.java new file mode 100644 index 00000000..7fb6e324 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SetAsWallpaperCallback.java @@ -0,0 +1,7 @@ +package ml.docilealligator.infinityforreddit; + +public interface SetAsWallpaperCallback { + void setToHomeScreen(int viewPagerPosition); + void setToLockScreen(int viewPagerPosition); + void setToBoth(int viewPagerPosition); +}
\ No newline at end of file |