Improve code style to be more consistent

This commit is contained in:
wb9688 2020-03-31 19:20:15 +02:00
parent 819e52cab3
commit fda5405e48
244 changed files with 10116 additions and 7222 deletions

View file

@ -1,64 +1,66 @@
package org.schabi.newpipe.views;
import android.content.Context;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.ProgressBar;
import androidx.annotation.Nullable;
public final class AnimatedProgressBar extends ProgressBar {
@Nullable
private ProgressBarAnimation animation = null;
@Nullable
private ProgressBarAnimation animation = null;
public AnimatedProgressBar(final Context context) {
super(context);
}
public AnimatedProgressBar(Context context) {
super(context);
}
public AnimatedProgressBar(final Context context, final AttributeSet attrs) {
super(context, attrs);
}
public AnimatedProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AnimatedProgressBar(final Context context, final AttributeSet attrs,
final int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public AnimatedProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public synchronized void setProgressAnimated(final int progress) {
cancelAnimation();
animation = new ProgressBarAnimation(this, getProgress(), progress);
startAnimation(animation);
}
public synchronized void setProgressAnimated(int progress) {
cancelAnimation();
animation = new ProgressBarAnimation(this, getProgress(), progress);
startAnimation(animation);
}
private void cancelAnimation() {
if (animation != null) {
animation.cancel();
animation = null;
}
clearAnimation();
}
private void cancelAnimation() {
if (animation != null) {
animation.cancel();
animation = null;
}
clearAnimation();
}
private static class ProgressBarAnimation extends Animation {
private static class ProgressBarAnimation extends Animation {
private final AnimatedProgressBar progressBar;
private final float from;
private final float to;
private final AnimatedProgressBar progressBar;
private final float from;
private final float to;
ProgressBarAnimation(final AnimatedProgressBar progressBar, final float from,
final float to) {
super();
this.progressBar = progressBar;
this.from = from;
this.to = to;
setDuration(500);
setInterpolator(new AccelerateDecelerateInterpolator());
}
ProgressBarAnimation(AnimatedProgressBar progressBar, float from, float to) {
super();
this.progressBar = progressBar;
this.from = from;
this.to = to;
setDuration(500);
setInterpolator(new AccelerateDecelerateInterpolator());
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
float value = from + (to - from) * interpolatedTime;
progressBar.setProgress((int) value);
}
}
@Override
protected void applyTransformation(final float interpolatedTime, final Transformation t) {
super.applyTransformation(interpolatedTime, t);
float value = from + (to - from) * interpolatedTime;
progressBar.setProgress((int) value);
}
}
}

View file

@ -23,13 +23,14 @@ import android.animation.ValueAnimator;
import android.content.Context;
import android.os.Build;
import android.os.Parcelable;
import androidx.annotation.IntDef;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.LinearLayout;
import androidx.annotation.IntDef;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import org.schabi.newpipe.util.AnimationUtils;
import java.lang.annotation.Retention;
@ -46,42 +47,39 @@ import static org.schabi.newpipe.MainActivity.DEBUG;
* A view that can be fully collapsed and expanded.
*/
public class CollapsibleView extends LinearLayout {
public static final int COLLAPSED = 0;
public static final int EXPANDED = 1;
private static final String TAG = CollapsibleView.class.getSimpleName();
private static final int ANIMATION_DURATION = 420;
private final List<StateListener> listeners = new ArrayList<>();
public CollapsibleView(Context context) {
super(context);
}
public CollapsibleView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public CollapsibleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public CollapsibleView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@State
@ViewMode
int currentState = COLLAPSED;
/*//////////////////////////////////////////////////////////////////////////
// Collapse/expand logic
//////////////////////////////////////////////////////////////////////////*/
private static final int ANIMATION_DURATION = 420;
public static final int COLLAPSED = 0, EXPANDED = 1;
@Retention(SOURCE)
@IntDef({COLLAPSED, EXPANDED})
public @interface ViewMode {}
@State @ViewMode int currentState = COLLAPSED;
private boolean readyToChangeState;
private int targetHeight = -1;
private ValueAnimator currentAnimator;
private final List<StateListener> listeners = new ArrayList<>();
public CollapsibleView(final Context context) {
super(context);
}
public CollapsibleView(final Context context, @Nullable final AttributeSet attrs) {
super(context, attrs);
}
public CollapsibleView(final Context context, @Nullable final AttributeSet attrs,
final int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public CollapsibleView(final Context context, final AttributeSet attrs, final int defStyleAttr,
final int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
/**
* This method recalculates the height of this view so it <b>must</b> be called when
@ -92,7 +90,8 @@ public class CollapsibleView extends LinearLayout {
Log.d(TAG, getDebugLogString("ready() called"));
}
measure(MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.AT_MOST), MeasureSpec.UNSPECIFIED);
measure(MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.AT_MOST),
MeasureSpec.UNSPECIFIED);
targetHeight = getMeasuredHeight();
getLayoutParams().height = currentState == COLLAPSED ? 0 : targetHeight;
@ -111,7 +110,9 @@ public class CollapsibleView extends LinearLayout {
Log.d(TAG, getDebugLogString("collapse() called"));
}
if (!readyToChangeState) return;
if (!readyToChangeState) {
return;
}
final int height = getHeight();
if (height == 0) {
@ -119,7 +120,9 @@ public class CollapsibleView extends LinearLayout {
return;
}
if (currentAnimator != null && currentAnimator.isRunning()) currentAnimator.cancel();
if (currentAnimator != null && currentAnimator.isRunning()) {
currentAnimator.cancel();
}
currentAnimator = AnimationUtils.animateHeight(this, ANIMATION_DURATION, 0);
setCurrentState(COLLAPSED);
@ -130,7 +133,9 @@ public class CollapsibleView extends LinearLayout {
Log.d(TAG, getDebugLogString("expand() called"));
}
if (!readyToChangeState) return;
if (!readyToChangeState) {
return;
}
final int height = getHeight();
if (height == this.targetHeight) {
@ -138,13 +143,17 @@ public class CollapsibleView extends LinearLayout {
return;
}
if (currentAnimator != null && currentAnimator.isRunning()) currentAnimator.cancel();
if (currentAnimator != null && currentAnimator.isRunning()) {
currentAnimator.cancel();
}
currentAnimator = AnimationUtils.animateHeight(this, ANIMATION_DURATION, this.targetHeight);
setCurrentState(EXPANDED);
}
public void switchState() {
if (!readyToChangeState) return;
if (!readyToChangeState) {
return;
}
if (currentState == COLLAPSED) {
expand();
@ -158,7 +167,7 @@ public class CollapsibleView extends LinearLayout {
return currentState;
}
public void setCurrentState(@ViewMode int currentState) {
public void setCurrentState(@ViewMode final int currentState) {
this.currentState = currentState;
broadcastState();
}
@ -171,6 +180,7 @@ public class CollapsibleView extends LinearLayout {
/**
* Add a listener which will be listening for changes in this view (i.e. collapsed or expanded).
* @param listener {@link StateListener} to be added
*/
public void addListener(final StateListener listener) {
if (listeners.contains(listener)) {
@ -182,11 +192,47 @@ public class CollapsibleView extends LinearLayout {
/**
* Remove a listener so it doesn't receive more state changes.
* @param listener {@link StateListener} to be removed
*/
public void removeListener(final StateListener listener) {
listeners.remove(listener);
}
@Nullable
@Override
public Parcelable onSaveInstanceState() {
return Icepick.saveInstanceState(this, super.onSaveInstanceState());
}
@Override
public void onRestoreInstanceState(final Parcelable state) {
super.onRestoreInstanceState(Icepick.restoreInstanceState(this, state));
ready();
}
/*//////////////////////////////////////////////////////////////////////////
// State Saving
//////////////////////////////////////////////////////////////////////////*/
public String getDebugLogString(final String description) {
return String.format("%-100s → %s",
description, "readyToChangeState = [" + readyToChangeState + "], "
+ "currentState = [" + currentState + "], "
+ "targetHeight = [" + targetHeight + "], "
+ "mW x mH = [" + getMeasuredWidth() + "x" + getMeasuredHeight() + "], "
+ "W x H = [" + getWidth() + "x" + getHeight() + "]");
}
@Retention(SOURCE)
@IntDef({COLLAPSED, EXPANDED})
public @interface ViewMode {
}
/*//////////////////////////////////////////////////////////////////////////
// Internal
//////////////////////////////////////////////////////////////////////////*/
/**
* Simple interface used for listening state changes of the {@link CollapsibleView}.
*/
@ -199,32 +245,4 @@ public class CollapsibleView extends LinearLayout {
*/
void onStateChanged(@ViewMode int newState);
}
/*//////////////////////////////////////////////////////////////////////////
// State Saving
//////////////////////////////////////////////////////////////////////////*/
@Nullable
@Override
public Parcelable onSaveInstanceState() {
return Icepick.saveInstanceState(this, super.onSaveInstanceState());
}
@Override
public void onRestoreInstanceState(Parcelable state) {
super.onRestoreInstanceState(Icepick.restoreInstanceState(this, state));
ready();
}
/*//////////////////////////////////////////////////////////////////////////
// Internal
//////////////////////////////////////////////////////////////////////////*/
public String getDebugLogString(String description) {
return String.format("%-100s → %s",
description, "readyToChangeState = [" + readyToChangeState + "], currentState = [" + currentState + "], targetHeight = [" + targetHeight + "]," +
" mW x mH = [" + getMeasuredWidth() + "x" + getMeasuredHeight() + "]" +
" W x H = [" + getWidth() + "x" + getHeight() + "]");
}
}

View file

@ -1,15 +1,12 @@
package org.schabi.newpipe.views;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import androidx.annotation.NonNull;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayout.Tab;
/**
* A TabLayout that is scrollable when tabs exceed its width.
@ -21,34 +18,36 @@ public class ScrollableTabLayout extends TabLayout {
private int layoutWidth = 0;
private int prevVisibility = View.GONE;
public ScrollableTabLayout(Context context) {
public ScrollableTabLayout(final Context context) {
super(context);
}
public ScrollableTabLayout(Context context, AttributeSet attrs) {
public ScrollableTabLayout(final Context context, final AttributeSet attrs) {
super(context, attrs);
}
public ScrollableTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
public ScrollableTabLayout(final Context context, final AttributeSet attrs,
final int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
protected void onLayout(final boolean changed, final int l, final int t, final int r,
final int b) {
super.onLayout(changed, l, t, r, b);
remeasureTabs();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
protected void onSizeChanged(final int w, final int h, final int oldw, final int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
layoutWidth = w;
}
@Override
public void addTab(@NonNull Tab tab, int position, boolean setSelected) {
public void addTab(@NonNull final Tab tab, final int position, final boolean setSelected) {
super.addTab(tab, position, setSelected);
hasMultipleTabs();
@ -60,22 +59,23 @@ public class ScrollableTabLayout extends TabLayout {
}
@Override
public void removeTabAt(int position) {
public void removeTabAt(final int position) {
super.removeTabAt(position);
hasMultipleTabs();
// Removing a tab won't increase total tabs' width so tabMode won't have to change to SCROLLABLE
// Removing a tab won't increase total tabs' width
// so tabMode won't have to change to SCROLLABLE
if (getTabMode() != MODE_FIXED) {
remeasureTabs();
}
}
@Override
protected void onVisibilityChanged(View changedView, int visibility) {
protected void onVisibilityChanged(final View changedView, final int visibility) {
super.onVisibilityChanged(changedView, visibility);
// Recheck content width in case some tabs have been added or removed while ScrollableTabLayout was invisible
// Check width if some tabs have been added/removed while ScrollableTabLayout was invisible
// We don't have to check if it was GONE because then requestLayout() will be called
if (changedView == this) {
if (prevVisibility == View.INVISIBLE) {
@ -85,14 +85,16 @@ public class ScrollableTabLayout extends TabLayout {
}
}
private void setMode(int mode) {
if (mode == getTabMode()) return;
private void setMode(final int mode) {
if (mode == getTabMode()) {
return;
}
setTabMode(mode);
}
/**
* Make ScrollableTabLayout not visible if there are less than two tabs
* Make ScrollableTabLayout not visible if there are less than two tabs.
*/
private void hasMultipleTabs() {
if (getTabCount() > 1) {
@ -103,11 +105,15 @@ public class ScrollableTabLayout extends TabLayout {
}
/**
* Calculate minimal width required by tabs and set tabMode accordingly
* Calculate minimal width required by tabs and set tabMode accordingly.
*/
private void remeasureTabs() {
if (prevVisibility != View.VISIBLE) return;
if (layoutWidth == 0) return;
if (prevVisibility != View.VISIBLE) {
return;
}
if (layoutWidth == 0) {
return;
}
final int count = getTabCount();
int contentWidth = 0;