2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.internal.policy.impl.keyguard;
19 import android.content.Context;
20 import android.content.res.Resources;
21 import android.graphics.Canvas;
22 import android.graphics.LinearGradient;
23 import android.graphics.Paint;
24 import android.graphics.PorterDuff;
25 import android.graphics.PorterDuffXfermode;
26 import android.graphics.Rect;
27 import android.graphics.Shader;
28 import android.graphics.drawable.Drawable;
29 import android.os.PowerManager;
30 import android.os.SystemClock;
31 import android.util.AttributeSet;
32 import android.view.MotionEvent;
33 import android.view.View;
34 import android.widget.FrameLayout;
36 import com.android.internal.R;
38 public class KeyguardWidgetFrame extends FrameLayout {
39 private final static PorterDuffXfermode sAddBlendMode =
40 new PorterDuffXfermode(PorterDuff.Mode.ADD);
42 private int mGradientColor;
43 private LinearGradient mForegroundGradient;
44 private LinearGradient mLeftToRightGradient;
45 private LinearGradient mRightToLeftGradient;
46 private Paint mGradientPaint = new Paint();
47 boolean mLeftToRight = true;
49 private float mOverScrollAmount = 0f;
50 private final Rect mForegroundRect = new Rect();
51 private int mForegroundAlpha = 0;
52 private PowerManager mPowerManager;
53 private boolean mDisableInteraction;
55 private float mBackgroundAlpha;
56 private float mBackgroundAlphaMultiplier;
57 private Drawable mBackgroundDrawable;
58 private Rect mBackgroundRect = new Rect();
60 public KeyguardWidgetFrame(Context context) {
61 this(context, null, 0);
64 public KeyguardWidgetFrame(Context context, AttributeSet attrs) {
65 this(context, attrs, 0);
68 public KeyguardWidgetFrame(Context context, AttributeSet attrs, int defStyle) {
69 super(context, attrs, defStyle);
71 mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
73 Resources res = context.getResources();
75 int hPadding = res.getDimensionPixelSize(R.dimen.kg_widget_pager_horizontal_padding);
76 int topPadding = res.getDimensionPixelSize(R.dimen.kg_widget_pager_top_padding);
77 int bottomPadding = res.getDimensionPixelSize(R.dimen.kg_widget_pager_bottom_padding);
78 setPadding(hPadding, topPadding, hPadding, bottomPadding);
80 // TODO: this padding should really correspond to the padding embedded in the background
81 // drawable (ie. outlines).
82 int padding = (int) (res.getDisplayMetrics().density * 8);
83 setPadding(padding, padding, padding, padding);
85 mBackgroundDrawable = res.getDrawable(R.drawable.security_frame);
86 mGradientColor = res.getColor(com.android.internal.R.color.kg_widget_pager_gradient);
87 mGradientPaint.setXfermode(sAddBlendMode);
90 public void setDisableUserInteraction(boolean disabled) {
91 mDisableInteraction = disabled;
95 public boolean onInterceptTouchEvent(MotionEvent ev) {
96 if (!mDisableInteraction) {
97 mPowerManager.userActivity(SystemClock.uptimeMillis(), false);
98 return super.onInterceptTouchEvent(ev);
104 protected void dispatchDraw(Canvas canvas) {
106 super.dispatchDraw(canvas);
107 drawGradientOverlay(canvas);
112 * Because this view has fading outlines, it is essential that we enable hardware
113 * layers on the content (child) so that updating the alpha of the outlines doesn't
114 * result in the content layer being recreated.
116 public void enableHardwareLayersForContent() {
117 View widget = getContent();
118 if (widget != null) {
119 widget.setLayerType(LAYER_TYPE_HARDWARE, null);
124 * Because this view has fading outlines, it is essential that we enable hardware
125 * layers on the content (child) so that updating the alpha of the outlines doesn't
126 * result in the content layer being recreated.
128 public void disableHardwareLayersForContent() {
129 View widget = getContent();
130 if (widget != null) {
131 widget.setLayerType(LAYER_TYPE_NONE, null);
135 public View getContent() {
136 return getChildAt(0);
139 private void drawGradientOverlay(Canvas c) {
140 mGradientPaint.setShader(mForegroundGradient);
141 mGradientPaint.setAlpha(mForegroundAlpha);
142 c.drawRect(mForegroundRect, mGradientPaint);
145 protected void drawBg(Canvas canvas) {
146 if (mBackgroundAlpha > 0.0f) {
147 Drawable bg = mBackgroundDrawable;
149 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
150 bg.setBounds(mBackgroundRect);
155 public float getBackgroundAlpha() {
156 return mBackgroundAlpha;
159 public void setBackgroundAlphaMultiplier(float multiplier) {
160 if (mBackgroundAlphaMultiplier != multiplier) {
161 mBackgroundAlphaMultiplier = multiplier;
166 public float getBackgroundAlphaMultiplier() {
167 return mBackgroundAlphaMultiplier;
170 public void setBackgroundAlpha(float alpha) {
171 if (mBackgroundAlpha != alpha) {
172 mBackgroundAlpha = alpha;
178 * Depending on whether the security is up, the widget size needs to change
180 * @param height The height of the widget, -1 for full height
182 public void setWidgetHeight(int height) {
183 boolean needLayout = false;
184 View widget = getContent();
185 if (widget != null) {
186 LayoutParams lp = (LayoutParams) widget.getLayoutParams();
187 if (lp.height != height) {
198 * Set the top location of the challenge.
200 * @param top The top of the challenge, in _local_ coordinates, or -1 to indicate the challenge
203 public void setChallengeTop(int top) {
204 // The widget starts below the padding, and extends to the top of the challengs.
205 int widgetHeight = top - getPaddingTop();
206 setWidgetHeight(widgetHeight);
209 public void resetSize() {
210 setWidgetHeight(LayoutParams.MATCH_PARENT);
214 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
215 super.onSizeChanged(w, h, oldw, oldh);
216 mForegroundRect.set(getPaddingLeft(), getPaddingTop(),
217 w - getPaddingRight(), h - getPaddingBottom());
218 float x0 = mLeftToRight ? 0 : mForegroundRect.width();
219 float x1 = mLeftToRight ? mForegroundRect.width(): 0;
220 mLeftToRightGradient = new LinearGradient(x0, 0f, x1, 0f,
221 mGradientColor, 0, Shader.TileMode.CLAMP);
222 mRightToLeftGradient = new LinearGradient(x1, 0f, x0, 0f,
223 mGradientColor, 0, Shader.TileMode.CLAMP);
224 mBackgroundRect.set(0, 0, w, h);
227 void setOverScrollAmount(float r, boolean left) {
228 if (Float.compare(mOverScrollAmount, r) != 0) {
229 mOverScrollAmount = r;
230 mForegroundGradient = left ? mLeftToRightGradient : mRightToLeftGradient;
231 mForegroundAlpha = (int) Math.round((0.85f * r * 255));