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