add seekbar functionality + misc changes

This commit is contained in:
tibbi 2016-06-09 00:13:30 +02:00
parent 309e52ef3a
commit 1923479591
6 changed files with 204 additions and 33 deletions

View file

@ -1,6 +1,7 @@
package com.simplemobiletools.gallery; package com.simplemobiletools.gallery;
import android.content.Context; import android.content.Context;
import android.content.res.Resources;
import android.widget.Toast; import android.widget.Toast;
public class Utils { public class Utils {
@ -11,4 +12,13 @@ public class Utils {
public static void showToast(Context context, int resId) { public static void showToast(Context context, int resId) {
Toast.makeText(context, context.getResources().getString(resId), Toast.LENGTH_SHORT).show(); 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;
}
} }

View file

@ -1,9 +1,12 @@
package com.simplemobiletools.gallery; package com.simplemobiletools.gallery;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.media.AudioManager; import android.media.AudioManager;
import android.media.MediaPlayer; import android.media.MediaPlayer;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -12,22 +15,33 @@ import android.view.SurfaceView;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ImageView; 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.ImageSource;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView; import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import com.simplemobiletools.gallery.activities.ViewPagerActivity; import com.simplemobiletools.gallery.activities.ViewPagerActivity;
import java.io.IOException; import java.io.IOException;
import java.util.Locale;
public class ViewPagerFragment extends Fragment 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 TAG = ViewPagerFragment.class.getSimpleName();
private static final String MEDIUM = "medium"; 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 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) { public void setMedium(Media medium) {
this.medium = medium; this.medium = medium;
@ -72,12 +86,62 @@ public class ViewPagerFragment extends Fragment
surfaceView.setOnClickListener(this); surfaceView.setOnClickListener(this);
surfaceHolder = surfaceView.getHolder(); surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this); surfaceHolder.addCallback(this);
initTimeHolder(view);
} }
public void itemDragged() { public void itemDragged() {
pauseVideo(); 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 @Override
public void onSaveInstanceState(Bundle outState) { public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
@ -105,15 +169,18 @@ public class ViewPagerFragment extends Fragment
if (getActivity() == null) if (getActivity() == null)
return; return;
if (mediaPlayer == null)
initMediaPlayer();
isPlaying = !isPlaying; isPlaying = !isPlaying;
if (isPlaying) { if (isPlaying) {
if (mediaPlayer != null) {
mediaPlayer.start(); mediaPlayer.start();
}
playOutline.setImageDrawable(null); playOutline.setImageDrawable(null);
} else { } else {
if (mediaPlayer != null) {
mediaPlayer.pause(); mediaPlayer.pause();
}
playOutline.setImageDrawable(getResources().getDrawable(R.mipmap.play_outline_big)); playOutline.setImageDrawable(getResources().getDrawable(R.mipmap.play_outline_big));
} }
} }
@ -133,6 +200,10 @@ public class ViewPagerFragment extends Fragment
mediaPlayer.prepare(); mediaPlayer.prepare();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
addPreviewImage(); addPreviewImage();
setupTimeHolder();
seekBar.setProgress(0);
currTimeView.setText(getTimeString(0));
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, "init media player " + e.getMessage()); Log.e(TAG, "init media player " + e.getMessage());
} }
@ -146,16 +217,25 @@ public class ViewPagerFragment extends Fragment
@Override @Override
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
releaseMediaPlayer(); cleanup();
} }
private void releaseMediaPlayer() { private void cleanup() {
pauseVideo(); pauseVideo();
if (currTimeView != null)
currTimeView.setText(getTimeString(0));
if (mediaPlayer != null) { if (mediaPlayer != null) {
mediaPlayer.release(); mediaPlayer.release();
mediaPlayer = null; mediaPlayer = null;
} }
if (seekBar != null)
seekBar.setProgress(0);
if (timerHandler != null)
timerHandler.removeCallbacksAndMessages(null);
} }
@Override @Override
@ -177,4 +257,49 @@ public class ViewPagerFragment extends Fragment
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) { public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
surfaceHolder.setFixedSize(width, 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;
}
} }

View file

@ -6,7 +6,6 @@ import android.content.res.Resources;
import android.database.Cursor; import android.database.Cursor;
import android.media.MediaScannerConnection; import android.media.MediaScannerConnection;
import android.net.Uri; import android.net.Uri;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager;
@ -322,25 +321,20 @@ public class ViewPagerActivity extends AppCompatActivity
} }
private void addUndoMargin() { private void addUndoMargin() {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { final Resources res = getResources();
final Resources resources = getResources(); final int height = Utils.getNavBarHeight(res);
int id = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (id > 0) {
final RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) undoBtn.getLayoutParams(); final RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) undoBtn.getLayoutParams();
final int navbarHeight = resources.getDimensionPixelSize(id);
int rightMargin = params.rightMargin; int rightMargin = params.rightMargin;
int bottomMargin = params.bottomMargin; int bottomMargin = params.bottomMargin;
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
bottomMargin = navbarHeight; bottomMargin = height;
} else { } else {
rightMargin = navbarHeight; rightMargin = height;
} }
params.setMargins(params.leftMargin, params.topMargin, rightMargin, bottomMargin); params.setMargins(params.leftMargin, params.topMargin, rightMargin, bottomMargin);
} }
}
}
@Override @Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
@ -374,9 +368,10 @@ public class ViewPagerActivity extends AppCompatActivity
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
if (media.size() <= 1) if (media.size() <= 1) {
reloadViewPager(); reloadViewPager();
} }
}
}); });
} }

View file

@ -25,12 +25,16 @@ public class MyPagerAdapter extends FragmentStatePagerAdapter {
@Override @Override
public Fragment getItem(int position) { public Fragment getItem(int position) {
if (fragment != null) {
fragment.fragmentHidden();
}
fragment = new ViewPagerFragment(); fragment = new ViewPagerFragment();
fragment.setMedium(media.get(position)); fragment.setMedium(media.get(position));
return fragment; return fragment;
} }
public void itemDragged() { public void itemDragged() {
if (fragment != null)
fragment.itemDragged(); fragment.itemDragged();
} }

View file

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/video_holder" <RelativeLayout android:id="@+id/video_holder"
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="100dp"> android:layout_height="match_parent">
<SurfaceView <SurfaceView
android:id="@+id/video_surface" android:id="@+id/video_surface"
@ -19,4 +19,40 @@
android:background="@android:color/transparent" android:background="@android:color/transparent"
android:src="@mipmap/play_outline_big"/> android:src="@mipmap/play_outline_big"/>
<RelativeLayout
android:id="@+id/video_time_holder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/actionbar_grey"
android:padding="@dimen/timer_padding">
<TextView
android:id="@+id/video_curr_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="00:00"
android:textColor="@android:color/white"/>
<SeekBar
android:id="@+id/video_seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/video_curr_time"
android:layout_marginLeft="@dimen/activity_margin"
android:layout_marginRight="@dimen/activity_margin"
android:layout_toLeftOf="@+id/video_duration"
android:layout_toRightOf="@+id/video_curr_time"/>
<TextView
android:id="@+id/video_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="01:10"
android:textColor="@android:color/white"/>
</RelativeLayout>
</RelativeLayout> </RelativeLayout>

View file

@ -5,4 +5,5 @@
<dimen name="play_outline_size">40dp</dimen> <dimen name="play_outline_size">40dp</dimen>
<dimen name="play_outline_size_big">160dp</dimen> <dimen name="play_outline_size_big">160dp</dimen>
<dimen name="undo_padding">8dp</dimen> <dimen name="undo_padding">8dp</dimen>
<dimen name="timer_padding">24dp</dimen>
</resources> </resources>