Android Open Source - NVS Syncronisation






From Project

Back to project page NVS.

License

The source code is released under:

Apache License

If you think the Android project NVS 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.jmuenster.extras;
//w  ww  . j  av  a 2 s.  c  o m
import java.net.InetAddress;
import java.util.ArrayList;

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

import com.jmuenster.db.DBHelper;
import com.jmuenster.server.ServerHandler;

/**
 * This class decides and organises calls between the server and the local DB. If the
 * server is available it is the preferred way to gather note information. All notes 
 * owned by this user will also be stored locally.
 * 
 * 
 * @author Julian Munster
 *
 */
public class Syncronisation {

  private Context context;
  private DBHelper localDB;
  private ServerHandler client;
  private boolean gotInternet;

  /**
   * Constructor, initialises all fields.
   * 
   * @param c
   */
  public Syncronisation (Context c) {
    this.context = c;
    localDB = new DBHelper(c);
    client = new ServerHandler();
  }
  
  /**
   * Setter for the gotInternet boolean field.
   * 
   * @param doWe
   */
  public void setGotInternet(boolean doWe) {
    gotInternet = doWe;
  }

  /**
   * Checks if the device can connect to the Internet.
   * 
   * @param context
   * @return true for success false for failures
   */
  public static boolean checkConnectivity(Context context) {
    String message = Constants.TOAST_INTERNET;
      ConnectivityManager cm = 
        (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo netInfo = cm.getActiveNetworkInfo();
      if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        message = Constants.TOAST_HOST;
        try {
        if (InetAddress.getByName(Constants.SERVER_IP).isReachable(3000)) {
          return true;
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
      }
    Toast.makeText(context, message, Toast.LENGTH_LONG).show();
      return false;
  }
  
  /**
   * This method gatherers all notes in a radius of 2000 meters.
   * It is only for the start of the application without any 
   * user input.
   * 
   * @return a list of Sticky Notes
   */
  public ArrayList<StickyNote> getAllNotes() {
    ArrayList<StickyNote> stickies = new ArrayList<StickyNote>();
    
    if (gotInternet) {
      stickies = client.getAllNotes();
    } else {
      stickies = localDB.getAllNotes();
    }
    
    return stickies;
  }
  
  /**
   * This method looks up a specific group of sticky notes. It 
   * filters them by the parameters passed in as a note itself.
   * 
   * @param sn
   * @return a list of Sticky Notes
   */
  public ArrayList<StickyNote> getNotes(StickyNote sn) {
    ArrayList<StickyNote> stickies = new ArrayList<StickyNote>();
    
    if (gotInternet) {
      stickies = client.getAllNotes();
    } else {
      stickies = localDB.getAllNotes();
    }
    
    return stickies;
  }
  
  /**
   * Creating a new Note.
   * 
   * @param sn
   * @return true for success or false for failure
   */
//  public boolean createNote(StickyNote sn) {
//    if (gotInternet) {
//      StickyNote newNote = client.addNewNote(sn);
//      if (newNote.getUnique_id() == null) {return false;}
//      localDB.insert(newNote);
//      return true;
//    } else {
//      Toast.makeText(context, "You must be connected to the " +
//          "Internet to create a note.", Toast.LENGTH_LONG).show();
//      return false;
//    }
//  }
  
  /**
   * Deleting a Note.
   * 
   * @param id
   * @return true for success or false for failure
   */
  public boolean deleteNote(String id) {
    if (id == null || id.equals("")) {return false;}
    if (gotInternet) {
      if (client.deleteNote(id)) {
        localDB.delete(id);
        return true;
      }
      return false;
    } else {
      Toast.makeText(context, "You must be connected to the " +
          "Internet to delete a note.", Toast.LENGTH_LONG).show();
      return false;
    }
  }
  
  // will do later
  public boolean updateNote(StickyNote sn) {
    return false;
  }
}




Java Source Code List

com.jmuenster.db.DBHelper.java
com.jmuenster.db.PointDbAdapter.java
com.jmuenster.extras.Constants.java
com.jmuenster.extras.Point.java
com.jmuenster.extras.StickyNote.java
com.jmuenster.extras.Syncronisation.java
com.jmuenster.gps.GPSHandler.java
com.jmuenster.gps.MyLocationListener.java
com.jmuenster.map.BalloonItemizedOverlay.java
com.jmuenster.map.BalloonOverlayView.java
com.jmuenster.map.MyItemizedOverlay.java
com.jmuenster.map.MyMapView.java
com.jmuenster.poi.AddEditDelNote.java
com.jmuenster.poi.PointOfInterest.java
com.jmuenster.poi.ViewNote.java
com.jmuenster.server.HTTPClient.java
com.jmuenster.server.JSONParser.java
com.jmuenster.server.ServerHandler.java