Android Open Source - sms-smap-gateway U I Request Controller






From Project

Back to project page sms-smap-gateway.

License

The source code is released under:

GNU General Public License

If you think the Android project sms-smap-gateway 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

package com.android.smap.controllers;
//  ww w.ja  va  2  s. co  m
import android.content.Context;

import com.android.smap.GatewayApp;
import com.android.smap.api.models.validator.ModelValidator;
import com.android.smap.api.requests.ApiRequest;
import com.android.smap.utils.Refresher;
import com.android.smap.utils.Refresher.RefreshListener;
import com.android.volley.ParseError;
import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;

/**
 * Base controller for fetching data and refreshing it. This is a core class.
 * Drink it up.
 * 
 * @author matt witherow
 * @param <T>
 */
public abstract class UIRequestController<T> implements
    Controller,
    Listener<T>,
    ErrorListener,
    RefreshListener {

  private static final int      DEFAULT_REFRESH_INTERVAL  = 30000;          // 30
                                                // seconds
  protected ControllerListener    mListener;
  protected ControllerErrorListener  mErrorListener;
  private final Context        mContext;
  private T              mModel;
  private Refresher          mRefresher;
  private int              mAutoRefreshInterval    = DEFAULT_REFRESH_INTERVAL;

  protected abstract ApiRequest<?> getRequest();

  public UIRequestController(Context context, ControllerListener listener,
      ControllerErrorListener errorListener) {

    if (listener == null || errorListener == null || context == null) {
      throw new IllegalArgumentException();
    }

    mListener = listener;
    mErrorListener = errorListener;
    mContext = context;
    mModel = null;
  }

  /**
   * Overwrite onReponse in the concrete controller to specify custom refresh
   * behaviour. Don't call super (you'll trip onControllerResult). validate()
   * is provided as a public util function for subclass use, use it. By
   * default, autorefresh is OFF. Enabling vanilla autorefresh will activate a
   * 30 second refresh.
   */
  @Override
  public void onResponse(T response) {
    validate(response);
    setModel(response);
    getControllerListener().onControllerResult();
  }

  public void validate(T response) {
    if (!ModelValidator.isModelValid(response)) {
      onErrorResponse(new ParseError());
      return;
    }
  }

  @Override
  public void onErrorResponse(VolleyError error) {

    ControllerError controllerError = new ControllerError(error, mContext);
    getControllerErrorListener().onControllerError(controllerError);
  }

  /**
   * Adds the request to the {@link RequestQueue} and tags the request with
   * this fragment
   * 
   * @param request
   */
  public void start() {
    ApiRequest<?> request = getRequest();
    request.setTag(this);
    GatewayApp.getRequestQueue().add(request);
  }

  /**
   * Cancels all requests in the {@link RequestQueue} tagged with this
   * controller. Also stops the refresh handler if one was running.
   */
  public void cancel() {
    setRefreshEnabled(false);
    GatewayApp.getRequestQueue().cancelAll(this);
  }

  /**
   * Set AutoRefresh on for the controller. User overloaded method to specify
   * a custom time interval. If you disable a non-existent refresh handler an
   * item will not be superfluously created
   * 
   * @param enabled
   */
  public void setRefreshEnabled(boolean enabled) {
    if (enabled && GatewayApp.getPreferenceWrapper().isAutoRefreshEnabled()) {
      getRefresher().reset();
    } else if (!enabled && mRefresher != null) {
      getRefresher().stop();
    }
  }

  public void setRefreshEnabled(boolean enabled, int interval) {
    mAutoRefreshInterval = interval;
    setRefreshEnabled(enabled);
  }

  private Refresher getRefresher() {
    if (mRefresher == null) {
      mRefresher = new Refresher(mAutoRefreshInterval, this);
    }
    return mRefresher;
  }

  /**
   * Callback from refresh handler. We simply restart our data fetching
   * process
   */
  public void onRefresh() {
    start();
  }

  /**
   * Override setModel in concrete controllers to specify how you wish the
   * model to be returned to others. This is your opportunity to santise the
   * model data. If you do not override this, you will accept what is given
   * raw from the feed
   * 
   * @param model
   * @return
   */
  public void setModel(T model) {
    this.mModel = model;
  }

  public T getModel() {
    return mModel;
  }

  public ControllerErrorListener getControllerErrorListener() {
    return mErrorListener;
  }

  @Override
  public ControllerListener getControllerListener() {
    return mListener;
  }

}




Java Source Code List

com.android.smap.AppConfig.java
com.android.smap.GatewayApp.java
com.android.smap.PhoneStateWrapper.java
com.android.smap.activities.BaseActivity.java
com.android.smap.activities.FragmentWrapperActivity.java
com.android.smap.activities.MainActivity.java
com.android.smap.adapters.MenuDrawerAdapter.java
com.android.smap.api.ApiConstants.java
com.android.smap.api.models.Gojo.java
com.android.smap.api.models.Node.java
com.android.smap.api.models.SmapException.java
com.android.smap.api.models.Status.java
com.android.smap.api.models.validator.ModelValidator.java
com.android.smap.api.models.validator.Required.java
com.android.smap.api.requests.ApiRequest.java
com.android.smap.api.requests.GojoRequest.java
com.android.smap.api.requests.StatusRequest.java
com.android.smap.commonsware.wakefull.AlarmReceiver.java
com.android.smap.commonsware.wakefull.WakefulIntentService.java
com.android.smap.controllers.ControllerErrorListener.java
com.android.smap.controllers.ControllerError.java
com.android.smap.controllers.ControllerFactory.java
com.android.smap.controllers.ControllerListener.java
com.android.smap.controllers.Controller.java
com.android.smap.controllers.EmailController.java
com.android.smap.controllers.GojoController.java
com.android.smap.controllers.UIPagingController.java
com.android.smap.controllers.UIRequestController.java
com.android.smap.fragments.BaseFragment.java
com.android.smap.fragments.HomeFragment.java
com.android.smap.models.MenuDrawerItem.java
com.android.smap.models.TextMessage.java
com.android.smap.samuel.Samuel.java
com.android.smap.sms.CellularModem.java
com.android.smap.sms.GatewayService.java
com.android.smap.sms.MonitorService.java
com.android.smap.sms.ScheduleMonitoringListener.java
com.android.smap.sms.ServiceRebooter.java
com.android.smap.ui.ArrayVelocAdapter.java
com.android.smap.ui.OnErrorTouchedListener.java
com.android.smap.ui.Operation.java
com.android.smap.ui.VelocAdapter.java
com.android.smap.ui.ViewBinder.java
com.android.smap.ui.ViewQuery.java
com.android.smap.ui.views.AnimatedNetworkImageView.java
com.android.smap.ui.views.ErrorView.java
com.android.smap.ui.views.FontableTextView.java
com.android.smap.utils.BitmapLruCache.java
com.android.smap.utils.MWAPNUtils.java
com.android.smap.utils.MWAnimUtil.java
com.android.smap.utils.MWAssetsUtils.java
com.android.smap.utils.MWConnectionUtils.java
com.android.smap.utils.MWEmailUtils.java
com.android.smap.utils.MWUiUtils.java
com.android.smap.utils.PreferenceWrapper.java
com.android.smap.utils.Refresher.java
com.android.smap.utils.UriBuilder.java