OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.media; | 5 package org.chromium.media; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.media.MediaPlayer; | 8 import android.media.MediaPlayer; |
9 import android.net.Uri; | 9 import android.net.Uri; |
10 import android.text.TextUtils; | 10 import android.text.TextUtils; |
11 import android.util.Log; | |
11 | 12 |
12 import org.chromium.base.CalledByNative; | 13 import org.chromium.base.CalledByNative; |
13 import org.chromium.base.JNINamespace; | 14 import org.chromium.base.JNINamespace; |
14 | 15 |
16 import java.io.Serializable; | |
17 import java.lang.reflect.Method; | |
15 import java.util.HashMap; | 18 import java.util.HashMap; |
16 | 19 |
17 @JNINamespace("media") | 20 @JNINamespace("media") |
18 class MediaPlayerBridge { | 21 class MediaPlayerBridge { |
22 private static class AllowedOperations implements Serializable { | |
bulach
2013/03/14 09:59:07
is Serializable needed?
qinmin
2013/03/14 18:53:03
no, removed
On 2013/03/14 09:59:07, bulach wrote:
| |
23 private final boolean mCanPause; | |
24 private final boolean mCanSeekForward; | |
25 private final boolean mCanSeekBackward; | |
26 | |
27 private AllowedOperations(boolean canPause, boolean canSeekForward, | |
28 boolean canSeekBackward) { | |
29 mCanPause = canPause; | |
30 mCanSeekForward = canSeekForward; | |
31 mCanSeekBackward = canSeekBackward; | |
32 } | |
33 | |
34 @CalledByNative("AllowedOperations") | |
35 private boolean canPause() { return mCanPause; } | |
36 | |
37 @CalledByNative("AllowedOperations") | |
38 private boolean canSeekForward() { return mCanSeekForward; } | |
39 | |
40 @CalledByNative("AllowedOperations") | |
41 private boolean canSeekBackward() { return mCanSeekBackward; } | |
42 } | |
43 | |
19 @CalledByNative | 44 @CalledByNative |
20 private static boolean setDataSource(MediaPlayer player, Context context, St ring url, | 45 private static boolean setDataSource(MediaPlayer player, Context context, St ring url, |
21 String cookies, boolean hideUrlLog) { | 46 String cookies, boolean hideUrlLog) { |
22 Uri uri = Uri.parse(url); | 47 Uri uri = Uri.parse(url); |
23 HashMap<String, String> headersMap = new HashMap<String, String>(); | 48 HashMap<String, String> headersMap = new HashMap<String, String>(); |
24 if (hideUrlLog) | 49 if (hideUrlLog) |
25 headersMap.put("x-hide-urls-from-log", "true"); | 50 headersMap.put("x-hide-urls-from-log", "true"); |
26 if (!TextUtils.isEmpty(cookies)) | 51 if (!TextUtils.isEmpty(cookies)) |
27 headersMap.put("Cookie", cookies); | 52 headersMap.put("Cookie", cookies); |
28 try { | 53 try { |
29 player.setDataSource(context, uri, headersMap); | 54 player.setDataSource(context, uri, headersMap); |
30 return true; | 55 return true; |
31 } catch (Exception e) { | 56 } catch (Exception e) { |
32 return false; | 57 return false; |
33 } | 58 } |
34 } | 59 } |
60 | |
61 /** | |
62 * Returns an AllowedOperations object to show all the operations that are | |
63 * allowed on the media player. | |
64 */ | |
65 @CalledByNative | |
66 private static AllowedOperations getAllowedOperations(MediaPlayer player) { | |
67 boolean canPause = true; | |
68 boolean canSeekForward = true; | |
69 boolean canSeekBackward = true; | |
70 try { | |
71 Method getMetadata = player.getClass().getDeclaredMethod( | |
72 "getMetadata", boolean.class, boolean.class); | |
73 getMetadata.setAccessible(true); | |
74 Object data = getMetadata.invoke(player, false, false); | |
75 if (data != null) { | |
76 Class<?> metadataClass = data.getClass(); | |
77 Method hasMethod = metadataClass.getDeclaredMethod("has", int.cl ass); | |
78 Method getBooleanMethod = metadataClass.getDeclaredMethod("getBo olean", int.class); | |
79 | |
80 int pause = (Integer) metadataClass.getField("PAUSE_AVAILABLE"). get(null); | |
81 int seekForward = | |
82 (Integer) metadataClass.getField("SEEK_FORWARD_AVAILABLE").g et(null); | |
83 int seekBackward = | |
84 (Integer) metadataClass.getField("SEEK_BACKWARD_AVAILABL E").get(null); | |
85 hasMethod.setAccessible(true); | |
86 getBooleanMethod.setAccessible(true); | |
87 canPause = !((Boolean) hasMethod.invoke(data, pause)) | |
88 || ((Boolean) getBooleanMethod.invoke(data, pause)); | |
89 canSeekForward = !((Boolean) hasMethod.invoke(data, seekForward) ) | |
90 || ((Boolean) getBooleanMethod.invoke(data, seekForward) ); | |
91 canSeekBackward = !((Boolean) hasMethod.invoke(data, seekBackwar d)) | |
92 || ((Boolean) getBooleanMethod.invoke(data, seekBackward )); | |
93 } | |
94 } catch (Exception e) { | |
95 Log.e("MediaPlayerBridge", "Cannot get metadata from the player: " + e); | |
96 } | |
97 return new AllowedOperations(canPause, canSeekForward, canSeekBackward); | |
98 } | |
35 } | 99 } |
OLD | NEW |