blob: 1f512a3f93253c9f3d917305c5045c1b3317710c [file] [log] [blame]
Michael Jurka5f1c5092010-09-03 14:15:02 -07001/*
2 * Copyright (C) 2010 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.launcher2;
18
19import com.android.launcher.R;
20
21import android.content.Context;
22import android.graphics.Bitmap;
23import android.graphics.Canvas;
Michael Jurka5f1c5092010-09-03 14:15:02 -070024import android.graphics.Paint;
25import android.graphics.PorterDuff;
26import android.view.View;
27
Adam Cohenf34bab52010-09-30 14:11:56 -070028public class DimmableAppWidgetHostView extends LauncherAppWidgetHostView implements Dimmable {
Michael Jurka5f1c5092010-09-03 14:15:02 -070029 public DimmableAppWidgetHostView(Context context) {
30 super(context);
31 mPaint.setFilterBitmap(true);
32 }
33
34 private final Paint mPaint = new Paint();
Michael Jurka5f1c5092010-09-03 14:15:02 -070035 private Bitmap mDimmedView;
36 private Canvas mDimmedViewCanvas;
37 private boolean isDimmedViewUpdatePass;
Adam Cohenf34bab52010-09-30 14:11:56 -070038 private float mDimmableProgress;
Michael Jurka5f1c5092010-09-03 14:15:02 -070039
40 private void setChildAlpha(float alpha) {
41 if (getChildCount() > 0) {
42 final View child = getChildAt(0);
43 if (child.getAlpha() != alpha) {
44 getChildAt(0).setAlpha(alpha);
45 }
46 }
47 }
48
49 private void updateChildAlpha() {
50 // hacky, but sometimes widgets get their alpha set back to 1.0f, so we call
51 // this to force them back
52 setChildAlpha(getAlpha());
53 }
54
Adam Cohenf34bab52010-09-30 14:11:56 -070055 //@Override
Michael Jurka5f1c5092010-09-03 14:15:02 -070056 public boolean onSetAlpha(int alpha) {
57 super.onSetAlpha(alpha);
58 return true;
59 }
60
Adam Cohenf34bab52010-09-30 14:11:56 -070061 public void setDimmableProgress(float progress) {
62 mDimmableProgress = progress;
63 }
64
65 public float getDimmableProgress() {
66 return mDimmableProgress;
Michael Jurka5f1c5092010-09-03 14:15:02 -070067 }
68
69 private void updateDimmedView() {
70 if (mDimmedView == null) {
71 mDimmedView = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(),
72 Bitmap.Config.ARGB_8888);
73 mDimmedViewCanvas = new Canvas(mDimmedView);
74 }
75 mDimmedViewCanvas.drawColor(0x00000000);
76 mDimmedViewCanvas.concat(getMatrix());
77 isDimmedViewUpdatePass = true;
78 draw(mDimmedViewCanvas);
79 // make the bitmap look "dimmed"
80 int dimmedColor = getContext().getResources().getColor(R.color.dimmed_view_color);
81 mDimmedViewCanvas.drawColor(dimmedColor, PorterDuff.Mode.SRC_IN);
82 isDimmedViewUpdatePass = false;
Adam Cohenf34bab52010-09-30 14:11:56 -070083 invalidate();
Michael Jurka5f1c5092010-09-03 14:15:02 -070084 }
85
86 @Override
87 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
88 super.onLayout(changed, left, top, right, bottom);
89
Adam Cohenf34bab52010-09-30 14:11:56 -070090 if (mDimmedView == null && mDimmableProgress > 0.0f) {
Michael Jurka5f1c5092010-09-03 14:15:02 -070091 updateDimmedView();
92 }
93 }
94
95 @Override
96 public void dispatchDraw(Canvas canvas) {
97 if (isDimmedViewUpdatePass) {
98 final float alpha = getAlpha();
99 canvas.save();
100 setAlpha(1.0f);
101 super.dispatchDraw(canvas);
102 canvas.restore();
103 setAlpha(alpha);
104 } else {
Adam Cohenf34bab52010-09-30 14:11:56 -0700105 if (mDimmedView != null && mDimmableProgress > 0) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700106 // draw the dimmed version of this widget
Adam Cohenf34bab52010-09-30 14:11:56 -0700107 mPaint.setAlpha((int) (mDimmableProgress * 255));
Michael Jurka5f1c5092010-09-03 14:15:02 -0700108 canvas.drawBitmap(mDimmedView, 0, 0, mPaint);
109 }
110
111 updateChildAlpha();
112 super.dispatchDraw(canvas);
113 }
114 }
Winson Chung5f941722010-09-28 16:36:43 -0700115}