StrictModeFlash.java revision bc68a59c024bdb745dac8e2ec7408a9f30595f1a
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
17package com.android.server.wm;
18
19
20import android.graphics.Canvas;
21import android.graphics.Color;
22import android.graphics.PixelFormat;
23import android.graphics.Rect;
24import android.graphics.Region;
25import android.util.DisplayMetrics;
26import android.util.Slog;
27import android.view.Display;
28import android.view.Surface;
29import android.view.SurfaceSession;
30
31class StrictModeFlash {
32    private static final String TAG = "StrictModeFlash";
33
34    Surface mSurface;
35    int mLastDW;
36    int mLastDH;
37    boolean mDrawNeeded;
38    final int mThickness = 20;
39
40    public StrictModeFlash(Display display, SurfaceSession session) {
41        try {
42            mSurface = new Surface(session, 0, "StrictModeFlash", -1, 1, 1, PixelFormat.TRANSLUCENT, 0);
43        } catch (Surface.OutOfResourcesException e) {
44            return;
45        }
46
47        mSurface.setLayer(WindowManagerService.TYPE_LAYER_MULTIPLIER * 101);  // one more than Watermark? arbitrary.
48        mSurface.setPosition(0, 0);
49        mDrawNeeded = true;
50    }
51
52    private void drawIfNeeded() {
53        if (!mDrawNeeded) {
54            return;
55        }
56        mDrawNeeded = false;
57        final int dw = mLastDW;
58        final int dh = mLastDH;
59
60        Rect dirty = new Rect(0, 0, dw, dh);
61        Canvas c = null;
62        try {
63            c = mSurface.lockCanvas(dirty);
64        } catch (IllegalArgumentException e) {
65        } catch (Surface.OutOfResourcesException e) {
66        }
67        if (c == null) {
68            return;
69        }
70
71        // Top
72        c.clipRect(new Rect(0, 0, dw, mThickness), Region.Op.REPLACE);
73        c.drawColor(Color.RED);
74        // Left
75        c.clipRect(new Rect(0, 0, mThickness, dh), Region.Op.REPLACE);
76        c.drawColor(Color.RED);
77        // Right
78        c.clipRect(new Rect(dw - mThickness, 0, dw, dh), Region.Op.REPLACE);
79        c.drawColor(Color.RED);
80        // Bottom
81        c.clipRect(new Rect(0, dh - mThickness, dw, dh), Region.Op.REPLACE);
82        c.drawColor(Color.RED);
83
84        mSurface.unlockCanvasAndPost(c);
85    }
86
87    // Note: caller responsible for being inside
88    // Surface.openTransaction() / closeTransaction()
89    public void setVisibility(boolean on) {
90        if (mSurface == null) {
91            return;
92        }
93        drawIfNeeded();
94        if (on) {
95            mSurface.show();
96        } else {
97            mSurface.hide();
98        }
99    }
100
101    void positionSurface(int dw, int dh) {
102        if (mLastDW == dw && mLastDH == dh) {
103            return;
104        }
105        mLastDW = dw;
106        mLastDH = dh;
107        mSurface.setSize(dw, dh);
108        mDrawNeeded = true;
109    }
110
111}
112