| Package | Description |
|---|---|
| com.github.dm.jrt.android.annotation |
Android platform specific annotation definitions.
|
| com.github.dm.jrt.android.builder |
Android routine builder definitions.
|
| com.github.dm.jrt.android.core |
Core classes and interfaces specific to the Android platform.
|
| com.github.dm.jrt.android.invocation |
Invocation classes and interfaces specific to the Android platform.
|
| com.github.dm.jrt.android.log |
Log classes specific to the Android platform.
|
| com.github.dm.jrt.android.routine |
Routine interfaces specific to the Android platform.
|
| com.github.dm.jrt.android.runner |
Runner related classes and definitions specific to the Android platform.
|
| com.github.dm.jrt.android.service |
Android service classes implementation.
|
| com.github.dm.jrt.android.v11.core |
Routine interfaces and implementations specific to the Android platform.
|
| com.github.dm.jrt.android.v4.core |
Routine interfaces and implementations with support for Android compatibility library.
|
Android platform customization of the jroutine library.
The code below shows how it is possible to load an image from the network in a background thread, by letting handle automatically any change in the configuration.
Example 1: simple approach.
Note that this approach is fairly naive, since it lets the framework retain the instance of the downloaded bitmap.
A smarter implementation would involve the use of a dedicated cache.
public class MainActivity extends Activity {
private static final String IMAGE_URI = "http://...";
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
try {
final ImageView imageView = (ImageView) findViewById(R.id.image);
JRoutine.on(RoutineContext.contextFrom(this), ClassToken.tokenOf(LoadBitmap.class))
.loaders()
.withCacheStrategy(CacheStrategyType.CACHE_IF_SUCCESS)
.set()
.asyncCall(new URI(IMAGE_URI))
.passTo(new TemplateOutputConsumer<Bitmap>() {
@Override
public void onError(@Nullable final Throwable error) {
Toast.makeText(MainTestActivity.this, (error != null) ? error.getMessage()
: "Cannot load image", Toast.LENGTH_LONG).show();
}
@Override
public void onOutput(final Bitmap bitmap) {
imageView.setImageBitmap(bitmap);
}
});
} catch (final URISyntaxException ignored) {
}
}
private static class LoadBitmap extends TemplateContextInvocation<URI, Bitmap> {
@Override
public void onInput(final URI uri, @Nonnull final ResultChannel<Bitmap> result) {
try {
final InputStream stream = uri.toURL().openConnection().getInputStream();
final Bitmap bitmap = BitmapFactory.decodeStream(stream);
if (bitmap != null) {
result.pass(bitmap);
} else {
result.abort();
}
} catch (final IOException e) {
throw new InvocationException(e);
} catch (final InterruptedException e) {
throw new InvocationInterruptedException(e);
}
}
}
}
Example 2: load in a service.
The above example can be slightly modified to make the real loading happen in a dedicated service.
Note that the service might even run in a separate process, since the Bitmap class implements the Parcelable interface. However, keep in mind that, depending on the specific image size, the amount of serialized data might exceed the parcel limits.
public class MainActivity extends Activity {
private static final String IMAGE_URI = "http://...";
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
try {
final ImageView imageView = (ImageView) findViewById(R.id.image);
JRoutine.on(RoutineContext.contextFrom(this), ClassToken.tokenOf(LoadBitmapFromService.class))
.loaders()
.withCacheStrategy(CacheStrategyType.CACHE_IF_SUCCESS)
.set()
.asyncCall(new URI(IMAGE_URI))
.passTo(new TemplateOutputConsumer<Bitmap>() {
@Override
public void onError(@Nullable final Throwable error) {
Toast.makeText(MainTestActivity.this, (error != null) ? error.getMessage()
: "Cannot load image", Toast.LENGTH_LONG).show();
}
@Override
public void onOutput(final Bitmap bitmap) {
imageView.setImageBitmap(bitmap);
}
});
} catch (final URISyntaxException ignored) {
}
}
private static class LoadBitmap extends TemplateContextInvocation<URI, Bitmap> {
@Override
public void onInput(final URI uri, @Nonnull final ResultChannel<Bitmap> result) {
try {
final InputStream stream = uri.toURL().openConnection().getInputStream();
final Bitmap bitmap = BitmapFactory.decodeStream(stream);
if (bitmap != null) {
result.pass(bitmap);
} else {
result.abort();
}
} catch (final IOException e) {
throw new InvocationException(e);
} catch (final InterruptedException e) {
throw new InvocationInterruptedException(e);
}
}
}
private static class LoadBitmapFromService extends TemplateContextInvocation<URI, Bitmap> {
private Routine<URI, Bitmap> mRoutine;
@Override
public void onContext(@Nonnull final Context context) {
super.onContext(context);
mRoutine = JRoutine.on(ServiceContext.serviceFrom(context, RoutineService.class),
ClassToken.tokenOf(LoadBitmap.class))
.buildRoutine();
}
@Override
public void onInput(final URI uri, @Nonnull final ResultChannel<Bitmap> result) {
result.pass(mRoutine.asyncCall(uri));
}
}
}