From f0c275e9adccb3f2baf993dfd239c28d9757c8a6 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Wed, 4 Aug 2021 11:58:48 +0800 Subject: [PATCH] Use bottom toolbar in ViewImgurImageFragment is available. --- .../activities/ViewImageOrGifActivity.java | 11 +- .../activities/ViewImgurMediaActivity.java | 20 +- .../fragments/ViewImgurImageFragment.java | 179 +++++++++++------- .../res/layout/activity_view_image_or_gif.xml | 1 + .../res/layout/fragment_view_imgur_image.xml | 97 ++++++++++ .../res/layout/fragment_view_imgur_images.xml | 40 ---- 6 files changed, 230 insertions(+), 118 deletions(-) create mode 100644 app/src/main/res/layout/fragment_view_imgur_image.xml delete mode 100644 app/src/main/res/layout/fragment_view_imgur_images.xml diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewImageOrGifActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewImageOrGifActivity.java index 34fb586d..9602da8b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewImageOrGifActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewImageOrGifActivity.java @@ -144,11 +144,6 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa 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))); - if (mSharedPreferences.getBoolean(SharedPreferencesUtils.SWIPE_VERTICALLY_TO_GO_BACK_FROM_MEDIA, true)) { Slidr.attach(this, new SlidrConfig.Builder().position(SlidrPosition.VERTICAL).distanceThreshold(0.125f).build()); } @@ -181,6 +176,7 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa if (useBottomAppBar) { getSupportActionBar().hide(); + bottomAppBar.setVisibility(View.VISIBLE); downloadImageView.setOnClickListener(view -> { if (isDownloading) { return; @@ -198,7 +194,10 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa setWallpaper(); }); } else { - bottomAppBar.setVisibility(View.GONE); + 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))); } mLoadErrorLinearLayout.setOnClickListener(view -> { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewImgurMediaActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewImgurMediaActivity.java index aee7b0fd..86c47adb 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewImgurMediaActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewImgurMediaActivity.java @@ -77,6 +77,7 @@ public class ViewImgurMediaActivity extends AppCompatActivity implements SetAsWa LinearLayout errorLinearLayout; private SectionsPagerAdapter sectionsPagerAdapter; private ArrayList images; + private boolean useBottomAppBar; @Inject @Named("imgur") Retrofit imgurRetrofit; @@ -116,12 +117,17 @@ public class ViewImgurMediaActivity extends AppCompatActivity implements SetAsWa 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))); + useBottomAppBar = sharedPreferences.getBoolean(SharedPreferencesUtils.USE_BOTTOM_TOOLBAR_IN_MEDIA_VIEWER, false); - setTitle(" "); + if (!useBottomAppBar) { + 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))); + setTitle(" "); + } else { + getSupportActionBar().hide(); + } String imgurId = getIntent().getStringExtra(EXTRA_IMGUR_ID); if (imgurId == null) { @@ -153,6 +159,10 @@ public class ViewImgurMediaActivity extends AppCompatActivity implements SetAsWa errorLinearLayout.setOnClickListener(view -> fetchImgurMedia(imgurId)); } + public boolean isUseBottomAppBar() { + return useBottomAppBar; + } + private void fetchImgurMedia(String imgurId) { errorLinearLayout.setVisibility(View.GONE); progressBar.setVisibility(View.VISIBLE); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewImgurImageFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewImgurImageFragment.java index d4d8c877..e9fe350a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewImgurImageFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewImgurImageFragment.java @@ -16,8 +16,10 @@ import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; +import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ProgressBar; +import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; @@ -36,6 +38,7 @@ import com.bumptech.glide.request.target.Target; import com.bumptech.glide.request.transition.Transition; import com.davemorrissey.labs.subscaleview.ImageSource; import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView; +import com.google.android.material.bottomappbar.BottomAppBar; import java.io.File; import java.util.concurrent.Executor; @@ -65,6 +68,16 @@ public class ViewImgurImageFragment extends Fragment { SubsamplingScaleImageView imageView; @BindView(R.id.load_image_error_linear_layout_view_imgur_image_fragment) LinearLayout errorLinearLayout; + @BindView(R.id.bottom_navigation_view_imgur_image_fragment) + BottomAppBar bottomAppBar; + @BindView(R.id.title_text_view_view_imgur_image_fragment) + TextView titleTextView; + @BindView(R.id.download_image_view_view_imgur_image_fragment) + ImageView downloadImageView; + @BindView(R.id.share_image_view_view_imgur_image_fragment) + ImageView shareImageView; + @BindView(R.id.wallpaper_image_view_view_imgur_image_fragment) + ImageView wallpaperImageView; @Inject Executor mExecutor; @@ -81,7 +94,7 @@ public class ViewImgurImageFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View rootView = inflater.inflate(R.layout.fragment_view_imgur_images, container, false); + View rootView = inflater.inflate(R.layout.fragment_view_imgur_image, container, false); ((Infinity) activity.getApplication()).getAppComponent().inject(this); @@ -100,6 +113,9 @@ public class ViewImgurImageFragment extends Fragment { | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); isActionBarHidden = false; + if (activity.isUseBottomAppBar()) { + bottomAppBar.setVisibility(View.VISIBLE); + } } else { activity.getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_STABLE @@ -109,6 +125,9 @@ public class ViewImgurImageFragment extends Fragment { | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE); isActionBarHidden = true; + if (activity.isUseBottomAppBar()) { + bottomAppBar.setVisibility(View.GONE); + } } }); imageView.setMinimumDpi(80); @@ -121,6 +140,23 @@ public class ViewImgurImageFragment extends Fragment { loadImage(); }); + if (activity.isUseBottomAppBar()) { + bottomAppBar.setVisibility(View.VISIBLE); + downloadImageView.setOnClickListener(view -> { + if (isDownloading) { + return; + } + isDownloading = true; + requestPermissionAndDownload(); + }); + shareImageView.setOnClickListener(view -> { + shareImage(); + }); + wallpaperImageView.setOnClickListener(view -> { + setWallpaper(); + }); + } + return rootView; } @@ -164,83 +200,39 @@ public class ViewImgurImageFragment extends Fragment { if (isDownloading) { return false; } - isDownloading = true; - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { - if (ContextCompat.checkSelfPermission(activity, - Manifest.permission.WRITE_EXTERNAL_STORAGE) - != PackageManager.PERMISSION_GRANTED) { - - // Permission is not granted - // No explanation needed; request the permission - requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, - PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE); - } else { - // Permission has already been granted - download(); - } - } else { - download(); - } - + requestPermissionAndDownload(); return true; } else if (itemId == R.id.action_share_view_imgur_image_fragment) { - glide.asBitmap().load(imgurMedia.getLink()).into(new CustomTarget() { - - @Override - public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition transition) { - if (activity.getExternalCacheDir() != null) { - Toast.makeText(activity, R.string.save_image_first, Toast.LENGTH_SHORT).show(); - SaveBitmapImageToFile.SaveBitmapImageToFile(mExecutor, new Handler(), resource, activity.getExternalCacheDir().getPath(), - imgurMedia.getFileName(), - new SaveBitmapImageToFile.SaveBitmapImageToFileListener() { - @Override - public void saveSuccess(File imageFile) { - Uri uri = FileProvider.getUriForFile(activity, - 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(activity, - R.string.cannot_save_image, Toast.LENGTH_SHORT).show(); - } - }); - } else { - Toast.makeText(activity, - R.string.cannot_get_storage, Toast.LENGTH_SHORT).show(); - } - } - - @Override - public void onLoadCleared(@Nullable Drawable placeholder) { - - } - }); + shareImage(); return true; } else if (itemId == R.id.action_set_wallpaper_view_imgur_image_fragment) { - 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 { - ((SetAsWallpaperCallback) activity).setToBoth(activity.getCurrentPagePosition()); - } + setWallpaper(); return true; } return false; } + private void requestPermissionAndDownload() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { + if (ContextCompat.checkSelfPermission(activity, + Manifest.permission.WRITE_EXTERNAL_STORAGE) + != PackageManager.PERMISSION_GRANTED) { + + // Permission is not granted + // No explanation needed; request the permission + requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, + PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE); + } else { + // Permission has already been granted + download(); + } + } else { + download(); + } + } + private void download() { isDownloading = false; @@ -252,6 +244,59 @@ public class ViewImgurImageFragment extends Fragment { Toast.makeText(activity, R.string.download_started, Toast.LENGTH_SHORT).show(); } + private void shareImage() { + glide.asBitmap().load(imgurMedia.getLink()).into(new CustomTarget() { + + @Override + public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition transition) { + if (activity.getExternalCacheDir() != null) { + Toast.makeText(activity, R.string.save_image_first, Toast.LENGTH_SHORT).show(); + SaveBitmapImageToFile.SaveBitmapImageToFile(mExecutor, new Handler(), resource, activity.getExternalCacheDir().getPath(), + imgurMedia.getFileName(), + new SaveBitmapImageToFile.SaveBitmapImageToFileListener() { + @Override + public void saveSuccess(File imageFile) { + Uri uri = FileProvider.getUriForFile(activity, + 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(activity, + R.string.cannot_save_image, Toast.LENGTH_SHORT).show(); + } + }); + } else { + Toast.makeText(activity, + R.string.cannot_get_storage, Toast.LENGTH_SHORT).show(); + } + } + + @Override + public void onLoadCleared(@Nullable Drawable placeholder) { + + } + }); + } + + private void setWallpaper() { + 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 { + ((SetAsWallpaperCallback) activity).setToBoth(activity.getCurrentPagePosition()); + } + } + @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE && grantResults.length > 0) { 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 index e97a422a..fa0a5aa8 100644 --- a/app/src/main/res/layout/activity_view_image_or_gif.xml +++ b/app/src/main/res/layout/activity_view_image_or_gif.xml @@ -46,6 +46,7 @@ android:layout_height="wrap_content" android:layout_gravity="bottom" android:backgroundTint="#80000000" + android:visibility="gone" style="@style/Widget.MaterialComponents.BottomAppBar"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_view_imgur_images.xml b/app/src/main/res/layout/fragment_view_imgur_images.xml deleted file mode 100644 index 7a658b92..00000000 --- a/app/src/main/res/layout/fragment_view_imgur_images.xml +++ /dev/null @@ -1,40 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file