Android-Zorn


Source link: https://github.com/HendrixString/Android-Zorn

Android-Zorn

Asynchronous Workers and Worker Managers for Android.

How to use

simply fork or download the project, you can also download and create .aar file yourself.

Notable Features

  • use Worker API as a replacement for AsyncTask
  • workers run on background threads and return callbacks on the main(calling) thread.
  • automate a batch of workers with two worker managers:
    • PriorityWorkerManager - workers are processed according to their priority both serially or unbounded.
    • TopologicalWorkerManager - workers are processed according to a binary topological relation and order.

Using a Worker

1. Simply anonymously instantiate AbstractWorker

AbstractWorker worker = new AbstractWorker() {

  @Override
  protected void onProgress() {

// runs on the calling/main thread
  
}

@Override
  protected void onComplete() {

// runs on the calling/main thread
  
}

@Override
  public void work() {

// here you put work to be done in a background thread
  
}
 
}
;  // run async worker.process();
 

2. Simply extend AbstractWorker

MyWorker worker = new MyWorker();
  worker.process(new WorkerObserver() {

  @Override
  public void onWorkerComplete(IWorker worker) {

// runs on the calling/main thread
  
}

@Override
  public void onWorkerProgress(IWorker worker) {

// runs on the calling/main thread
  
}

@Override
  public void onWorkerError(IWorker worker) {

// runs on the calling/main thread
  
}
 
}
);
 

3. use SimpleWorker with a IWork object (like Runnable)

SimpleWorker sw = new SimpleWorker(new IWork() {

  @Override
  public void work() {

// here you put work to be done in a background thread
  
}
 
}
);
  sw.process();
 

notes

  • IWorker.process(..) method also have an overloaded version where one can pass ExecutorService
  • IWorker supports many more methods and ideas. I did not go through all.
  • in the future, I will add support for Java native FutureTask and Callable so worker can have cancelling feature.

Using a Worker Manager

Worker managers support a lot of functionality such as pause, start, stop etc.. You can also implement a Worker manager using BaseAbstractWorkerManager, it is very easy. Contributions of new Worker managers are most welcome.

Priority Worker Manager.

pm = new PriorityWorkerManager("myId");
 // serial mode pm.setExecutionMode(AbstractWorkerManager.EXECUTION_MODE.SERIAL);
  IWorker worker = null; String  id; int
  priority;  for(int ix = 0; ix < 20; ix++) {

  id

  = String.valueOf(ix);

  priority  = ix;

 worker
 = new TestWorker(id, priority);

pm.enqueue(worker);
 
}
  pm.setListener(new WorkerManagerObserver() {

  @Override
  public void onComplete(IWorkerManager wm) {

// runs on the calling/main thread
  
}

@Override
  public void onProgress(String id) {

// runs on the calling/main thread
  
}

@Override
  public void onError(WorkerManagerErrorInfo err) {

// runs on the calling/main thread
  
}
 
}
);
  pm.start();

Topological Worker Manager.

Use the TopologicalWorkerManager.Builder or Zorn.newTopologicalWorkerManager() to create a worker manager that takes into account a directed binary relation among workers.

TestWorker a1 = new TestWorker("a1");
 TestWorker a2 = new TestWorker("a2");
 TestWorker a3 = new TestWorker("a3");
 TestWorker a4 = new TestWorker("a4");
 TestWorker a5 = new TestWorker("a5");
  TopologicalWorkerManager tm = new TopologicalWorkerManager.Builder().id("topological_test")

  .listener(this)

  .before(a1, a3)

  .before(a2, a3)

  .after(a4, a3)

  .after(a5, a4)

  .build();

tm.setListener(...);
  tm.start();

 

Dependencies

Terms

Contact Author

Resources

A powerful library for creating notifications in android platform.

A very simple arc layout library for Android.

Java annotation processor library used to dispatch and concatenate background tasks in a decoupled way through a simple syntax.

This is the advanced version of the android-process-button.

TimberLorry is a log collector without data loss (e.g. reboot, exit application). Library stores logs in the buffer (internal database by default), and periodically sends it to an endpoint. If no internet connection available, the periodical work will not be executed.

Simple Android color picker with color wheel and lightness bar.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes