1 /* 2 * Copyright (C) 2012 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.camera.ui; 18 19 import android.content.Context; 20 import android.graphics.Canvas; 21 import android.util.AttributeSet; 22 import android.view.MotionEvent; 23 import android.view.View; 24 import android.widget.FrameLayout; 25 26 import java.util.ArrayList; 27 import java.util.List; 28 29 public class RenderOverlay extends FrameLayout { 30 31 private static final String TAG = "CAM_Overlay"; 32 33 interface Renderer { 34 handlesTouch()35 public boolean handlesTouch(); onTouchEvent(MotionEvent evt)36 public boolean onTouchEvent(MotionEvent evt); setOverlay(RenderOverlay overlay)37 public void setOverlay(RenderOverlay overlay); layout(int left, int top, int right, int bottom)38 public void layout(int left, int top, int right, int bottom); draw(Canvas canvas)39 public void draw(Canvas canvas); 40 41 } 42 43 private RenderView mRenderView; 44 private List<Renderer> mClients; 45 46 // reverse list of touch clients 47 private List<Renderer> mTouchClients; 48 private int[] mPosition = new int[2]; 49 RenderOverlay(Context context, AttributeSet attrs)50 public RenderOverlay(Context context, AttributeSet attrs) { 51 super(context, attrs); 52 mRenderView = new RenderView(context); 53 addView(mRenderView, new LayoutParams(LayoutParams.MATCH_PARENT, 54 LayoutParams.MATCH_PARENT)); 55 mClients = new ArrayList<Renderer>(10); 56 mTouchClients = new ArrayList<Renderer>(10); 57 setWillNotDraw(false); 58 } 59 addRenderer(Renderer renderer)60 public void addRenderer(Renderer renderer) { 61 mClients.add(renderer); 62 renderer.setOverlay(this); 63 if (renderer.handlesTouch()) { 64 mTouchClients.add(0, renderer); 65 } 66 renderer.layout(getLeft(), getTop(), getRight(), getBottom()); 67 } 68 addRenderer(int pos, Renderer renderer)69 public void addRenderer(int pos, Renderer renderer) { 70 mClients.add(pos, renderer); 71 renderer.setOverlay(this); 72 renderer.layout(getLeft(), getTop(), getRight(), getBottom()); 73 } 74 remove(Renderer renderer)75 public void remove(Renderer renderer) { 76 mClients.remove(renderer); 77 renderer.setOverlay(null); 78 } 79 getClientSize()80 public int getClientSize() { 81 return mClients.size(); 82 } 83 84 @Override dispatchTouchEvent(MotionEvent m)85 public boolean dispatchTouchEvent(MotionEvent m) { 86 return false; 87 } 88 directDispatchTouch(MotionEvent m, Renderer target)89 public boolean directDispatchTouch(MotionEvent m, Renderer target) { 90 mRenderView.setTouchTarget(target); 91 boolean res = super.dispatchTouchEvent(m); 92 mRenderView.setTouchTarget(null); 93 return res; 94 } 95 adjustPosition()96 private void adjustPosition() { 97 getLocationInWindow(mPosition); 98 } 99 getWindowPositionX()100 public int getWindowPositionX() { 101 return mPosition[0]; 102 } 103 getWindowPositionY()104 public int getWindowPositionY() { 105 return mPosition[1]; 106 } 107 update()108 public void update() { 109 mRenderView.invalidate(); 110 } 111 112 private class RenderView extends View { 113 114 private Renderer mTouchTarget; 115 RenderView(Context context)116 public RenderView(Context context) { 117 super(context); 118 setWillNotDraw(false); 119 } 120 setTouchTarget(Renderer target)121 public void setTouchTarget(Renderer target) { 122 mTouchTarget = target; 123 } 124 125 @Override onTouchEvent(MotionEvent evt)126 public boolean onTouchEvent(MotionEvent evt) { 127 if (mTouchTarget != null) { 128 return mTouchTarget.onTouchEvent(evt); 129 } 130 if (mTouchClients != null) { 131 boolean res = false; 132 for (Renderer client : mTouchClients) { 133 res |= client.onTouchEvent(evt); 134 } 135 return res; 136 } 137 return false; 138 } 139 140 @Override onLayout(boolean changed, int left, int top, int right, int bottom)141 public void onLayout(boolean changed, int left, int top, int right, int bottom) { 142 adjustPosition(); 143 super.onLayout(changed, left, top, right, bottom); 144 if (mClients == null) return; 145 for (Renderer renderer : mClients) { 146 renderer.layout(left, top, right, bottom); 147 } 148 } 149 150 @Override draw(Canvas canvas)151 public void draw(Canvas canvas) { 152 super.draw(canvas); 153 if (mClients == null) return; 154 boolean redraw = false; 155 for (Renderer renderer : mClients) { 156 renderer.draw(canvas); 157 redraw = redraw || ((OverlayRenderer) renderer).isVisible(); 158 } 159 if (redraw) { 160 invalidate(); 161 } 162 } 163 } 164 165 } 166