package com.annimon.paperstyle;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.ProgressBar;

/**
 * Material horizontal progress bar.
 * @author aNNiMON
 */
public class PaperProgressBar extends ProgressBar {

    private static final int GRAY_LINE_COLOR = 0xFFC8C8C8;

    private final Paint mColorPaint = new Paint();

    private int mProgressColor;
    private int mSecondaryProgressColor;

    public PaperProgressBar(Context context) {
        this(context, null);
    }

    public PaperProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.progressBarStyleHorizontal);
    }

    public PaperProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        final TypedArray a = context.getTheme().obtainStyledAttributes(
                attrs, R.styleable.PaperProgressBar, defStyleAttr, 0);
        try {
            mProgressColor = a.getColor(R.styleable.PaperProgressBar_pw_progressColor, 0xFF0F9D58);
            // Secondary progress by default is same color, but half transparent
            mSecondaryProgressColor = a.getColor(R.styleable.PaperProgressBar_pw_secondaryProgressColor,
                    0x7F000000 | (mProgressColor & 0xFFFFFF));
        } finally {
            a.recycle();
        }

        mColorPaint.setColor(mProgressColor);
        mColorPaint.setStyle(Paint.Style.FILL);
        mColorPaint.setAntiAlias(true);
    }

    public int getProgressColor() {
        return mProgressColor;
    }

    public void setProgressColor(int progressColor) {
        mProgressColor = progressColor;
        invalidate();
    }

    public int getSecondaryProgressColor() {
        return mSecondaryProgressColor;
    }

    public void setSecondaryProgressColor(int secondaryProgressColor) {
        mSecondaryProgressColor = secondaryProgressColor;
        invalidate();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mColorPaint.setStrokeWidth(h / 3f);
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        final float width = getWidth(), h2 = getHeight() / 2;
        mColorPaint.setColor(GRAY_LINE_COLOR);
        canvas.drawLine(0, h2, width, h2, mColorPaint);
        if (getSecondaryProgress() > 0) {
            final float lineWidth = getSecondaryProgress() * width / getMax();
            mColorPaint.setColor(mSecondaryProgressColor);
            canvas.drawLine(0, h2, lineWidth, h2, mColorPaint);
        }
        if (getProgress() > 0) {
            final float lineWidth = getProgress() * width / getMax();
            mColorPaint.setColor(mProgressColor);
            canvas.drawLine(0, h2, lineWidth, h2, mColorPaint);
        }
    }
}

@@ -160,16 +160,70 @@
 .Paint;%0A
+import android.os.Handler;%0Aimport android.os.Message;%0A
 import a
@@ -248,16 +248,16 @@
 uteSet;%0A
-
 import a
@@ -401,16 +401,67 @@
 sBar %7B%0A%0A
+    private static final int ANIMATION_DELAY = 20;%0A
     priv
@@ -636,16 +636,58 @@
 ssColor;
+%0A    private float mIndeterminateProgress;
 %0A%0A    pu
@@ -1736,16 +1736,151 @@
 s(true);
+%0A%0A        mIndeterminateProgress = 0;%0A        if (isIndeterminate()) %7B%0A            mIndeterminateHandler.sendEmptyMessage(0);%0A        %7D
 %0A    %7D%0A%0A
@@ -2319,32 +2319,386 @@
 idate();%0A    %7D%0A%0A
+    @Override%0A    public synchronized void setIndeterminate(boolean indeterminate) %7B%0A        super.setIndeterminate(indeterminate);%0A        if (mIndeterminateHandler == null) return;%0A        if (indeterminate) %7B%0A            mIndeterminateHandler.sendEmptyMessage(0);%0A        %7D else %7B%0A            mIndeterminateHandler.removeMessages(0);%0A        %7D%0A    %7D%0A%0A
     @Override%0A  
@@ -3089,32 +3089,454 @@
 , mColorPaint);%0A
+%0A        if (isIndeterminate()) %7B%0A            final float scaleX = 0.3f * (50 - Math.abs(mIndeterminateProgress - 50)) / 50f;%0A            final float lineX = mIndeterminateProgress * width / 100f;%0A            final float lineWidth = width * scaleX / 2;%0A            mColorPaint.setColor(mProgressColor);%0A            canvas.drawLine(lineX - lineWidth, h2, lineX + lineWidth, h2, mColorPaint);%0A            return;%0A        %7D%0A%0A
         if (getS
@@ -4000,16 +4000,473 @@
       %7D%0A
-
     %7D
+%0A%0A    private final Handler mIndeterminateHandler = new Handler() %7B%0A%0A        @Override%0A        public void handleMessage(Message msg) %7B%0A            mIndeterminateProgress++;%0A            if (mIndeterminateProgress %3E= 100) %7B%0A                mIndeterminateProgress = 0;%0A            %7D%0A            invalidate();%0A            if (isIndeterminate()) %7B%0A                mIndeterminateHandler.sendEmptyMessageDelayed(0, ANIMATION_DELAY);%0A            %7D%0A        %7D%0A    %7D;
 %0A%7D%0A

Add indeterminate state to PaperProgressBar