[Android] Code cleaning in advanced options dialog

Geoffrey Métais git at videolan.org
Tue Sep 19 17:51:42 CEST 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Sep 19 17:51:23 2017 +0200| [39f05cd888b659a947564fe7cb308836bcd37835] | committer: Geoffrey Métais

Code cleaning in advanced options dialog

> https://code.videolan.org/videolan/vlc-android/commit/39f05cd888b659a947564fe7cb308836bcd37835
---

 .../res/layout/fragment_advanced_options.xml       |  4 +-
 .../videolan/vlc/gui/dialogs/AdvOptionsDialog.java | 74 ++++++++--------------
 2 files changed, 27 insertions(+), 51 deletions(-)

diff --git a/vlc-android/res/layout/fragment_advanced_options.xml b/vlc-android/res/layout/fragment_advanced_options.xml
index ed1b37dd9..8ebbd0fd1 100644
--- a/vlc-android/res/layout/fragment_advanced_options.xml
+++ b/vlc-android/res/layout/fragment_advanced_options.xml
@@ -2,5 +2,5 @@
 <org.videolan.vlc.gui.view.AutoFitRecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
-    android:padding="@dimen/adv_options_margin"/>
-
+    android:padding="@dimen/adv_options_margin"
+    android:background="?attr/rounded_bg"/>
diff --git a/vlc-android/src/org/videolan/vlc/gui/dialogs/AdvOptionsDialog.java b/vlc-android/src/org/videolan/vlc/gui/dialogs/AdvOptionsDialog.java
index fa6171c73..91a0805bb 100644
--- a/vlc-android/src/org/videolan/vlc/gui/dialogs/AdvOptionsDialog.java
+++ b/vlc-android/src/org/videolan/vlc/gui/dialogs/AdvOptionsDialog.java
@@ -135,12 +135,11 @@ public class AdvOptionsDialog extends DialogFragment implements View.OnClickList
     }
 
     @Override
-    public void onAttach(Activity activity) {
-        super.onAttach(activity);
-        if (mMode == MODE_VIDEO) {
-            mPlaybackController = (IPlaybackSettingsController) activity;
-        }
-        mActivity = activity;
+    public void onAttach(Context context) {
+        super.onAttach(context);
+        if (mMode == MODE_VIDEO)
+            mPlaybackController = (IPlaybackSettingsController) context;
+        mActivity = (Activity) context;
     }
 
     @Override
@@ -167,13 +166,11 @@ public class AdvOptionsDialog extends DialogFragment implements View.OnClickList
         mToast.setGravity(Gravity.TOP,0,100);
 
         //Get default color
-        int[] attrs = new int[] { android.R.attr.textColorSecondary };
-        TypedArray a = getActivity().getTheme().obtainStyledAttributes(R.style.Theme_VLC, attrs);
+        final int[] attrs = new int[] { android.R.attr.textColorSecondary };
+        final TypedArray a = getActivity().getTheme().obtainStyledAttributes(R.style.Theme_VLC, attrs);
         mTextColor = a.getColor(0, Color.LTGRAY);
         a.recycle();
 
-        getDialog().getWindow().setBackgroundDrawableResource(UiTools.getResourceFromAttribute(getActivity(), R.attr.rounded_bg));
-
         return mRecyclerView;
     }
 
@@ -183,21 +180,6 @@ public class AdvOptionsDialog extends DialogFragment implements View.OnClickList
         getDialog().setOnKeyListener(this);
     }
 
-    private void setDialogDimensions(int offset) {
-        if (getDialog() == null)
-            return;
-        int dialogWidth = getResources().getDimensionPixelSize(R.dimen.option_width) * SPAN_COUNT + mRecyclerView.getPaddingLeft()+ mRecyclerView.getRight();
-
-        int count = mAdapter.getItemCount()-offset;
-        int rows = offset + count / SPAN_COUNT;
-        if (count % SPAN_COUNT != 0)
-            rows++;
-
-        int dialogHeight = getResources().getDimensionPixelSize(R.dimen.option_height) * rows + mRecyclerView.getPaddingBottom()+ mRecyclerView.getPaddingTop();
-
-        getDialog().getWindow().setLayout(dialogWidth, dialogHeight);
-    }
-
     private void showFragment(int id) {
         DialogFragment newFragment;
         String tag;
@@ -252,16 +234,14 @@ public class AdvOptionsDialog extends DialogFragment implements View.OnClickList
     }
 
     public static void setSleep(Calendar time) {
-        AlarmManager alarmMgr = (AlarmManager) VLCApplication.getAppContext().getSystemService(Context.ALARM_SERVICE);
-        Intent intent = new Intent(VLCApplication.SLEEP_INTENT);
-        PendingIntent sleepPendingIntent = PendingIntent.getBroadcast(VLCApplication.getAppContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
+        final AlarmManager alarmMgr = (AlarmManager) VLCApplication.getAppContext().getSystemService(Context.ALARM_SERVICE);
+        final Intent intent = new Intent(VLCApplication.SLEEP_INTENT);
+        final PendingIntent sleepPendingIntent = PendingIntent.getBroadcast(VLCApplication.getAppContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
 
-        if (time != null) {
+        if (time != null)
             alarmMgr.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), sleepPendingIntent);
-        }
-        else {
+        else
             alarmMgr.cancel(sleepPendingIntent);
-        }
         VLCApplication.sPlayerSleepTime = time;
     }
 
@@ -296,7 +276,7 @@ public class AdvOptionsDialog extends DialogFragment implements View.OnClickList
     }
 
     private void initSpuDelay() {
-        long spudelay = mService.getSpuDelay() / 1000L;
+        final long spudelay = mService.getSpuDelay() / 1000L;
         if (spudelay == 0L) {
             mSpuDelay.setText(null);
             mSpuDelay.setCompoundDrawablesWithIntrinsicBounds(0,
@@ -311,7 +291,7 @@ public class AdvOptionsDialog extends DialogFragment implements View.OnClickList
     }
 
     private void initAudioDelay() {
-        long audiodelay = mService.getAudioDelay() / 1000L;
+        final long audiodelay = mService.getAudioDelay() / 1000L;
         if (audiodelay == 0L) {
             mAudioDelay.setText(null);
             mAudioDelay.setCompoundDrawablesWithIntrinsicBounds(0,
@@ -337,7 +317,7 @@ public class AdvOptionsDialog extends DialogFragment implements View.OnClickList
                 break;
             case PlaybackService.REPEAT_ONE:
                 mRepeat.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_repeat_one, 0, 0);
-                    break;
+                break;
         }
     }
 
@@ -447,7 +427,7 @@ public class AdvOptionsDialog extends DialogFragment implements View.OnClickList
                 }
                 break;
             case ID_PLAYBACK_SPEED:
-                    showFragment(ID_PLAYBACK_SPEED);
+                showFragment(ID_PLAYBACK_SPEED);
                 break;
             case ID_CHAPTER_TITLE:
                 showFragment(ID_CHAPTER_TITLE);
@@ -523,7 +503,6 @@ public class AdvOptionsDialog extends DialogFragment implements View.OnClickList
     public void onConnected(PlaybackService service) {
         mAdapter.clear();
         mService = service;
-        int large_items = 0;
         boolean tvUi = VLCApplication.showTvUi();
 
         mAdapter.addOption(new Option(ID_SLEEP, R.attr.ic_sleep_normal_style, getString(R.string.sleep_title)));
@@ -544,14 +523,11 @@ public class AdvOptionsDialog extends DialogFragment implements View.OnClickList
 
             final MediaPlayer.Chapter[] chapters = mService.getChapters(-1);
             final int chaptersCount = chapters != null ? chapters.length : 0;
-            if (chaptersCount > 1) {
+            if (chaptersCount > 1)
                 mAdapter.addOption(new Option(ID_CHAPTER_TITLE, R.attr.ic_chapter_normal_style, getString(R.string.go_to_chapter)));
-                large_items++;
-            }
         } else {
             mAdapter.addOption(new Option(ID_SAVE_PLAYLIST, R.attr.ic_save, getString(R.string.playlist_save)));
         }
-        setDialogDimensions(large_items);
     }
 
     @Override
@@ -600,10 +576,10 @@ public class AdvOptionsDialog extends DialogFragment implements View.OnClickList
 
     private class AdvOptionsAdapter extends RecyclerView.Adapter<AdvOptionsAdapter.ViewHolder> {
 
-        private ArrayList<Option> mList = new ArrayList<>();
+        private final ArrayList<Option> mList = new ArrayList<>();
         private int mSelection = 0;
 
-        public AdvOptionsAdapter() {
+        AdvOptionsAdapter() {
             SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(AdvOptionsDialog.this.getContext());
             if (!(prefs.getBoolean("enable_volume_gesture",false) || prefs.getBoolean("enable_volume_gesture",false)))
                 mSelection = 0;
@@ -620,12 +596,12 @@ public class AdvOptionsDialog extends DialogFragment implements View.OnClickList
 
         @Override
         public void onBindViewHolder(ViewHolder holder, int position) {
-            Option option = mList.get(position);
-            TextView tv = (TextView) holder.itemView;
+            final Option option = mList.get(position);
+            final TextView tv = (TextView) holder.itemView;
             if (mSelection == position)
                 tv.requestFocus();
             tv.setId(option.id);
-            int icon = UiTools.getResourceFromAttribute(mActivity, option.icon);
+            final int icon = UiTools.getResourceFromAttribute(mActivity, option.icon);
             if (option.id == ID_CHAPTER_TITLE)
                 tv.setCompoundDrawablesWithIntrinsicBounds(icon, 0, 0, 0);
             else
@@ -647,12 +623,12 @@ public class AdvOptionsDialog extends DialogFragment implements View.OnClickList
             return mList.get(position).id;
         }
 
-        public void addOption(Option opt) {
+        void addOption(Option opt) {
             mList.add(opt);
             notifyItemInserted(mList.size()-1);
         }
 
-        public String getSelectedAdvOptionHelp () {
+        String getSelectedAdvOptionHelp () {
             return mList.get(getSelection()).text;
         }
 
@@ -671,7 +647,7 @@ public class AdvOptionsDialog extends DialogFragment implements View.OnClickList
             mList.remove(opt);
         }
 
-        public class ViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener {
+        class ViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener {
 
             public ViewHolder(View itemView) {
                 super(itemView);



More information about the Android mailing list