Android Open Source - Dumbledroid Data Controller






From Project

Back to project page Dumbledroid.

License

The source code is released under:

Copyright (c) 2013, Leocadio Tin? 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 Dumbledroid 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 io.leocad.dumbledroid.data;
//from  w w w.  jav a 2  s  .  c o m
import io.leocad.dumbledroid.data.cache.DiskCache;
import io.leocad.dumbledroid.data.cache.MemoryCache;
import io.leocad.dumbledroid.data.cache.ModelHolder;
import io.leocad.dumbledroid.data.cache.ObjectCopier;
import io.leocad.dumbledroid.data.xml.Node;
import io.leocad.dumbledroid.data.xml.SaxParser;
import io.leocad.dumbledroid.net.HttpLoader;
import io.leocad.dumbledroid.net.HttpMethod;
import io.leocad.dumbledroid.net.NoConnectionException;
import io.leocad.dumbledroid.net.TimeoutException;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.impl.cookie.DateParseException;
import org.apache.http.impl.cookie.DateUtils;
import org.json.JSONException;
import org.xml.sax.SAXException;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;


public class DataController {

  public static void load(Context ctx, AbstractModel receiver, DataType dataType, List<NameValuePair> params, HttpMethod method) throws Exception {

    final String cacheKey = getKey(receiver, params);
    HttpResponse httpResponse = null;
    boolean checkedConnection = false;
    ModelHolder modelHolder = null;

    //Get cached version
    if (receiver.cacheDuration > 0) {

      //In memory
      modelHolder = MemoryCache.getInstance().getCached(cacheKey);
      if (modelHolder != null && !modelHolder.isExpired() && ObjectCopier.copy(modelHolder.model, receiver)) {
        return;
      }

      //In disk
      if(modelHolder == null) {
        modelHolder = DiskCache.getInstance(ctx).getCached(cacheKey);
        if (modelHolder != null && !modelHolder.isExpired() && ObjectCopier.copy(modelHolder.model, receiver)) {
          MemoryCache.getInstance().cache(cacheKey, modelHolder);
          return;
        }
      }

      // Check also if it was modified on the server before downloading it
      if(modelHolder != null) {
        try {
          checkConnection(ctx);
          checkedConnection = true;
          httpResponse = HttpLoader.getHttpResponse(receiver.url, receiver.encoding, params, method);
          final Header lastModHeader = httpResponse.getFirstHeader("Last-Modified");

          if (lastModHeader != null) {
            final String lastMod = lastModHeader.getValue();
            long lastModTimeMillis = Long.MAX_VALUE;
            try {
              lastModTimeMillis = DateUtils.parseDate(lastMod).getTime();
            } catch (DateParseException e) {}

            if (modelHolder != null && lastModTimeMillis <= modelHolder.timestamp && ObjectCopier.copy(modelHolder.model, receiver)) {
              //Discard the connection and return the cached version renewing the timestamp
              modelHolder.timestamp = System.currentTimeMillis();
              MemoryCache.getInstance().cache(cacheKey, modelHolder);
              DiskCache.getInstance(ctx).cache(cacheKey, modelHolder);
              return;
            }
          }

        //If there is some error on the connection, return the last cached version
        } catch (TimeoutException e) {
          if(modelHolder !=  null && ObjectCopier.copy(modelHolder.model, receiver)) {
            return;
          }
        } catch (NoConnectionException e) {
          if(modelHolder !=  null && ObjectCopier.copy(modelHolder.model, receiver)) {
            return;
          }
        } catch (IOException e) {
          if(modelHolder !=  null && ObjectCopier.copy(modelHolder.model, receiver)) {
            return;
          }
        }
      }
    }

    if (!checkedConnection) {
      checkConnection(ctx);
    }

    if (httpResponse == null) {
      httpResponse = HttpLoader.getHttpResponse(receiver.url, receiver.encoding, params, method);
    }
    final InputStream is = HttpLoader.getHttpContent(httpResponse);

    switch (dataType) {
    case JSON:
      processJson(receiver, is);
      break;

    case XML:
      processXml(receiver, is);
      break;
    }

    //Cache
    if (receiver.cacheDuration > 0) {
      modelHolder = new ModelHolder(receiver, System.currentTimeMillis());
      MemoryCache.getInstance().cache(cacheKey, modelHolder);
      DiskCache.getInstance(ctx).cache(cacheKey, modelHolder);
    }
  }

  private static void checkConnection(Context ctx) throws NoConnectionException {

    ConnectivityManager cm = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();

    if (netInfo == null || !netInfo.isConnectedOrConnecting()) {
      throw new NoConnectionException();
    }
  }

  private static void processJson(AbstractModel receiver, InputStream is)
      throws IOException, JSONException, InstantiationException {

    String content = HttpLoader.streamToString(is);
    JsonReflector.reflectJsonString(receiver, content);
  }

  private static void processXml(AbstractModel receiver, InputStream is) throws SAXException, IOException, SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException, InstantiationException {

    Node parsedNode = SaxParser.parse(is, receiver.encoding);
    XmlReflector.reflectXmlRootNode(receiver, parsedNode);
  }

  private static String getKey(AbstractModel model, List<NameValuePair> params) {

    StringBuffer sb = new StringBuffer(model.url);

    if (params != null) {
      for (NameValuePair nameValuePair : params) {
        sb.append(nameValuePair.getName())
        .append('-')
        .append(nameValuePair.getValue());
      }
    }

    return sb.toString();
  }
}




Java Source Code List

io.leocad.dumbledoreexample.activities.AboutActivity.java
io.leocad.dumbledoreexample.activities.BaseActivity.java
io.leocad.dumbledoreexample.activities.FlickrActivity.java
io.leocad.dumbledoreexample.activities.JediActivity.java
io.leocad.dumbledoreexample.activities.MainActivity.java
io.leocad.dumbledoreexample.activities.SithActivity.java
io.leocad.dumbledoreexample.adapters.FlickrAdapter.java
io.leocad.dumbledoreexample.models.FlickrPhotos.java
io.leocad.dumbledoreexample.models.Jedi.java
io.leocad.dumbledoreexample.models.Media.java
io.leocad.dumbledoreexample.models.PhotoItem.java
io.leocad.dumbledoreexample.models.Sith.java
io.leocad.dumbledoreexample.models.Suit.java
io.leocad.dumbledroid.data.AbstractModel.java
io.leocad.dumbledroid.data.DataController.java
io.leocad.dumbledroid.data.DataType.java
io.leocad.dumbledroid.data.JsonReflector.java
io.leocad.dumbledroid.data.ReflectionHelper.java
io.leocad.dumbledroid.data.XmlReflector.java
io.leocad.dumbledroid.data.cache.DiskCache.java
io.leocad.dumbledroid.data.cache.FileController.java
io.leocad.dumbledroid.data.cache.MemoryCache.java
io.leocad.dumbledroid.data.cache.ModelHolder.java
io.leocad.dumbledroid.data.cache.ObjectCopier.java
io.leocad.dumbledroid.data.xml.Node.java
io.leocad.dumbledroid.data.xml.SaxHandler.java
io.leocad.dumbledroid.data.xml.SaxParser.java
io.leocad.dumbledroid.net.HttpLoader.java
io.leocad.dumbledroid.net.HttpMethod.java
io.leocad.dumbledroid.net.NoConnectionException.java
io.leocad.dumbledroid.net.TimeoutException.java
io.leocad.dumbledroidplugin.core.ClassMapper.java
io.leocad.dumbledroidplugin.core.ClassWriter.java
io.leocad.dumbledroidplugin.core.DumbledroidClassCreator.java
io.leocad.dumbledroidplugin.core.FileUtils.java
io.leocad.dumbledroidplugin.core.JsonReverseReflector.java
io.leocad.dumbledroidplugin.core.XmlReverseReflector.java
io.leocad.dumbledroidplugin.exceptions.InvalidContentException.java
io.leocad.dumbledroidplugin.exceptions.InvalidUrlException.java
io.leocad.dumbledroidplugin.exceptions.UnsupportedContentTypeException.java
io.leocad.dumbledroidplugin.wizards.DataInputPage.java
io.leocad.dumbledroidplugin.wizards.FileCreationPage.java
io.leocad.dumbledroidplugin.wizards.NewModelWizard.java
org.apache.commons.validator.routines.DomainValidator.java
org.apache.commons.validator.routines.InetAddressValidator.java
org.apache.commons.validator.routines.RegexValidator.java
org.apache.commons.validator.routines.UrlValidator.java