Android Open Source - proximo Async Backend

From Project

Back to project page proximo.


The source code is released under:

Copyright (c) 2010 Evan Martin. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: *...

If you think the Android project proximo listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

// Copyright (c) 2010 Evan Martin.  All rights reserved.
// Use of this source code is governed by a BSD-style license that can
// be found in the LICENSE file.
//from w w w. java2 s . co  m
package org.neugierig.proximo;

import android.content.*;
import android.os.*;
import android.widget.Toast;
import org.json.*;

// The AsyncBackend provides an asynchronous interface to the
// (blocking) Backend.  Its callbacks always come back on the thread
// it's queried from, but internally it's running the queries on a
// separate thread.
class AsyncBackend {
  public interface APIResultCallback {
    // A query can either result in a positive response or an
    // exception.  Any exception that happens in the backend is
    // proxied over to onException here.
    public void onAPIResult(Object obj);
    public void onException(Exception exn);

  // A Query is a snippet of code that is passed to the backend
  // and run.  Its resulting object comes back asynchronously via
  // the APIResultCallback interface.
  public interface Query {
    public Object runQuery(Backend backend) throws Exception;

  AsyncBackend(Context context) {
    mContext = context;
    mBackend = new Backend(context);

  // All the thread magic happens in this single function, which
  // hopefully helps reasoning about what needs locks.  TODO: maybe
  // lock around not having two pending queries out at once, which
  // doesn't make sense from an API perspective but could race in
  // mBackend.
  public void startQuery(final Query query,
                         final APIResultCallback callback) {
    final int MSG_RESULT = 0;
    final int MSG_EXCEPTION = 1;

    final Handler handler = new Handler() {
        public void handleMessage(Message msg) {
          switch (msg.what) {
          case MSG_RESULT:
          case MSG_EXCEPTION:

    Thread thread = new Thread(new Runnable() {
      public void run() {
        try {
          Object result = query.runQuery(mBackend);
          handler.sendMessage(handler.obtainMessage(MSG_RESULT, result));
        } catch (Exception exn) {
    }, "Network Fetch");

  private Context mContext;
  private Backend mBackend;

Java Source Code List