From d428baaa7bf45c2e176885c02787c1cff6347af8 Mon Sep 17 00:00:00 2001 From: Hermes Junior Date: Mon, 15 Jun 2020 21:40:02 +0200 Subject: Refactor ViewImage and ViewGif activities. Allow zooming in gifs. --- app/src/main/AndroidManifest.xml | 8 +- .../Activity/LinkResolverActivity.java | 16 +- .../Activity/ViewGIFActivity.java | 319 -------------- .../Activity/ViewImageActivity.java | 416 ------------------ .../Activity/ViewImageOrGifActivity.java | 477 +++++++++++++++++++++ .../Activity/ViewSubredditDetailActivity.java | 12 +- .../Activity/ViewUserDetailActivity.java | 12 +- .../Adapter/CommentAndPostRecyclerViewAdapter.java | 35 +- .../Adapter/PostRecyclerViewAdapter.java | 65 ++- .../infinityforreddit/AppComponent.java | 7 +- app/src/main/res/layout/activity_view_gif.xml | 57 --- app/src/main/res/layout/activity_view_image.xml | 58 --- .../main/res/layout/activity_view_image_or_gif.xml | 64 +++ app/src/main/res/menu/view_gif_activity.xml | 17 - app/src/main/res/menu/view_image_activity.xml | 23 - .../main/res/menu/view_image_or_gif_activity.xml | 24 ++ 16 files changed, 638 insertions(+), 972 deletions(-) delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewGIFActivity.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageActivity.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageOrGifActivity.java delete mode 100644 app/src/main/res/layout/activity_view_gif.xml delete mode 100644 app/src/main/res/layout/activity_view_image.xml create mode 100644 app/src/main/res/layout/activity_view_image_or_gif.xml delete mode 100644 app/src/main/res/menu/view_gif_activity.xml delete mode 100644 app/src/main/res/menu/view_image_activity.xml create mode 100644 app/src/main/res/menu/view_image_or_gif_activity.xml (limited to 'app/src') diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ff60bc3f..829f2f2b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -87,10 +87,6 @@ android:label="@string/multi_reddit_listing_activity_label" android:parentActivityName=".Activity.MainActivity" android:theme="@style/AppTheme.NoActionBar" /> - - \ No newline at end of file + diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/LinkResolverActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/LinkResolverActivity.java index 0b7cf47f..e6fb7c3d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/LinkResolverActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/LinkResolverActivity.java @@ -79,20 +79,20 @@ public class LinkResolverActivity extends AppCompatActivity { } if (path.endsWith("jpg") || path.endsWith("png")) { - Intent intent = new Intent(this, ViewImageActivity.class); + Intent intent = new Intent(this, ViewImageOrGifActivity.class); String url = uri.toString(); String fileName = url.substring(url.lastIndexOf('/') + 1); - intent.putExtra(ViewImageActivity.IMAGE_URL_KEY, url); - intent.putExtra(ViewImageActivity.FILE_NAME_KEY, fileName); - intent.putExtra(ViewImageActivity.POST_TITLE_KEY, fileName); + intent.putExtra(ViewImageOrGifActivity.IMAGE_URL_KEY, url); + intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, fileName); + intent.putExtra(ViewImageOrGifActivity.POST_TITLE_KEY, fileName); startActivity(intent); } else if (path.endsWith("gif")) { - Intent intent = new Intent(this, ViewGIFActivity.class); + Intent intent = new Intent(this, ViewImageOrGifActivity.class); String url = uri.toString(); String fileName = url.substring(url.lastIndexOf('/') + 1); - intent.putExtra(ViewGIFActivity.GIF_URL_KEY, url); - intent.putExtra(ViewGIFActivity.FILE_NAME_KEY, fileName); - intent.putExtra(ViewGIFActivity.POST_TITLE_KEY, fileName); + intent.putExtra(ViewImageOrGifActivity.GIF_URL_KEY, url); + intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, fileName); + intent.putExtra(ViewImageOrGifActivity.POST_TITLE_KEY, fileName); startActivity(intent); } else if (path.endsWith("mp4")) { Intent intent = new Intent(this, ViewVideoActivity.class); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewGIFActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewGIFActivity.java deleted file mode 100644 index a1802a90..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewGIFActivity.java +++ /dev/null @@ -1,319 +0,0 @@ -package ml.docilealligator.infinityforreddit.Activity; - -import android.Manifest; -import android.app.DownloadManager; -import android.content.Context; -import android.content.Intent; -import android.content.SharedPreferences; -import android.content.pm.PackageManager; -import android.graphics.drawable.ColorDrawable; -import android.graphics.drawable.Drawable; -import android.net.Uri; -import android.os.Build; -import android.os.Bundle; -import android.os.Environment; -import android.text.Html; -import android.view.Menu; -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.annotation.Nullable; -import androidx.appcompat.app.ActionBar; -import androidx.appcompat.app.AppCompatActivity; -import androidx.core.app.ActivityCompat; -import androidx.core.content.ContextCompat; -import androidx.core.content.FileProvider; - -import com.bumptech.glide.Glide; -import com.bumptech.glide.RequestManager; -import com.bumptech.glide.load.DataSource; -import com.bumptech.glide.load.engine.GlideException; -import com.bumptech.glide.load.resource.gif.GifDrawable; -import com.bumptech.glide.request.RequestListener; -import com.bumptech.glide.request.RequestOptions; -import com.bumptech.glide.request.target.Target; -import com.thefuntasty.hauler.HaulerView; - -import java.io.File; - -import javax.inject.Inject; -import javax.inject.Named; - -import butterknife.BindView; -import butterknife.ButterKnife; -import ml.docilealligator.infinityforreddit.AsyncTask.SaveGIFToFileAsyncTask; -import ml.docilealligator.infinityforreddit.BuildConfig; -import ml.docilealligator.infinityforreddit.Font.ContentFontFamily; -import ml.docilealligator.infinityforreddit.Font.ContentFontStyle; -import ml.docilealligator.infinityforreddit.Font.FontFamily; -import ml.docilealligator.infinityforreddit.Font.FontStyle; -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.Utils.SharedPreferencesUtils; -import pl.droidsonroids.gif.GifImageView; - -public class ViewGIFActivity extends AppCompatActivity { - - public static final String GIF_URL_KEY = "GUK"; - public static final String FILE_NAME_KEY = "FNK"; - public static final String POST_TITLE_KEY = "PTK"; - private static final int PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE = 0; - @BindView(R.id.hauler_view_view_gif_activity) - HaulerView mHaulerView; - @BindView(R.id.progress_bar_view_gif_activity) - ProgressBar mProgressBar; - @BindView(R.id.image_view_view_gif_activity) - GifImageView mImageView; - @BindView(R.id.load_image_error_linear_layout_view_gif_activity) - LinearLayout mLoadErrorLinearLayout; - @Inject - @Named("default") - SharedPreferences mSharedPreferences; - private boolean isActionBarHidden = false; - private boolean isDownloading = false; - private RequestManager glide; - private String mImageUrl; - private String mImageFileName; - private boolean isSwiping = false; - private String postTitle; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - ((Infinity) getApplication()).getAppComponent().inject(this); - - getTheme().applyStyle(R.style.Theme_Normal, true); - - getTheme().applyStyle(FontStyle.valueOf(mSharedPreferences - .getString(SharedPreferencesUtils.FONT_SIZE_KEY, FontStyle.Normal.name())).getResId(), true); - - getTheme().applyStyle(TitleFontStyle.valueOf(mSharedPreferences - .getString(SharedPreferencesUtils.TITLE_FONT_SIZE_KEY, TitleFontStyle.Normal.name())).getResId(), true); - - getTheme().applyStyle(ContentFontStyle.valueOf(mSharedPreferences - .getString(SharedPreferencesUtils.CONTENT_FONT_SIZE_KEY, ContentFontStyle.Normal.name())).getResId(), true); - - getTheme().applyStyle(FontFamily.valueOf(mSharedPreferences - .getString(SharedPreferencesUtils.FONT_FAMILY_KEY, FontFamily.Default.name())).getResId(), true); - - getTheme().applyStyle(TitleFontFamily.valueOf(mSharedPreferences - .getString(SharedPreferencesUtils.TITLE_FONT_FAMILY_KEY, TitleFontFamily.Default.name())).getResId(), true); - - getTheme().applyStyle(ContentFontFamily.valueOf(mSharedPreferences - .getString(SharedPreferencesUtils.CONTENT_FONT_FAMILY_KEY, ContentFontFamily.Default.name())).getResId(), true); - - setContentView(R.layout.activity_view_gif); - - ButterKnife.bind(this); - - ActionBar actionBar = getSupportActionBar(); - Drawable upArrow = getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp); - actionBar.setHomeAsUpIndicator(upArrow); - actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.transparentActionBarAndExoPlayerControllerColor))); - - mHaulerView.setOnDragDismissedListener(dragDirection -> finish()); - - glide = Glide.with(this); - - Intent intent = getIntent(); - mImageUrl = intent.getStringExtra(GIF_URL_KEY); - mImageFileName = intent.getStringExtra(FILE_NAME_KEY); - postTitle = intent.getStringExtra(POST_TITLE_KEY); - - if (postTitle != null) { - setTitle(Html.fromHtml(String.format("%s", postTitle))); - } else { - setTitle(""); - } - - mLoadErrorLinearLayout.setOnClickListener(view -> { - if (!isSwiping) { - mProgressBar.setVisibility(View.VISIBLE); - mLoadErrorLinearLayout.setVisibility(View.GONE); - loadImage(); - } - }); - - loadImage(); - - mImageView.setOnClickListener(view -> { - if (isActionBarHidden) { - getWindow().getDecorView().setSystemUiVisibility( - View.SYSTEM_UI_FLAG_LAYOUT_STABLE - | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION - | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); - isActionBarHidden = false; - } else { - getWindow().getDecorView().setSystemUiVisibility( - View.SYSTEM_UI_FLAG_LAYOUT_STABLE - | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION - | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN - | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION - | View.SYSTEM_UI_FLAG_FULLSCREEN - | View.SYSTEM_UI_FLAG_IMMERSIVE); - isActionBarHidden = true; - } - }); - } - - private void loadImage() { - glide.load(mImageUrl).listener(new RequestListener() { - @Override - public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { - mProgressBar.setVisibility(View.GONE); - mLoadErrorLinearLayout.setVisibility(View.VISIBLE); - return false; - } - - @Override - public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { - mProgressBar.setVisibility(View.GONE); - return false; - } - }).apply(new RequestOptions().fitCenter()).into(mImageView); - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.view_gif_activity, menu); - return true; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: - finish(); - return true; - case R.id.action_download_view_gif_activity: - if (isDownloading) { - return false; - } - - isDownloading = true; - - if (Build.VERSION.SDK_INT >= 23) { - if (ContextCompat.checkSelfPermission(this, - Manifest.permission.WRITE_EXTERNAL_STORAGE) - != PackageManager.PERMISSION_GRANTED) { - - // Permission is not granted - // No explanation needed; request the permission - ActivityCompat.requestPermissions(this, - new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, - PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE); - } else { - // Permission has already been granted - download(); - } - } else { - download(); - } - - return true; - case R.id.action_share_view_gif_activity: - Toast.makeText(ViewGIFActivity.this, R.string.save_gif_first, Toast.LENGTH_SHORT).show(); - glide.asGif().load(mImageUrl).listener(new RequestListener() { - @Override - public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { - return false; - } - - @Override - public boolean onResourceReady(GifDrawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { - if (getExternalCacheDir() != null) { - new SaveGIFToFileAsyncTask(resource, getExternalCacheDir().getPath(), mImageFileName, - new SaveGIFToFileAsyncTask.SaveGIFToFileAsyncTaskListener() { - @Override - public void saveSuccess(File imageFile) { - Uri uri = FileProvider.getUriForFile(ViewGIFActivity.this, - BuildConfig.APPLICATION_ID + ".provider", imageFile); - Intent shareIntent = new Intent(); - shareIntent.setAction(Intent.ACTION_SEND); - shareIntent.putExtra(Intent.EXTRA_STREAM, uri); - shareIntent.setType("image/*"); - shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); - startActivity(Intent.createChooser(shareIntent, getString(R.string.share))); - } - - @Override - public void saveFailed() { - Toast.makeText(ViewGIFActivity.this, - R.string.cannot_save_gif, Toast.LENGTH_SHORT).show(); - } - }).execute(); - } else { - Toast.makeText(ViewGIFActivity.this, - R.string.cannot_get_storage, Toast.LENGTH_SHORT).show(); - } - return false; - } - }).submit(); - return true; - } - - return false; - } - - @Override - public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { - if (requestCode == PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE && grantResults.length > 0) { - if (grantResults[0] == PackageManager.PERMISSION_DENIED) { - Toast.makeText(this, R.string.no_storage_permission, Toast.LENGTH_SHORT).show(); - } else if (grantResults[0] == PackageManager.PERMISSION_GRANTED && isDownloading) { - download(); - } - isDownloading = false; - } - } - - private void download() { - DownloadManager.Request request = new DownloadManager.Request(Uri.parse(mImageUrl)); - request.setTitle(mImageFileName); - - request.allowScanningByMediaScanner(); - request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); - - //Android Q support - if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, mImageFileName); - } else { - String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString(); - File directory = new File(path + "/Infinity/"); - boolean saveToInfinityFolder = true; - if (!directory.exists()) { - if (!directory.mkdir()) { - saveToInfinityFolder = false; - } - } else { - if (directory.isFile()) { - if (!(directory.delete() && directory.mkdir())) { - saveToInfinityFolder = false; - } - } - } - - if (saveToInfinityFolder) { - request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES + "/Infinity/", mImageFileName); - } else { - request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, mImageFileName); - } - } - - DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); - - if (manager == null) { - Toast.makeText(this, R.string.download_failed, Toast.LENGTH_SHORT).show(); - return; - } - - manager.enqueue(request); - Toast.makeText(this, R.string.download_started, Toast.LENGTH_SHORT).show(); - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageActivity.java deleted file mode 100644 index 62fa12a5..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageActivity.java +++ /dev/null @@ -1,416 +0,0 @@ -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; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.appcompat.app.ActionBar; -import androidx.appcompat.app.AppCompatActivity; -import androidx.core.app.ActivityCompat; -import androidx.core.content.ContextCompat; -import androidx.core.content.FileProvider; - -import com.alexvasilkov.gestures.views.GestureImageView; -import com.bumptech.glide.Glide; -import com.bumptech.glide.RequestManager; -import com.bumptech.glide.load.DataSource; -import com.bumptech.glide.load.engine.GlideException; -import com.bumptech.glide.request.RequestListener; -import com.bumptech.glide.request.target.CustomTarget; -import com.bumptech.glide.request.target.Target; -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; - -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; -import ml.docilealligator.infinityforreddit.Font.FontFamily; -import ml.docilealligator.infinityforreddit.Font.FontStyle; -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 implements SetAsWallpaperCallback { - - public static final String IMAGE_URL_KEY = "IUK"; - public static final String FILE_NAME_KEY = "FNK"; - public static final String POST_TITLE_KEY = "PTK"; - private static final int PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE = 0; - @BindView(R.id.hauler_view_view_image_activity) - HaulerView mHaulerView; - @BindView(R.id.progress_bar_view_image_activity) - ProgressBar mProgressBar; - @BindView(R.id.image_view_view_image_activity) - GestureImageView mImageView; - @BindView(R.id.load_image_error_linear_layout_view_image_activity) - LinearLayout mLoadErrorLinearLayout; - @Inject - @Named("default") - SharedPreferences mSharedPreferences; - private boolean isActionBarHidden = false; - private boolean isDownloading = false; - private String mImageUrl; - private String mImageFileName; - private RequestManager glide; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - ((Infinity) getApplication()).getAppComponent().inject(this); - - getTheme().applyStyle(R.style.Theme_Normal, true); - - getTheme().applyStyle(FontStyle.valueOf(mSharedPreferences - .getString(SharedPreferencesUtils.FONT_SIZE_KEY, FontStyle.Normal.name())).getResId(), true); - - getTheme().applyStyle(TitleFontStyle.valueOf(mSharedPreferences - .getString(SharedPreferencesUtils.TITLE_FONT_SIZE_KEY, TitleFontStyle.Normal.name())).getResId(), true); - - getTheme().applyStyle(ContentFontStyle.valueOf(mSharedPreferences - .getString(SharedPreferencesUtils.CONTENT_FONT_SIZE_KEY, ContentFontStyle.Normal.name())).getResId(), true); - - getTheme().applyStyle(FontFamily.valueOf(mSharedPreferences - .getString(SharedPreferencesUtils.FONT_FAMILY_KEY, FontFamily.Default.name())).getResId(), true); - - getTheme().applyStyle(TitleFontFamily.valueOf(mSharedPreferences - .getString(SharedPreferencesUtils.TITLE_FONT_FAMILY_KEY, TitleFontFamily.Default.name())).getResId(), true); - - getTheme().applyStyle(ContentFontFamily.valueOf(mSharedPreferences - .getString(SharedPreferencesUtils.CONTENT_FONT_FAMILY_KEY, ContentFontFamily.Default.name())).getResId(), true); - - setContentView(R.layout.activity_view_image); - - ButterKnife.bind(this); - - ActionBar actionBar = getSupportActionBar(); - Drawable upArrow = getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp); - actionBar.setHomeAsUpIndicator(upArrow); - actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.transparentActionBarAndExoPlayerControllerColor))); - - glide = Glide.with(this); - - Intent intent = getIntent(); - mImageUrl = intent.getStringExtra(IMAGE_URL_KEY); - mImageFileName = intent.getStringExtra(FILE_NAME_KEY); - String postTitle = intent.getStringExtra(POST_TITLE_KEY); - - if (postTitle != null) { - setTitle(Html.fromHtml(String.format("%s", postTitle))); - } else { - setTitle(""); - } - - mHaulerView.setOnDragDismissedListener(dragDirection -> finish()); - - mLoadErrorLinearLayout.setOnClickListener(view -> { - mProgressBar.setVisibility(View.VISIBLE); - mLoadErrorLinearLayout.setVisibility(View.GONE); - loadImage(); - }); - - loadImage(); - - mImageView.getController().getSettings().setMaxZoom(10f).setDoubleTapZoom(2f).setPanEnabled(true); - - mImageView.setOnClickListener(view -> { - if (isActionBarHidden) { - getWindow().getDecorView().setSystemUiVisibility( - View.SYSTEM_UI_FLAG_LAYOUT_STABLE - | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION - | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); - isActionBarHidden = false; - } else { - getWindow().getDecorView().setSystemUiVisibility( - View.SYSTEM_UI_FLAG_LAYOUT_STABLE - | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION - | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN - | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION - | View.SYSTEM_UI_FLAG_FULLSCREEN - | View.SYSTEM_UI_FLAG_IMMERSIVE); - isActionBarHidden = true; - } - }); - } - - private void loadImage() { - glide.load(mImageUrl).listener(new RequestListener() { - @Override - public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { - mProgressBar.setVisibility(View.GONE); - mLoadErrorLinearLayout.setVisibility(View.VISIBLE); - return false; - } - - @Override - public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { - mProgressBar.setVisibility(View.GONE); - return false; - } - }).override(Target.SIZE_ORIGINAL).into(mImageView); - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.view_image_activity, menu); - return true; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: - finish(); - return true; - case R.id.action_download_view_image_activity: - if (isDownloading) { - return false; - } - - isDownloading = true; - - if (Build.VERSION.SDK_INT >= 23) { - if (ContextCompat.checkSelfPermission(this, - Manifest.permission.WRITE_EXTERNAL_STORAGE) - != PackageManager.PERMISSION_GRANTED) { - - // Permission is not granted - // No explanation needed; request the permission - ActivityCompat.requestPermissions(this, - new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, - PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE); - } else { - // Permission has already been granted - download(); - } - } else { - download(); - } - - return true; - case R.id.action_share_view_image_activity: - glide.asBitmap().load(mImageUrl).into(new CustomTarget() { - - @Override - public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition transition) { - if (getExternalCacheDir() != null) { - Toast.makeText(ViewImageActivity.this, R.string.save_image_first, Toast.LENGTH_SHORT).show(); - new SaveImageToFileAsyncTask(resource, getExternalCacheDir().getPath(), mImageFileName, - new SaveImageToFileAsyncTask.SaveImageToFileAsyncTaskListener() { - @Override - public void saveSuccess(File imageFile) { - Uri uri = FileProvider.getUriForFile(ViewImageActivity.this, - BuildConfig.APPLICATION_ID + ".provider",imageFile); - Intent shareIntent = new Intent(); - shareIntent.setAction(Intent.ACTION_SEND); - shareIntent.putExtra(Intent.EXTRA_STREAM, uri); - shareIntent.setType("image/*"); - shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); - startActivity(Intent.createChooser(shareIntent, getString(R.string.share))); - } - - @Override - public void saveFailed() { - Toast.makeText(ViewImageActivity.this, - R.string.cannot_save_image, Toast.LENGTH_SHORT).show(); - } - }).execute(); - } else { - Toast.makeText(ViewImageActivity.this, - R.string.cannot_get_storage, Toast.LENGTH_SHORT).show(); - } - } - - @Override - public void onLoadCleared(@Nullable Drawable placeholder) { - - } - }); - 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; - } - - @Override - public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { - if (requestCode == PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE && grantResults.length > 0) { - if (grantResults[0] == PackageManager.PERMISSION_DENIED) { - Toast.makeText(this, R.string.no_storage_permission, Toast.LENGTH_SHORT).show(); - } else if (grantResults[0] == PackageManager.PERMISSION_GRANTED && isDownloading) { - download(); - } - isDownloading = false; - } - } - - private void download() { - DownloadManager.Request request = new DownloadManager.Request(Uri.parse(mImageUrl)); - request.setTitle(mImageFileName); - - request.allowScanningByMediaScanner(); - request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); - - //Android Q support - if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, mImageFileName); - } else { - String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString(); - File directory = new File(path + "/Infinity/"); - boolean saveToInfinityFolder = true; - if (!directory.exists()) { - if (!directory.mkdir()) { - saveToInfinityFolder = false; - } - } else { - if (directory.isFile()) { - if (!(directory.delete() && directory.mkdir())) { - saveToInfinityFolder = false; - } - } - } - - if (saveToInfinityFolder) { - request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES + "/Infinity/", mImageFileName); - } else { - request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, mImageFileName); - } - } - - DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); - - if (manager == null) { - Toast.makeText(this, R.string.download_failed, Toast.LENGTH_SHORT).show(); - return; - } - - 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() { - @Override - public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition 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/ViewImageOrGifActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageOrGifActivity.java new file mode 100644 index 00000000..62c7726d --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageOrGifActivity.java @@ -0,0 +1,477 @@ +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; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.app.ActionBar; +import androidx.appcompat.app.AppCompatActivity; +import androidx.core.app.ActivityCompat; +import androidx.core.content.ContextCompat; +import androidx.core.content.FileProvider; + +import com.alexvasilkov.gestures.views.GestureFrameLayout; +import com.bumptech.glide.Glide; +import com.bumptech.glide.RequestManager; +import com.bumptech.glide.load.DataSource; +import com.bumptech.glide.load.engine.GlideException; +import com.bumptech.glide.load.resource.gif.GifDrawable; +import com.bumptech.glide.request.RequestListener; +import com.bumptech.glide.request.target.CustomTarget; +import com.bumptech.glide.request.target.Target; +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; + +import butterknife.BindView; +import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.AsyncTask.SaveGIFToFileAsyncTask; +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; +import ml.docilealligator.infinityforreddit.Font.FontFamily; +import ml.docilealligator.infinityforreddit.Font.FontStyle; +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; +import pl.droidsonroids.gif.GifImageView; + +public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWallpaperCallback { + + public static final String IMAGE_URL_KEY = "IUK"; + public static final String GIF_URL_KEY = "GUK"; + public static final String FILE_NAME_KEY = "FNK"; + public static final String POST_TITLE_KEY = "PTK"; + private static final int PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE = 0; + @BindView(R.id.hauler_view_view_image_or_gif_activity) + HaulerView mHaulerView; + @BindView(R.id.progress_bar_view_image_or_gif_activity) + ProgressBar mProgressBar; + @BindView(R.id.image_view_view_image_or_gif_activity) + GifImageView mImageView; + @BindView(R.id.gesture_layout_view_image_or_gif_activity) + GestureFrameLayout gestureLayout; + @BindView(R.id.load_image_error_linear_layout_view_image_or_gif_activity) + LinearLayout mLoadErrorLinearLayout; + @Inject + @Named("default") + SharedPreferences mSharedPreferences; + private boolean isActionBarHidden = false; + private boolean isDownloading = false; + private RequestManager glide; + private String mImageUrl; + private String mImageFileName; + private boolean isGif = true; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + ((Infinity) getApplication()).getAppComponent().inject(this); + + getTheme().applyStyle(R.style.Theme_Normal, true); + + getTheme().applyStyle(FontStyle.valueOf(mSharedPreferences + .getString(SharedPreferencesUtils.FONT_SIZE_KEY, FontStyle.Normal.name())).getResId(), true); + + getTheme().applyStyle(TitleFontStyle.valueOf(mSharedPreferences + .getString(SharedPreferencesUtils.TITLE_FONT_SIZE_KEY, TitleFontStyle.Normal.name())).getResId(), true); + + getTheme().applyStyle(ContentFontStyle.valueOf(mSharedPreferences + .getString(SharedPreferencesUtils.CONTENT_FONT_SIZE_KEY, ContentFontStyle.Normal.name())).getResId(), true); + + getTheme().applyStyle(FontFamily.valueOf(mSharedPreferences + .getString(SharedPreferencesUtils.FONT_FAMILY_KEY, FontFamily.Default.name())).getResId(), true); + + getTheme().applyStyle(TitleFontFamily.valueOf(mSharedPreferences + .getString(SharedPreferencesUtils.TITLE_FONT_FAMILY_KEY, TitleFontFamily.Default.name())).getResId(), true); + + getTheme().applyStyle(ContentFontFamily.valueOf(mSharedPreferences + .getString(SharedPreferencesUtils.CONTENT_FONT_FAMILY_KEY, ContentFontFamily.Default.name())).getResId(), true); + + setContentView(R.layout.activity_view_image_or_gif); + + ButterKnife.bind(this); + + ActionBar actionBar = getSupportActionBar(); + Drawable upArrow = getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp); + actionBar.setHomeAsUpIndicator(upArrow); + actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.transparentActionBarAndExoPlayerControllerColor))); + + mHaulerView.setOnDragDismissedListener(dragDirection -> finish()); + + glide = Glide.with(this); + + Intent intent = getIntent(); + mImageUrl = intent.getStringExtra(GIF_URL_KEY); + if (mImageUrl == null) { + isGif = false; + mImageUrl = intent.getStringExtra(IMAGE_URL_KEY); + } + mImageFileName = intent.getStringExtra(FILE_NAME_KEY); + postTitle = intent.getStringExtra(POST_TITLE_KEY); + + if (postTitle != null) { + setTitle(Html.fromHtml(String.format("%s", postTitle))); + } else { + setTitle(""); + } + + mLoadErrorLinearLayout.setOnClickListener(view -> { + mProgressBar.setVisibility(View.VISIBLE); + mLoadErrorLinearLayout.setVisibility(View.GONE); + loadImage(); + }); + + loadImage(); + + gestureLayout.getController().getSettings().setMaxZoom(10f).setDoubleTapZoom(2f).setPanEnabled(true); + + mImageView.setOnClickListener(view -> { + if (isActionBarHidden) { + getWindow().getDecorView().setSystemUiVisibility( + View.SYSTEM_UI_FLAG_LAYOUT_STABLE + | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION + | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); + isActionBarHidden = false; + } else { + getWindow().getDecorView().setSystemUiVisibility( + View.SYSTEM_UI_FLAG_LAYOUT_STABLE + | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION + | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN + | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION + | View.SYSTEM_UI_FLAG_FULLSCREEN + | View.SYSTEM_UI_FLAG_IMMERSIVE); + isActionBarHidden = true; + } + }); + } + + private void loadImage() { + glide.load(mImageUrl).listener(new RequestListener() { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { + mProgressBar.setVisibility(View.GONE); + mLoadErrorLinearLayout.setVisibility(View.VISIBLE); + return false; + } + + @Override + public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { + mProgressBar.setVisibility(View.GONE); + return false; + } + }).override(Target.SIZE_ORIGINAL).into(mImageView); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.view_image_or_gif_activity, menu); + if (!isGif) + menu.findItem(R.id.action_set_wallpaper_view_image_or_gif_activity).setVisible(true); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + finish(); + return true; + case R.id.action_download_view_image_or_gif_activity: + if (isDownloading) { + return false; + } + + isDownloading = true; + + if (Build.VERSION.SDK_INT >= 23) { + if (ContextCompat.checkSelfPermission(this, + Manifest.permission.WRITE_EXTERNAL_STORAGE) + != PackageManager.PERMISSION_GRANTED) { + + // Permission is not granted + // No explanation needed; request the permission + ActivityCompat.requestPermissions(this, + new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, + PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE); + } else { + // Permission has already been granted + download(mImageUrl, mImageFileName); + } + } else { + download(mImageUrl, mImageFileName); + } + + return true; + case R.id.action_share_view_image_or_gif_activity: + if (isGif) + shareGif(); + else + shareImage(); + return true; + case R.id.action_set_wallpaper_view_image_or_gif_activity: + if (!isGif) { + 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; + } + + private void shareImage() { + glide.asBitmap().load(mImageUrl).into(new CustomTarget() { + + @Override + public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition transition) { + if (getExternalCacheDir() != null) { + Toast.makeText(ViewImageOrGifActivity.this, R.string.save_image_first, Toast.LENGTH_SHORT).show(); + new SaveImageToFileAsyncTask(resource, getExternalCacheDir().getPath(), mImageFileName, + new SaveImageToFileAsyncTask.SaveImageToFileAsyncTaskListener() { + @Override + public void saveSuccess(File imageFile) { + Uri uri = FileProvider.getUriForFile(ViewImageOrGifActivity.this, + BuildConfig.APPLICATION_ID + ".provider", imageFile); + Intent shareIntent = new Intent(); + shareIntent.setAction(Intent.ACTION_SEND); + shareIntent.putExtra(Intent.EXTRA_STREAM, uri); + shareIntent.setType("image/*"); + shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + startActivity(Intent.createChooser(shareIntent, getString(R.string.share))); + } + + @Override + public void saveFailed() { + Toast.makeText(ViewImageOrGifActivity.this, + R.string.cannot_save_image, Toast.LENGTH_SHORT).show(); + } + }).execute(); + } else { + Toast.makeText(ViewImageOrGifActivity.this, + R.string.cannot_get_storage, Toast.LENGTH_SHORT).show(); + } + } + + @Override + public void onLoadCleared(@Nullable Drawable placeholder) { + + } + }); + } + + private void shareGif() { + Toast.makeText(ViewImageOrGifActivity.this, R.string.save_gif_first, Toast.LENGTH_SHORT).show(); + glide.asGif().load(mImageUrl).listener(new RequestListener() { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { + return false; + } + + @Override + public boolean onResourceReady(GifDrawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { + if (getExternalCacheDir() != null) { + new SaveGIFToFileAsyncTask(resource, getExternalCacheDir().getPath(), mImageFileName, + new SaveGIFToFileAsyncTask.SaveGIFToFileAsyncTaskListener() { + @Override + public void saveSuccess(File imageFile) { + Uri uri = FileProvider.getUriForFile(ViewImageOrGifActivity.this, + BuildConfig.APPLICATION_ID + ".provider", imageFile); + Intent shareIntent = new Intent(); + shareIntent.setAction(Intent.ACTION_SEND); + shareIntent.putExtra(Intent.EXTRA_STREAM, uri); + shareIntent.setType("image/*"); + shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + startActivity(Intent.createChooser(shareIntent, getString(R.string.share))); + } + + @Override + public void saveFailed() { + Toast.makeText(ViewImageOrGifActivity.this, + R.string.cannot_save_gif, Toast.LENGTH_SHORT).show(); + } + }).execute(); + } else { + Toast.makeText(ViewImageOrGifActivity.this, + R.string.cannot_get_storage, Toast.LENGTH_SHORT).show(); + } + return false; + } + }).submit(); + } + + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + if (requestCode == PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE && grantResults.length > 0) { + if (grantResults[0] == PackageManager.PERMISSION_DENIED) { + Toast.makeText(this, R.string.no_storage_permission, Toast.LENGTH_SHORT).show(); + } else if (grantResults[0] == PackageManager.PERMISSION_GRANTED && isDownloading) { + download(mImageUrl, mImageFileName); + } + isDownloading = false; + } + } + + void download(String mImageUrl, String mImageFileName) { + DownloadManager.Request request = new DownloadManager.Request(Uri.parse(mImageUrl)); + request.setTitle(mImageFileName); + + request.allowScanningByMediaScanner(); + request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); + + //Android Q support + if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, mImageFileName); + } else { + String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString(); + File directory = new File(path + "/Infinity/"); + boolean saveToInfinityFolder = true; + if (!directory.exists()) { + if (!directory.mkdir()) { + saveToInfinityFolder = false; + } + } else { + if (directory.isFile()) { + if (!(directory.delete() && directory.mkdir())) { + saveToInfinityFolder = false; + } + } + } + + if (saveToInfinityFolder) { + request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES + "/Infinity/", mImageFileName); + } else { + request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, mImageFileName); + } + } + + DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); + + if (manager == null) { + Toast.makeText(this, R.string.download_failed, Toast.LENGTH_SHORT).show(); + return; + } + + manager.enqueue(request); + Toast.makeText(this, R.string.download_started, Toast.LENGTH_SHORT).show(); + } + + 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() { + @Override + public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition 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); + } + + @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/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java index 17c80234..439c976e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java @@ -348,9 +348,9 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp } else { glide.load(subredditData.getBannerUrl()).into(bannerImageView); bannerImageView.setOnClickListener(view -> { - Intent intent = new Intent(ViewSubredditDetailActivity.this, ViewImageActivity.class); - intent.putExtra(ViewImageActivity.IMAGE_URL_KEY, subredditData.getBannerUrl()); - intent.putExtra(ViewImageActivity.FILE_NAME_KEY, subredditName + "-banner.jpg"); + Intent intent = new Intent(ViewSubredditDetailActivity.this, ViewImageOrGifActivity.class); + intent.putExtra(ViewImageOrGifActivity.IMAGE_URL_KEY, subredditData.getBannerUrl()); + intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, subredditName + "-banner.jpg"); startActivity(intent); }); } @@ -369,9 +369,9 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(216, 0)))) .into(iconGifImageView); iconGifImageView.setOnClickListener(view -> { - Intent intent = new Intent(ViewSubredditDetailActivity.this, ViewImageActivity.class); - intent.putExtra(ViewImageActivity.IMAGE_URL_KEY, subredditData.getIconUrl()); - intent.putExtra(ViewImageActivity.FILE_NAME_KEY, subredditName + "-icon.jpg"); + Intent intent = new Intent(ViewSubredditDetailActivity.this, ViewImageOrGifActivity.class); + intent.putExtra(ViewImageOrGifActivity.IMAGE_URL_KEY, subredditData.getIconUrl()); + intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, subredditName + "-icon.jpg"); startActivity(intent); }); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java index a72c7389..fd120df2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java @@ -295,9 +295,9 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele } else { glide.load(userData.getBanner()).into(bannerImageView); bannerImageView.setOnClickListener(view -> { - Intent intent = new Intent(this, ViewImageActivity.class); - intent.putExtra(ViewImageActivity.IMAGE_URL_KEY, userData.getBanner()); - intent.putExtra(ViewImageActivity.FILE_NAME_KEY, username + "-banner.jpg"); + Intent intent = new Intent(this, ViewImageOrGifActivity.class); + intent.putExtra(ViewImageOrGifActivity.IMAGE_URL_KEY, userData.getBanner()); + intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, username + "-banner.jpg"); startActivity(intent); }); } @@ -315,9 +315,9 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele .into(iconGifImageView); iconGifImageView.setOnClickListener(view -> { - Intent intent = new Intent(this, ViewImageActivity.class); - intent.putExtra(ViewImageActivity.IMAGE_URL_KEY, userData.getIconUrl()); - intent.putExtra(ViewImageActivity.FILE_NAME_KEY, username + "-icon.jpg"); + Intent intent = new Intent(this, ViewImageOrGifActivity.class); + intent.putExtra(ViewImageOrGifActivity.IMAGE_URL_KEY, userData.getIconUrl()); + intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, username + "-icon.jpg"); startActivity(intent); }); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java index c7984bfb..0510fae6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java @@ -81,8 +81,7 @@ import jp.wasabeef.glide.transformations.RoundedCornersTransformation; import ml.docilealligator.infinityforreddit.Activity.CommentActivity; import ml.docilealligator.infinityforreddit.Activity.FilteredThingActivity; import ml.docilealligator.infinityforreddit.Activity.LinkResolverActivity; -import ml.docilealligator.infinityforreddit.Activity.ViewGIFActivity; -import ml.docilealligator.infinityforreddit.Activity.ViewImageActivity; +import ml.docilealligator.infinityforreddit.Activity.ViewImageOrGifActivity; import ml.docilealligator.infinityforreddit.Activity.ViewPostDetailActivity; import ml.docilealligator.infinityforreddit.Activity.ViewSubredditDetailActivity; import ml.docilealligator.infinityforreddit.Activity.ViewUserDetailActivity; @@ -307,7 +306,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { int commentPosition = mIsSingleCommentThreadMode ? holder.getAdapterPosition() - 2 : holder.getAdapterPosition() - 1; int parentPosition = getParentPosition(commentPosition); - if(parentPosition >= 0) { + if (parentPosition >= 0) { CommentData parentComment = mVisibleComments.get(parentPosition); mVisibleComments.get(commentPosition).setLoadingMoreChildren(true); @@ -1172,7 +1171,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter imageRequestBuilder = mGlide.load(mPost.getPreviewUrl()) .listener(new RequestListener() { @Override @@ -2255,11 +2254,11 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { if (mPost.getPostType() == Post.IMAGE_TYPE) { - Intent intent = new Intent(mActivity, ViewImageActivity.class); - intent.putExtra(ViewImageActivity.IMAGE_URL_KEY, mPost.getUrl()); - intent.putExtra(ViewImageActivity.FILE_NAME_KEY, mPost.getSubredditNamePrefixed().substring(2) + Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class); + intent.putExtra(ViewImageOrGifActivity.IMAGE_URL_KEY, mPost.getUrl()); + intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, mPost.getSubredditNamePrefixed().substring(2) + "-" + mPost.getId().substring(3) + ".jpg"); - intent.putExtra(ViewImageActivity.POST_TITLE_KEY, mPost.getTitle()); + intent.putExtra(ViewImageOrGifActivity.POST_TITLE_KEY, mPost.getTitle()); mActivity.startActivity(intent); } else if (mPost.getPostType() == Post.GIF_TYPE) { - Intent intent = new Intent(mActivity, ViewGIFActivity.class); - intent.putExtra(ViewGIFActivity.FILE_NAME_KEY, mPost.getSubredditName() + Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class); + intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, mPost.getSubredditName() + "-" + mPost.getId() + ".gif"); - intent.putExtra(ViewGIFActivity.GIF_URL_KEY, mPost.getVideoUrl()); - intent.putExtra(ViewImageActivity.POST_TITLE_KEY, mPost.getTitle()); + intent.putExtra(ViewImageOrGifActivity.GIF_URL_KEY, mPost.getVideoUrl()); + intent.putExtra(ViewImageOrGifActivity.POST_TITLE_KEY, mPost.getTitle()); mActivity.startActivity(intent); } }); @@ -2934,7 +2933,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { if (expandButton.getVisibility() == View.VISIBLE) { int commentPosition = mIsSingleCommentThreadMode ? getAdapterPosition() - 2 : getAdapterPosition() - 1; - if(commentPosition >= 0 && commentPosition < mVisibleComments.size()) { + if (commentPosition >= 0 && commentPosition < mVisibleComments.size()) { CommentData comment = getCurrentComment(); if (mVisibleComments.get(commentPosition).isExpanded()) { collapseChildren(commentPosition); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java index e54a0eea..a7af4e11 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java @@ -65,8 +65,7 @@ import jp.wasabeef.glide.transformations.BlurTransformation; import jp.wasabeef.glide.transformations.RoundedCornersTransformation; import ml.docilealligator.infinityforreddit.Activity.FilteredThingActivity; import ml.docilealligator.infinityforreddit.Activity.LinkResolverActivity; -import ml.docilealligator.infinityforreddit.Activity.ViewGIFActivity; -import ml.docilealligator.infinityforreddit.Activity.ViewImageActivity; +import ml.docilealligator.infinityforreddit.Activity.ViewImageOrGifActivity; import ml.docilealligator.infinityforreddit.Activity.ViewPostDetailActivity; import ml.docilealligator.infinityforreddit.Activity.ViewSubredditDetailActivity; import ml.docilealligator.infinityforreddit.Activity.ViewUserDetailActivity; @@ -836,9 +835,9 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - Intent intent = new Intent(mActivity, ViewImageActivity.class); - intent.putExtra(ViewImageActivity.IMAGE_URL_KEY, imageUrl); - intent.putExtra(ViewImageActivity.FILE_NAME_KEY, subredditName + Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class); + intent.putExtra(ViewImageOrGifActivity.IMAGE_URL_KEY, imageUrl); + intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, subredditName + "-" + id + ".jpg"); mActivity.startActivity(intent); }); @@ -866,11 +865,11 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - Intent intent = new Intent(mActivity, ViewGIFActivity.class); + Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class); intent.setData(gifVideoUri); - intent.putExtra(ViewGIFActivity.FILE_NAME_KEY, subredditName + intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, subredditName + "-" + id + ".gif"); - intent.putExtra(ViewGIFActivity.GIF_URL_KEY, post.getVideoUrl()); + intent.putExtra(ViewImageOrGifActivity.GIF_URL_KEY, post.getVideoUrl()); mActivity.startActivity(intent); }); @@ -1209,18 +1208,18 @@ public class PostRecyclerViewAdapter extends PagedListAdapter imageRequestBuilder = mGlide.load(previewUrl) .error(R.drawable.ic_error_outline_black_24dp).listener(new RequestListener() { - @Override - public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { - ((PostCompactViewHolder) holder).progressBar.setVisibility(View.GONE); - return false; - } + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { + ((PostCompactViewHolder) holder).progressBar.setVisibility(View.GONE); + return false; + } - @Override - public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { - ((PostCompactViewHolder) holder).progressBar.setVisibility(View.GONE); - return false; - } - }); + @Override + public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { + ((PostCompactViewHolder) holder).progressBar.setVisibility(View.GONE); + return false; + } + }); if ((post.isNSFW() && mNeedBlurNSFW) || post.isSpoiler() && mNeedBlurSpoiler) { imageRequestBuilder .transform(new BlurTransformation(50, 2)) @@ -1310,7 +1309,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/layout/activity_view_image.xml b/app/src/main/res/layout/activity_view_image.xml deleted file mode 100644 index 9a80a732..00000000 --- a/app/src/main/res/layout/activity_view_image.xml +++ /dev/null @@ -1,58 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/layout/activity_view_image_or_gif.xml b/app/src/main/res/layout/activity_view_image_or_gif.xml new file mode 100644 index 00000000..50bfffca --- /dev/null +++ b/app/src/main/res/layout/activity_view_image_or_gif.xml @@ -0,0 +1,64 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/menu/view_gif_activity.xml b/app/src/main/res/menu/view_gif_activity.xml deleted file mode 100644 index 0dc5aba5..00000000 --- a/app/src/main/res/menu/view_gif_activity.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/app/src/main/res/menu/view_image_activity.xml b/app/src/main/res/menu/view_image_activity.xml deleted file mode 100644 index 0c080ae9..00000000 --- a/app/src/main/res/menu/view_image_activity.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/menu/view_image_or_gif_activity.xml b/app/src/main/res/menu/view_image_or_gif_activity.xml new file mode 100644 index 00000000..682516a4 --- /dev/null +++ b/app/src/main/res/menu/view_image_or_gif_activity.xml @@ -0,0 +1,24 @@ + + + + + + + + -- cgit v1.2.3