1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.webkit;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.MediaController;
import android.widget.VideoView;
import java.util.HashMap;
/**
* <p>A View that displays Videos. Instances of this class
* are created on the WebCore thread. However, their code
* executes on the UI thread. Right now there is only one
* such view for fullscreen video rendering.
*
*/
class HTML5VideoViewProxy extends Handler {
// Logging tag.
private static final String LOGTAG = "HTML5VideoViewProxy";
// Message Ids
private static final int INIT = 100;
private static final int PLAY = 101;
// The singleton instance.
private static HTML5VideoViewProxy sInstance;
// The VideoView driven via this proxy.
private VideoView mVideoView;
// The context object used to initialize the VideoView and the
// MediaController.
private Context mContext;
/**
* Private constructor.
* @param context is the application context.
*/
private HTML5VideoViewProxy(Context context) {
// This handler is for the main (UI) thread.
super(Looper.getMainLooper());
// Save the context object.
mContext = context;
// Send a message to the UI thread to create the VideoView.
// This need to be done on the UI thread, or else the
// event Handlers used by the VideoView and MediaController
// will be attached to the wrong thread.
Message message = obtainMessage(INIT);
sendMessage(message);
}
@Override
public void handleMessage(Message msg) {
// This executes on the UI thread.
switch (msg.what) {
case INIT:
// Create the video view and set a default controller.
mVideoView = new VideoView(mContext);
mVideoView.setMediaController(new MediaController(mContext));
break;
case PLAY:
// Check if the fullscreen video view is currently playing.
// If it is, ignore the message.
if (!mVideoView.isPlaying()) {
HashMap<String, Object> map =
(HashMap<String, Object>) msg.obj;
String url = (String) map.get("url");
WebView webview = (WebView) map.get("webview");
WebChromeClient client = webview.getWebChromeClient();
if (client != null) {
mVideoView.setVideoURI(Uri.parse(url));
mVideoView.start();
client.onShowCustomView(mVideoView);
}
}
break;
}
}
/**
* Play a video stream.
* @param url is the URL of the video stream.
* @param webview is the WebViewCore that is requesting the playback.
*/
public void play(String url, WebViewCore webviewCore) {
// We need to know the webview that is requesting the playback.
Message message = obtainMessage(PLAY);
HashMap<String, Object> map = new HashMap();
map.put("url", url);
map.put("webview", webviewCore.getWebView());
message.obj = map;
sendMessage(message);
}
/**
* The factory for HTML5VideoViewProxy instances. Right now,
* it only produces a singleton.
* @param webViewCore is the WebViewCore that is requesting the proxy.
*
* @return the HTML5VideoViewProxy singleton.
*/
public static HTML5VideoViewProxy getInstance(WebViewCore webViewCore) {
if (sInstance == null) {
sInstance = new HTML5VideoViewProxy(webViewCore.getWebView().getContext());
}
return sInstance;
}
}
|