Android Open Source - Team7-301Project Network Checker






From Project

Back to project page Team7-301Project.

License

The source code is released under:

Apache License

If you think the Android project Team7-301Project 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 ca.ualberta.cs.remote_server;
// ww w .java2s. co  m
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import android.content.Context;
import android.net.ConnectivityManager;
import android.text.style.AlignmentSpan.Standard;
import ca.ualberta.cs.intent_singletons.ContextSingleton;
import ca.ualberta.cs.models.Answer;
import ca.ualberta.cs.models.DataManager;
import ca.ualberta.cs.models.ForumEntry;

/**
 * Checks whether the device is hooked up to 
 * a network. 
 * If a question is asked with no network it saves
 * it to a file instead 
 * 
 * If a network connection is created it adds them to 
 * the elastic search server
 * 
 * @author Lexie
 */
public class NetworkChecker {
  
  private Boolean isOnline;
  private String FILENAME = "pushOnline.sav";
  private Gson gson;
  private Context ctx;
  
  public NetworkChecker(){
    gson = new Gson();
    //checkStatus();
  }
  public Boolean getIsOnline(){
    checkStatus();
    return isOnline;
  }
  public void dealWithNetworkStuff(){
    checkStatus();
    if(isOnline){
      AddThread addThread = new AddThread();
      addThread.start();
    }
  }
  public void setContext(Context context){
    ctx = context;
  }
  public void checkStatus()
  {
    if(ctx == null){
    ctx = ContextSingleton.getInstance().getContext();
    }
      ConnectivityManager cm = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
     
    // test for connection
            if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isAvailable()
                    && cm.getActiveNetworkInfo().isConnected()) {
                isOnline = true;
            } else {
                isOnline = false;
            }
  }
  public void saveForLater(ForumEntry forumEntry){
    Context ctx = ContextSingleton.getInstance().getContext();
    try
    {
      FileOutputStream fos = ctx.openFileOutput(FILENAME,
          Context.MODE_PRIVATE);
      String json = gson.toJson(forumEntry);
      fos.write(json.getBytes());
      fos.close();

    } catch (Exception e)
    {
      e.printStackTrace();
    }
  }
  public void addOnline(){
    ArrayList<ForumEntry> fel = new ArrayList<ForumEntry>();
    Context ctx = ContextSingleton.getInstance().getContext();
    DataManager dm = new DataManager();
    
    try {
      FileOutputStream fos;
      fos = ctx.openFileOutput(FILENAME,Context.MODE_PRIVATE);
      
      OutputStreamWriter osw = new OutputStreamWriter(fos);
      
      Gson gson=new GsonBuilder().create();
      gson.toJson(fel,osw);
      osw.flush();
      osw.close();
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    
    if(fel.size()!=0){
      for(int i = 0; i<fel.size(); i++){
        dm.addForumEntry(fel.get(i));
      }
    }
  }
  
  class AddThread extends Thread
  {
    public AddThread()
    {
    }
    
    @Override
    public void run()
    {
      addOnline();
    }
  }
}




Java Source Code List

ca.ualberta.cs.controllers.AuthorController.java
ca.ualberta.cs.controllers.BrowseController.java
ca.ualberta.cs.controllers.ForumEntryController.java
ca.ualberta.cs.controllers.SearchController.java
ca.ualberta.cs.f14t07_application.Hits.java
ca.ualberta.cs.f14t07_application.LogoActivity.java
ca.ualberta.cs.f14t07_application.Post.java
ca.ualberta.cs.intent_singletons.BrowseRequestSingleton.java
ca.ualberta.cs.intent_singletons.ContextSingleton.java
ca.ualberta.cs.intent_singletons.EntrySingleton.java
ca.ualberta.cs.intent_singletons.ForumEntrySingleton.java
ca.ualberta.cs.models.Answer.java
ca.ualberta.cs.models.AuthorModel.java
ca.ualberta.cs.models.DataManager.java
ca.ualberta.cs.models.Entry.java
ca.ualberta.cs.models.ForumEntryList.java
ca.ualberta.cs.models.ForumEntry.java
ca.ualberta.cs.models.JsonDriver.java
ca.ualberta.cs.models.Observable.java
ca.ualberta.cs.models.Question.java
ca.ualberta.cs.models.Reply.java
ca.ualberta.cs.remote_server.NetworkChecker.java
ca.ualberta.cs.remote_server.SearchHit.java
ca.ualberta.cs.remote_server.SearchResponse.java
ca.ualberta.cs.remote_server.SimpleSearchCommand.java
ca.ualberta.cs.views.AnswerActivity.java
ca.ualberta.cs.views.AnswerListAdapter.java
ca.ualberta.cs.views.AskActivity.java
ca.ualberta.cs.views.BrowseActivity.java
ca.ualberta.cs.views.HelpActivity.java
ca.ualberta.cs.views.MainScreenActivity.java
ca.ualberta.cs.views.Observer.java
ca.ualberta.cs.views.QuestionActivity.java
ca.ualberta.cs.views.ReplyListAdapter.java