1692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren/*
2692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren * Copyright (C) 2012 The Android Open Source Project
3692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren *
4692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren * Licensed under the Apache License, Version 2.0 (the "License");
5692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren * you may not use this file except in compliance with the License.
6692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren * You may obtain a copy of the License at
7692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren *
8692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren *      http://www.apache.org/licenses/LICENSE-2.0
9692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren *
10692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren * Unless required by applicable law or agreed to in writing, software
11692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren * distributed under the License is distributed on an "AS IS" BASIS,
12692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren * See the License for the specific language governing permissions and
14692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren * limitations under the License.
15692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren */
16692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren
17692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wrenpackage com.android.internal.policy.impl.keyguard;
18692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren
19692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wrenimport android.graphics.Bitmap;
20692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wrenimport android.graphics.Canvas;
21692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wrenimport android.graphics.Color;
22692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wrenimport android.graphics.ColorFilter;
23692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wrenimport android.graphics.Paint;
24692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wrenimport android.graphics.Path;
25692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wrenimport android.graphics.PixelFormat;
26692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wrenimport android.graphics.PorterDuff;
27692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wrenimport android.graphics.PorterDuffXfermode;
28692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wrenimport android.graphics.Rect;
29692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wrenimport android.graphics.RectF;
30692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wrenimport android.graphics.drawable.Drawable;
31692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren
32692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wrenimport android.util.Log;
33692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren
34692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wrenclass KeyguardCircleFramedDrawable extends Drawable {
35692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren
36692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren    private final Bitmap mBitmap;
37692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren    private final int mSize;
38692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren    private final Paint mPaint;
39692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren    private final float mShadowRadius;
40692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren    private final float mStrokeWidth;
41692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren    private final int mFrameColor;
42692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren    private final int mHighlightColor;
43692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren    private final int mFrameShadowColor;
44692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren
45692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren    private float mScale;
46692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren    private Path mFramePath;
47692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren    private Rect mSrcRect;
48692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren    private RectF mDstRect;
49692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren    private RectF mFrameRect;
50692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren    private boolean mPressed;
51692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren
52692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren    public KeyguardCircleFramedDrawable(Bitmap bitmap, int size,
53692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren            int frameColor, float strokeWidth,
54692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren            int frameShadowColor, float shadowRadius,
55692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren            int highlightColor) {
56692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        super();
57692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        mSize = size;
58692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        mShadowRadius = shadowRadius;
59692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        mFrameColor = frameColor;
60692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        mFrameShadowColor = frameShadowColor;
61692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        mStrokeWidth = strokeWidth;
62692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        mHighlightColor = highlightColor;
63692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren
64692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        mBitmap = Bitmap.createBitmap(mSize, mSize, Bitmap.Config.ARGB_8888);
65692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        final Canvas canvas = new Canvas(mBitmap);
66692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren
67692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        final int width = bitmap.getWidth();
68692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        final int height = bitmap.getHeight();
69692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        final int square = Math.min(width, height);
70692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren
71692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        final Rect cropRect = new Rect((width - square) / 2, (height - square) / 2, square, square);
72692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        final RectF circleRect = new RectF(0f, 0f, mSize, mSize);
73692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        circleRect.inset(mStrokeWidth / 2f, mStrokeWidth / 2f);
74692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        circleRect.inset(mShadowRadius, mShadowRadius);
75692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren
76692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        final Path fillPath = new Path();
77692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        fillPath.addArc(circleRect, 0f, 360f);
78692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren
79692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        canvas.drawColor(0, PorterDuff.Mode.CLEAR);
80692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren
81692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        // opaque circle matte
82692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        mPaint = new Paint();
83692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        mPaint.setAntiAlias(true);
84692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        mPaint.setColor(Color.BLACK);
85692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        mPaint.setStyle(Paint.Style.FILL);
86692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        canvas.drawPath(fillPath, mPaint);
87692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren
88692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        // mask in the icon where the bitmap is opaque
89692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
90692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        canvas.drawBitmap(bitmap, cropRect, circleRect, mPaint);
91692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren
92692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        // prepare paint for frame drawing
93692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        mPaint.setXfermode(null);
94692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren
95692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        mScale = 1f;
96692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren
97692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        mSrcRect = new Rect(0, 0, mSize, mSize);
98692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        mDstRect = new RectF(0, 0, mSize, mSize);
99692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        mFrameRect = new RectF(mDstRect);
100692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        mFramePath = new Path();
101692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren    }
102692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren
103692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren    @Override
104692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren    public void draw(Canvas canvas) {
105692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        // clear background
106692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        final float outside = Math.min(canvas.getWidth(), canvas.getHeight());
107692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        final float inside = mScale * outside;
108692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        final float pad = (outside - inside) / 2f;
109692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren
110692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        mDstRect.set(pad, pad, outside - pad, outside - pad);
111692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        canvas.drawBitmap(mBitmap, mSrcRect, mDstRect, null);
112692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren
113692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        mFrameRect.set(mDstRect);
114692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        mFrameRect.inset(mStrokeWidth / 2f, mStrokeWidth / 2f);
115692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        mFrameRect.inset(mShadowRadius, mShadowRadius);
116692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren
117692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        mFramePath.reset();
118692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        mFramePath.addArc(mFrameRect, 0f, 360f);
119692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren
120692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        // white frame
121692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        if (mPressed) {
122692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren            mPaint.setStyle(Paint.Style.FILL);
123692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren            mPaint.setColor(Color.argb((int) (0.33f * 255),
124692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren                            Color.red(mHighlightColor),
125692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren                            Color.green(mHighlightColor),
126692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren                            Color.blue(mHighlightColor)));
127692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren            canvas.drawPath(mFramePath, mPaint);
128692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        }
129692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        mPaint.setStrokeWidth(mStrokeWidth);
130692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        mPaint.setStyle(Paint.Style.STROKE);
131692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        mPaint.setColor(mPressed ? mHighlightColor : mFrameColor);
132692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        mPaint.setShadowLayer(mShadowRadius, 0f, 0f, mFrameShadowColor);
133692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        canvas.drawPath(mFramePath, mPaint);
134692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren    }
135692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren
136692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren    public void setScale(float scale) {
137692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        Log.i("KFD", "scale: " + scale);
138692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        mScale = scale;
139692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren    }
140692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren
141692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren    public float getScale() {
142692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        return mScale;
143692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren    }
144692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren
145692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren    public void setPressed(boolean pressed) {
146692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        mPressed = pressed;
147692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren    }
148692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren
149692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren    @Override
150692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren    public int getOpacity() {
151692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren        return PixelFormat.TRANSLUCENT;
152692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren    }
153692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren
154692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren    @Override
155692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren    public void setAlpha(int alpha) {
156692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren    }
157692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren
158692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren    @Override
159692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren    public void setColorFilter(ColorFilter cf) {
160692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren    }
161692bb13352b9ad5166adc0dd55ae693d9181f611Chris Wren}
162