1/* 2 * Copyright (C) 2011 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.server.wm; 18 19import android.graphics.Canvas; 20import android.graphics.Paint; 21import android.graphics.PixelFormat; 22import android.graphics.PorterDuff; 23import android.graphics.Rect; 24import android.graphics.Typeface; 25import android.graphics.Paint.FontMetricsInt; 26import android.util.DisplayMetrics; 27import android.util.Log; 28import android.util.TypedValue; 29import android.view.Display; 30import android.view.Surface; 31import android.view.SurfaceSession; 32import android.view.Surface.OutOfResourcesException; 33 34/** 35 * Displays a watermark on top of the window manager's windows. 36 */ 37class Watermark { 38 final String[] mTokens; 39 final String mText; 40 final Paint mTextPaint; 41 final int mTextWidth; 42 final int mTextHeight; 43 final int mTextAscent; 44 final int mTextDescent; 45 final int mDeltaX; 46 final int mDeltaY; 47 48 Surface mSurface; 49 int mLastDW; 50 int mLastDH; 51 boolean mDrawNeeded; 52 53 Watermark(DisplayMetrics dm, SurfaceSession session, String[] tokens) { 54 if (false) { 55 Log.i(WindowManagerService.TAG, "*********************** WATERMARK"); 56 for (int i=0; i<tokens.length; i++) { 57 Log.i(WindowManagerService.TAG, " TOKEN #" + i + ": " + tokens[i]); 58 } 59 } 60 61 mTokens = tokens; 62 63 StringBuilder builder = new StringBuilder(32); 64 int len = mTokens[0].length(); 65 len = len & ~1; 66 for (int i=0; i<len; i+=2) { 67 int c1 = mTokens[0].charAt(i); 68 int c2 = mTokens[0].charAt(i+1); 69 if (c1 >= 'a' && c1 <= 'f') c1 = c1 - 'a' + 10; 70 else if (c1 >= 'A' && c1 <= 'F') c1 = c1 - 'A' + 10; 71 else c1 -= '0'; 72 if (c2 >= 'a' && c2 <= 'f') c2 = c2 - 'a' + 10; 73 else if (c2 >= 'A' && c2 <= 'F') c2 = c2 - 'A' + 10; 74 else c2 -= '0'; 75 builder.append((char)(255-((c1*16)+c2))); 76 } 77 mText = builder.toString(); 78 if (false) { 79 Log.i(WindowManagerService.TAG, "Final text: " + mText); 80 } 81 82 int fontSize = WindowManagerService.getPropertyInt(tokens, 1, 83 TypedValue.COMPLEX_UNIT_DIP, 20, dm); 84 85 mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 86 mTextPaint.setTextSize(fontSize); 87 mTextPaint.setTypeface(Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD)); 88 89 FontMetricsInt fm = mTextPaint.getFontMetricsInt(); 90 mTextWidth = (int)mTextPaint.measureText(mText); 91 mTextAscent = fm.ascent; 92 mTextDescent = fm.descent; 93 mTextHeight = fm.descent - fm.ascent; 94 95 mDeltaX = WindowManagerService.getPropertyInt(tokens, 2, 96 TypedValue.COMPLEX_UNIT_PX, mTextWidth*2, dm); 97 mDeltaY = WindowManagerService.getPropertyInt(tokens, 3, 98 TypedValue.COMPLEX_UNIT_PX, mTextHeight*3, dm); 99 int shadowColor = WindowManagerService.getPropertyInt(tokens, 4, 100 TypedValue.COMPLEX_UNIT_PX, 0xb0000000, dm); 101 int color = WindowManagerService.getPropertyInt(tokens, 5, 102 TypedValue.COMPLEX_UNIT_PX, 0x60ffffff, dm); 103 int shadowRadius = WindowManagerService.getPropertyInt(tokens, 6, 104 TypedValue.COMPLEX_UNIT_PX, 7, dm); 105 int shadowDx = WindowManagerService.getPropertyInt(tokens, 8, 106 TypedValue.COMPLEX_UNIT_PX, 0, dm); 107 int shadowDy = WindowManagerService.getPropertyInt(tokens, 9, 108 TypedValue.COMPLEX_UNIT_PX, 0, dm); 109 110 mTextPaint.setColor(color); 111 mTextPaint.setShadowLayer(shadowRadius, shadowDx, shadowDy, shadowColor); 112 113 try { 114 mSurface = new Surface(session, 0, 115 "WatermarkSurface", -1, 1, 1, PixelFormat.TRANSLUCENT, 0); 116 mSurface.setLayer(WindowManagerService.TYPE_LAYER_MULTIPLIER*100); 117 mSurface.setPosition(0, 0); 118 mSurface.show(); 119 } catch (OutOfResourcesException e) { 120 } 121 } 122 123 void positionSurface(int dw, int dh) { 124 if (mLastDW != dw || mLastDH != dh) { 125 mLastDW = dw; 126 mLastDH = dh; 127 mSurface.setSize(dw, dh); 128 mDrawNeeded = true; 129 } 130 } 131 132 void drawIfNeeded() { 133 if (mDrawNeeded) { 134 final int dw = mLastDW; 135 final int dh = mLastDH; 136 137 mDrawNeeded = false; 138 Rect dirty = new Rect(0, 0, dw, dh); 139 Canvas c = null; 140 try { 141 c = mSurface.lockCanvas(dirty); 142 } catch (IllegalArgumentException e) { 143 } catch (OutOfResourcesException e) { 144 } 145 if (c != null) { 146 c.drawColor(0, PorterDuff.Mode.CLEAR); 147 148 int deltaX = mDeltaX; 149 int deltaY = mDeltaY; 150 151 // deltaX shouldn't be close to a round fraction of our 152 // x step, or else things will line up too much. 153 int div = (dw+mTextWidth)/deltaX; 154 int rem = (dw+mTextWidth) - (div*deltaX); 155 int qdelta = deltaX/4; 156 if (rem < qdelta || rem > (deltaX-qdelta)) { 157 deltaX += deltaX/3; 158 } 159 160 int y = -mTextHeight; 161 int x = -mTextWidth; 162 while (y < (dh+mTextHeight)) { 163 c.drawText(mText, x, y, mTextPaint); 164 x += deltaX; 165 if (x >= dw) { 166 x -= (dw+mTextWidth); 167 y += deltaY; 168 } 169 } 170 mSurface.unlockCanvasAndPost(c); 171 } 172 } 173 } 174}