KeyguardWidgetFrame.java revision 258341c377b6aa9f1bd29a9b507a97967e432dfe
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.content.Context;
20import android.content.res.Resources;
21import android.graphics.Canvas;
22import android.graphics.Paint;
23import android.graphics.PorterDuff;
24import android.graphics.PorterDuffXfermode;
25import android.graphics.Rect;
26import android.graphics.drawable.Drawable;
27import android.graphics.drawable.NinePatchDrawable;
28import android.util.AttributeSet;
29import android.widget.FrameLayout;
30
31import com.android.internal.R;
32
33public class KeyguardWidgetFrame extends FrameLayout {
34    private final static PorterDuffXfermode sAddBlendMode =
35            new PorterDuffXfermode(PorterDuff.Mode.ADD);
36    private static int sWidgetPagePadding;
37    private static Drawable sLeftOverscrollDrawable;
38    private static Drawable sRightOverscrollDrawable;
39
40    private Drawable mForegroundDrawable;
41    private final Rect mForegroundRect = new Rect();
42    private int mForegroundAlpha = 0;
43
44    public KeyguardWidgetFrame(Context context) {
45        this(context, null, 0);
46    }
47
48    public KeyguardWidgetFrame(Context context, AttributeSet attrs) {
49        this(context, attrs, 0);
50    }
51
52    public KeyguardWidgetFrame(Context context, AttributeSet attrs, int defStyle) {
53        super(context, attrs, defStyle);
54        if (sLeftOverscrollDrawable == null) {
55            Resources res = context.getResources();
56            sLeftOverscrollDrawable = res.getDrawable(R.drawable.kg_widget_overscroll_layer_left);
57            sRightOverscrollDrawable = res.getDrawable(R.drawable.kg_widget_overscroll_layer_right);
58            sWidgetPagePadding = res.getDimensionPixelSize(R.dimen.kg_widget_page_padding);
59        }
60        setPadding(sWidgetPagePadding, sWidgetPagePadding, sWidgetPagePadding, sWidgetPagePadding);
61    }
62
63    @Override
64    protected void dispatchDraw(Canvas canvas) {
65        super.dispatchDraw(canvas);
66        if (mForegroundAlpha > 0) {
67            mForegroundDrawable.setBounds(mForegroundRect);
68            Paint p = ((NinePatchDrawable) mForegroundDrawable).getPaint();
69            p.setXfermode(sAddBlendMode);
70            mForegroundDrawable.draw(canvas);
71            p.setXfermode(null);
72        }
73    }
74
75    @Override
76    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
77        super.onSizeChanged(w, h, oldw, oldh);
78        mForegroundRect.set(sWidgetPagePadding, sWidgetPagePadding,
79                w - sWidgetPagePadding, h - sWidgetPagePadding);
80    }
81
82    void setOverScrollAmount(float r, boolean left) {
83        if (left && mForegroundDrawable != sLeftOverscrollDrawable) {
84            mForegroundDrawable = sLeftOverscrollDrawable;
85        } else if (!left && mForegroundDrawable != sRightOverscrollDrawable) {
86            mForegroundDrawable = sRightOverscrollDrawable;
87        }
88
89        mForegroundAlpha = (int) Math.round((r * 255));
90        mForegroundDrawable.setAlpha(mForegroundAlpha);
91        invalidate();
92    }
93}
94