StrictModeFlash.java revision 784707187d96b731df5256c1c2adb0aaf9037239
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 final DisplayMetrics dm = new DisplayMetrics(); 42 display.getRealMetrics(dm); 43 44 try { 45 mSurface = new Surface(session, 0, "StrictModeFlash", -1, 1, 1, PixelFormat.TRANSLUCENT, 0); 46 } catch (Surface.OutOfResourcesException e) { 47 return; 48 } 49 50 mSurface.setLayer(WindowManagerService.TYPE_LAYER_MULTIPLIER * 101); // one more than Watermark? arbitrary. 51 mSurface.setPosition(0, 0); 52 mDrawNeeded = true; 53 } 54 55 private void drawIfNeeded() { 56 if (!mDrawNeeded) { 57 return; 58 } 59 mDrawNeeded = false; 60 final int dw = mLastDW; 61 final int dh = mLastDH; 62 63 Rect dirty = new Rect(0, 0, dw, dh); 64 Canvas c = null; 65 try { 66 c = mSurface.lockCanvas(dirty); 67 } catch (IllegalArgumentException e) { 68 } catch (Surface.OutOfResourcesException e) { 69 } 70 if (c == null) { 71 return; 72 } 73 74 // Top 75 c.clipRect(new Rect(0, 0, dw, mThickness), Region.Op.REPLACE); 76 c.drawColor(Color.RED); 77 // Left 78 c.clipRect(new Rect(0, 0, mThickness, dh), Region.Op.REPLACE); 79 c.drawColor(Color.RED); 80 // Right 81 c.clipRect(new Rect(dw - mThickness, 0, dw, dh), Region.Op.REPLACE); 82 c.drawColor(Color.RED); 83 // Bottom 84 c.clipRect(new Rect(0, dh - mThickness, dw, dh), Region.Op.REPLACE); 85 c.drawColor(Color.RED); 86 87 mSurface.unlockCanvasAndPost(c); 88 } 89 90 // Note: caller responsible for being inside 91 // Surface.openTransaction() / closeTransaction() 92 public void setVisibility(boolean on) { 93 if (mSurface == null) { 94 return; 95 } 96 drawIfNeeded(); 97 if (on) { 98 mSurface.show(); 99 } else { 100 mSurface.hide(); 101 } 102 } 103 104 void positionSurface(int dw, int dh) { 105 if (mLastDW == dw && mLastDH == dh) { 106 return; 107 } 108 mLastDW = dw; 109 mLastDH = dh; 110 mSurface.setSize(dw, dh); 111 mDrawNeeded = true; 112 } 113 114} 115