[Android] Refactor sign management in PickTimeFragment

Geoffrey Métais git at videolan.org
Mon Feb 2 13:48:43 CET 2015


vlc-ports/android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Feb  2 13:43:05 2015 +0100| [aefe75032f7ff1c4ab989413592a5b61a5e99efe] | committer: Geoffrey Métais

Refactor sign management in PickTimeFragment

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=aefe75032f7ff1c4ab989413592a5b61a5e99efe
---

 .../videolan/vlc/gui/dialogs/AudioDelayDialog.java |   14 +---
 .../videolan/vlc/gui/dialogs/JumpToTimeDialog.java |    5 ++
 .../videolan/vlc/gui/dialogs/PickTimeFragment.java |   77 ++++++++------------
 .../videolan/vlc/gui/dialogs/SubsDelayDialog.java  |   10 +--
 4 files changed, 39 insertions(+), 67 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/dialogs/AudioDelayDialog.java b/vlc-android/src/org/videolan/vlc/gui/dialogs/AudioDelayDialog.java
index f6a4b14..38965f0 100644
--- a/vlc-android/src/org/videolan/vlc/gui/dialogs/AudioDelayDialog.java
+++ b/vlc-android/src/org/videolan/vlc/gui/dialogs/AudioDelayDialog.java
@@ -23,7 +23,6 @@
 package org.videolan.vlc.gui.dialogs;
 
 import android.os.Bundle;
-import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
@@ -53,21 +52,14 @@ public class AudioDelayDialog extends PickTimeFragment {
 
         mActionButton.setText(android.R.string.cancel);
         long delay = mLibVLC.getAudioDelay();
-        initTime(delay);
-
+        if (delay != 0f)
+            initTime(delay);
         return view;
     }
 
     @Override
     protected void executeAction(){
-        long minutes = Long.parseLong(mMinutes.getText().toString());
-        long seconds = Long.parseLong(mSeconds.getText().toString());
-        long millis = Long.parseLong(mMillis.getText().toString());
-        long delay = minutes * MINUTES_IN_MICROS + seconds * SECONDS_IN_MICROS + millis * MILLIS_IN_MICROS;
-        if (mSign.getText().equals("-"))
-            delay = -delay;
-        mLibVLC.setAudioDelay(delay);
-        Log.d(TAG, "setting audio delay to: " + delay);
+        mLibVLC.setAudioDelay(getTime());
     }
 
     @Override
diff --git a/vlc-android/src/org/videolan/vlc/gui/dialogs/JumpToTimeDialog.java b/vlc-android/src/org/videolan/vlc/gui/dialogs/JumpToTimeDialog.java
index d3c92b3..d1ccb95 100644
--- a/vlc-android/src/org/videolan/vlc/gui/dialogs/JumpToTimeDialog.java
+++ b/vlc-android/src/org/videolan/vlc/gui/dialogs/JumpToTimeDialog.java
@@ -32,6 +32,11 @@ import org.videolan.vlc.R;
 
 public class JumpToTimeDialog extends PickTimeFragment {
 
+    public JumpToTimeDialog(){
+        super();
+        mLiveAction = false;
+    }
+
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
                              Bundle savedInstanceState) {
diff --git a/vlc-android/src/org/videolan/vlc/gui/dialogs/PickTimeFragment.java b/vlc-android/src/org/videolan/vlc/gui/dialogs/PickTimeFragment.java
index 4b34c70..8f11184 100644
--- a/vlc-android/src/org/videolan/vlc/gui/dialogs/PickTimeFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/dialogs/PickTimeFragment.java
@@ -44,6 +44,7 @@ public abstract class PickTimeFragment extends DialogFragment implements DialogI
     public static final int ACTION_SPU_DELAY = 1;
     public static final int ACTION_AUDIO_DELAY = 2;
     protected int mTextColor;
+    protected boolean mLiveAction = true;
 
     protected static long MILLIS_IN_MICROS = 1000;
     protected static long SECONDS_IN_MICROS = 1000 * MILLIS_IN_MICROS;
@@ -109,10 +110,6 @@ public abstract class PickTimeFragment extends DialogFragment implements DialogI
             case KeyEvent.KEYCODE_DPAD_DOWN:
                 updateViews(keyCode);
                 return true;
-//            case KeyEvent.KEYCODE_DPAD_CENTER:
-//            case KeyEvent.KEYCODE_ENTER:
-//            case KeyEvent.KEYCODE_NUMPAD_ENTER:
-//                executeAction();
         }
         return false;
     }
@@ -162,7 +159,7 @@ public abstract class PickTimeFragment extends DialogFragment implements DialogI
     }
 
     private void updateViews(int keyCode){
-        int delta = keyCode == KeyEvent.KEYCODE_DPAD_UP ? 1 : -1;
+        long delta = keyCode == KeyEvent.KEYCODE_DPAD_UP ? 1 : -1;
         int id = 0;
         if (mMillis.hasFocus()) {
             id = mMillis.getId();
@@ -176,53 +173,24 @@ public abstract class PickTimeFragment extends DialogFragment implements DialogI
         updateValue(delta, id);
     }
 
-    private void updateValue(int delta, int resId) {
-        int max = 59;
-        String format = "%02d";
-        long length = mLibVLC.getLength() * 1000; //work in µSeconds
-        TextView edit = null;
-        switch(resId){
+    private void updateValue(long delta, int resId) {
+        long slide = 0l;
+        switch(resId) {
             case R.id.jump_hours:
-                edit = mHours;
-                if (length < 59 * HOURS_IN_MICROS)
-                    max = (int) (length/ HOURS_IN_MICROS);
+                slide = delta * HOURS_IN_MICROS;
                 break;
             case R.id.jump_minutes:
-                edit = mMinutes;
-                if (this instanceof JumpToTimeDialog) {
-                    if (mHours != null)
-                        length -= Long.decode(mHours.getText().toString()).longValue() * HOURS_IN_MICROS;
-                    if (length < 59 * MINUTES_IN_MICROS)
-                        max = (int) (length / MINUTES_IN_MICROS);
-                }
+                slide = delta * MINUTES_IN_MICROS;
                 break;
             case R.id.jump_seconds:
-                edit = mSeconds;
-                if (this instanceof JumpToTimeDialog) {
-                    if (mHours != null)
-                        length -= Long.decode(mHours.getText().toString()).longValue() * HOURS_IN_MICROS;
-                    length -= Long.decode(mMinutes.getText().toString()).longValue() * MINUTES_IN_MICROS;
-                    if (length < 59000)
-                        max = (int) (length / SECONDS_IN_MICROS);
-                }
+                slide = delta * SECONDS_IN_MICROS;
                 break;
             case R.id.jump_millis:
-                edit = mMillis;
-                max = 999;
-                format = "%03d";
-                break;
-        }
-        if (edit != null) {
-            int value = Integer.parseInt(edit.getText().toString()) + delta;
-            if (value < 0)
-                value = max;
-            else if (value > max)
-                value = 0;
-            edit.setText(String.format(format, value));
-
-            if (this instanceof AudioDelayDialog || this instanceof SubsDelayDialog)
-                executeAction();
+                slide = delta * MILLIS_IN_MICROS;
         }
+        initTime(getTime() + slide);
+        if (mLiveAction)
+            executeAction();
     }
 
     @Override
@@ -232,11 +200,18 @@ public abstract class PickTimeFragment extends DialogFragment implements DialogI
     }
 
     protected void initTime(long delay) {
+//        long delayToDisplay;
         if (delay < 0l){
-            delay = -delay;
-            mSign.setText("-");
-        } else
+            if ( mSign.getVisibility() == View.VISIBLE) {
+                delay = -delay;
+                mSign.setText("-");
+            } else {
+                delay = 0l;
+            }
+        } else {
             mSign.setText("+");
+            delay = delay;
+        }
         long minutes = 0;
         long seconds = 0;
         long millis = 0;
@@ -250,6 +225,14 @@ public abstract class PickTimeFragment extends DialogFragment implements DialogI
         mMillis.setText(String.format("%03d", millis));
     }
 
+    protected long getTime(){
+        long sign = mSign.getText().equals("-") ? -1 : 1;
+        long minutes = Long.parseLong(mMinutes.getText().toString());
+        long seconds = Long.parseLong(mSeconds.getText().toString());
+        long millis = Long.parseLong(mMillis.getText().toString());
+        return sign * (minutes * MINUTES_IN_MICROS + seconds * SECONDS_IN_MICROS + millis * MILLIS_IN_MICROS);
+    }
+
     abstract protected int getTitle();
     abstract protected void executeAction();
     abstract protected void buttonAction();
diff --git a/vlc-android/src/org/videolan/vlc/gui/dialogs/SubsDelayDialog.java b/vlc-android/src/org/videolan/vlc/gui/dialogs/SubsDelayDialog.java
index 720552f..129c1f4 100644
--- a/vlc-android/src/org/videolan/vlc/gui/dialogs/SubsDelayDialog.java
+++ b/vlc-android/src/org/videolan/vlc/gui/dialogs/SubsDelayDialog.java
@@ -23,7 +23,6 @@
 package org.videolan.vlc.gui.dialogs;
 
 import android.os.Bundle;
-import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
@@ -64,14 +63,7 @@ public class SubsDelayDialog extends PickTimeFragment {
 
     @Override
     protected void executeAction(){
-        long minutes = Long.parseLong(mMinutes.getText().toString());
-        long seconds = Long.parseLong(mSeconds.getText().toString());
-        long millis = Long.parseLong(mMillis.getText().toString());
-        long delay = minutes * MINUTES_IN_MICROS + seconds * SECONDS_IN_MICROS + millis * MILLIS_IN_MICROS;
-        if (mSign.getText().equals("-"))
-            delay = -delay;
-        mLibVLC.setSpuDelay(delay);
-        Log.d(TAG, "setting spu delay to: " + delay);
+        mLibVLC.setSpuDelay(getTime());
     }
 
     @Override



More information about the Android mailing list