Android Open Source - BusTicketer Connection Thread






From Project

Back to project page BusTicketer.

License

The source code is released under:

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

If you think the Android project BusTicketer 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 bus.ticketer.connection;
// w w w .j a v a2  s  .  c o m
import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.SparseArray;
import android.view.View;
import bus.ticketer.adapters.DialogAdapter;
import bus.ticketer.objects.Ticket;
import bus.ticketer.passenger.BusTicketer;
import bus.ticketer.runnable.BuyRunnable;
import bus.ticketer.runnable.ShowRunnable;
import bus.ticketer.utils.Method;
import bus.ticketer.utils.RESTFunction;

public class ConnectionThread extends Thread {

  private ConnectionRunnable runConnection;
  private Handler mHandler;
  private ProgressDialog progDialog;
  private RESTFunction currentFunction;
  private View view;
  private Activity context;
  private BusTicketer app;
  
  public ConnectionThread(String link, Method method,
      ArrayList<NameValuePair> payload, Handler handler,
      ProgressDialog progDialog, RESTFunction function,
      View view, Activity context) {
    runConnection = new ConnectionRunnable(link, method.toString(), payload);
    mHandler = handler;
    this.progDialog = progDialog;
    this.currentFunction = function;
    this.view = view;
    this.context = context;
    this.app = ((BusTicketer) context.getApplicationContext());
  }

  @Override
  public void run() {
    Looper.prepare();
    runConnection.run();
    threadMsg();
    
    if(!currentFunction.toString().equals(RESTFunction.BUY_CLIENT_TICKETS_CLICK.toString()) 
        && !currentFunction.toString().equals(RESTFunction.BUY_CONFIRMATION_CLIENT.toString()))
      fillList();
    
    handleView();

    if (progDialog != null)
      progDialog.dismiss();
    
    Looper.loop();
  }

  public JSONObject getJSON() {
    return runConnection.getResultObject();
  }
  
  private void threadMsg() {
    Message msgObj = mHandler.obtainMessage();
    msgObj.obj = getJSON();
    mHandler.sendMessage(msgObj);
  }
  
  private void fillList() {
        JSONObject ticketListing = getJSON();
        
        if(ticketListing == null) 
          DialogAdapter.connectionIssues(context);
        else {
          SparseArray<ArrayList<Ticket>> tickets = app.getTickets();
       
          try {
            if(tickets.size() == 0) {
              tickets.put(1, new ArrayList<Ticket>());
              tickets.put(2, new ArrayList<Ticket>());
              tickets.put(3, new ArrayList<Ticket>());
            }
            
            for(int i = 1; i <= 3; i++) {
              JSONArray typeTickets = ticketListing.getJSONArray("t"+i);
              ArrayList<Ticket> currentTickets = tickets.get(i);
              for(int j = 0; j < typeTickets.length(); j++) {
                Ticket t = new Ticket(typeTickets.getInt(j));
                if(!currentTickets.contains(t))
                  currentTickets.add(t);
              }
            }
                      
              app.setTickets(tickets);
          } catch (JSONException e) {
                e.printStackTrace();
          }
        }
  }
  
  private void handleView() {
    switch(currentFunction) {
      case GET_CLIENT_TICKETS:
        view.post(new ShowRunnable(app, context, view));
        break;
      case BUY_CLIENT_TICKETS:
        view.post(new BuyRunnable(app, view));        
        break;
      default:
        break;
    }
  }

}




Java Source Code List

bus.ticketer.adapters.CentralPagerAdapter.java
bus.ticketer.adapters.DialogAdapter.java
bus.ticketer.adapters.SpinnerAdapter.java
bus.ticketer.connection.ConnectionRunnable.java
bus.ticketer.connection.ConnectionRunnable.java
bus.ticketer.connection.ConnectionRunnable.java
bus.ticketer.connection.ConnectionThread.java
bus.ticketer.connection.ConnectionThread.java
bus.ticketer.connection.ConnectionThread.java
bus.ticketer.filters.MonthInputFilter.java
bus.ticketer.fragments.BuyTicketsFragment.java
bus.ticketer.fragments.ShowTicketsFragment.java
bus.ticketer.inspector.BusInspector.java
bus.ticketer.inspector.MainActivity.java
bus.ticketer.inspector.ResultActivity.java
bus.ticketer.inspector.ScanActivity.java
bus.ticketer.listeners.BusTabListener.java
bus.ticketer.listeners.PurchaseListener.java
bus.ticketer.listeners.RadioGroupListener.java
bus.ticketer.listeners.SwipeListener.java
bus.ticketer.listeners.TicketPurchaseListener.java
bus.ticketer.listeners.ValidationListener.java
bus.ticketer.objects.Ticket.java
bus.ticketer.passenger.BeamActivity.java
bus.ticketer.passenger.BusTicketer.java
bus.ticketer.passenger.CentralActivity.java
bus.ticketer.passenger.MainActivity.java
bus.ticketer.runnable.BuyRunnable.java
bus.ticketer.runnable.ShowRunnable.java
bus.ticketer.runnable.TimerRunnable.java
bus.ticketer.runnable.TimerThread.java
bus.ticketer.utils.BusTimer.java
bus.ticketer.utils.BusUtils.java
bus.ticketer.utils.FileHandler.java
bus.ticketer.utils.FileWriter.java
bus.ticketer.utils.Method.java
bus.ticketer.utils.Method.java
bus.ticketer.utils.Method.java
bus.ticketer.utils.RESTFunction.java
bus.ticketer.utils.RESTFunction.java
bus.ticketer.utils.RESTFunction.java
bus.ticketer.utils.SpinnerAdapter.java
bus.ticketer.validation.MainActivity.java
bus.ticketer.validation.ResultActivity.java