Chromium Code Reviews| 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.lang.reflect.Method; | |
| 15 import java.util.HashMap; | 17 import java.util.HashMap; |
| 16 | 18 |
| 17 @JNINamespace("media") | 19 @JNINamespace("media") |
| 18 class MediaPlayerBridge { | 20 class MediaPlayerBridge { |
| 19 @CalledByNative | 21 @CalledByNative |
| 20 private static boolean setDataSource(MediaPlayer player, Context context, St ring url, | 22 private static boolean setDataSource(MediaPlayer player, Context context, St ring url, |
| 21 String cookies, boolean hideUrlLog) { | 23 String cookies, boolean hideUrlLog) { |
| 22 Uri uri = Uri.parse(url); | 24 Uri uri = Uri.parse(url); |
| 23 HashMap<String, String> headersMap = new HashMap<String, String>(); | 25 HashMap<String, String> headersMap = new HashMap<String, String>(); |
| 24 if (hideUrlLog) | 26 if (hideUrlLog) |
| 25 headersMap.put("x-hide-urls-from-log", "true"); | 27 headersMap.put("x-hide-urls-from-log", "true"); |
| 26 if (!TextUtils.isEmpty(cookies)) | 28 if (!TextUtils.isEmpty(cookies)) |
| 27 headersMap.put("Cookie", cookies); | 29 headersMap.put("Cookie", cookies); |
| 28 try { | 30 try { |
| 29 player.setDataSource(context, uri, headersMap); | 31 player.setDataSource(context, uri, headersMap); |
| 30 return true; | 32 return true; |
| 31 } catch (Exception e) { | 33 } catch (Exception e) { |
| 32 return false; | 34 return false; |
| 33 } | 35 } |
| 34 } | 36 } |
| 37 | |
| 38 /** | |
| 39 * Returns an array of booleans for allowed operations on the media player. | |
| 40 * The first boolean shows whether the player can be paused. | |
| 41 * The 2nd boolean shows whether the player can seek forward. | |
| 42 * The 3rd boolean shows whether the player can seek backward. | |
|
bulach
2013/03/13 15:06:10
the knowledge of which boolean is what is now dist
qinmin
2013/03/13 18:26:58
Done.
| |
| 43 */ | |
| 44 @CalledByNative | |
| 45 private static boolean[] getMetadata(MediaPlayer player) { | |
| 46 boolean ret[] = { true, true, true }; | |
| 47 try { | |
| 48 Method getMetadata = player.getClass().getDeclaredMethod( | |
| 49 "getMetadata", boolean.class, boolean.class); | |
| 50 getMetadata.setAccessible(true); | |
| 51 Object data = getMetadata.invoke(player, false, false); | |
| 52 if (data != null) { | |
| 53 Class<?> metadataClass = data.getClass(); | |
| 54 Method hasMethod = metadataClass.getDeclaredMethod("has", int.cl ass); | |
| 55 Method getBooleanMethod = metadataClass.getDeclaredMethod("getBo olean", int.class); | |
| 56 | |
| 57 int pause = (Integer) metadataClass.getField("PAUSE_AVAILABLE"). get(null); | |
| 58 int seekForward = | |
| 59 (Integer) metadataClass.getField("SEEK_FORWARD_AVAILABLE").g et(null); | |
| 60 int seekBackward = | |
| 61 (Integer) metadataClass.getField("SEEK_BACKWARD_AVAILABL E").get(null); | |
| 62 hasMethod.setAccessible(true); | |
| 63 getBooleanMethod.setAccessible(true); | |
| 64 ret[0] = !((Boolean) hasMethod.invoke(data, pause)) | |
| 65 || ((Boolean) getBooleanMethod.invoke(data, pause)); | |
| 66 ret[1] = !((Boolean) hasMethod.invoke(data, seekForward)) | |
| 67 || ((Boolean) getBooleanMethod.invoke(data, seekForward )); | |
| 68 ret[2] = !((Boolean) hasMethod.invoke(data, seekBackward)) | |
| 69 || ((Boolean) getBooleanMethod.invoke(data, seekBackwar d)); | |
| 70 } | |
| 71 } catch (Exception e) { | |
| 72 Log.e("MediaPlayerBridge", "Cannot get metadata from the player: " + e); | |
| 73 } | |
| 74 return ret; | |
| 75 } | |
| 35 } | 76 } |
| OLD | NEW |