1057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong/*
2057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong * Copyright (C) 2011 The Android Open Source Project
3057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong *
4057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong * Licensed under the Apache License, Version 2.0 (the "License");
5057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong * you may not use this file except in compliance with the License.
6057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong * You may obtain a copy of the License at
7057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong *
8057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong *      http://www.apache.org/licenses/LICENSE-2.0
9057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong *
10057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong * Unless required by applicable law or agreed to in writing, software
11057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong * distributed under the License is distributed on an "AS IS" BASIS,
12057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong * See the License for the specific language governing permissions and
14057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong * limitations under the License.
15057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong */
16057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong
179a59de880cc82d8175e40a3bfcebffbadbb33efdPin Tingpackage com.android.camera;
18057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong
19057040c0e19834d98719143c2627a9f0df3e4c35Angus Kongimport android.content.Context;
20057040c0e19834d98719143c2627a9f0df3e4c35Angus Kongimport android.graphics.Canvas;
21057040c0e19834d98719143c2627a9f0df3e4c35Angus Kongimport android.graphics.Paint;
22057040c0e19834d98719143c2627a9f0df3e4c35Angus Kongimport android.graphics.RectF;
23057040c0e19834d98719143c2627a9f0df3e4c35Angus Kongimport android.util.AttributeSet;
24057040c0e19834d98719143c2627a9f0df3e4c35Angus Kongimport android.widget.ImageView;
25057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong
26057040c0e19834d98719143c2627a9f0df3e4c35Angus Kongclass PanoProgressBar extends ImageView {
27913f3784d368a5e11fee5d5db2c355ef832685daWu-cheng Li    @SuppressWarnings("unused")
28057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    private static final String TAG = "PanoProgressBar";
296108e0ed3a6b03deed2a8586d98607a6756637b1Angus Kong    public static final int DIRECTION_NONE = 0;
306108e0ed3a6b03deed2a8586d98607a6756637b1Angus Kong    public static final int DIRECTION_LEFT = 1;
316108e0ed3a6b03deed2a8586d98607a6756637b1Angus Kong    public static final int DIRECTION_RIGHT = 2;
32057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    private float mProgress = 0;
33057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    private float mMaxProgress = 0;
34057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    private float mLeftMostProgress = 0;
35057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    private float mRightMostProgress = 0;
36057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    private float mProgressOffset = 0;
37057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    private float mIndicatorWidth = 0;
386108e0ed3a6b03deed2a8586d98607a6756637b1Angus Kong    private int mDirection = 0;
39057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    private final Paint mBackgroundPaint = new Paint();
40057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    private final Paint mDoneAreaPaint = new Paint();
41057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    private final Paint mIndicatorPaint = new Paint();
42057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    private float mWidth;
43057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    private float mHeight;
44057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    private RectF mDrawBounds;
456108e0ed3a6b03deed2a8586d98607a6756637b1Angus Kong    private OnDirectionChangeListener mListener = null;
466108e0ed3a6b03deed2a8586d98607a6756637b1Angus Kong
476108e0ed3a6b03deed2a8586d98607a6756637b1Angus Kong    public interface OnDirectionChangeListener {
486108e0ed3a6b03deed2a8586d98607a6756637b1Angus Kong        public void onDirectionChange(int direction);
496108e0ed3a6b03deed2a8586d98607a6756637b1Angus Kong    }
50057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong
51057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    public PanoProgressBar(Context context, AttributeSet attrs) {
52057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong        super(context, attrs);
53057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong        mDoneAreaPaint.setStyle(Paint.Style.FILL);
54057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong        mDoneAreaPaint.setAlpha(0xff);
55057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong
56057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong        mBackgroundPaint.setStyle(Paint.Style.FILL);
57057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong        mBackgroundPaint.setAlpha(0xff);
58057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong
59057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong        mIndicatorPaint.setStyle(Paint.Style.FILL);
60057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong        mIndicatorPaint.setAlpha(0xff);
61057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong
62057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong        mDrawBounds = new RectF();
63057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    }
64057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong
656108e0ed3a6b03deed2a8586d98607a6756637b1Angus Kong    public void setOnDirectionChangeListener(OnDirectionChangeListener l) {
666108e0ed3a6b03deed2a8586d98607a6756637b1Angus Kong        mListener = l;
676108e0ed3a6b03deed2a8586d98607a6756637b1Angus Kong    }
686108e0ed3a6b03deed2a8586d98607a6756637b1Angus Kong
696108e0ed3a6b03deed2a8586d98607a6756637b1Angus Kong    private void setDirection(int direction) {
70fa959aede9b1fe9d510108e883a2f16cf28c9033Angus Kong        if (mDirection != direction) {
71fa959aede9b1fe9d510108e883a2f16cf28c9033Angus Kong            mDirection = direction;
72fa959aede9b1fe9d510108e883a2f16cf28c9033Angus Kong            if (mListener != null) {
73fa959aede9b1fe9d510108e883a2f16cf28c9033Angus Kong                mListener.onDirectionChange(mDirection);
74fa959aede9b1fe9d510108e883a2f16cf28c9033Angus Kong            }
75fa959aede9b1fe9d510108e883a2f16cf28c9033Angus Kong            invalidate();
766108e0ed3a6b03deed2a8586d98607a6756637b1Angus Kong        }
776108e0ed3a6b03deed2a8586d98607a6756637b1Angus Kong    }
786108e0ed3a6b03deed2a8586d98607a6756637b1Angus Kong
796108e0ed3a6b03deed2a8586d98607a6756637b1Angus Kong    public int getDirection() {
806108e0ed3a6b03deed2a8586d98607a6756637b1Angus Kong        return mDirection;
816108e0ed3a6b03deed2a8586d98607a6756637b1Angus Kong    }
826108e0ed3a6b03deed2a8586d98607a6756637b1Angus Kong
83913f3784d368a5e11fee5d5db2c355ef832685daWu-cheng Li    @Override
84057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    public void setBackgroundColor(int color) {
85057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong        mBackgroundPaint.setColor(color);
86fa959aede9b1fe9d510108e883a2f16cf28c9033Angus Kong        invalidate();
87057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    }
88057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong
89057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    public void setDoneColor(int color) {
90057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong        mDoneAreaPaint.setColor(color);
91fa959aede9b1fe9d510108e883a2f16cf28c9033Angus Kong        invalidate();
92057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    }
93057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong
94057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    public void setIndicatorColor(int color) {
95057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong        mIndicatorPaint.setColor(color);
96fa959aede9b1fe9d510108e883a2f16cf28c9033Angus Kong        invalidate();
97057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    }
98057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong
99913f3784d368a5e11fee5d5db2c355ef832685daWu-cheng Li    @Override
100057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
101057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong        mWidth = w;
102057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong        mHeight = h;
103057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong        mDrawBounds.set(0, 0, mWidth, mHeight);
104057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    }
105057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong
106057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    public void setMaxProgress(int progress) {
107057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong        mMaxProgress = progress;
108057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    }
109057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong
110057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    public void setIndicatorWidth(float w) {
111057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong        mIndicatorWidth = w;
112fa959aede9b1fe9d510108e883a2f16cf28c9033Angus Kong        invalidate();
113057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    }
114057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong
115057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    public void setRightIncreasing(boolean rightIncreasing) {
116057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong        if (rightIncreasing) {
117057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong            mLeftMostProgress = 0;
118057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong            mRightMostProgress = 0;
119057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong            mProgressOffset = 0;
1206108e0ed3a6b03deed2a8586d98607a6756637b1Angus Kong            setDirection(DIRECTION_RIGHT);
121057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong        } else {
122057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong            mLeftMostProgress = mWidth;
123057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong            mRightMostProgress = mWidth;
124057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong            mProgressOffset = mWidth;
1256108e0ed3a6b03deed2a8586d98607a6756637b1Angus Kong            setDirection(DIRECTION_LEFT);
126057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong        }
127215f7ecf1fcc861986397fdf6724602d4775ab2cAngus Kong        invalidate();
128057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    }
129057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong
130057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    public void setProgress(int progress) {
131057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong        // The panning direction will be decided after user pan more than 10 degrees in one
132057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong        // direction.
133057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong        if (mDirection == DIRECTION_NONE) {
134057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong            if (progress > 10) {
135057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong                setRightIncreasing(true);
136215f7ecf1fcc861986397fdf6724602d4775ab2cAngus Kong            } else if (progress < -10) {
137057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong                setRightIncreasing(false);
138057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong            }
139215f7ecf1fcc861986397fdf6724602d4775ab2cAngus Kong        }
140215f7ecf1fcc861986397fdf6724602d4775ab2cAngus Kong        // mDirection might be modified by setRightIncreasing() above. Need to check again.
141215f7ecf1fcc861986397fdf6724602d4775ab2cAngus Kong        if (mDirection != DIRECTION_NONE) {
142057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong            mProgress = progress * mWidth / mMaxProgress + mProgressOffset;
143057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong            // value bounds.
144057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong            mProgress = Math.min(mWidth, Math.max(0, mProgress));
145057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong            if (mDirection == DIRECTION_RIGHT) {
146057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong                // The right most progress is adjusted.
147057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong                mRightMostProgress = Math.max(mRightMostProgress, mProgress);
148057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong            }
149057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong            if (mDirection == DIRECTION_LEFT) {
150057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong                // The left most progress is adjusted.
151057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong                mLeftMostProgress = Math.min(mLeftMostProgress, mProgress);
152057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong            }
153215f7ecf1fcc861986397fdf6724602d4775ab2cAngus Kong            invalidate();
154057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong        }
155057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    }
156057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong
157057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    public void reset() {
158057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong        mProgress = 0;
159057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong        mProgressOffset = 0;
1606108e0ed3a6b03deed2a8586d98607a6756637b1Angus Kong        setDirection(DIRECTION_NONE);
161215f7ecf1fcc861986397fdf6724602d4775ab2cAngus Kong        invalidate();
162057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    }
163057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong
164057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    @Override
165057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    protected void onDraw(Canvas canvas) {
166057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong        // the background
167057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong        canvas.drawRect(mDrawBounds, mBackgroundPaint);
168057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong        if (mDirection != DIRECTION_NONE) {
169057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong            // the progress area
170057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong            canvas.drawRect(mLeftMostProgress, mDrawBounds.top, mRightMostProgress,
171057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong                    mDrawBounds.bottom, mDoneAreaPaint);
172057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong            // the indication bar
173057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong            float l;
174057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong            float r;
175057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong            if (mDirection == DIRECTION_RIGHT) {
176057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong                l = Math.max(mProgress - mIndicatorWidth, 0f);
177057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong                r = mProgress;
178057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong            } else {
179057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong                l = mProgress;
180057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong                r = Math.min(mProgress + mIndicatorWidth, mWidth);
181057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong            }
182057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong            canvas.drawRect(l, mDrawBounds.top, r, mDrawBounds.bottom, mIndicatorPaint);
183057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong        }
184057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong
185057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong        // draw the mask image on the top for shaping.
186057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong        super.onDraw(canvas);
187057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong    }
188057040c0e19834d98719143c2627a9f0df3e4c35Angus Kong}
189