Skip to content
This repository has been archived by the owner on Dec 31, 2020. It is now read-only.

Commit

Permalink
Merge branch '1.0.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
Niklas Klein committed Aug 9, 2014
2 parents 7413985 + ea13b64 commit fc429b1
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 2 deletions.
3 changes: 2 additions & 1 deletion project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ object Build extends sbt.Build
{
val main = Project( "communicator", file( "." ) ).settings(
autoScalaLibrary := false,
exportJars := true,
libraryDependencies += "com.google.android" % "android" % "4.4" % "provided" from ( "file://" + System.getenv( "ANDROID_HOME" ) + "/platforms/android-19/android.jar" ),
name := "android-communicator",
organization := "com.taig",
scalaVersion := "2.11.2",
version := "1.0.2"
version := "1.0.3"
)
}
38 changes: 38 additions & 0 deletions src/main/java/com/taig/communicator/request/Read.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
*/
public abstract class Read<R extends Response, E extends Event<R>, T> extends Request<R, E>
{
protected Progress progress = null;

/**
* Create a {@link Read} object.
*
Expand All @@ -34,6 +36,22 @@ public Read( Method.Type method, URL url, E event )
super( method, url, event );
}

@Override
protected LoadingState getLoadingState()
{
return new LoadingState();
}

/**
* Retrieve the {@link Request Request's} reading {@link Progress}.
*
* @return The Request's reading Progress or <code>null</code> if no reading was done yet.
*/
public Progress getReadProgress()
{
return progress;
}

@Override
public HttpURLConnection connect() throws IOException
{
Expand Down Expand Up @@ -98,6 +116,26 @@ protected T receive( HttpURLConnection connection ) throws IOException
*/
protected abstract R summarize( URL url, int code, String message, Map<String, List<String>> headers, T body );

protected class LoadingState extends Request<R, E>.LoadingState
{
@Override
public void receive()
{
super.receive();

progress = new Progress( 0, -1 );
}

@Override
public void receiving( int current, long total )
{
progress.setCurrent( current );
progress.setTotal( total );

super.receiving( current, total );
}
}

/**
* An internal {@link Updateable.Stream.Input} class used to trigger the {@link Event#onReceive(long, long)}
* callback and to regularly check if the {@link Request} has been cancelled.
Expand Down
65 changes: 64 additions & 1 deletion src/main/java/com/taig/communicator/request/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public abstract class Request<R extends Response, E extends Event<R>> implements

protected Event<R>.Proxy event;

protected LoadingState state = new LoadingState();
protected LoadingState state = getLoadingState();

protected boolean cancelled = false;

Expand Down Expand Up @@ -124,6 +124,16 @@ public Request<R, E> setEvent( E event )
return this;
}

/**
* Retrieve the {@link Request Request's} {@link LoadingState} implementation.
*
* @return The Request's LoadingState implementation.
*/
protected LoadingState getLoadingState()
{
return new LoadingState();
}

/**
* Whether the {@link Request} has been cancelled or not.
*
Expand Down Expand Up @@ -604,4 +614,57 @@ public void failure( Throwable error )
}
}
}

/**
* Holds information of a communication process providing the currently transferred amount of bytes and the total
* amount of bytes that will be sent.
*/
public static class Progress
{
private long current;

private long total;

protected Progress( long current, long total )
{
this.current = current;
this.total = total;
}

/**
* Retrieve the amount of transferred bytes.
*
* @return The amount of transferred bytes.
*/
public long getCurrent()
{
return current;
}

protected void setCurrent( long current )
{
this.current = current;
}

/**
* Retrieve the amount of bytes that will be sent in total.
*
* @return The amount of bytes that will be sent in total or <code>-1</code> if not available.
*/
public long getTotal()
{
return total;
}

protected void setTotal( long total )
{
this.total = total;
}

@Override
public String toString()
{
return String.format( "%d / %d Bytes", current, total );
}
}
}
38 changes: 38 additions & 0 deletions src/main/java/com/taig/communicator/request/Write.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
public abstract class Write<R extends Response, E extends Event<R>, T> extends Read<R, E, T>
{
protected Progress progress = null;

private Data data;

/**
Expand All @@ -37,6 +39,22 @@ public Write( Method.Type method, URL url, Data data, E event )
this.data = data;
}

@Override
protected LoadingState getLoadingState()
{
return new LoadingState();
}

/**
* Retrieve the {@link Request Request's} writing {@link Progress}.
*
* @return The Request's writing Progress or <code>null</code> if no writing was done yet.
*/
public Progress getWriteProgress()
{
return progress;
}

/**
* Retrieve the {@link Data} that will serve as request body.
*
Expand Down Expand Up @@ -121,6 +139,26 @@ protected void write( Updateable.Stream.Output output, InputStream data ) throws
}
}

protected class LoadingState extends Read<R, E, T>.LoadingState
{
@Override
public void send()
{
super.send();

progress = new Progress( 0, -1 );
}

@Override
public void sending( int current, long total )
{
progress.setCurrent( current );
progress.setTotal( total );

super.sending( current, total );
}
}

/**
* An internal {@link Updateable.Stream.Output} class used to trigger the {@link Event#onSend(long, long)}
* callback and to regularly check if the {@link Request} has been cancelled.
Expand Down

0 comments on commit fc429b1

Please sign in to comment.