From 53c7c78801f3715ee2b2f5fbd2c3178d24237212 Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 3 Oct 2016 23:27:56 +0200 Subject: [PATCH] handle orientation changes manually to prevent video glitches --- app/src/main/AndroidManifest.xml | 6 ++- .../activities/PhotoVideoActivity.java | 16 +++++-- .../gallery/activities/ViewPagerActivity.java | 7 +++ .../gallery/adapters/MyPagerAdapter.java | 9 ++++ .../gallery/fragments/PhotoFragment.java | 34 ++++++++------- .../gallery/fragments/VideoFragment.java | 43 +++++++++++-------- .../gallery/fragments/ViewPagerFragment.java | 2 + 7 files changed, 77 insertions(+), 40 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 59d6b2d09..430f6d32f 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,7 +1,7 @@ + xmlns:android="http://schemas.android.com/apk/res/android" + package="com.simplemobiletools.gallery"> @@ -54,6 +54,7 @@ @@ -101,6 +102,7 @@ diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/PhotoVideoActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/PhotoVideoActivity.java index eb6860196..bb4414322 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/PhotoVideoActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/PhotoVideoActivity.java @@ -1,6 +1,7 @@ package com.simplemobiletools.gallery.activities; import android.content.Intent; +import android.content.res.Configuration; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.ActionBar; @@ -20,6 +21,7 @@ import java.io.File; public class PhotoVideoActivity extends SimpleActivity implements ViewPagerFragment.FragmentClickListener { private static ActionBar mActionbar; private static Uri mUri; + private static ViewPagerFragment mFragment; private static boolean mIsFullScreen; @@ -44,15 +46,21 @@ public class PhotoVideoActivity extends SimpleActivity implements ViewPagerFragm bundle.putSerializable(Constants.MEDIUM, medium); if (savedInstanceState == null) { - final ViewPagerFragment fragment = (mIsVideo ? new VideoFragment() : new PhotoFragment()); - fragment.setListener(this); - fragment.setArguments(bundle); - getSupportFragmentManager().beginTransaction().replace(R.id.fragment_holder, fragment).commit(); + mFragment = (mIsVideo ? new VideoFragment() : new PhotoFragment()); + mFragment.setListener(this); + mFragment.setArguments(bundle); + getSupportFragmentManager().beginTransaction().replace(R.id.fragment_holder, mFragment).commit(); } hideSystemUI(); setTitle(Utils.getFilename(mUri.toString())); } + @Override + public void onConfigurationChanged(Configuration newConfig) { + super.onConfigurationChanged(newConfig); + mFragment.confChanged(); + } + @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.photo_video_menu, menu); diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java index d50eb6d33..c2d92ba44 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java @@ -164,6 +164,13 @@ public class ViewPagerActivity extends SimpleActivity } } + @Override + public void onConfigurationChanged(Configuration newConfig) { + super.onConfigurationChanged(newConfig); + final MyPagerAdapter adapter = (MyPagerAdapter) mPager.getAdapter(); + adapter.confChanged(mPos); + } + private void setAsWallpaper() { final Bitmap bitmap = BitmapFactory.decodeFile(getCurrentFile().getAbsolutePath()); final WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext()); diff --git a/app/src/main/java/com/simplemobiletools/gallery/adapters/MyPagerAdapter.java b/app/src/main/java/com/simplemobiletools/gallery/adapters/MyPagerAdapter.java index ba66b06ab..63cd7214b 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/adapters/MyPagerAdapter.java +++ b/app/src/main/java/com/simplemobiletools/gallery/adapters/MyPagerAdapter.java @@ -67,6 +67,15 @@ public class MyPagerAdapter extends FragmentStatePagerAdapter { } } + public void confChanged(int pos) { + for (int i = -1; i <= 1; i++) { + final ViewPagerFragment fragment = mFragments.get(pos + i); + if (fragment != null) { + fragment.confChanged(); + } + } + } + public void updateItems(List newPaths) { mMedia.clear(); mMedia.addAll(newPaths); diff --git a/app/src/main/java/com/simplemobiletools/gallery/fragments/PhotoFragment.java b/app/src/main/java/com/simplemobiletools/gallery/fragments/PhotoFragment.java index 2f98c7f05..629f76b77 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/fragments/PhotoFragment.java +++ b/app/src/main/java/com/simplemobiletools/gallery/fragments/PhotoFragment.java @@ -15,36 +15,33 @@ import com.simplemobiletools.gallery.R; import com.simplemobiletools.gallery.models.Medium; public class PhotoFragment extends ViewPagerFragment implements View.OnClickListener { + private View mView; + private SubsamplingScaleImageView mSubsamplingView; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - final View view = inflater.inflate(R.layout.pager_photo_item, container, false); + mView = inflater.inflate(R.layout.pager_photo_item, container, false); final Medium medium = (Medium) getArguments().getSerializable(Constants.MEDIUM); if (medium == null) - return view; + return mView; - final SubsamplingScaleImageView subsamplingView = (SubsamplingScaleImageView) view.findViewById(R.id.photo_view); + mSubsamplingView = (SubsamplingScaleImageView) mView.findViewById(R.id.photo_view); if (medium.isGif()) { - subsamplingView.setVisibility(View.GONE); - final ImageView imageView = (ImageView) view.findViewById(R.id.gif_view); + mSubsamplingView.setVisibility(View.GONE); + final ImageView imageView = (ImageView) mView.findViewById(R.id.gif_view); imageView.setVisibility(View.VISIBLE); Glide.with(getContext()).load(medium.getPath()).asGif().diskCacheStrategy(DiskCacheStrategy.NONE).into(imageView); imageView.setOnClickListener(this); } else { - subsamplingView.setOrientation(SubsamplingScaleImageView.ORIENTATION_USE_EXIF); - subsamplingView.setImage(ImageSource.uri(medium.getPath())); - subsamplingView.setMaxScale(4f); - subsamplingView.setMinimumTileDpi(200); - subsamplingView.setOnClickListener(this); + mSubsamplingView.setOrientation(SubsamplingScaleImageView.ORIENTATION_USE_EXIF); + mSubsamplingView.setImage(ImageSource.uri(medium.getPath())); + mSubsamplingView.setMaxScale(4f); + mSubsamplingView.setMinimumTileDpi(200); + mSubsamplingView.setOnClickListener(this); } - return view; - } - - @Override - public void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); + return mView; } @Override @@ -57,6 +54,11 @@ public class PhotoFragment extends ViewPagerFragment implements View.OnClickList } + @Override + public void confChanged() { + + } + @Override public void onClick(View v) { photoClicked(); diff --git a/app/src/main/java/com/simplemobiletools/gallery/fragments/VideoFragment.java b/app/src/main/java/com/simplemobiletools/gallery/fragments/VideoFragment.java index 0fbdb60f8..a9e31de63 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/fragments/VideoFragment.java +++ b/app/src/main/java/com/simplemobiletools/gallery/fragments/VideoFragment.java @@ -47,6 +47,7 @@ public class VideoFragment extends ViewPagerFragment private SeekBar mSeekBar; private Medium mMedium; private View mTimeHolder; + private View mView; private boolean mIsPlaying; private boolean mIsDragged; @@ -56,7 +57,7 @@ public class VideoFragment extends ViewPagerFragment @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - final View view = inflater.inflate(R.layout.pager_video_item, container, false); + mView = inflater.inflate(R.layout.pager_video_item, container, false); mMedium = (Medium) getArguments().getSerializable(Constants.MEDIUM); if (savedInstanceState != null) { @@ -65,25 +66,25 @@ public class VideoFragment extends ViewPagerFragment mIsFullscreen = (getActivity().getWindow().getDecorView().getSystemUiVisibility() & View.SYSTEM_UI_FLAG_FULLSCREEN) == View.SYSTEM_UI_FLAG_FULLSCREEN; - setupPlayer(view); - view.setOnClickListener(this); + setupPlayer(); + mView.setOnClickListener(this); - return view; + return mView; } - private void setupPlayer(View view) { + private void setupPlayer() { if (getActivity() == null) return; - mPlayOutline = (ImageView) view.findViewById(R.id.video_play_outline); + mPlayOutline = (ImageView) mView.findViewById(R.id.video_play_outline); mPlayOutline.setOnClickListener(this); - mSurfaceView = (SurfaceView) view.findViewById(R.id.video_surface); + mSurfaceView = (SurfaceView) mView.findViewById(R.id.video_surface); mSurfaceView.setOnClickListener(this); mSurfaceHolder = mSurfaceView.getHolder(); mSurfaceHolder.addCallback(this); - initTimeHolder(view); + initTimeHolder(); } public void itemDragged() { @@ -98,14 +99,20 @@ public class VideoFragment extends ViewPagerFragment } } - private void initTimeHolder(View view) { - mTimeHolder = view.findViewById(R.id.video_time_holder); + @Override + public void confChanged() { + setVideoSize(); + initTimeHolder(); + } + + private void initTimeHolder() { + mTimeHolder = mView.findViewById(R.id.video_time_holder); final Resources res = getResources(); final int height = Utils.getNavBarHeight(res); final int left = mTimeHolder.getPaddingLeft(); final int top = mTimeHolder.getPaddingTop(); - int right = mTimeHolder.getPaddingRight(); - int bottom = mTimeHolder.getPaddingBottom(); + int right = (int) getResources().getDimension(R.dimen.timer_padding); + int bottom = 0; if (Utils.hasNavBar(getActivity())) { if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { @@ -116,9 +123,9 @@ public class VideoFragment extends ViewPagerFragment mTimeHolder.setPadding(left, top, right, bottom); } - mCurrTimeView = (TextView) view.findViewById(R.id.video_curr_time); - mDurationView = (TextView) view.findViewById(R.id.video_duration); - mSeekBar = (SeekBar) view.findViewById(R.id.video_seekbar); + mCurrTimeView = (TextView) mView.findViewById(R.id.video_curr_time); + mDurationView = (TextView) mView.findViewById(R.id.video_duration); + mSeekBar = (SeekBar) mView.findViewById(R.id.video_seekbar); mSeekBar.setOnSeekBarChangeListener(this); if (mIsFullscreen) @@ -296,14 +303,14 @@ public class VideoFragment extends ViewPagerFragment @Override public void onVideoSizeChanged(MediaPlayer mp, int width, int height) { - setVideoSize(width, height); + setVideoSize(); } - private void setVideoSize(int videoWidth, int videoHeight) { + private void setVideoSize() { if (getActivity() == null) return; - final float videoProportion = (float) videoWidth / (float) videoHeight; + final float videoProportion = (float) mMediaPlayer.getVideoWidth() / (float) mMediaPlayer.getVideoHeight(); final Display display = getActivity().getWindowManager().getDefaultDisplay(); int screenWidth; int screenHeight; diff --git a/app/src/main/java/com/simplemobiletools/gallery/fragments/ViewPagerFragment.java b/app/src/main/java/com/simplemobiletools/gallery/fragments/ViewPagerFragment.java index 235839c92..31062b032 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/fragments/ViewPagerFragment.java +++ b/app/src/main/java/com/simplemobiletools/gallery/fragments/ViewPagerFragment.java @@ -13,6 +13,8 @@ public abstract class ViewPagerFragment extends Fragment { public abstract void systemUiVisibilityChanged(boolean toFullscreen); + public abstract void confChanged(); + public interface FragmentClickListener { void fragmentClicked(); }