Use bottom toolbar in ViewImgurImageFragment is available.

This commit is contained in:
Alex Ning 2021-08-04 11:58:48 +08:00
parent 7fe451b7e1
commit f0c275e9ad
6 changed files with 230 additions and 118 deletions

View File

@ -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 -> {

View File

@ -77,6 +77,7 @@ public class ViewImgurMediaActivity extends AppCompatActivity implements SetAsWa
LinearLayout errorLinearLayout;
private SectionsPagerAdapter sectionsPagerAdapter;
private ArrayList<ImgurMedia> 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);

View File

@ -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<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> 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<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> 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) {

View File

@ -46,6 +46,7 @@
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:backgroundTint="#80000000"
android:visibility="gone"
style="@style/Widget.MaterialComponents.BottomAppBar">
<LinearLayout

View File

@ -0,0 +1,97 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.ViewImgurImageFragment">
<ProgressBar
android:id="@+id/progress_bar_view_imgur_image_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
android:id="@+id/image_view_view_imgur_image_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/load_image_error_linear_layout_view_imgur_image_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_error_outline_white_24dp"
android:layout_gravity="center"
android:gravity="center"
android:textColor="@android:color/white"
android:text="@string/error_loading_image_tap_to_retry"
android:textSize="?attr/font_default"
android:fontFamily="?attr/font_family" />
</LinearLayout>
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottom_navigation_view_imgur_image_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:backgroundTint="#80000000"
android:visibility="gone"
style="@style/Widget.MaterialComponents.BottomAppBar">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title_text_view_view_imgur_image_fragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:textColor="#FFFFFF"
android:textSize="?attr/font_20"
android:fontFamily="?attr/font_family"
android:maxLines="1"
android:ellipsize="end" />
<ImageView
android:id="@+id/download_image_view_view_imgur_image_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:src="@drawable/ic_file_download_toolbar_white_24dp"
android:background="?attr/selectableItemBackgroundBorderless" />
<ImageView
android:id="@+id/share_image_view_view_imgur_image_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:src="@drawable/ic_share_toolbar_white_24dp"
android:background="?attr/selectableItemBackgroundBorderless" />
<ImageView
android:id="@+id/wallpaper_image_view_view_imgur_image_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:padding="16dp"
android:src="@drawable/ic_wallpaper_white_24dp"
android:background="?attr/selectableItemBackgroundBorderless" />
</LinearLayout>
</com.google.android.material.bottomappbar.BottomAppBar>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

View File

@ -1,40 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.ViewImgurImageFragment">
<ProgressBar
android:id="@+id/progress_bar_view_imgur_image_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
android:id="@+id/image_view_view_imgur_image_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/load_image_error_linear_layout_view_imgur_image_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_error_outline_white_24dp"
android:layout_gravity="center"
android:gravity="center"
android:textColor="@android:color/white"
android:text="@string/error_loading_image_tap_to_retry"
android:textSize="?attr/font_default"
android:fontFamily="?attr/font_family" />
</LinearLayout>
</RelativeLayout>