blob: 171e575c1801b47e39487418ccf5ce552afa2ee2 [file] [log] [blame]
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001/*
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
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080019import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
20
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080021import android.graphics.Canvas;
22import android.graphics.Paint;
23import android.graphics.PixelFormat;
24import android.graphics.PorterDuff;
25import android.graphics.Rect;
26import android.graphics.Typeface;
27import android.graphics.Paint.FontMetricsInt;
28import android.util.DisplayMetrics;
29import android.util.Log;
30import android.util.TypedValue;
31import android.view.Display;
Igor Murashkina86ab6402013-08-30 12:58:36 -070032import android.view.Surface.OutOfResourcesException;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080033import android.view.Surface;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080034import android.view.SurfaceControl;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080035import android.view.SurfaceSession;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080036
37/**
38 * Displays a watermark on top of the window manager's windows.
39 */
40class Watermark {
Mathias Agopian3866f0d2013-02-11 22:08:48 -080041 private final Display mDisplay;
42 private final String[] mTokens;
43 private final String mText;
44 private final Paint mTextPaint;
45 private final int mTextWidth;
46 private final int mTextHeight;
47 private final int mDeltaX;
48 private final int mDeltaY;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080049
Mathias Agopian3866f0d2013-02-11 22:08:48 -080050 private final SurfaceControl mSurfaceControl;
51 private final Surface mSurface = new Surface();
52 private int mLastDW;
53 private int mLastDH;
54 private boolean mDrawNeeded;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080055
Jeff Brown98365d72012-08-19 20:30:52 -070056 Watermark(Display display, DisplayMetrics dm, SurfaceSession session, String[] tokens) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080057 if (false) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080058 Log.i(TAG_WM, "*********************** WATERMARK");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080059 for (int i=0; i<tokens.length; i++) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080060 Log.i(TAG_WM, " TOKEN #" + i + ": " + tokens[i]);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080061 }
62 }
63
Jeff Brown98365d72012-08-19 20:30:52 -070064 mDisplay = display;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080065 mTokens = tokens;
66
67 StringBuilder builder = new StringBuilder(32);
68 int len = mTokens[0].length();
69 len = len & ~1;
70 for (int i=0; i<len; i+=2) {
71 int c1 = mTokens[0].charAt(i);
72 int c2 = mTokens[0].charAt(i+1);
73 if (c1 >= 'a' && c1 <= 'f') c1 = c1 - 'a' + 10;
74 else if (c1 >= 'A' && c1 <= 'F') c1 = c1 - 'A' + 10;
75 else c1 -= '0';
76 if (c2 >= 'a' && c2 <= 'f') c2 = c2 - 'a' + 10;
77 else if (c2 >= 'A' && c2 <= 'F') c2 = c2 - 'A' + 10;
78 else c2 -= '0';
79 builder.append((char)(255-((c1*16)+c2)));
80 }
81 mText = builder.toString();
82 if (false) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080083 Log.i(TAG_WM, "Final text: " + mText);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080084 }
85
86 int fontSize = WindowManagerService.getPropertyInt(tokens, 1,
87 TypedValue.COMPLEX_UNIT_DIP, 20, dm);
88
Chris Craik6a49dde2015-05-12 10:28:14 -070089 mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080090 mTextPaint.setTextSize(fontSize);
91 mTextPaint.setTypeface(Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD));
92
93 FontMetricsInt fm = mTextPaint.getFontMetricsInt();
94 mTextWidth = (int)mTextPaint.measureText(mText);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080095 mTextHeight = fm.descent - fm.ascent;
96
97 mDeltaX = WindowManagerService.getPropertyInt(tokens, 2,
98 TypedValue.COMPLEX_UNIT_PX, mTextWidth*2, dm);
99 mDeltaY = WindowManagerService.getPropertyInt(tokens, 3,
100 TypedValue.COMPLEX_UNIT_PX, mTextHeight*3, dm);
101 int shadowColor = WindowManagerService.getPropertyInt(tokens, 4,
102 TypedValue.COMPLEX_UNIT_PX, 0xb0000000, dm);
103 int color = WindowManagerService.getPropertyInt(tokens, 5,
104 TypedValue.COMPLEX_UNIT_PX, 0x60ffffff, dm);
105 int shadowRadius = WindowManagerService.getPropertyInt(tokens, 6,
106 TypedValue.COMPLEX_UNIT_PX, 7, dm);
107 int shadowDx = WindowManagerService.getPropertyInt(tokens, 8,
108 TypedValue.COMPLEX_UNIT_PX, 0, dm);
109 int shadowDy = WindowManagerService.getPropertyInt(tokens, 9,
110 TypedValue.COMPLEX_UNIT_PX, 0, dm);
111
112 mTextPaint.setColor(color);
113 mTextPaint.setShadowLayer(shadowRadius, shadowDx, shadowDy, shadowColor);
114
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800115 SurfaceControl ctrl = null;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800116 try {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800117 ctrl = new SurfaceControl(session, "WatermarkSurface",
118 1, 1, PixelFormat.TRANSLUCENT, SurfaceControl.HIDDEN);
119 ctrl.setLayerStack(mDisplay.getLayerStack());
120 ctrl.setLayer(WindowManagerService.TYPE_LAYER_MULTIPLIER*100);
121 ctrl.setPosition(0, 0);
122 ctrl.show();
123 mSurface.copyFrom(ctrl);
Igor Murashkina86ab6402013-08-30 12:58:36 -0700124 } catch (OutOfResourcesException e) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800125 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800126 mSurfaceControl = ctrl;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800127 }
128
129 void positionSurface(int dw, int dh) {
130 if (mLastDW != dw || mLastDH != dh) {
131 mLastDW = dw;
132 mLastDH = dh;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800133 mSurfaceControl.setSize(dw, dh);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800134 mDrawNeeded = true;
135 }
136 }
137
138 void drawIfNeeded() {
139 if (mDrawNeeded) {
140 final int dw = mLastDW;
141 final int dh = mLastDH;
142
143 mDrawNeeded = false;
144 Rect dirty = new Rect(0, 0, dw, dh);
145 Canvas c = null;
146 try {
147 c = mSurface.lockCanvas(dirty);
148 } catch (IllegalArgumentException e) {
Igor Murashkina86ab6402013-08-30 12:58:36 -0700149 } catch (Surface.OutOfResourcesException e) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800150 }
151 if (c != null) {
152 c.drawColor(0, PorterDuff.Mode.CLEAR);
Igor Murashkina86ab6402013-08-30 12:58:36 -0700153
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800154 int deltaX = mDeltaX;
155 int deltaY = mDeltaY;
156
157 // deltaX shouldn't be close to a round fraction of our
158 // x step, or else things will line up too much.
159 int div = (dw+mTextWidth)/deltaX;
160 int rem = (dw+mTextWidth) - (div*deltaX);
161 int qdelta = deltaX/4;
162 if (rem < qdelta || rem > (deltaX-qdelta)) {
163 deltaX += deltaX/3;
164 }
165
166 int y = -mTextHeight;
167 int x = -mTextWidth;
168 while (y < (dh+mTextHeight)) {
169 c.drawText(mText, x, y, mTextPaint);
170 x += deltaX;
171 if (x >= dw) {
172 x -= (dw+mTextWidth);
173 y += deltaY;
174 }
175 }
176 mSurface.unlockCanvasAndPost(c);
177 }
178 }
179 }
Jeff Brown64a55af2012-08-26 02:47:39 -0700180}