From 7fe451b7e15f97b486e61e4217178f5cf454accf Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 3 Aug 2021 22:03:45 +0800 Subject: Use bottom toolbar in ViewImageOrGifActivity is available. --- .../activities/ViewImageOrGifActivity.java | 135 +++++++++++++++------ .../utils/SharedPreferencesUtils.java | 1 + .../main/res/drawable/ic_wallpaper_white_24dp.xml | 5 + app/src/main/res/layout/activity_theme_preview.xml | 1 + .../main/res/layout/activity_view_image_or_gif.xml | 62 +++++++++- app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/interface_preferences.xml | 5 + 7 files changed, 167 insertions(+), 43 deletions(-) create mode 100644 app/src/main/res/drawable/ic_wallpaper_white_24dp.xml (limited to 'app') 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 f0227961..34fb586d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewImageOrGifActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewImageOrGifActivity.java @@ -12,11 +12,14 @@ import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.text.Html; +import android.text.Spanned; import android.view.Menu; import android.view.MenuItem; import android.view.View; +import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ProgressBar; +import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; @@ -42,6 +45,7 @@ import com.github.piasy.biv.loader.ImageLoader; import com.github.piasy.biv.loader.glide.GlideImageLoader; import com.github.piasy.biv.view.BigImageView; import com.github.piasy.biv.view.GlideImageViewFactory; +import com.google.android.material.bottomappbar.BottomAppBar; import com.r0adkll.slidr.Slidr; import com.r0adkll.slidr.model.SlidrConfig; import com.r0adkll.slidr.model.SlidrPosition; @@ -85,6 +89,16 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa BigImageView mImageView; @BindView(R.id.load_image_error_linear_layout_view_image_or_gif_activity) LinearLayout mLoadErrorLinearLayout; + @BindView(R.id.bottom_navigation_view_image_or_gif_activity) + BottomAppBar bottomAppBar; + @BindView(R.id.title_text_view_view_image_or_gif_activity) + TextView titleTextView; + @BindView(R.id.download_image_view_view_image_or_gif_activity) + ImageView downloadImageView; + @BindView(R.id.share_image_view_view_image_or_gif_activity) + ImageView shareImageView; + @BindView(R.id.wallpaper_image_view_view_image_or_gif_activity) + ImageView wallpaperImageView; @Inject @Named("default") SharedPreferences mSharedPreferences; @@ -151,10 +165,40 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa String postTitle = intent.getStringExtra(EXTRA_POST_TITLE_KEY); mSubredditName = intent.getStringExtra(EXTRA_SUBREDDIT_OR_USERNAME_KEY); + boolean useBottomAppBar = mSharedPreferences.getBoolean(SharedPreferencesUtils.USE_BOTTOM_TOOLBAR_IN_MEDIA_VIEWER, false); if (postTitle != null) { - setTitle(Html.fromHtml(String.format("%s", postTitle))); + Spanned title = Html.fromHtml(String.format("%s", postTitle)); + if (useBottomAppBar) { + titleTextView.setText(title); + } else { + setTitle(title); + } + } else { + if (!useBottomAppBar) { + setTitle(""); + } + } + + if (useBottomAppBar) { + getSupportActionBar().hide(); + downloadImageView.setOnClickListener(view -> { + if (isDownloading) { + return; + } + isDownloading = true; + requestPermissionAndDownload(); + }); + shareImageView.setOnClickListener(view -> { + if (isGif) + shareGif(); + else + shareImage(); + }); + wallpaperImageView.setOnClickListener(view -> { + setWallpaper(); + }); } else { - setTitle(""); + bottomAppBar.setVisibility(View.GONE); } mLoadErrorLinearLayout.setOnClickListener(view -> { @@ -170,6 +214,9 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); isActionBarHidden = false; + if (useBottomAppBar) { + bottomAppBar.setVisibility(View.VISIBLE); + } } else { getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_STABLE @@ -179,6 +226,9 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE); isActionBarHidden = true; + if (useBottomAppBar) { + bottomAppBar.setVisibility(View.GONE); + } } }); @@ -287,26 +337,8 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa 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(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(); - } - + requestPermissionAndDownload(); return true; } else if (itemId == R.id.action_share_view_image_or_gif_activity) { if (isGif) @@ -315,31 +347,32 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa shareImage(); return true; } else if (itemId == 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 { - WallpaperSetter.set(mExecutor, new Handler(), mImageUrl, WallpaperSetter.BOTH_SCREENS, this, - new WallpaperSetter.SetWallpaperListener() { - @Override - public void success() { - Toast.makeText(ViewImageOrGifActivity.this, R.string.wallpaper_set, Toast.LENGTH_SHORT).show(); - } - - @Override - public void failed() { - Toast.makeText(ViewImageOrGifActivity.this, R.string.error_set_wallpaper, Toast.LENGTH_SHORT).show(); - } - }); - } - } + 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(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(); + } + } + private void download() { isDownloading = false; @@ -433,6 +466,28 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa }).submit(); } + private void setWallpaper() { + if (!isGif) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + SetAsWallpaperBottomSheetFragment setAsWallpaperBottomSheetFragment = new SetAsWallpaperBottomSheetFragment(); + setAsWallpaperBottomSheetFragment.show(getSupportFragmentManager(), setAsWallpaperBottomSheetFragment.getTag()); + } else { + WallpaperSetter.set(mExecutor, new Handler(), mImageUrl, WallpaperSetter.BOTH_SCREENS, this, + new WallpaperSetter.SetWallpaperListener() { + @Override + public void success() { + Toast.makeText(ViewImageOrGifActivity.this, R.string.wallpaper_set, Toast.LENGTH_SHORT).show(); + } + + @Override + public void failed() { + Toast.makeText(ViewImageOrGifActivity.this, R.string.error_set_wallpaper, Toast.LENGTH_SHORT).show(); + } + }); + } + } + } + @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/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java index 69b5917b..47b1f109 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java @@ -185,6 +185,7 @@ public class SharedPreferencesUtils { public static final String VIDEO_PLAYER_AUTOMATIC_LANDSCAPE_ORIENTATION = "video_player_automatic_landscape_orientation"; public static final String REMEMBER_MUTING_OPTION_IN_POST_FEED = "remember_muting_option_in_post_feed"; public static final String DEFAULT_LINK_POST_LAYOUT_KEY = "default_link_post_layout"; + public static final String USE_BOTTOM_TOOLBAR_IN_MEDIA_VIEWER = "use_bottom_toolbar_in_media_viewer"; public static final String DEFAULT_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit_preferences"; public static final String MAIN_PAGE_TABS_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.main_page_tabs"; diff --git a/app/src/main/res/drawable/ic_wallpaper_white_24dp.xml b/app/src/main/res/drawable/ic_wallpaper_white_24dp.xml new file mode 100644 index 00000000..9db92d6f --- /dev/null +++ b/app/src/main/res/drawable/ic_wallpaper_white_24dp.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/layout/activity_theme_preview.xml b/app/src/main/res/layout/activity_theme_preview.xml index 6ca8a0b0..67a75368 100644 --- a/app/src/main/res/layout/activity_theme_preview.xml +++ b/app/src/main/res/layout/activity_theme_preview.xml @@ -194,6 +194,7 @@ android:background="?attr/selectableItemBackgroundBorderless" /> + - + android:layout_gravity="center" /> - + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7d528f8e..30b81fd4 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -592,6 +592,7 @@ Post Details Separate Post And Comments in Portrait Mode Separate Post And Comments in Landscape Mode + Use Bottom Toolbar in Media Viewer Cannot get the link diff --git a/app/src/main/res/xml/interface_preferences.xml b/app/src/main/res/xml/interface_preferences.xml index 47207d0a..1a4e0f86 100644 --- a/app/src/main/res/xml/interface_preferences.xml +++ b/app/src/main/res/xml/interface_preferences.xml @@ -37,6 +37,11 @@ app:key="hide_subreddit_description" app:title="@string/settings_hide_subreddit_description_title" /> + +