View difference between Paste ID: eSyZ3v3P and PbdaJ4h4
SHOW: | | - or go back to the newest paste.
1
import java.io.IOException;
2
import android.annotation.SuppressLint;
3
import android.content.Context;
4
import android.graphics.Canvas;
5
import android.graphics.Color;
6
import android.hardware.Camera;
7
import android.util.Log;
8
import android.view.SurfaceHolder;
9
import android.view.SurfaceView;
10
import android.view.ViewGroup;
11
import android.webkit.WebView;
12
13
import com.MyCustomContext;
14
15
16
public class CameraPluginView extends ViewGroup implements SurfaceHolder.Callback {
17
	private final String TAG = "camera.CameraPluginView";
18
	
19
	MyCustomContext customContext;
20
	SurfaceView surfaceView;
21
	public WebView overlayWebView;
22
23
	Camera camera;
24
	
25
	public CameraPluginView(final MyCustomContext customContext){
26
		super(customContext);
27
		this.setBackgroundColor(Color.WHITE);
28
		this.customContext = customContext;
29-
		this.surfaceView = new SurfaceView(context);
29+
		this.surfaceView = new SurfaceView(customContext);
30
		this.surfaceView.getHolder().addCallback(this);
31
		this.addView(this.surfaceView);
32
		context.getActivity().runOnUiThread(new Runnable() {
33
			@SuppressLint("SetJavaScriptEnabled")
34
			public void run(){
35
				addView(surfaceView);
36-
				overlayWebView = new OverlayWebView(context);
36+
				overlayWebView = new OverlayWebView(customContext);
37
				overlayWebView.getSettings().setJavaScriptEnabled(true);
38
				overlayWebView.getSettings().setDatabaseEnabled(true);
39
				overlayWebView.addJavascriptInterface(
40
					new Object() {
41
						@SuppressWarnings("unused")
42
						public void callParentView(final String js){
43
							context.getActivity().runOnUiThread(new Runnable(){
44
								public void run(){
45
									context.loadUrl("javascript:" + js);
46
								}
47
							});
48
						}
49
					},
50
					"root"
51
				);
52
				overlayWebView.setBackgroundColor(0 << 24);
53
				overlayWebView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
54
				addView(overlayWebView);
55
			}
56
		});
57
	}
58
	
59
	public CameraPluginView(Context context){
60
		super(context);
61
	}
62
	
63
	public void setCamera(Camera camera){
64
		this.camera = camera;
65
		if(camera != null){
66
			try {
67
				this.camera.setPreviewDisplay(this.surfaceView.getHolder());
68
				this.camera.startPreview();
69
			} catch (IOException e) {
70
				e.printStackTrace();
71
			}
72
			this.requestLayout();
73
		}
74
	}
75
76
	@Override
77
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
78
		if(this.camera != null){
79
			this.setCamera(this.camera);
80
		}
81
	}
82
83
	@Override
84
	public void surfaceCreated(SurfaceHolder holder) {
85
		if(this.camera != null){
86
			this.setCamera(this.camera);
87
		}
88
	}
89
90
	@Override
91
	protected void onLayout(boolean changed, int l, int t, int r, int b){
92
		int width = r - l;
93
		int height = b - t;
94
		this.getChildAt(0).layout(0, 0, width, height);
95
		if(this.getChildCount() > 1){
96
			this.getChildAt(1).layout(0, 0, width, height);
97
		}
98
	}
99
}
100
101
class OverlayWebView extends WebView {
102
	
103
	public OverlayWebView(Context context) {
104
		super(context);
105
	}
106
	
107
	@Override
108
    protected void onDraw(Canvas canvas) {
109
		super.onDraw(canvas);
110
		invalidate(); // Invalidate all the time so there is no lag on display.
111
	}
112
	
113
}