| Index: net/cronet/android/sample/src/org/chromium/cronet_sample_apk/CronetSampleActivity.java
|
| diff --git a/net/cronet/android/sample/src/org/chromium/cronet_sample_apk/CronetSampleActivity.java b/net/cronet/android/sample/src/org/chromium/cronet_sample_apk/CronetSampleActivity.java
|
| index 3adce6749ef8eaaf56eb7a87a8723c98647add27..bfee19541337cbde7bc1fb36cdfb214d9452630d 100644
|
| --- a/net/cronet/android/sample/src/org/chromium/cronet_sample_apk/CronetSampleActivity.java
|
| +++ b/net/cronet/android/sample/src/org/chromium/cronet_sample_apk/CronetSampleActivity.java
|
| @@ -13,6 +13,9 @@ import android.util.Log;
|
| import android.widget.EditText;
|
| import android.widget.Toast;
|
|
|
| +import org.chromium.net.HttpUrlRequest;
|
| +import org.chromium.net.HttpUrlRequestFactory;
|
| +import org.chromium.net.HttpUrlRequestListener;
|
| import org.chromium.net.UrlRequest;
|
| import org.chromium.net.UrlRequestContext;
|
|
|
| @@ -29,144 +32,185 @@ import java.util.Map;
|
| * Activity for managing the Cronet Sample.
|
| */
|
| public class CronetSampleActivity extends Activity {
|
| - private static final String TAG = "CronetSampleActivity";
|
| - public static final String COMMAND_LINE_ARGS_KEY = "commandLineArgs";
|
| - public static final String POST_DATA_KEY = "postData";
|
| -
|
| - UrlRequestContext mRequestContext;
|
| - String mUrl;
|
| - boolean mLoading = false;
|
| - int mHttpStatusCode = 0;
|
| -
|
| - class SampleRequestContext extends UrlRequestContext {
|
| - public SampleRequestContext() {
|
| - super(getApplicationContext(),
|
| - "Cronet Sample",
|
| - UrlRequestContext.LOG_VERBOSE);
|
| + private static final String TAG = "CronetSampleActivity";
|
| +
|
| + public static final String COMMAND_LINE_ARGS_KEY = "commandLineArgs";
|
| +
|
| + public static final String POST_DATA_KEY = "postData";
|
| +
|
| + UrlRequestContext mRequestContext;
|
| +
|
| + String mUrl;
|
| +
|
| + boolean mLoading = false;
|
| +
|
| + int mHttpStatusCode = 0;
|
| +
|
| + class SampleRequestContext extends UrlRequestContext {
|
| + public SampleRequestContext() {
|
| + super(getApplicationContext(), "Cronet Sample",
|
| + UrlRequestContext.LOG_VERBOSE);
|
| + }
|
| + }
|
| +
|
| + class SampleRequest extends UrlRequest {
|
| + public SampleRequest(UrlRequestContext requestContext, String url,
|
| + int priority, Map<String, String> headers,
|
| + WritableByteChannel sink) {
|
| + super(requestContext, url, priority, headers, sink);
|
| + }
|
| +
|
| + @Override
|
| + protected void onRequestComplete() {
|
| + mHttpStatusCode = super.getHttpStatusCode();
|
| + Log.i(TAG, "****** Request Complete, status code is "
|
| + + mHttpStatusCode);
|
| + Intent intent = new Intent(getApplicationContext(),
|
| + CronetSampleActivity.class);
|
| + startActivity(intent);
|
| + final String url = super.getUrl();
|
| + final CharSequence text = "Completed " + url + " ("
|
| + + mHttpStatusCode + ")";
|
| + CronetSampleActivity.this.runOnUiThread(new Runnable() {
|
| + public void run() {
|
| + mLoading = false;
|
| + Toast toast = Toast.makeText(getApplicationContext(), text,
|
| + Toast.LENGTH_SHORT);
|
| + toast.show();
|
| + promptForURL(url);
|
| + }
|
| + });
|
| + }
|
| }
|
| - }
|
| -
|
| - class SampleRequest extends UrlRequest {
|
| - public SampleRequest(UrlRequestContext requestContext,
|
| - String url,
|
| - int priority,
|
| - Map<String, String> headers,
|
| - WritableByteChannel sink) {
|
| - super(requestContext, url, priority, headers, sink);
|
| +
|
| + class SampleHttpUrlRequestListener implements HttpUrlRequestListener {
|
| + public SampleHttpUrlRequestListener() {
|
| + }
|
| +
|
| + @Override
|
| + public void onRequestComplete(HttpUrlRequest request) {
|
| + Log.i(TAG, "****** Request Complete, status code is "
|
| + + getHttpStatusCode());
|
| + Intent intent = new Intent(getApplicationContext(),
|
| + CronetSampleActivity.class);
|
| + startActivity(intent);
|
| + final String url = request.getUrl();
|
| + final CharSequence text = "Completed " + request.getUrl() + " ("
|
| + + request.getHttpStatusCode() + ")";
|
| + mHttpStatusCode = request.getHttpStatusCode();
|
| + CronetSampleActivity.this.runOnUiThread(new Runnable() {
|
| + public void run() {
|
| + mLoading = false;
|
| + Toast toast = Toast.makeText(getApplicationContext(), text,
|
| + Toast.LENGTH_SHORT);
|
| + toast.show();
|
| + promptForURL(url);
|
| + }
|
| + });
|
| + }
|
| }
|
|
|
| @Override
|
| - protected void onRequestComplete() {
|
| - mHttpStatusCode = super.getHttpStatusCode();
|
| - Log.i(TAG, "****** Request Complete, status code is " +
|
| - mHttpStatusCode);
|
| - Intent intent = new Intent(getApplicationContext(),
|
| - CronetSampleActivity.class);
|
| - startActivity(intent);
|
| - final String url = super.getUrl();
|
| - final CharSequence text = "Completed " + url + " (" +
|
| - mHttpStatusCode + ")";
|
| - CronetSampleActivity.this.runOnUiThread(new Runnable() {
|
| - public void run() {
|
| - mLoading = false;
|
| - Toast toast = Toast.makeText(getApplicationContext(),
|
| - text,
|
| - Toast.LENGTH_SHORT);
|
| - toast.show();
|
| - promptForURL(url);
|
| + protected void onCreate(final Bundle savedInstanceState) {
|
| + super.onCreate(savedInstanceState);
|
| +
|
| + try {
|
| + LibraryLoader.ensureInitialized();
|
| + } catch (UnsatisfiedLinkError e) {
|
| + Log.e(TAG, "libcronet_sample initialization failed.", e);
|
| + finish();
|
| + return;
|
| + }
|
| +
|
| + mRequestContext = new SampleRequestContext();
|
| +
|
| + String appUrl = getUrlFromIntent(getIntent());
|
| + if (appUrl == null) {
|
| + promptForURL("https://");
|
| + } else {
|
| + startWithURL(appUrl);
|
| }
|
| - });
|
| - }
|
| - };
|
| -
|
| - @Override
|
| - protected void onCreate(final Bundle savedInstanceState) {
|
| - super.onCreate(savedInstanceState);
|
| -
|
| - try {
|
| - LibraryLoader.ensureInitialized();
|
| - } catch (UnsatisfiedLinkError e) {
|
| - Log.e(TAG, "libcronet_sample initialization failed.", e);
|
| - finish();
|
| - return;
|
| }
|
|
|
| - mRequestContext = new SampleRequestContext();
|
| + private void promptForURL(String url) {
|
| + Log.i(TAG, "No URL provided via intent, prompting user...");
|
| + AlertDialog.Builder alert = new AlertDialog.Builder(this);
|
| + alert.setTitle("Enter a URL");
|
| + alert.setMessage("Enter a URL");
|
| + final EditText input = new EditText(this);
|
| + input.setText(url);
|
| + alert.setView(input);
|
| + alert.setPositiveButton("Load", new DialogInterface.OnClickListener() {
|
| + public void onClick(DialogInterface dialog, int button) {
|
| + String url = input.getText().toString();
|
| + startWithURL(url);
|
| + }
|
| + });
|
| + alert.show();
|
| + }
|
|
|
| - String appUrl = getUrlFromIntent(getIntent());
|
| - if (appUrl == null) {
|
| - promptForURL("https://");
|
| - } else {
|
| - startWithURL(appUrl);
|
| + private static String getUrlFromIntent(Intent intent) {
|
| + return intent != null ? intent.getDataString() : null;
|
| }
|
| - }
|
| -
|
| - private void promptForURL(String url) {
|
| - Log.i(TAG, "No URL provided via intent, prompting user...");
|
| - AlertDialog.Builder alert = new AlertDialog.Builder(this);
|
| - alert.setTitle("Enter a URL");
|
| - alert.setMessage("Enter a URL");
|
| - final EditText input = new EditText(this);
|
| - input.setText(url);
|
| - alert.setView(input);
|
| - alert.setPositiveButton("Load", new DialogInterface.OnClickListener() {
|
| - public void onClick(DialogInterface dialog, int button) {
|
| - String url = input.getText().toString();
|
| - startWithURL(url);
|
| - }
|
| - });
|
| - alert.show();
|
| - }
|
| -
|
| - private static String getUrlFromIntent(Intent intent) {
|
| - return intent != null ? intent.getDataString() : null;
|
| - }
|
| -
|
| - private void applyCommandLineToRequest(UrlRequest request) {
|
| - Intent intent = getIntent();
|
| - Bundle extras = intent.getExtras();
|
| - Log.i(TAG, "Cronet extras: " + extras);
|
| - if (extras != null) {
|
| - String[] commandLineArgs = extras.getStringArray(COMMAND_LINE_ARGS_KEY);
|
| - if (commandLineArgs != null) {
|
| - for (int i = 0; i < commandLineArgs.length; ++i) {
|
| - Log.i(TAG, "Cronet commandLine[" + i + "]=" + commandLineArgs[i]);
|
| - if (commandLineArgs[i].equals(POST_DATA_KEY)) {
|
| - InputStream dataStream = new ByteArrayInputStream(
|
| - commandLineArgs[++i].getBytes());
|
| - ReadableByteChannel dataChannel = Channels.newChannel(dataStream);
|
| - request.setUploadChannel("text/plain", dataChannel);
|
| - }
|
| +
|
| + private void applyCommandLineToRequest(UrlRequest request) {
|
| + Intent intent = getIntent();
|
| + Bundle extras = intent.getExtras();
|
| + Log.i(TAG, "Cronet extras: " + extras);
|
| + if (extras != null) {
|
| + String[] commandLine = extras.getStringArray(COMMAND_LINE_ARGS_KEY);
|
| + if (commandLine != null) {
|
| + for (int i = 0; i < commandLine.length; ++i) {
|
| + Log.i(TAG,
|
| + "Cronet commandLine[" + i + "]=" + commandLine[i]);
|
| + if (commandLine[i].equals(POST_DATA_KEY)) {
|
| + InputStream dataStream = new ByteArrayInputStream(
|
| + commandLine[++i].getBytes());
|
| + ReadableByteChannel dataChannel = Channels.newChannel(
|
| + dataStream);
|
| + request.setUploadChannel("text/plain", dataChannel);
|
| + }
|
| + }
|
| + }
|
| }
|
| - }
|
| }
|
| - }
|
| -
|
| - private void startWithURL(String url) {
|
| - Log.i(TAG, "Cronet started: " + url);
|
| - mUrl = url;
|
| - mLoading = true;
|
| -
|
| - HashMap<String, String> headers = new HashMap<String, String>();
|
| - WritableByteChannel sink = Channels.newChannel(System.out);
|
| - UrlRequest request = new SampleRequest(mRequestContext,
|
| - url,
|
| - UrlRequest.REQUEST_PRIORITY_MEDIUM,
|
| - headers,
|
| - sink);
|
| - applyCommandLineToRequest(request);
|
| - request.start();
|
| - }
|
| -
|
| - public String getUrl() {
|
| - return mUrl;
|
| - }
|
| -
|
| - public boolean isLoading() {
|
| - return mLoading;
|
| - }
|
| -
|
| - public int getHttpStatusCode() {
|
| - return mHttpStatusCode;
|
| - }
|
| +
|
| + private void startWithURL(String url) {
|
| + Log.i(TAG, "Cronet started: " + url);
|
| + mUrl = url;
|
| + mLoading = true;
|
| +
|
| + HashMap<String, String> headers = new HashMap<String, String>();
|
| + HttpUrlRequestListener listener = new SampleHttpUrlRequestListener();
|
| +
|
| + HttpUrlRequest request = HttpUrlRequestFactory.newRequest(
|
| + getApplicationContext(), url,
|
| + UrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener);
|
| + request.start();
|
| + }
|
| +
|
| + private void startWithURL_UrlRequest(String url) {
|
| + Log.i(TAG, "Cronet started: " + url);
|
| + mUrl = url;
|
| + mLoading = true;
|
| +
|
| + HashMap<String, String> headers = new HashMap<String, String>();
|
| + WritableByteChannel sink = Channels.newChannel(System.out);
|
| + UrlRequest request = new SampleRequest(mRequestContext, url,
|
| + UrlRequest.REQUEST_PRIORITY_MEDIUM, headers, sink);
|
| + applyCommandLineToRequest(request);
|
| + request.start();
|
| + }
|
| +
|
| + public String getUrl() {
|
| + return mUrl;
|
| + }
|
| +
|
| + public boolean isLoading() {
|
| + return mLoading;
|
| + }
|
| +
|
| + public int getHttpStatusCode() {
|
| + return mHttpStatusCode;
|
| + }
|
| }
|
|
|