blob: 973cb0103a3a03e269efb05b199c7eaa3586d0a2 [file] [log] [blame]
Andrei Popescu6fa29582009-06-19 14:54:09 +01001/*
2 * Copyright (C) 2009 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 android.webkit;
18
19import android.content.Context;
20import android.net.Uri;
21import android.os.Bundle;
22import android.os.Handler;
23import android.os.Looper;
24import android.os.Message;
25import android.util.Log;
26import android.view.View;
27import android.widget.MediaController;
28import android.widget.VideoView;
29
30import java.util.HashMap;
31
32/**
33 * <p>A View that displays Videos. Instances of this class
34 * are created on the WebCore thread. However, their code
35 * executes on the UI thread. Right now there is only one
36 * such view for fullscreen video rendering.
37 *
38 */
39class HTML5VideoViewProxy extends Handler {
40 // Logging tag.
41 private static final String LOGTAG = "HTML5VideoViewProxy";
42
43 // Message Ids
44 private static final int INIT = 100;
45 private static final int PLAY = 101;
46
47 // The singleton instance.
48 private static HTML5VideoViewProxy sInstance;
49 // The VideoView driven via this proxy.
50 private VideoView mVideoView;
51 // The context object used to initialize the VideoView and the
52 // MediaController.
53 private Context mContext;
54
55 /**
56 * Private constructor.
57 * @param context is the application context.
58 */
59 private HTML5VideoViewProxy(Context context) {
60 // This handler is for the main (UI) thread.
61 super(Looper.getMainLooper());
62 // Save the context object.
63 mContext = context;
64 // Send a message to the UI thread to create the VideoView.
65 // This need to be done on the UI thread, or else the
66 // event Handlers used by the VideoView and MediaController
67 // will be attached to the wrong thread.
68 Message message = obtainMessage(INIT);
69 sendMessage(message);
70 }
71
72 @Override
73 public void handleMessage(Message msg) {
74 // This executes on the UI thread.
75 switch (msg.what) {
76 case INIT:
77 // Create the video view and set a default controller.
78 mVideoView = new VideoView(mContext);
79 mVideoView.setMediaController(new MediaController(mContext));
80 break;
81 case PLAY:
82 // Check if the fullscreen video view is currently playing.
83 // If it is, ignore the message.
84 if (!mVideoView.isPlaying()) {
85 HashMap<String, Object> map =
86 (HashMap<String, Object>) msg.obj;
87 String url = (String) map.get("url");
88 WebView webview = (WebView) map.get("webview");
89 WebChromeClient client = webview.getWebChromeClient();
90 if (client != null) {
91 mVideoView.setVideoURI(Uri.parse(url));
92 mVideoView.start();
93 client.onShowCustomView(mVideoView);
94 }
95 }
96 break;
97 }
98 }
99
100 /**
101 * Play a video stream.
102 * @param url is the URL of the video stream.
103 * @param webview is the WebViewCore that is requesting the playback.
104 */
105 public void play(String url, WebViewCore webviewCore) {
106 // We need to know the webview that is requesting the playback.
107 Message message = obtainMessage(PLAY);
108 HashMap<String, Object> map = new HashMap();
109 map.put("url", url);
110 map.put("webview", webviewCore.getWebView());
111 message.obj = map;
112 sendMessage(message);
113 }
114
115 /**
116 * The factory for HTML5VideoViewProxy instances. Right now,
117 * it only produces a singleton.
118 * @param webViewCore is the WebViewCore that is requesting the proxy.
119 *
120 * @return the HTML5VideoViewProxy singleton.
121 */
122 public static HTML5VideoViewProxy getInstance(WebViewCore webViewCore) {
123 if (sInstance == null) {
124 sInstance = new HTML5VideoViewProxy(webViewCore.getWebView().getContext());
125 }
126 return sInstance;
127 }
128}