diff --git a/app/src/main/java/com/simplemobiletools/gallery/ViewPagerFragment.java b/app/src/main/java/com/simplemobiletools/gallery/ViewPagerFragment.java index 62baae60c..427d13420 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/ViewPagerFragment.java +++ b/app/src/main/java/com/simplemobiletools/gallery/ViewPagerFragment.java @@ -1,18 +1,33 @@ package com.simplemobiletools.gallery; +import android.media.AudioManager; +import android.media.MediaPlayer; +import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; +import android.util.Log; import android.view.LayoutInflater; +import android.view.SurfaceHolder; +import android.view.SurfaceView; import android.view.View; import android.view.ViewGroup; +import android.widget.ImageView; import com.davemorrissey.labs.subscaleview.ImageSource; import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView; import com.simplemobiletools.gallery.activities.ViewPagerActivity; -public class ViewPagerFragment extends Fragment implements View.OnClickListener { +import java.io.IOException; + +public class ViewPagerFragment extends Fragment + implements View.OnClickListener, SurfaceHolder.Callback, MediaPlayer.OnCompletionListener, MediaPlayer.OnVideoSizeChangedListener { + private static final String TAG = ViewPagerFragment.class.getSimpleName(); private static final String MEDIUM = "medium"; private Media medium; + private static SurfaceHolder surfaceHolder; + private static ImageView playOutline; + private static boolean isPlaying; + private static MediaPlayer mediaPlayer; public void setMedium(Media medium) { this.medium = medium; @@ -20,23 +35,49 @@ public class ViewPagerFragment extends Fragment implements View.OnClickListener @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - final View view = inflater.inflate(R.layout.pager_item, container, false); + View view; if (medium == null && savedInstanceState != null) { medium = (Media) savedInstanceState.getSerializable(MEDIUM); } if (medium != null) { - final SubsamplingScaleImageView imageView = (SubsamplingScaleImageView) view.findViewById(R.id.photo); - imageView.setOrientation(SubsamplingScaleImageView.ORIENTATION_USE_EXIF); - imageView.setImage(ImageSource.uri(medium.getPath())); - imageView.setMaxScale(5f); - imageView.setOnClickListener(this); + if (medium.getIsVideo()) { + view = inflater.inflate(R.layout.pager_video_item, container, false); + setupPlayer(view); + } else { + view = inflater.inflate(R.layout.pager_photo_item, container, false); + final SubsamplingScaleImageView imageView = (SubsamplingScaleImageView) view.findViewById(R.id.photo_view); + imageView.setOrientation(SubsamplingScaleImageView.ORIENTATION_USE_EXIF); + imageView.setImage(ImageSource.uri(medium.getPath())); + imageView.setMaxScale(5f); + } + } else { + view = inflater.inflate(R.layout.pager_photo_item, container, false); } + view.setOnClickListener(this); + return view; } + private void setupPlayer(View view) { + if (getActivity() == null) + return; + + playOutline = (ImageView) view.findViewById(R.id.video_play_outline); + playOutline.setOnClickListener(this); + + final SurfaceView surfaceView = (SurfaceView) view.findViewById(R.id.video_surface); + surfaceView.setOnClickListener(this); + surfaceHolder = surfaceView.getHolder(); + surfaceHolder.addCallback(this); + } + + public void itemDragged() { + pauseVideo(); + } + @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); @@ -45,6 +86,95 @@ public class ViewPagerFragment extends Fragment implements View.OnClickListener @Override public void onClick(View v) { - ((ViewPagerActivity) getActivity()).photoClicked(); + switch (v.getId()) { + case R.id.video_play_outline: + togglePlayPause(); + break; + default: + ((ViewPagerActivity) getActivity()).photoClicked(); + } + } + + private void pauseVideo() { + if (isPlaying) { + togglePlayPause(); + } + } + + private void togglePlayPause() { + if (getActivity() == null) + return; + + if (mediaPlayer == null) + initMediaPlayer(); + + isPlaying = !isPlaying; + if (isPlaying) { + mediaPlayer.start(); + playOutline.setImageDrawable(null); + } else { + mediaPlayer.pause(); + playOutline.setImageDrawable(getResources().getDrawable(R.mipmap.play_outline_big)); + } + } + + @Override + public void surfaceCreated(SurfaceHolder holder) { + initMediaPlayer(); + } + + private void initMediaPlayer() { + try { + mediaPlayer = new MediaPlayer(); + mediaPlayer.setDataSource(getContext(), Uri.parse(medium.getPath())); + mediaPlayer.setDisplay(surfaceHolder); + mediaPlayer.setOnCompletionListener(this); + mediaPlayer.setOnVideoSizeChangedListener(this); + mediaPlayer.prepare(); + mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); + addPreviewImage(); + } catch (IOException e) { + Log.e(TAG, "init media player " + e.getMessage()); + } + } + + private void addPreviewImage() { + mediaPlayer.start(); + mediaPlayer.pause(); + } + + @Override + public void onPause() { + super.onPause(); + releaseMediaPlayer(); + } + + private void releaseMediaPlayer() { + pauseVideo(); + + if (mediaPlayer != null) { + mediaPlayer.release(); + mediaPlayer = null; + } + } + + @Override + public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { + + } + + @Override + public void surfaceDestroyed(SurfaceHolder holder) { + + } + + @Override + public void onCompletion(MediaPlayer mp) { + pauseVideo(); + } + + @Override + public void onVideoSizeChanged(MediaPlayer mp, int width, int height) { + surfaceHolder.setFixedSize(width, height); } } diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java index dc9926837..16942a084 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java @@ -146,7 +146,7 @@ public class MediaActivity extends AppCompatActivity if (curPath.matches(pattern) && !toBeDeleted.contains(curPath)) { final File file = new File(curPath); if (file.exists()) { - myMedia.add(new Media(cursor.getString(pathIndex), (i == 1))); + myMedia.add(new Media(curPath, (i == 1))); } else { invalidFiles.add(file.getAbsolutePath()); } 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 9e2713833..f0d1b3180 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java @@ -248,7 +248,7 @@ public class ViewPagerActivity extends AppCompatActivity } private List getMedia() { - final List media = new ArrayList<>(); + final List myMedia = new ArrayList<>(); int j = 0; for (int i = 0; i < 2; i++) { Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; @@ -267,7 +267,7 @@ public class ViewPagerActivity extends AppCompatActivity do { final String curPath = cursor.getString(pathIndex); if (curPath.matches(pattern) && !curPath.equals(toBeDeleted) && !curPath.equals(beingDeleted)) { - media.add(new Media(curPath, j == 1)); + myMedia.add(new Media(curPath, i == 1)); if (curPath.equals(path)) { pos = j; @@ -279,7 +279,7 @@ public class ViewPagerActivity extends AppCompatActivity cursor.close(); } } - return media; + return myMedia; } public void photoClicked() { @@ -355,7 +355,10 @@ public class ViewPagerActivity extends AppCompatActivity @Override public void onPageScrollStateChanged(int state) { - + if (state == ViewPager.SCROLL_STATE_DRAGGING) { + final MyPagerAdapter adapter = (MyPagerAdapter) pager.getAdapter(); + adapter.itemDragged(); + } } @Override 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 6ede1c19c..15be5733e 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/adapters/MyPagerAdapter.java +++ b/app/src/main/java/com/simplemobiletools/gallery/adapters/MyPagerAdapter.java @@ -11,6 +11,7 @@ import java.util.List; public class MyPagerAdapter extends FragmentStatePagerAdapter { private List media; + private ViewPagerFragment fragment; public MyPagerAdapter(FragmentManager fm, List media) { super(fm); @@ -24,11 +25,15 @@ public class MyPagerAdapter extends FragmentStatePagerAdapter { @Override public Fragment getItem(int position) { - final ViewPagerFragment fragment = new ViewPagerFragment(); + fragment = new ViewPagerFragment(); fragment.setMedium(media.get(position)); return fragment; } + public void itemDragged() { + fragment.itemDragged(); + } + public void updateItems(List newPaths) { media.clear(); media.addAll(newPaths); diff --git a/app/src/main/res/layout/pager_item.xml b/app/src/main/res/layout/pager_photo_item.xml similarity index 88% rename from app/src/main/res/layout/pager_item.xml rename to app/src/main/res/layout/pager_photo_item.xml index f88b4cf14..d46d2edb1 100644 --- a/app/src/main/res/layout/pager_item.xml +++ b/app/src/main/res/layout/pager_photo_item.xml @@ -1,6 +1,6 @@ diff --git a/app/src/main/res/layout/pager_video_item.xml b/app/src/main/res/layout/pager_video_item.xml new file mode 100644 index 000000000..e9013909e --- /dev/null +++ b/app/src/main/res/layout/pager_video_item.xml @@ -0,0 +1,22 @@ + + + + + + + + diff --git a/app/src/main/res/mipmap-hdpi/play_outline_big.png b/app/src/main/res/mipmap-hdpi/play_outline_big.png new file mode 100644 index 000000000..d8625dd59 Binary files /dev/null and b/app/src/main/res/mipmap-hdpi/play_outline_big.png differ diff --git a/app/src/main/res/mipmap-mdpi/play_outline_big.png b/app/src/main/res/mipmap-mdpi/play_outline_big.png new file mode 100644 index 000000000..d8625dd59 Binary files /dev/null and b/app/src/main/res/mipmap-mdpi/play_outline_big.png differ diff --git a/app/src/main/res/mipmap-xhdpi/play_outline_big.png b/app/src/main/res/mipmap-xhdpi/play_outline_big.png new file mode 100644 index 000000000..c101a4139 Binary files /dev/null and b/app/src/main/res/mipmap-xhdpi/play_outline_big.png differ diff --git a/app/src/main/res/mipmap-xxhdpi/play_outline_big.png b/app/src/main/res/mipmap-xxhdpi/play_outline_big.png new file mode 100644 index 000000000..cc828f46a Binary files /dev/null and b/app/src/main/res/mipmap-xxhdpi/play_outline_big.png differ diff --git a/app/src/main/res/mipmap-xxxhdpi/play_outline_big.png b/app/src/main/res/mipmap-xxxhdpi/play_outline_big.png new file mode 100644 index 000000000..a655fff79 Binary files /dev/null and b/app/src/main/res/mipmap-xxxhdpi/play_outline_big.png differ diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index 986f5b7ac..f08683311 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -3,5 +3,6 @@ 150dp 100dp 40dp + 160dp 8dp