blob: 5b2c0e2c2fc055bcd2c4796e11c01e20dad507c6 [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;
Patrick Scott0a5ce012009-07-02 08:56:10 -040027import android.webkit.ViewManager.ChildView;
Andrei Popescu6fa29582009-06-19 14:54:09 +010028import android.widget.MediaController;
29import android.widget.VideoView;
30
31import java.util.HashMap;
32
33/**
34 * <p>A View that displays Videos. Instances of this class
35 * are created on the WebCore thread. However, their code
36 * executes on the UI thread. Right now there is only one
37 * such view for fullscreen video rendering.
38 *
39 */
40class HTML5VideoViewProxy extends Handler {
41 // Logging tag.
42 private static final String LOGTAG = "HTML5VideoViewProxy";
43
44 // Message Ids
45 private static final int INIT = 100;
46 private static final int PLAY = 101;
47
48 // The singleton instance.
49 private static HTML5VideoViewProxy sInstance;
Andrei Popescu6fa29582009-06-19 14:54:09 +010050 // The context object used to initialize the VideoView and the
51 // MediaController.
52 private Context mContext;
53
54 /**
55 * Private constructor.
56 * @param context is the application context.
57 */
58 private HTML5VideoViewProxy(Context context) {
59 // This handler is for the main (UI) thread.
60 super(Looper.getMainLooper());
61 // Save the context object.
62 mContext = context;
Andrei Popescu6fa29582009-06-19 14:54:09 +010063 }
64
65 @Override
66 public void handleMessage(Message msg) {
67 // This executes on the UI thread.
68 switch (msg.what) {
69 case INIT:
Patrick Scott0a5ce012009-07-02 08:56:10 -040070 ChildView child = (ChildView) msg.obj;
Andrei Popescu6fa29582009-06-19 14:54:09 +010071 // Create the video view and set a default controller.
Patrick Scott0a5ce012009-07-02 08:56:10 -040072 VideoView v = new VideoView(mContext);
73 // This is needed because otherwise there will be a black square
74 // stuck on the screen.
75 v.setWillNotDraw(false);
76 v.setMediaController(new MediaController(mContext));
77 child.mView = v;
Andrei Popescu6fa29582009-06-19 14:54:09 +010078 break;
79 case PLAY:
Patrick Scott0a5ce012009-07-02 08:56:10 -040080 HashMap<String, Object> map =
Andrei Popescu6fa29582009-06-19 14:54:09 +010081 (HashMap<String, Object>) msg.obj;
Patrick Scott0a5ce012009-07-02 08:56:10 -040082 String url = (String) map.get("url");
83 VideoView view = (VideoView) map.get("view");
84 view.setVideoURI(Uri.parse(url));
85 view.start();
Andrei Popescu6fa29582009-06-19 14:54:09 +010086 break;
87 }
88 }
89
90 /**
91 * Play a video stream.
92 * @param url is the URL of the video stream.
93 * @param webview is the WebViewCore that is requesting the playback.
94 */
Patrick Scott0a5ce012009-07-02 08:56:10 -040095 public void play(String url, ChildView child) {
Andrei Popescu6fa29582009-06-19 14:54:09 +010096 // We need to know the webview that is requesting the playback.
97 Message message = obtainMessage(PLAY);
98 HashMap<String, Object> map = new HashMap();
99 map.put("url", url);
Patrick Scott0a5ce012009-07-02 08:56:10 -0400100 map.put("view", child.mView);
Andrei Popescu6fa29582009-06-19 14:54:09 +0100101 message.obj = map;
102 sendMessage(message);
103 }
104
Patrick Scott0a5ce012009-07-02 08:56:10 -0400105 public ChildView createView(WebViewCore core) {
106 WebView w = core.getWebView();
107 if (w == null) {
108 return null;
109 }
110 ChildView child = w.mViewManager.createView();
111 sendMessage(obtainMessage(INIT, child));
112 return child;
113 }
114
115 public void attachView(ChildView child, int x, int y, int width,
116 int height) {
117 child.attachView(x, y, width, height);
118 }
119
120 public void removeView(ChildView child) {
121 child.removeView();
122 }
123
Andrei Popescu6fa29582009-06-19 14:54:09 +0100124 /**
125 * The factory for HTML5VideoViewProxy instances. Right now,
126 * it only produces a singleton.
127 * @param webViewCore is the WebViewCore that is requesting the proxy.
128 *
129 * @return the HTML5VideoViewProxy singleton.
130 */
131 public static HTML5VideoViewProxy getInstance(WebViewCore webViewCore) {
132 if (sInstance == null) {
133 sInstance = new HTML5VideoViewProxy(webViewCore.getWebView().getContext());
134 }
135 return sInstance;
136 }
137}