OSDN Git Service

am e03c0244: Merge "Clear data deletes too much" into jb-mr1-dev
[android-x86/frameworks-base.git] / policy / src / com / android / internal / policy / impl / keyguard / KeyguardWidgetFrame.java
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
17 package com.android.internal.policy.impl.keyguard;
18
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;
35
36 import com.android.internal.R;
37
38 public class KeyguardWidgetFrame extends FrameLayout {
39     private final static PorterDuffXfermode sAddBlendMode =
40             new PorterDuffXfermode(PorterDuff.Mode.ADD);
41
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;
48
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;
54
55     private float mBackgroundAlpha;
56     private float mBackgroundAlphaMultiplier;
57     private Drawable mBackgroundDrawable;
58     private Rect mBackgroundRect = new Rect();
59
60     public KeyguardWidgetFrame(Context context) {
61         this(context, null, 0);
62     }
63
64     public KeyguardWidgetFrame(Context context, AttributeSet attrs) {
65         this(context, attrs, 0);
66     }
67
68     public KeyguardWidgetFrame(Context context, AttributeSet attrs, int defStyle) {
69         super(context, attrs, defStyle);
70
71         mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
72
73         Resources res = context.getResources();
74         /*
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);
79         */
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);
84
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);
88     }
89
90     public void setDisableUserInteraction(boolean disabled) {
91         mDisableInteraction = disabled;
92     }
93
94     @Override
95     public boolean onInterceptTouchEvent(MotionEvent ev) {
96         if (!mDisableInteraction) {
97             mPowerManager.userActivity(SystemClock.uptimeMillis(), false);
98             return super.onInterceptTouchEvent(ev);
99         }
100         return true;
101     }
102
103     @Override
104     protected void dispatchDraw(Canvas canvas) {
105         drawBg(canvas);
106         super.dispatchDraw(canvas);
107         drawGradientOverlay(canvas);
108
109     }
110
111     /**
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.
115      */
116     public void enableHardwareLayersForContent() {
117         View widget = getContent();
118         if (widget != null) {
119             widget.setLayerType(LAYER_TYPE_HARDWARE, null);
120         }
121     }
122
123     /**
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.
127      */
128     public void disableHardwareLayersForContent() {
129         View widget = getContent();
130         if (widget != null) {
131             widget.setLayerType(LAYER_TYPE_NONE, null);
132         }
133     }
134
135     public View getContent() {
136         return getChildAt(0);
137     }
138
139     private void drawGradientOverlay(Canvas c) {
140         mGradientPaint.setShader(mForegroundGradient);
141         mGradientPaint.setAlpha(mForegroundAlpha);
142         c.drawRect(mForegroundRect, mGradientPaint);
143     }
144
145     protected void drawBg(Canvas canvas) {
146         if (mBackgroundAlpha > 0.0f) {
147             Drawable bg = mBackgroundDrawable;
148
149             bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
150             bg.setBounds(mBackgroundRect);
151             bg.draw(canvas);
152         }
153     }
154
155     public float getBackgroundAlpha() {
156         return mBackgroundAlpha;
157     }
158
159     public void setBackgroundAlphaMultiplier(float multiplier) {
160         if (mBackgroundAlphaMultiplier != multiplier) {
161             mBackgroundAlphaMultiplier = multiplier;
162             invalidate();
163         }
164     }
165
166     public float getBackgroundAlphaMultiplier() {
167         return mBackgroundAlphaMultiplier;
168     }
169
170     public void setBackgroundAlpha(float alpha) {
171         if (mBackgroundAlpha != alpha) {
172             mBackgroundAlpha = alpha;
173             invalidate();
174         }
175     }
176
177     /**
178      * Depending on whether the security is up, the widget size needs to change
179      * 
180      * @param height The height of the widget, -1 for full height
181      */
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) {
188                 needLayout = true;
189                 lp.height = height;
190             }
191         }
192         if (needLayout) {
193             requestLayout();
194         }
195     }
196
197     /**
198      * Set the top location of the challenge.
199      *
200      * @param top The top of the challenge, in _local_ coordinates, or -1 to indicate the challenge
201      *              is down.
202      */
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);
207     }
208
209     public void resetSize() {
210         setWidgetHeight(LayoutParams.MATCH_PARENT);
211     }
212
213     @Override
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);
225     }
226
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));
232             invalidate();
233         }
234     }
235 }