handle orientation changes manually to prevent video glitches

This commit is contained in:
tibbi 2016-10-03 23:27:56 +02:00
parent 48fd0c9b8e
commit 53c7c78801
7 changed files with 77 additions and 40 deletions

View file

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<manifest <manifest
package="com.simplemobiletools.gallery" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res/android"> package="com.simplemobiletools.gallery">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.SET_WALLPAPER"/> <uses-permission android:name="android.permission.SET_WALLPAPER"/>
@ -54,6 +54,7 @@
<activity <activity
android:name=".activities.ViewPagerActivity" android:name=".activities.ViewPagerActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:parentActivityName=".activities.MediaActivity" android:parentActivityName=".activities.MediaActivity"
android:theme="@style/FullScreenTheme"> android:theme="@style/FullScreenTheme">
@ -101,6 +102,7 @@
<activity <activity
android:name=".activities.VideoActivity" android:name=".activities.VideoActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:theme="@style/FullScreenTheme"> android:theme="@style/FullScreenTheme">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.VIEW"/> <action android:name="android.intent.action.VIEW"/>

View file

@ -1,6 +1,7 @@
package com.simplemobiletools.gallery.activities; package com.simplemobiletools.gallery.activities;
import android.content.Intent; import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar;
@ -20,6 +21,7 @@ import java.io.File;
public class PhotoVideoActivity extends SimpleActivity implements ViewPagerFragment.FragmentClickListener { public class PhotoVideoActivity extends SimpleActivity implements ViewPagerFragment.FragmentClickListener {
private static ActionBar mActionbar; private static ActionBar mActionbar;
private static Uri mUri; private static Uri mUri;
private static ViewPagerFragment mFragment;
private static boolean mIsFullScreen; private static boolean mIsFullScreen;
@ -44,15 +46,21 @@ public class PhotoVideoActivity extends SimpleActivity implements ViewPagerFragm
bundle.putSerializable(Constants.MEDIUM, medium); bundle.putSerializable(Constants.MEDIUM, medium);
if (savedInstanceState == null) { if (savedInstanceState == null) {
final ViewPagerFragment fragment = (mIsVideo ? new VideoFragment() : new PhotoFragment()); mFragment = (mIsVideo ? new VideoFragment() : new PhotoFragment());
fragment.setListener(this); mFragment.setListener(this);
fragment.setArguments(bundle); mFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_holder, fragment).commit(); getSupportFragmentManager().beginTransaction().replace(R.id.fragment_holder, mFragment).commit();
} }
hideSystemUI(); hideSystemUI();
setTitle(Utils.getFilename(mUri.toString())); setTitle(Utils.getFilename(mUri.toString()));
} }
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mFragment.confChanged();
}
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.photo_video_menu, menu); getMenuInflater().inflate(R.menu.photo_video_menu, menu);

View file

@ -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() { private void setAsWallpaper() {
final Bitmap bitmap = BitmapFactory.decodeFile(getCurrentFile().getAbsolutePath()); final Bitmap bitmap = BitmapFactory.decodeFile(getCurrentFile().getAbsolutePath());
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext()); final WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());

View file

@ -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<Medium> newPaths) { public void updateItems(List<Medium> newPaths) {
mMedia.clear(); mMedia.clear();
mMedia.addAll(newPaths); mMedia.addAll(newPaths);

View file

@ -15,36 +15,33 @@ import com.simplemobiletools.gallery.R;
import com.simplemobiletools.gallery.models.Medium; import com.simplemobiletools.gallery.models.Medium;
public class PhotoFragment extends ViewPagerFragment implements View.OnClickListener { public class PhotoFragment extends ViewPagerFragment implements View.OnClickListener {
private View mView;
private SubsamplingScaleImageView mSubsamplingView;
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 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); final Medium medium = (Medium) getArguments().getSerializable(Constants.MEDIUM);
if (medium == null) 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()) { if (medium.isGif()) {
subsamplingView.setVisibility(View.GONE); mSubsamplingView.setVisibility(View.GONE);
final ImageView imageView = (ImageView) view.findViewById(R.id.gif_view); final ImageView imageView = (ImageView) mView.findViewById(R.id.gif_view);
imageView.setVisibility(View.VISIBLE); imageView.setVisibility(View.VISIBLE);
Glide.with(getContext()).load(medium.getPath()).asGif().diskCacheStrategy(DiskCacheStrategy.NONE).into(imageView); Glide.with(getContext()).load(medium.getPath()).asGif().diskCacheStrategy(DiskCacheStrategy.NONE).into(imageView);
imageView.setOnClickListener(this); imageView.setOnClickListener(this);
} else { } else {
subsamplingView.setOrientation(SubsamplingScaleImageView.ORIENTATION_USE_EXIF); mSubsamplingView.setOrientation(SubsamplingScaleImageView.ORIENTATION_USE_EXIF);
subsamplingView.setImage(ImageSource.uri(medium.getPath())); mSubsamplingView.setImage(ImageSource.uri(medium.getPath()));
subsamplingView.setMaxScale(4f); mSubsamplingView.setMaxScale(4f);
subsamplingView.setMinimumTileDpi(200); mSubsamplingView.setMinimumTileDpi(200);
subsamplingView.setOnClickListener(this); mSubsamplingView.setOnClickListener(this);
} }
return view; return mView;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
} }
@Override @Override
@ -57,6 +54,11 @@ public class PhotoFragment extends ViewPagerFragment implements View.OnClickList
} }
@Override
public void confChanged() {
}
@Override @Override
public void onClick(View v) { public void onClick(View v) {
photoClicked(); photoClicked();

View file

@ -47,6 +47,7 @@ public class VideoFragment extends ViewPagerFragment
private SeekBar mSeekBar; private SeekBar mSeekBar;
private Medium mMedium; private Medium mMedium;
private View mTimeHolder; private View mTimeHolder;
private View mView;
private boolean mIsPlaying; private boolean mIsPlaying;
private boolean mIsDragged; private boolean mIsDragged;
@ -56,7 +57,7 @@ public class VideoFragment extends ViewPagerFragment
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 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); mMedium = (Medium) getArguments().getSerializable(Constants.MEDIUM);
if (savedInstanceState != null) { if (savedInstanceState != null) {
@ -65,25 +66,25 @@ public class VideoFragment extends ViewPagerFragment
mIsFullscreen = (getActivity().getWindow().getDecorView().getSystemUiVisibility() & View.SYSTEM_UI_FLAG_FULLSCREEN) == mIsFullscreen = (getActivity().getWindow().getDecorView().getSystemUiVisibility() & View.SYSTEM_UI_FLAG_FULLSCREEN) ==
View.SYSTEM_UI_FLAG_FULLSCREEN; View.SYSTEM_UI_FLAG_FULLSCREEN;
setupPlayer(view); setupPlayer();
view.setOnClickListener(this); mView.setOnClickListener(this);
return view; return mView;
} }
private void setupPlayer(View view) { private void setupPlayer() {
if (getActivity() == null) if (getActivity() == null)
return; return;
mPlayOutline = (ImageView) view.findViewById(R.id.video_play_outline); mPlayOutline = (ImageView) mView.findViewById(R.id.video_play_outline);
mPlayOutline.setOnClickListener(this); mPlayOutline.setOnClickListener(this);
mSurfaceView = (SurfaceView) view.findViewById(R.id.video_surface); mSurfaceView = (SurfaceView) mView.findViewById(R.id.video_surface);
mSurfaceView.setOnClickListener(this); mSurfaceView.setOnClickListener(this);
mSurfaceHolder = mSurfaceView.getHolder(); mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this); mSurfaceHolder.addCallback(this);
initTimeHolder(view); initTimeHolder();
} }
public void itemDragged() { public void itemDragged() {
@ -98,14 +99,20 @@ public class VideoFragment extends ViewPagerFragment
} }
} }
private void initTimeHolder(View view) { @Override
mTimeHolder = view.findViewById(R.id.video_time_holder); public void confChanged() {
setVideoSize();
initTimeHolder();
}
private void initTimeHolder() {
mTimeHolder = mView.findViewById(R.id.video_time_holder);
final Resources res = getResources(); final Resources res = getResources();
final int height = Utils.getNavBarHeight(res); final int height = Utils.getNavBarHeight(res);
final int left = mTimeHolder.getPaddingLeft(); final int left = mTimeHolder.getPaddingLeft();
final int top = mTimeHolder.getPaddingTop(); final int top = mTimeHolder.getPaddingTop();
int right = mTimeHolder.getPaddingRight(); int right = (int) getResources().getDimension(R.dimen.timer_padding);
int bottom = mTimeHolder.getPaddingBottom(); int bottom = 0;
if (Utils.hasNavBar(getActivity())) { if (Utils.hasNavBar(getActivity())) {
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
@ -116,9 +123,9 @@ public class VideoFragment extends ViewPagerFragment
mTimeHolder.setPadding(left, top, right, bottom); mTimeHolder.setPadding(left, top, right, bottom);
} }
mCurrTimeView = (TextView) view.findViewById(R.id.video_curr_time); mCurrTimeView = (TextView) mView.findViewById(R.id.video_curr_time);
mDurationView = (TextView) view.findViewById(R.id.video_duration); mDurationView = (TextView) mView.findViewById(R.id.video_duration);
mSeekBar = (SeekBar) view.findViewById(R.id.video_seekbar); mSeekBar = (SeekBar) mView.findViewById(R.id.video_seekbar);
mSeekBar.setOnSeekBarChangeListener(this); mSeekBar.setOnSeekBarChangeListener(this);
if (mIsFullscreen) if (mIsFullscreen)
@ -296,14 +303,14 @@ public class VideoFragment extends ViewPagerFragment
@Override @Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) { 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) if (getActivity() == null)
return; return;
final float videoProportion = (float) videoWidth / (float) videoHeight; final float videoProportion = (float) mMediaPlayer.getVideoWidth() / (float) mMediaPlayer.getVideoHeight();
final Display display = getActivity().getWindowManager().getDefaultDisplay(); final Display display = getActivity().getWindowManager().getDefaultDisplay();
int screenWidth; int screenWidth;
int screenHeight; int screenHeight;

View file

@ -13,6 +13,8 @@ public abstract class ViewPagerFragment extends Fragment {
public abstract void systemUiVisibilityChanged(boolean toFullscreen); public abstract void systemUiVisibilityChanged(boolean toFullscreen);
public abstract void confChanged();
public interface FragmentClickListener { public interface FragmentClickListener {
void fragmentClicked(); void fragmentClicked();
} }