1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.internal.policy.impl.keyguard;
18
19import android.graphics.Bitmap;
20import android.graphics.Canvas;
21import android.graphics.Color;
22import android.graphics.ColorFilter;
23import android.graphics.Paint;
24import android.graphics.Path;
25import android.graphics.PixelFormat;
26import android.graphics.PorterDuff;
27import android.graphics.PorterDuffXfermode;
28import android.graphics.Rect;
29import android.graphics.RectF;
30import android.graphics.drawable.Drawable;
31
32import android.util.Log;
33
34class KeyguardCircleFramedDrawable extends Drawable {
35
36    private final Bitmap mBitmap;
37    private final int mSize;
38    private final Paint mPaint;
39    private final float mShadowRadius;
40    private final float mStrokeWidth;
41    private final int mFrameColor;
42    private final int mHighlightColor;
43    private final int mFrameShadowColor;
44
45    private float mScale;
46    private Path mFramePath;
47    private Rect mSrcRect;
48    private RectF mDstRect;
49    private RectF mFrameRect;
50    private boolean mPressed;
51
52    public KeyguardCircleFramedDrawable(Bitmap bitmap, int size,
53            int frameColor, float strokeWidth,
54            int frameShadowColor, float shadowRadius,
55            int highlightColor) {
56        super();
57        mSize = size;
58        mShadowRadius = shadowRadius;
59        mFrameColor = frameColor;
60        mFrameShadowColor = frameShadowColor;
61        mStrokeWidth = strokeWidth;
62        mHighlightColor = highlightColor;
63
64        mBitmap = Bitmap.createBitmap(mSize, mSize, Bitmap.Config.ARGB_8888);
65        final Canvas canvas = new Canvas(mBitmap);
66
67        final int width = bitmap.getWidth();
68        final int height = bitmap.getHeight();
69        final int square = Math.min(width, height);
70
71        final Rect cropRect = new Rect((width - square) / 2, (height - square) / 2, square, square);
72        final RectF circleRect = new RectF(0f, 0f, mSize, mSize);
73        circleRect.inset(mStrokeWidth / 2f, mStrokeWidth / 2f);
74        circleRect.inset(mShadowRadius, mShadowRadius);
75
76        final Path fillPath = new Path();
77        fillPath.addArc(circleRect, 0f, 360f);
78
79        canvas.drawColor(0, PorterDuff.Mode.CLEAR);
80
81        // opaque circle matte
82        mPaint = new Paint();
83        mPaint.setAntiAlias(true);
84        mPaint.setColor(Color.BLACK);
85        mPaint.setStyle(Paint.Style.FILL);
86        canvas.drawPath(fillPath, mPaint);
87
88        // mask in the icon where the bitmap is opaque
89        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
90        canvas.drawBitmap(bitmap, cropRect, circleRect, mPaint);
91
92        // prepare paint for frame drawing
93        mPaint.setXfermode(null);
94
95        mScale = 1f;
96
97        mSrcRect = new Rect(0, 0, mSize, mSize);
98        mDstRect = new RectF(0, 0, mSize, mSize);
99        mFrameRect = new RectF(mDstRect);
100        mFramePath = new Path();
101    }
102
103    @Override
104    public void draw(Canvas canvas) {
105        // clear background
106        final float outside = Math.min(canvas.getWidth(), canvas.getHeight());
107        final float inside = mScale * outside;
108        final float pad = (outside - inside) / 2f;
109
110        mDstRect.set(pad, pad, outside - pad, outside - pad);
111        canvas.drawBitmap(mBitmap, mSrcRect, mDstRect, null);
112
113        mFrameRect.set(mDstRect);
114        mFrameRect.inset(mStrokeWidth / 2f, mStrokeWidth / 2f);
115        mFrameRect.inset(mShadowRadius, mShadowRadius);
116
117        mFramePath.reset();
118        mFramePath.addArc(mFrameRect, 0f, 360f);
119
120        // white frame
121        if (mPressed) {
122            mPaint.setStyle(Paint.Style.FILL);
123            mPaint.setColor(Color.argb((int) (0.33f * 255),
124                            Color.red(mHighlightColor),
125                            Color.green(mHighlightColor),
126                            Color.blue(mHighlightColor)));
127            canvas.drawPath(mFramePath, mPaint);
128        }
129        mPaint.setStrokeWidth(mStrokeWidth);
130        mPaint.setStyle(Paint.Style.STROKE);
131        mPaint.setColor(mPressed ? mHighlightColor : mFrameColor);
132        mPaint.setShadowLayer(mShadowRadius, 0f, 0f, mFrameShadowColor);
133        canvas.drawPath(mFramePath, mPaint);
134    }
135
136    public void setScale(float scale) {
137        Log.i("KFD", "scale: " + scale);
138        mScale = scale;
139    }
140
141    public float getScale() {
142        return mScale;
143    }
144
145    public void setPressed(boolean pressed) {
146        mPressed = pressed;
147    }
148
149    @Override
150    public int getOpacity() {
151        return PixelFormat.TRANSLUCENT;
152    }
153
154    @Override
155    public void setAlpha(int alpha) {
156    }
157
158    @Override
159    public void setColorFilter(ColorFilter cf) {
160    }
161}
162