From 19234795919a3e95aa96e9b50d2ba914cf1363e2 Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 9 Jun 2016 00:13:30 +0200 Subject: [PATCH] add seekbar functionality + misc changes --- .../com/simplemobiletools/gallery/Utils.java | 10 ++ .../gallery/ViewPagerFragment.java | 149 ++++++++++++++++-- .../gallery/activities/ViewPagerActivity.java | 31 ++-- .../gallery/adapters/MyPagerAdapter.java | 6 +- app/src/main/res/layout/pager_video_item.xml | 40 ++++- app/src/main/res/values/dimens.xml | 1 + 6 files changed, 204 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/com/simplemobiletools/gallery/Utils.java b/app/src/main/java/com/simplemobiletools/gallery/Utils.java index edc1037ab..9a87b8572 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/Utils.java +++ b/app/src/main/java/com/simplemobiletools/gallery/Utils.java @@ -1,6 +1,7 @@ package com.simplemobiletools.gallery; import android.content.Context; +import android.content.res.Resources; import android.widget.Toast; public class Utils { @@ -11,4 +12,13 @@ public class Utils { public static void showToast(Context context, int resId) { Toast.makeText(context, context.getResources().getString(resId), Toast.LENGTH_SHORT).show(); } + + public static int getNavBarHeight(Resources res) { + int id = res.getIdentifier("navigation_bar_height", "dimen", "android"); + if (id > 0) { + return res.getDimensionPixelSize(id); + } + + return 0; + } } diff --git a/app/src/main/java/com/simplemobiletools/gallery/ViewPagerFragment.java b/app/src/main/java/com/simplemobiletools/gallery/ViewPagerFragment.java index 427d13420..0f8d7c155 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/ViewPagerFragment.java +++ b/app/src/main/java/com/simplemobiletools/gallery/ViewPagerFragment.java @@ -1,9 +1,12 @@ package com.simplemobiletools.gallery; +import android.content.res.Configuration; +import android.content.res.Resources; import android.media.AudioManager; import android.media.MediaPlayer; import android.net.Uri; import android.os.Bundle; +import android.os.Handler; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; @@ -12,22 +15,33 @@ import android.view.SurfaceView; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; +import android.widget.RelativeLayout; +import android.widget.SeekBar; +import android.widget.TextView; import com.davemorrissey.labs.subscaleview.ImageSource; import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView; import com.simplemobiletools.gallery.activities.ViewPagerActivity; import java.io.IOException; +import java.util.Locale; public class ViewPagerFragment extends Fragment - implements View.OnClickListener, SurfaceHolder.Callback, MediaPlayer.OnCompletionListener, MediaPlayer.OnVideoSizeChangedListener { + implements View.OnClickListener, SurfaceHolder.Callback, MediaPlayer.OnCompletionListener, MediaPlayer.OnVideoSizeChangedListener, + SeekBar.OnSeekBarChangeListener { 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; + private SurfaceHolder surfaceHolder; + + private ImageView playOutline; + private TextView currTimeView; + private TextView durationView; + private Handler timerHandler; + private SeekBar seekBar; + private Media medium; + private boolean isPlaying; + private boolean isDragged; public void setMedium(Media medium) { this.medium = medium; @@ -72,12 +86,62 @@ public class ViewPagerFragment extends Fragment surfaceView.setOnClickListener(this); surfaceHolder = surfaceView.getHolder(); surfaceHolder.addCallback(this); + + initTimeHolder(view); } public void itemDragged() { pauseVideo(); } + public void fragmentHidden() { + cleanup(); + } + + private void initTimeHolder(View view) { + RelativeLayout timeHolder = (RelativeLayout) view.findViewById(R.id.video_time_holder); + final Resources res = getResources(); + final int height = Utils.getNavBarHeight(res); + final int left = timeHolder.getPaddingLeft(); + final int top = timeHolder.getPaddingTop(); + final int right = timeHolder.getPaddingRight(); + final int bottom = timeHolder.getPaddingBottom(); + + if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { + timeHolder.setPadding(left, top, right, bottom + height); + } else { + timeHolder.setPadding(left, top, right + height, bottom); + } + + currTimeView = (TextView) view.findViewById(R.id.video_curr_time); + durationView = (TextView) view.findViewById(R.id.video_duration); + seekBar = (SeekBar) view.findViewById(R.id.video_seekbar); + seekBar.setOnSeekBarChangeListener(this); + } + + private void setupTimeHolder() { + final int duration = mediaPlayer.getDuration() / 1000; + seekBar.setMax(duration); + durationView.setText(getTimeString(duration)); + timerHandler = new Handler(); + setupTimer(); + } + + private void setupTimer() { + getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + if (mediaPlayer != null && !isDragged && isPlaying) { + int currPos = mediaPlayer.getCurrentPosition() / 1000; + seekBar.setProgress(currPos); + currTimeView.setText(getTimeString(currPos)); + } + + timerHandler.postDelayed(this, 1000); + } + }); + } + @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); @@ -105,15 +169,18 @@ public class ViewPagerFragment extends Fragment if (getActivity() == null) return; - if (mediaPlayer == null) - initMediaPlayer(); - isPlaying = !isPlaying; if (isPlaying) { - mediaPlayer.start(); + if (mediaPlayer != null) { + mediaPlayer.start(); + } + playOutline.setImageDrawable(null); } else { - mediaPlayer.pause(); + if (mediaPlayer != null) { + mediaPlayer.pause(); + } + playOutline.setImageDrawable(getResources().getDrawable(R.mipmap.play_outline_big)); } } @@ -133,6 +200,10 @@ public class ViewPagerFragment extends Fragment mediaPlayer.prepare(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); addPreviewImage(); + setupTimeHolder(); + + seekBar.setProgress(0); + currTimeView.setText(getTimeString(0)); } catch (IOException e) { Log.e(TAG, "init media player " + e.getMessage()); } @@ -146,16 +217,25 @@ public class ViewPagerFragment extends Fragment @Override public void onPause() { super.onPause(); - releaseMediaPlayer(); + cleanup(); } - private void releaseMediaPlayer() { + private void cleanup() { pauseVideo(); + if (currTimeView != null) + currTimeView.setText(getTimeString(0)); + if (mediaPlayer != null) { mediaPlayer.release(); mediaPlayer = null; } + + if (seekBar != null) + seekBar.setProgress(0); + + if (timerHandler != null) + timerHandler.removeCallbacksAndMessages(null); } @Override @@ -177,4 +257,49 @@ public class ViewPagerFragment extends Fragment public void onVideoSizeChanged(MediaPlayer mp, int width, int height) { surfaceHolder.setFixedSize(width, height); } + + private String getTimeString(int duration) { + final StringBuilder sb = new StringBuilder(8); + final int hours = duration / (60 * 60); + final int minutes = (duration % (60 * 60)) / 60; + final int seconds = ((duration % (60 * 60)) % 60); + + if (mediaPlayer != null && mediaPlayer.getDuration() > 3600000) { + sb.append(String.format(Locale.getDefault(), "%02d", hours)).append(":"); + } + + sb.append(String.format(Locale.getDefault(), "%02d", minutes)); + sb.append(":").append(String.format(Locale.getDefault(), "%02d", seconds)); + + return sb.toString(); + } + + @Override + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { + if (mediaPlayer != null && fromUser) { + mediaPlayer.seekTo(progress * 1000); + seekBar.setProgress(progress); + currTimeView.setText(getTimeString(progress)); + } + } + + @Override + public void onStartTrackingTouch(SeekBar seekBar) { + if (mediaPlayer == null) + initMediaPlayer(); + + mediaPlayer.pause(); + isDragged = true; + } + + @Override + public void onStopTrackingTouch(SeekBar seekBar) { + if (!isPlaying) { + togglePlayPause(); + } else { + mediaPlayer.start(); + } + + isDragged = false; + } } 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 f0d1b3180..116794b64 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java @@ -6,7 +6,6 @@ import android.content.res.Resources; import android.database.Cursor; import android.media.MediaScannerConnection; import android.net.Uri; -import android.os.Build; import android.os.Bundle; import android.provider.MediaStore; import android.support.v4.view.ViewPager; @@ -322,24 +321,19 @@ public class ViewPagerActivity extends AppCompatActivity } private void addUndoMargin() { - if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - final Resources resources = getResources(); - int id = resources.getIdentifier("navigation_bar_height", "dimen", "android"); - if (id > 0) { - final RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) undoBtn.getLayoutParams(); - final int navbarHeight = resources.getDimensionPixelSize(id); - int rightMargin = params.rightMargin; - int bottomMargin = params.bottomMargin; + final Resources res = getResources(); + final int height = Utils.getNavBarHeight(res); + final RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) undoBtn.getLayoutParams(); + int rightMargin = params.rightMargin; + int bottomMargin = params.bottomMargin; - if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { - bottomMargin = navbarHeight; - } else { - rightMargin = navbarHeight; - } - - params.setMargins(params.leftMargin, params.topMargin, rightMargin, bottomMargin); - } + if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { + bottomMargin = height; + } else { + rightMargin = height; } + + params.setMargins(params.leftMargin, params.topMargin, rightMargin, bottomMargin); } @Override @@ -374,8 +368,9 @@ public class ViewPagerActivity extends AppCompatActivity runOnUiThread(new Runnable() { @Override public void run() { - if (media.size() <= 1) + if (media.size() <= 1) { reloadViewPager(); + } } }); } 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 15be5733e..e073e2d25 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/adapters/MyPagerAdapter.java +++ b/app/src/main/java/com/simplemobiletools/gallery/adapters/MyPagerAdapter.java @@ -25,13 +25,17 @@ public class MyPagerAdapter extends FragmentStatePagerAdapter { @Override public Fragment getItem(int position) { + if (fragment != null) { + fragment.fragmentHidden(); + } fragment = new ViewPagerFragment(); fragment.setMedium(media.get(position)); return fragment; } public void itemDragged() { - fragment.itemDragged(); + if (fragment != null) + fragment.itemDragged(); } public void updateItems(List newPaths) { diff --git a/app/src/main/res/layout/pager_video_item.xml b/app/src/main/res/layout/pager_video_item.xml index e9013909e..3f7fba30e 100644 --- a/app/src/main/res/layout/pager_video_item.xml +++ b/app/src/main/res/layout/pager_video_item.xml @@ -1,8 +1,8 @@ + android:layout_width="match_parent" + android:layout_height="match_parent"> + + + + + + + + + + diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index f08683311..d52c4690e 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -5,4 +5,5 @@ 40dp 160dp 8dp + 24dp