diff options
8 files changed, 175 insertions, 22 deletions
diff --git a/AndroidAsync/src/com/koushikdutta/async/AsyncSocketImpl.java b/AndroidAsync/src/com/koushikdutta/async/AsyncSocketImpl.java index 83e372c..2c06b10 100644 --- a/AndroidAsync/src/com/koushikdutta/async/AsyncSocketImpl.java +++ b/AndroidAsync/src/com/koushikdutta/async/AsyncSocketImpl.java @@ -122,7 +122,7 @@ class AsyncSocketImpl implements AsyncSocket { break; } } - Assert.assertEquals(list.remaining(), 0); +// Assert.assertEquals(list.remaining(), 0); } if (closed) diff --git a/AndroidAsync/src/com/koushikdutta/async/http/AsyncHttpClient.java b/AndroidAsync/src/com/koushikdutta/async/http/AsyncHttpClient.java index e9b5e80..c6ed775 100644 --- a/AndroidAsync/src/com/koushikdutta/async/http/AsyncHttpClient.java +++ b/AndroidAsync/src/com/koushikdutta/async/http/AsyncHttpClient.java @@ -1,13 +1,17 @@ package com.koushikdutta.async.http; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; import java.net.HttpURLConnection; import java.net.URI; import java.net.URISyntaxException; import java.nio.ByteBuffer; -import java.util.ArrayList; import java.util.HashSet; import java.util.Hashtable; -import java.util.List; + +import android.os.Handler; import com.koushikdutta.async.AsyncServer; import com.koushikdutta.async.AsyncSocket; @@ -21,6 +25,7 @@ import com.koushikdutta.async.callback.DataCallback; import com.koushikdutta.async.callback.ResultCallback; import com.koushikdutta.async.http.callback.HttpConnectCallback; import com.koushikdutta.async.http.libcore.RawHeaders; +import com.koushikdutta.async.stream.OutputStreamDataCallback; public class AsyncHttpClient { private static Hashtable<String, HashSet<AsyncSocket>> mSockets = new Hashtable<String, HashSet<AsyncSocket>>(); @@ -51,7 +56,7 @@ public class AsyncHttpClient { } } - ConnectCallback socketConnected = new ConnectCallback() { + final ConnectCallback socketConnected = new ConnectCallback() { @Override public void onConnectCompleted(Exception ex, final AsyncSocket socket) { if (ex != null) { @@ -73,7 +78,8 @@ public class AsyncHttpClient { } String kas = headers.get("Connection"); - if (kas != null && "keep-alive".toLowerCase().equals(kas.toLowerCase())) + // wip + if (false && kas != null && "keep-alive".toLowerCase().equals(kas.toLowerCase())) keepalive = true; } catch (Exception ex) { @@ -116,10 +122,15 @@ public class AsyncHttpClient { HashSet<AsyncSocket> sockets = mSockets.get(uri.getHost()); if (sockets != null) { synchronized (sockets) { - for (AsyncSocket socket: sockets) { + for (final AsyncSocket socket: sockets) { if (socket.isConnected()) { socket.setClosedCallback(null); - socketConnected.onConnectCompleted(null, socket); + server.post(new Runnable() { + @Override + public void run() { + socketConnected.onConnectCompleted(null, socket); + } + }); return; } } @@ -147,6 +158,9 @@ public class AsyncHttpClient { public static interface StringCallback extends ResultCallback<String> { } + public static interface FileCallback extends ResultCallback<File> { + } + private interface ResultConvert { public Object convert(ByteBufferList bb); } @@ -173,13 +187,70 @@ public class AsyncHttpClient { }); } + private static void invoke(Handler handler, final ResultCallback callback, final Exception e, final Object result) { + handler.post(new Runnable() { + @Override + public void run() { + callback.onCompleted(e, result); + } + }); + } + + public static void download(String uri, final String filename, final FileCallback callback) { + final Handler handler = new Handler(); + final File file = new File(filename); + final FileOutputStream fout; + try { + fout = new FileOutputStream(file); + } + catch (FileNotFoundException e) { + invoke(handler, callback, e, null); + return; + } + connect(uri, new HttpConnectCallback() { + ByteBufferList buffer = new ByteBufferList(); + @Override + public void onConnectCompleted(Exception ex, AsyncHttpResponse response) { + if (ex != null) { + try { + fout.close(); + } + catch (IOException e) { + } + invoke(handler, callback, ex, null); + return; + } + + response.setDataCallback(new OutputStreamDataCallback(fout)); + response.setCompletedCallback(new CompletedCallback() { + @Override + public void onCompleted(Exception ex) { + try { + fout.close(); + } + catch (IOException e) { + invoke(handler, callback, e, null); + return; + } + if (ex != null) { + invoke(handler, callback, ex, null); + return; + } + invoke(handler, callback, null, file); + } + }); + } + }); + } + private static void download(String uri, final ResultCallback callback, final ResultConvert convert) { + final Handler handler = new Handler(); connect(uri, new HttpConnectCallback() { ByteBufferList buffer = new ByteBufferList(); @Override public void onConnectCompleted(Exception ex, AsyncHttpResponse response) { if (ex != null) { - callback.onCompleted(ex, null); + invoke(handler, callback, ex, null); return; } @@ -193,7 +264,7 @@ public class AsyncHttpClient { response.setCompletedCallback(new CompletedCallback() { @Override public void onCompleted(Exception ex) { - callback.onCompleted(ex, buffer != null ? convert.convert(buffer) : null); + invoke(handler, callback, ex, buffer != null ? convert.convert(buffer) : null); } }); } diff --git a/AndroidAsyncSample/AndroidManifest.xml b/AndroidAsyncSample/AndroidManifest.xml index e92f241..6b4d84e 100644 --- a/AndroidAsyncSample/AndroidManifest.xml +++ b/AndroidAsyncSample/AndroidManifest.xml @@ -6,6 +6,7 @@ <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> + <uses-permission android:name="android.permission.INTERNET"/> <application android:icon="@drawable/ic_launcher" diff --git a/AndroidAsyncSample/gen/com/koushikdutta/async/R.java b/AndroidAsyncSample/gen/com/koushikdutta/async/R.java index fd5a33a..f3dd226 100644 --- a/AndroidAsyncSample/gen/com/koushikdutta/async/R.java +++ b/AndroidAsyncSample/gen/com/koushikdutta/async/R.java @@ -15,7 +15,11 @@ public final class R { public static final int ic_launcher=0x7f020001; } public static final class id { - public static final int menu_settings=0x7f070000; + public static final int desksms=0x7f070002; + public static final int go=0x7f070000; + public static final int menu_settings=0x7f070004; + public static final int rommanager=0x7f070001; + public static final int tether=0x7f070003; } public static final class layout { public static final int activity_main=0x7f030000; @@ -25,6 +29,7 @@ public final class R { } public static final class string { public static final int app_name=0x7f040001; + public static final int download=0x7f040005; public static final int hello_world=0x7f040002; public static final int menu_settings=0x7f040003; public static final int name=0x7f040000; diff --git a/AndroidAsyncSample/gen/com/koushikdutta/async/test/R.java b/AndroidAsyncSample/gen/com/koushikdutta/async/test/R.java index 4972632..e6faee1 100644 --- a/AndroidAsyncSample/gen/com/koushikdutta/async/test/R.java +++ b/AndroidAsyncSample/gen/com/koushikdutta/async/test/R.java @@ -15,7 +15,11 @@ public final class R { public static final int ic_launcher=0x7f020001; } public static final class id { - public static final int menu_settings=0x7f070000; + public static final int desksms=0x7f070002; + public static final int go=0x7f070000; + public static final int menu_settings=0x7f070004; + public static final int rommanager=0x7f070001; + public static final int tether=0x7f070003; } public static final class layout { public static final int activity_main=0x7f030000; @@ -25,6 +29,7 @@ public final class R { } public static final class string { public static final int app_name=0x7f040001; + public static final int download=0x7f040005; public static final int hello_world=0x7f040002; public static final int menu_settings=0x7f040003; public static final int name=0x7f040000; diff --git a/AndroidAsyncSample/res/layout/activity_main.xml b/AndroidAsyncSample/res/layout/activity_main.xml index 8f4c7f8..5b30f9c 100644 --- a/AndroidAsyncSample/res/layout/activity_main.xml +++ b/AndroidAsyncSample/res/layout/activity_main.xml @@ -1,14 +1,30 @@ -<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" - android:layout_height="match_parent" > + android:layout_height="match_parent" + android:orientation="vertical" > - <TextView + <Button + android:layout_gravity="center" + android:text="@string/download" + android:id="@+id/go" android:layout_width="wrap_content" - android:layout_height="wrap_content" - android:layout_centerHorizontal="true" - android:layout_centerVertical="true" - android:text="@string/hello_world" - tools:context=".MainActivity" /> + android:layout_height="wrap_content" > + </Button> -</RelativeLayout> + <ImageView + android:id="@+id/rommanager" + android:layout_width="wrap_content" + android:layout_height="wrap_content" /> + + <ImageView + android:id="@+id/desksms" + android:layout_width="wrap_content" + android:layout_height="wrap_content" /> + + <ImageView + android:id="@+id/tether" + android:layout_width="wrap_content" + android:layout_height="wrap_content" /> + +</LinearLayout>
\ No newline at end of file diff --git a/AndroidAsyncSample/res/values/strings.xml b/AndroidAsyncSample/res/values/strings.xml index 2022835..7004c0a 100644 --- a/AndroidAsyncSample/res/values/strings.xml +++ b/AndroidAsyncSample/res/values/strings.xml @@ -4,5 +4,5 @@ <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string> <string name="title_activity_main">MainActivity</string> - + <string name="download">Download Images</string> </resources>
\ No newline at end of file diff --git a/AndroidAsyncSample/src/com/koushikdutta/async/test/MainActivity.java b/AndroidAsyncSample/src/com/koushikdutta/async/test/MainActivity.java index 00d6a19..5ee9a75 100644 --- a/AndroidAsyncSample/src/com/koushikdutta/async/test/MainActivity.java +++ b/AndroidAsyncSample/src/com/koushikdutta/async/test/MainActivity.java @@ -1,15 +1,42 @@ package com.koushikdutta.async.test; -import android.os.Bundle; +import java.io.File; + import android.app.Activity; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.drawable.BitmapDrawable; +import android.os.Bundle; import android.view.Menu; +import android.view.View; +import android.view.View.OnClickListener; +import android.widget.Button; +import android.widget.ImageView; + +import com.koushikdutta.async.http.AsyncHttpClient; public class MainActivity extends Activity { + ImageView rommanager; + ImageView tether; + ImageView desksms; + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + + Button b = (Button)findViewById(R.id.go); + b.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + refresh(); + } + }); + + rommanager = (ImageView)findViewById(R.id.rommanager); + tether = (ImageView)findViewById(R.id.tether); + desksms = (ImageView)findViewById(R.id.desksms); } @Override @@ -17,4 +44,32 @@ public class MainActivity extends Activity { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } + + private void downloadFile(final ImageView iv, String url, final String filename) { + AsyncHttpClient.download(url, filename, new AsyncHttpClient.FileCallback() { + @Override + public void onCompleted(Exception e, File result) { + if (e != null) { + e.printStackTrace(); + return; + } + System.out.println(result.getAbsolutePath()); + Bitmap bitmap = BitmapFactory.decodeFile(filename); + if (bitmap == null) + return; + BitmapDrawable bd = new BitmapDrawable(bitmap); + iv.setImageDrawable(bd); + } + }); + } + + private void refresh() { + rommanager.setImageBitmap(null); + tether.setImageBitmap(null); + desksms.setImageBitmap(null); + + downloadFile(rommanager, "https://raw.github.com/koush/AndroidAsync/master/rommanager.png", getFileStreamPath("rommanager.png").getAbsolutePath()); + downloadFile(tether, "https://raw.github.com/koush/AndroidAsync/master/tether.png", getFileStreamPath("tether.png").getAbsolutePath()); + downloadFile(desksms, "https://raw.github.com/koush/AndroidAsync/master/desksms.png", getFileStreamPath("desksms.png").getAbsolutePath()); + } } |