This source file includes following definitions.
- getInputStream
- closeOutputStream
- shouldInterceptRequest
- getDefaultVideoPosterURL
- generateDefaulVideoPosterURL
package org.chromium.android_webview;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.util.Log;
import org.chromium.base.ThreadUtils;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.Random;
public class DefaultVideoPosterRequestHandler {
private static InputStream getInputStream(final AwContentsClient contentClient)
throws IOException {
final PipedInputStream inputStream = new PipedInputStream();
final PipedOutputStream outputStream = new PipedOutputStream(inputStream);
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
final Bitmap defaultVideoPoster = contentClient.getDefaultVideoPoster();
if (defaultVideoPoster == null) {
closeOutputStream(outputStream);
return;
}
AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() {
@Override
public void run() {
try {
defaultVideoPoster.compress(Bitmap.CompressFormat.PNG, 100,
outputStream);
outputStream.flush();
} catch (IOException e) {
Log.e(TAG, null, e);
} finally {
closeOutputStream(outputStream);
}
}
});
}
});
return inputStream;
}
private static void closeOutputStream(OutputStream outputStream) {
try {
outputStream.close();
} catch (IOException e) {
Log.e(TAG, null, e);
}
}
private static final String TAG = "DefaultVideoPosterRequestHandler";
private String mDefaultVideoPosterURL;
private AwContentsClient mContentClient;
public DefaultVideoPosterRequestHandler(AwContentsClient contentClient) {
mDefaultVideoPosterURL = generateDefaulVideoPosterURL();
mContentClient = contentClient;
}
public InterceptedRequestData shouldInterceptRequest(final String url) {
if (!mDefaultVideoPosterURL.equals(url)) return null;
try {
return new InterceptedRequestData("image/png", null, getInputStream(mContentClient));
} catch (IOException e) {
Log.e(TAG, null, e);
return null;
}
}
public String getDefaultVideoPosterURL() {
return mDefaultVideoPosterURL;
}
private static String generateDefaulVideoPosterURL() {
Random randomGenerator = new Random();
String path = String.valueOf(randomGenerator.nextLong());
return "android-webview-video-poster:default_video_poster/" + path;
}
}