User require*() for activity context

This commit is contained in:
Naveen 2022-09-24 01:13:47 +05:30
parent 99715ca47c
commit c05b351b9f
2 changed files with 57 additions and 47 deletions

View file

@ -42,9 +42,11 @@ import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.helpers.isRPlus import com.simplemobiletools.commons.helpers.isRPlus
import com.simplemobiletools.commons.helpers.mydebug
import com.simplemobiletools.gallery.pro.R import com.simplemobiletools.gallery.pro.R
import com.simplemobiletools.gallery.pro.activities.* import com.simplemobiletools.gallery.pro.activities.PanoramaPhotoActivity
import com.simplemobiletools.gallery.pro.activities.PhotoActivity
import com.simplemobiletools.gallery.pro.activities.PhotoVideoActivity
import com.simplemobiletools.gallery.pro.activities.ViewPagerActivity
import com.simplemobiletools.gallery.pro.adapters.PortraitPhotosAdapter import com.simplemobiletools.gallery.pro.adapters.PortraitPhotosAdapter
import com.simplemobiletools.gallery.pro.extensions.config import com.simplemobiletools.gallery.pro.extensions.config
import com.simplemobiletools.gallery.pro.extensions.saveRotatedImageToFile import com.simplemobiletools.gallery.pro.extensions.saveRotatedImageToFile
@ -99,12 +101,16 @@ class PhotoFragment : ViewPagerFragment() {
private lateinit var mMedium: Medium private lateinit var mMedium: Medium
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
val context = requireContext()
val activity = requireActivity()
val arguments = requireArguments()
mView = (inflater.inflate(R.layout.pager_photo_item, container, false) as ViewGroup) mView = (inflater.inflate(R.layout.pager_photo_item, container, false) as ViewGroup)
if (!arguments!!.getBoolean(SHOULD_INIT_FRAGMENT, true)) { if (!arguments.getBoolean(SHOULD_INIT_FRAGMENT, true)) {
return mView return mView
} }
mMedium = arguments!!.getSerializable(MEDIUM) as Medium mMedium = arguments.getSerializable(MEDIUM) as Medium
mOriginalPath = mMedium.path mOriginalPath = mMedium.path
mView.apply { mView.apply {
@ -118,7 +124,7 @@ class PhotoFragment : ViewPagerFragment() {
instant_prev_item.parentView = container instant_prev_item.parentView = container
instant_next_item.parentView = container instant_next_item.parentView = container
photo_brightness_controller.initialize(activity!!, slide_info, true, container, singleTap = { x, y -> photo_brightness_controller.initialize(activity, slide_info, true, container, singleTap = { x, y ->
mView.apply { mView.apply {
if (subsampling_view.isVisible()) { if (subsampling_view.isVisible()) {
subsampling_view.sendFakeClick(x, y) subsampling_view.sendFakeClick(x, y)
@ -214,7 +220,7 @@ class PhotoFragment : ViewPagerFragment() {
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
val config = context!!.config val config = requireContext().config
if (mWasInit && (config.showExtendedDetails != mStoredShowExtendedDetails || config.extendedDetails != mStoredExtendedDetails)) { if (mWasInit && (config.showExtendedDetails != mStoredShowExtendedDetails || config.extendedDetails != mStoredExtendedDetails)) {
initExtendedDetails() initExtendedDetails()
} }
@ -253,7 +259,7 @@ class PhotoFragment : ViewPagerFragment() {
try { try {
if (context != null) { if (context != null) {
Glide.with(context!!).clear(mView.gestures_view) Glide.with(requireContext()).clear(mView.gestures_view)
} }
} catch (ignored: Exception) { } catch (ignored: Exception) {
} }
@ -307,7 +313,7 @@ class PhotoFragment : ViewPagerFragment() {
} }
private fun storeStateVariables() { private fun storeStateVariables() {
context!!.config.apply { requireContext().config.apply {
mStoredShowExtendedDetails = showExtendedDetails mStoredShowExtendedDetails = showExtendedDetails
mStoredHideExtendedDetails = hideExtendedDetails mStoredHideExtendedDetails = hideExtendedDetails
mStoredAllowDeepZoomableImages = allowZoomingImages mStoredAllowDeepZoomableImages = allowZoomingImages
@ -379,7 +385,7 @@ class PhotoFragment : ViewPagerFragment() {
try { try {
val pathToLoad = getPathToLoad(mMedium) val pathToLoad = getPathToLoad(mMedium)
val source = if (pathToLoad.startsWith("content://") || pathToLoad.startsWith("file://")) { val source = if (pathToLoad.startsWith("content://") || pathToLoad.startsWith("file://")) {
InputSource.UriSource(context!!.contentResolver, Uri.parse(pathToLoad)) InputSource.UriSource(requireContext().contentResolver, Uri.parse(pathToLoad))
} else { } else {
InputSource.FileSource(pathToLoad) InputSource.FileSource(pathToLoad)
} }
@ -400,7 +406,7 @@ class PhotoFragment : ViewPagerFragment() {
private fun loadSVG() { private fun loadSVG() {
if (context != null) { if (context != null) {
Glide.with(context!!) Glide.with(requireContext())
.`as`(PictureDrawable::class.java) .`as`(PictureDrawable::class.java)
.listener(SvgSoftwareLayerSetter()) .listener(SvgSoftwareLayerSetter())
.load(mMedium.path) .load(mMedium.path)
@ -448,7 +454,7 @@ class PhotoFragment : ViewPagerFragment() {
options.diskCacheStrategy(DiskCacheStrategy.NONE) options.diskCacheStrategy(DiskCacheStrategy.NONE)
} }
Glide.with(context!!) Glide.with(requireContext())
.load(path) .load(path)
.apply(options) .apply(options)
.listener(object : RequestListener<Drawable> { .listener(object : RequestListener<Drawable> {
@ -517,7 +523,7 @@ class PhotoFragment : ViewPagerFragment() {
private fun showPortraitStripe() { private fun showPortraitStripe() {
val files = File(mMedium.parentPath).listFiles()?.toMutableList() as? ArrayList<File> val files = File(mMedium.parentPath).listFiles()?.toMutableList() as? ArrayList<File>
if (files != null) { if (files != null) {
val screenWidth = context!!.realScreenSize.x val screenWidth = requireContext().realScreenSize.x
val itemWidth = resources.getDimension(R.dimen.portrait_photos_stripe_height).toInt() + resources.getDimension(R.dimen.one_dp).toInt() val itemWidth = resources.getDimension(R.dimen.portrait_photos_stripe_height).toInt() + resources.getDimension(R.dimen.one_dp).toInt()
val sideWidth = screenWidth / 2 - itemWidth / 2 val sideWidth = screenWidth / 2 - itemWidth / 2
val fakeItemsCnt = ceil(sideWidth / itemWidth.toDouble()).toInt() val fakeItemsCnt = ceil(sideWidth / itemWidth.toDouble()).toInt()
@ -529,7 +535,7 @@ class PhotoFragment : ViewPagerFragment() {
} }
val sideElementWidth = curWidth - screenWidth val sideElementWidth = curWidth - screenWidth
val adapter = PortraitPhotosAdapter(context!!, paths, sideElementWidth) { position, x -> val adapter = PortraitPhotosAdapter(requireContext(), paths, sideElementWidth) { position, x ->
if (mIsFullscreen) { if (mIsFullscreen) {
return@PortraitPhotosAdapter return@PortraitPhotosAdapter
} }
@ -579,8 +585,8 @@ class PhotoFragment : ViewPagerFragment() {
} }
private fun setupStripeBottomMargin() { private fun setupStripeBottomMargin() {
var bottomMargin = context!!.navigationBarHeight + resources.getDimension(R.dimen.normal_margin).toInt() var bottomMargin = requireContext().navigationBarHeight + resources.getDimension(R.dimen.normal_margin).toInt()
if (context!!.config.bottomActions) { if (requireContext().config.bottomActions) {
bottomMargin += resources.getDimension(R.dimen.bottom_actions_height).toInt() bottomMargin += resources.getDimension(R.dimen.bottom_actions_height).toInt()
} }
(mView.photo_portrait_stripe_wrapper.layoutParams as RelativeLayout.LayoutParams).bottomMargin = bottomMargin (mView.photo_portrait_stripe_wrapper.layoutParams as RelativeLayout.LayoutParams).bottomMargin = bottomMargin
@ -647,7 +653,7 @@ class PhotoFragment : ViewPagerFragment() {
private fun addZoomableView() { private fun addZoomableView() {
val rotation = degreesForRotation(mImageOrientation) val rotation = degreesForRotation(mImageOrientation)
mIsSubsamplingVisible = true mIsSubsamplingVisible = true
val config = context!!.config val config = requireContext().config
val showHighestQuality = config.showHighestQuality val showHighestQuality = config.showHighestQuality
val minTileDpi = if (showHighestQuality) -1 else getMinTileDpi() val minTileDpi = if (showHighestQuality) -1 else getMinTileDpi()
@ -734,7 +740,7 @@ class PhotoFragment : ViewPagerFragment() {
private fun checkIfPanorama() { private fun checkIfPanorama() {
mIsPanorama = try { mIsPanorama = try {
val inputStream = if (mMedium.path.startsWith("content:/")) { val inputStream = if (mMedium.path.startsWith("content:/")) {
context!!.contentResolver.openInputStream(Uri.parse(mMedium.path)) requireContext().contentResolver.openInputStream(Uri.parse(mMedium.path))
} else { } else {
File(mMedium.path).inputStream() File(mMedium.path).inputStream()
} }
@ -763,7 +769,7 @@ class PhotoFragment : ViewPagerFragment() {
try { try {
val path = getFilePathToShow() val path = getFilePathToShow()
orient = if (path.startsWith("content:/")) { orient = if (path.startsWith("content:/")) {
val inputStream = context!!.contentResolver.openInputStream(Uri.parse(path)) val inputStream = requireContext().contentResolver.openInputStream(Uri.parse(path))
val exif = ExifInterface() val exif = ExifInterface()
exif.readExif(inputStream, ExifInterface.Options.OPTION_ALL) exif.readExif(inputStream, ExifInterface.Options.OPTION_ALL)
val tag = exif.getTag(ExifInterface.TAG_ORIENTATION) val tag = exif.getTag(ExifInterface.TAG_ORIENTATION)
@ -773,9 +779,9 @@ class PhotoFragment : ViewPagerFragment() {
exif.getAttributeInt(TAG_ORIENTATION, defaultOrientation) exif.getAttributeInt(TAG_ORIENTATION, defaultOrientation)
} }
if (orient == defaultOrientation || context!!.isPathOnOTG(getFilePathToShow())) { if (orient == defaultOrientation || requireContext().isPathOnOTG(getFilePathToShow())) {
val uri = if (path.startsWith("content:/")) Uri.parse(path) else Uri.fromFile(File(path)) val uri = if (path.startsWith("content:/")) Uri.parse(path) else Uri.fromFile(File(path))
val inputStream = context!!.contentResolver.openInputStream(uri) val inputStream = requireContext().contentResolver.openInputStream(uri)
val exif2 = ExifInterface() val exif2 = ExifInterface()
exif2.readExif(inputStream, ExifInterface.Options.OPTION_ALL) exif2.readExif(inputStream, ExifInterface.Options.OPTION_ALL)
orient = exif2.getTag(ExifInterface.TAG_ORIENTATION)?.getValueAsInt(defaultOrientation) ?: defaultOrientation orient = exif2.getTag(ExifInterface.TAG_ORIENTATION)?.getValueAsInt(defaultOrientation) ?: defaultOrientation
@ -792,13 +798,13 @@ class PhotoFragment : ViewPagerFragment() {
return if (context == null || Math.abs(bitmapAspectRatio - screenAspectRatio) < SAME_ASPECT_RATIO_THRESHOLD) { return if (context == null || Math.abs(bitmapAspectRatio - screenAspectRatio) < SAME_ASPECT_RATIO_THRESHOLD) {
DEFAULT_DOUBLE_TAP_ZOOM DEFAULT_DOUBLE_TAP_ZOOM
} else if (context!!.portrait && bitmapAspectRatio <= screenAspectRatio) { } else if (requireContext().portrait && bitmapAspectRatio <= screenAspectRatio) {
mScreenHeight / height.toFloat() mScreenHeight / height.toFloat()
} else if (context!!.portrait && bitmapAspectRatio > screenAspectRatio) { } else if (requireContext().portrait && bitmapAspectRatio > screenAspectRatio) {
mScreenWidth / width.toFloat() mScreenWidth / width.toFloat()
} else if (!context!!.portrait && bitmapAspectRatio >= screenAspectRatio) { } else if (!requireContext().portrait && bitmapAspectRatio >= screenAspectRatio) {
mScreenWidth / width.toFloat() mScreenWidth / width.toFloat()
} else if (!context!!.portrait && bitmapAspectRatio < screenAspectRatio) { } else if (!requireContext().portrait && bitmapAspectRatio < screenAspectRatio) {
mScreenHeight / height.toFloat() mScreenHeight / height.toFloat()
} else { } else {
DEFAULT_DOUBLE_TAP_ZOOM DEFAULT_DOUBLE_TAP_ZOOM
@ -817,7 +823,7 @@ class PhotoFragment : ViewPagerFragment() {
} }
private fun initExtendedDetails() { private fun initExtendedDetails() {
if (context!!.config.showExtendedDetails) { if (requireContext().config.showExtendedDetails) {
mView.photo_details.apply { mView.photo_details.apply {
beInvisible() // make it invisible so we can measure it, but not show yet beInvisible() // make it invisible so we can measure it, but not show yet
text = getMediumExtendedDetails(mMedium) text = getMediumExtendedDetails(mMedium)
@ -827,7 +833,7 @@ class PhotoFragment : ViewPagerFragment() {
if (realY > 0) { if (realY > 0) {
y = realY y = realY
beVisibleIf(text.isNotEmpty()) beVisibleIf(text.isNotEmpty())
alpha = if (!context!!.config.hideExtendedDetails || !mIsFullscreen) 1f else 0f alpha = if (!requireContext().config.hideExtendedDetails || !mIsFullscreen) 1f else 0f
} }
} }
} }
@ -881,8 +887,8 @@ class PhotoFragment : ViewPagerFragment() {
private fun getExtendedDetailsY(height: Int): Float { private fun getExtendedDetailsY(height: Int): Float {
val smallMargin = context?.resources?.getDimension(R.dimen.small_margin) ?: return 0f val smallMargin = context?.resources?.getDimension(R.dimen.small_margin) ?: return 0f
val fullscreenOffset = smallMargin + if (mIsFullscreen) 0 else context!!.navigationBarHeight val fullscreenOffset = smallMargin + if (mIsFullscreen) 0 else requireContext().navigationBarHeight
val actionsHeight = if (context!!.config.bottomActions && !mIsFullscreen) resources.getDimension(R.dimen.bottom_actions_height) else 0f val actionsHeight = if (requireContext().config.bottomActions && !mIsFullscreen) resources.getDimension(R.dimen.bottom_actions_height) else 0f
return context!!.realScreenSize.y - height - actionsHeight - fullscreenOffset return requireContext().realScreenSize.y - height - actionsHeight - fullscreenOffset
} }
} }

View file

@ -79,9 +79,13 @@ class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, S
private lateinit var mPlayPauseButton: ImageView private lateinit var mPlayPauseButton: ImageView
private lateinit var mSeekBar: SeekBar private lateinit var mSeekBar: SeekBar
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
mMedium = arguments!!.getSerializable(MEDIUM) as Medium val context = requireContext()
mConfig = context!!.config val activity = requireActivity()
val arguments = requireArguments()
mMedium = arguments.getSerializable(MEDIUM) as Medium
mConfig = context.config
mView = inflater.inflate(R.layout.pager_video_item, container, false).apply { mView = inflater.inflate(R.layout.pager_video_item, container, false).apply {
panorama_outline.setOnClickListener { openPanorama() } panorama_outline.setOnClickListener { openPanorama() }
video_curr_time.setOnClickListener { skip(false) } video_curr_time.setOnClickListener { skip(false) }
@ -154,24 +158,24 @@ class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, S
} }
} }
if (!arguments!!.getBoolean(SHOULD_INIT_FRAGMENT, true)) { if (!arguments.getBoolean(SHOULD_INIT_FRAGMENT, true)) {
return mView return mView
} }
storeStateVariables() storeStateVariables()
Glide.with(context!!).load(mMedium.path).into(mView.video_preview) Glide.with(context).load(mMedium.path).into(mView.video_preview)
// setMenuVisibility is not called at VideoActivity (third party intent) // setMenuVisibility is not called at VideoActivity (third party intent)
if (!mIsFragmentVisible && activity is VideoActivity) { if (!mIsFragmentVisible && activity is VideoActivity) {
mIsFragmentVisible = true mIsFragmentVisible = true
} }
mIsFullscreen = activity!!.window.decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_FULLSCREEN == View.SYSTEM_UI_FLAG_FULLSCREEN mIsFullscreen = activity.window.decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_FULLSCREEN == View.SYSTEM_UI_FLAG_FULLSCREEN
initTimeHolder() initTimeHolder()
checkIfPanorama() checkIfPanorama()
ensureBackgroundThread { ensureBackgroundThread {
activity?.getVideoResolution(mMedium.path)?.apply { activity.getVideoResolution(mMedium.path)?.apply {
mVideoSize.x = x mVideoSize.x = x
mVideoSize.y = y mVideoSize.y = y
} }
@ -183,7 +187,7 @@ class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, S
video_play_outline.beGone() video_play_outline.beGone()
mVolumeSideScroll.beGone() mVolumeSideScroll.beGone()
mBrightnessSideScroll.beGone() mBrightnessSideScroll.beGone()
Glide.with(context!!).load(mMedium.path).into(video_preview) Glide.with(context).load(mMedium.path).into(video_preview)
} }
} }
@ -196,7 +200,7 @@ class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, S
setVideoSize() setVideoSize()
mView.apply { mView.apply {
mBrightnessSideScroll.initialize(activity!!, slide_info, true, container, singleTap = { x, y -> mBrightnessSideScroll.initialize(activity, slide_info, true, container, singleTap = { x, y ->
if (mConfig.allowInstantChange) { if (mConfig.allowInstantChange) {
listener?.goToPrevItem() listener?.goToPrevItem()
} else { } else {
@ -206,7 +210,7 @@ class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, S
doSkip(false) doSkip(false)
}) })
mVolumeSideScroll.initialize(activity!!, slide_info, false, container, singleTap = { x, y -> mVolumeSideScroll.initialize(activity, slide_info, false, container, singleTap = { x, y ->
if (mConfig.allowInstantChange) { if (mConfig.allowInstantChange) {
listener?.goToNextItem() listener?.goToNextItem()
} else { } else {
@ -234,8 +238,8 @@ class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, S
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
mConfig = context!!.config // make sure we get a new config, in case the user changed something in the app settings mConfig = requireContext().config // make sure we get a new config, in case the user changed something in the app settings
activity!!.updateTextColors(mView.video_holder) requireActivity().updateTextColors(mView.video_holder)
val allowVideoGestures = mConfig.allowVideoGestures val allowVideoGestures = mConfig.allowVideoGestures
mTextureView.beGoneIf(mConfig.openVideosOnSeparateScreen || mIsPanorama) mTextureView.beGoneIf(mConfig.openVideosOnSeparateScreen || mIsPanorama)
mView.video_surface_frame.beGoneIf(mTextureView.isGone()) mView.video_surface_frame.beGoneIf(mTextureView.isGone())
@ -456,13 +460,13 @@ class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, S
private fun initTimeHolder() { private fun initTimeHolder() {
var right = 0 var right = 0
var bottom = context!!.navigationBarHeight var bottom = requireContext().navigationBarHeight
if (mConfig.bottomActions) { if (mConfig.bottomActions) {
bottom += resources.getDimension(R.dimen.bottom_actions_height).toInt() bottom += resources.getDimension(R.dimen.bottom_actions_height).toInt()
} }
if (resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE && activity?.hasNavBar() == true) { if (resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE && activity?.hasNavBar() == true) {
right += activity!!.navigationBarWidth right += requireActivity().navigationBarWidth
} }
(mTimeHolder.layoutParams as RelativeLayout.LayoutParams).apply { (mTimeHolder.layoutParams as RelativeLayout.LayoutParams).apply {
@ -476,7 +480,7 @@ class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, S
try { try {
val fis = FileInputStream(File(mMedium.path)) val fis = FileInputStream(File(mMedium.path))
fis.use { fis.use {
context!!.parseFileChannel(mMedium.path, it.channel, 0, 0, 0) { requireContext().parseFileChannel(mMedium.path, it.channel, 0, 0, 0) {
mIsPanorama = true mIsPanorama = true
} }
} }
@ -518,7 +522,7 @@ class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, S
private fun getExtendedDetailsY(height: Int): Float { private fun getExtendedDetailsY(height: Int): Float {
val smallMargin = context?.resources?.getDimension(R.dimen.small_margin) ?: return 0f val smallMargin = context?.resources?.getDimension(R.dimen.small_margin) ?: return 0f
val fullscreenOffset = smallMargin + if (mIsFullscreen) 0 else context!!.navigationBarHeight val fullscreenOffset = smallMargin + if (mIsFullscreen) 0 else requireContext().navigationBarHeight
var actionsHeight = 0f var actionsHeight = 0f
if (!mIsFullscreen) { if (!mIsFullscreen) {
actionsHeight += resources.getDimension(R.dimen.video_player_play_pause_size) actionsHeight += resources.getDimension(R.dimen.video_player_play_pause_size)
@ -526,7 +530,7 @@ class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, S
actionsHeight += resources.getDimension(R.dimen.bottom_actions_height) actionsHeight += resources.getDimension(R.dimen.bottom_actions_height)
} }
} }
return context!!.realScreenSize.y - height - actionsHeight - fullscreenOffset return requireContext().realScreenSize.y - height - actionsHeight - fullscreenOffset
} }
private fun skip(forward: Boolean) { private fun skip(forward: Boolean) {
@ -774,7 +778,7 @@ class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, S
} }
val videoProportion = mVideoSize.x.toFloat() / mVideoSize.y.toFloat() val videoProportion = mVideoSize.x.toFloat() / mVideoSize.y.toFloat()
val display = activity!!.windowManager.defaultDisplay val display = requireActivity().windowManager.defaultDisplay
val screenWidth: Int val screenWidth: Int
val screenHeight: Int val screenHeight: Int