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 Display mDisplay;
39    final String[] mTokens;
40    final String mText;
41    final Paint mTextPaint;
42    final int mTextWidth;
43    final int mTextHeight;
44    final int mTextAscent;
45    final int mTextDescent;
46    final int mDeltaX;
47    final int mDeltaY;
48
49    Surface mSurface;
50    int mLastDW;
51    int mLastDH;
52    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        mTextAscent = fm.ascent;
94        mTextDescent = fm.descent;
95        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
115        try {
116            mSurface = new Surface(session, "WatermarkSurface",
117                    1, 1, PixelFormat.TRANSLUCENT, Surface.HIDDEN);
118            mSurface.setLayerStack(mDisplay.getLayerStack());
119            mSurface.setLayer(WindowManagerService.TYPE_LAYER_MULTIPLIER*100);
120            mSurface.setPosition(0, 0);
121            mSurface.show();
122        } catch (OutOfResourcesException e) {
123        }
124    }
125
126    void positionSurface(int dw, int dh) {
127        if (mLastDW != dw || mLastDH != dh) {
128            mLastDW = dw;
129            mLastDH = dh;
130            mSurface.setSize(dw, dh);
131            mDrawNeeded = true;
132        }
133    }
134
135    void drawIfNeeded() {
136        if (mDrawNeeded) {
137            final int dw = mLastDW;
138            final int dh = mLastDH;
139
140            mDrawNeeded = false;
141            Rect dirty = new Rect(0, 0, dw, dh);
142            Canvas c = null;
143            try {
144                c = mSurface.lockCanvas(dirty);
145            } catch (IllegalArgumentException e) {
146            } catch (OutOfResourcesException e) {
147            }
148            if (c != null) {
149                c.drawColor(0, PorterDuff.Mode.CLEAR);
150
151                int deltaX = mDeltaX;
152                int deltaY = mDeltaY;
153
154                // deltaX shouldn't be close to a round fraction of our
155                // x step, or else things will line up too much.
156                int div = (dw+mTextWidth)/deltaX;
157                int rem = (dw+mTextWidth) - (div*deltaX);
158                int qdelta = deltaX/4;
159                if (rem < qdelta || rem > (deltaX-qdelta)) {
160                    deltaX += deltaX/3;
161                }
162
163                int y = -mTextHeight;
164                int x = -mTextWidth;
165                while (y < (dh+mTextHeight)) {
166                    c.drawText(mText, x, y, mTextPaint);
167                    x += deltaX;
168                    if (x >= dw) {
169                        x -= (dw+mTextWidth);
170                        y += deltaY;
171                    }
172                }
173                mSurface.unlockCanvasAndPost(c);
174            }
175        }
176    }
177}
178