Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.launcher2; |
| 18 | |
| 19 | import com.android.launcher.R; |
| 20 | |
| 21 | import android.content.Context; |
| 22 | import android.graphics.Bitmap; |
| 23 | import android.graphics.Canvas; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 24 | import android.graphics.Paint; |
| 25 | import android.graphics.PorterDuff; |
| 26 | import android.view.View; |
| 27 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 28 | public class DimmableAppWidgetHostView extends LauncherAppWidgetHostView implements Dimmable { |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 29 | public DimmableAppWidgetHostView(Context context) { |
| 30 | super(context); |
| 31 | mPaint.setFilterBitmap(true); |
| 32 | } |
| 33 | |
| 34 | private final Paint mPaint = new Paint(); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 35 | private Bitmap mDimmedView; |
| 36 | private Canvas mDimmedViewCanvas; |
| 37 | private boolean isDimmedViewUpdatePass; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 38 | private float mDimmableProgress; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 39 | |
| 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 Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 55 | //@Override |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 56 | public boolean onSetAlpha(int alpha) { |
| 57 | super.onSetAlpha(alpha); |
| 58 | return true; |
| 59 | } |
| 60 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 61 | public void setDimmableProgress(float progress) { |
| 62 | mDimmableProgress = progress; |
| 63 | } |
| 64 | |
| 65 | public float getDimmableProgress() { |
| 66 | return mDimmableProgress; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 67 | } |
| 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 Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 83 | invalidate(); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 84 | } |
| 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 Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 90 | if (mDimmedView == null && mDimmableProgress > 0.0f) { |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 91 | 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 Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 105 | if (mDimmedView != null && mDimmableProgress > 0) { |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 106 | // draw the dimmed version of this widget |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 107 | mPaint.setAlpha((int) (mDimmableProgress * 255)); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 108 | canvas.drawBitmap(mDimmedView, 0, 0, mPaint); |
| 109 | } |
| 110 | |
| 111 | updateChildAlpha(); |
| 112 | super.dispatchDraw(canvas); |
| 113 | } |
| 114 | } |
Winson Chung | 5f94172 | 2010-09-28 16:36:43 -0700 | [diff] [blame] | 115 | } |