Merge "Add method to get redirected Uri"

This commit is contained in:
Marco Nelissen
2014-02-24 17:14:28 +00:00
committed by Android (Google) Code Review
2 changed files with 19 additions and 7 deletions

View File

@@ -29,5 +29,6 @@ interface IMediaHTTPConnection
int readAt(long offset, int size); int readAt(long offset, int size);
long getSize(); long getSize();
String getMIMEType(); String getMIMEType();
String getUri();
} }

View File

@@ -16,7 +16,6 @@
package android.media; package android.media;
import android.net.Uri;
import android.os.IBinder; import android.os.IBinder;
import android.os.StrictMode; import android.os.StrictMode;
import android.util.Log; import android.util.Log;
@@ -52,6 +51,7 @@ public class MediaHTTPConnection extends IMediaHTTPConnection.Stub {
native_setup(); native_setup();
} }
@Override
public IBinder connect(String uri, String headers) { public IBinder connect(String uri, String headers) {
if (VERBOSE) { if (VERBOSE) {
Log.d(TAG, "connect: uri=" + uri + ", headers=" + headers); Log.d(TAG, "connect: uri=" + uri + ", headers=" + headers);
@@ -85,6 +85,7 @@ public class MediaHTTPConnection extends IMediaHTTPConnection.Stub {
return map; return map;
} }
@Override
public void disconnect() { public void disconnect() {
teardownConnection(); teardownConnection();
mHeaders = null; mHeaders = null;
@@ -120,7 +121,11 @@ public class MediaHTTPConnection extends IMediaHTTPConnection.Stub {
"Range", "bytes=" + offset + "-"); "Range", "bytes=" + offset + "-");
} }
if (mConnection.getResponseCode() == HttpURLConnection.HTTP_PARTIAL) { int response = mConnection.getResponseCode();
// remember the current, possibly redirected URL
mURL = mConnection.getURL();
if (response == HttpURLConnection.HTTP_PARTIAL) {
// Partial content, we cannot just use getContentLength // Partial content, we cannot just use getContentLength
// because what we want is not just the length of the range // because what we want is not just the length of the range
// returned but the size of the full content if available. // returned but the size of the full content if available.
@@ -145,16 +150,13 @@ public class MediaHTTPConnection extends IMediaHTTPConnection.Stub {
} }
} }
} }
} else if (mConnection.getResponseCode() } else if (response != HttpURLConnection.HTTP_OK) {
!= HttpURLConnection.HTTP_OK) {
throw new IOException(); throw new IOException();
} else { } else {
mTotalSize = mConnection.getContentLength(); mTotalSize = mConnection.getContentLength();
} }
if (offset > 0 if (offset > 0 && response != HttpURLConnection.HTTP_PARTIAL) {
&& mConnection.getResponseCode()
!= HttpURLConnection.HTTP_PARTIAL) {
// Some servers simply ignore "Range" requests and serve // Some servers simply ignore "Range" requests and serve
// data from the start of the content. // data from the start of the content.
throw new IOException(); throw new IOException();
@@ -174,6 +176,7 @@ public class MediaHTTPConnection extends IMediaHTTPConnection.Stub {
} }
} }
@Override
public int readAt(long offset, int size) { public int readAt(long offset, int size) {
return native_readAt(offset, size); return native_readAt(offset, size);
} }
@@ -218,6 +221,7 @@ public class MediaHTTPConnection extends IMediaHTTPConnection.Stub {
} }
} }
@Override
public long getSize() { public long getSize() {
if (mConnection == null) { if (mConnection == null) {
try { try {
@@ -230,6 +234,7 @@ public class MediaHTTPConnection extends IMediaHTTPConnection.Stub {
return mTotalSize; return mTotalSize;
} }
@Override
public String getMIMEType() { public String getMIMEType() {
if (mConnection == null) { if (mConnection == null) {
try { try {
@@ -242,6 +247,11 @@ public class MediaHTTPConnection extends IMediaHTTPConnection.Stub {
return mConnection.getContentType(); return mConnection.getContentType();
} }
@Override
public String getUri() {
return mURL.toString();
}
@Override @Override
protected void finalize() { protected void finalize() {
native_finalize(); native_finalize();
@@ -260,4 +270,5 @@ public class MediaHTTPConnection extends IMediaHTTPConnection.Stub {
} }
private int mNativeContext; private int mNativeContext;
} }