Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.webkit; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.net.Uri; |
| 21 | import android.os.Bundle; |
| 22 | import android.os.Handler; |
| 23 | import android.os.Looper; |
| 24 | import android.os.Message; |
| 25 | import android.util.Log; |
| 26 | import android.view.View; |
Patrick Scott | 0a5ce01 | 2009-07-02 08:56:10 -0400 | [diff] [blame^] | 27 | import android.webkit.ViewManager.ChildView; |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 28 | import android.widget.MediaController; |
| 29 | import android.widget.VideoView; |
| 30 | |
| 31 | import 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 | */ |
| 40 | class 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 Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 50 | // 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 Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public void handleMessage(Message msg) { |
| 67 | // This executes on the UI thread. |
| 68 | switch (msg.what) { |
| 69 | case INIT: |
Patrick Scott | 0a5ce01 | 2009-07-02 08:56:10 -0400 | [diff] [blame^] | 70 | ChildView child = (ChildView) msg.obj; |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 71 | // Create the video view and set a default controller. |
Patrick Scott | 0a5ce01 | 2009-07-02 08:56:10 -0400 | [diff] [blame^] | 72 | 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 Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 78 | break; |
| 79 | case PLAY: |
Patrick Scott | 0a5ce01 | 2009-07-02 08:56:10 -0400 | [diff] [blame^] | 80 | HashMap<String, Object> map = |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 81 | (HashMap<String, Object>) msg.obj; |
Patrick Scott | 0a5ce01 | 2009-07-02 08:56:10 -0400 | [diff] [blame^] | 82 | String url = (String) map.get("url"); |
| 83 | VideoView view = (VideoView) map.get("view"); |
| 84 | view.setVideoURI(Uri.parse(url)); |
| 85 | view.start(); |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 86 | 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 Scott | 0a5ce01 | 2009-07-02 08:56:10 -0400 | [diff] [blame^] | 95 | public void play(String url, ChildView child) { |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 96 | // 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 Scott | 0a5ce01 | 2009-07-02 08:56:10 -0400 | [diff] [blame^] | 100 | map.put("view", child.mView); |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 101 | message.obj = map; |
| 102 | sendMessage(message); |
| 103 | } |
| 104 | |
Patrick Scott | 0a5ce01 | 2009-07-02 08:56:10 -0400 | [diff] [blame^] | 105 | 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 Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 124 | /** |
| 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 | } |