Android Open Source - trip-chain-android Trip Recorder






From Project

Back to project page trip-chain-android.

License

The source code is released under:

MIT License

If you think the Android project trip-chain-android 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 fi.aalto.tripchain.route;
/*w  w w.  j  a va  2  s  .c o  m*/
import java.io.IOException;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

import fi.aalto.tripchain.Client;
import fi.aalto.tripchain.Configuration;
import android.app.Service;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.os.AsyncTask;
import android.util.Log;

/**
 * Handles recording trips.
 *
 */
public class TripRecorder {
  private final static String TAG = TripRecorder.class.getSimpleName();
  
  private ActivityListener activityListener;
  private LocationListener locationListener;
  private Trip trip;

  private Service context;
  
  private SharedPreferences preferences;
  
  /**
   * Stops receiving activities and locations. Sends recorded trip to web service.
   */
  public void stop() {
    this.activityListener.stop();
    this.locationListener.stop();
    
    this.trip.stop();
    
    new AsyncTask<Void, Void, Void>() {
      @Override
      protected Void doInBackground(Void... params) {
        report();

        context.stopForeground(true);
        
        return null;
      }
    }.execute();
  }
  
  public void start() {
    this.activityListener.start();
    this.locationListener.start();
    this.trip.start();
  }
  
  public TripRecorder(Service context, List<Client> clients) {
    this.context = context;
    this.trip = new Trip(clients);
    this.activityListener = new ActivityListener(context, trip);
    this.locationListener = new LocationListener(context, trip);    
    
    preferences = context.getSharedPreferences(Configuration.SHARED_PREFERENCES, Context.MODE_MULTI_PROCESS);
  }
  
  private void report() {
    try {
      JSONObject j = trip.toJson();
      j.put("userId", preferences.getString(Configuration.KEY_LOGIN_ID, null));
      PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
      j.put("clientVersion", pInfo.versionName);
      
      Log.d(TAG, j.toString(2));
      postTrip(j);
    } catch (Exception e) {
      Log.d(TAG, "Failed to post trip", e);
    }
  }
  
  private void postTrip(JSONObject trip) throws ClientProtocolException, IOException {
      HttpClient client = new DefaultHttpClient();
      client.getParams().setParameter("http.protocol.content-charset", "UTF-8");
      HttpPost httpPost = new HttpPost("http://tripchaingame.herokuapp.com/api/trip.json");
      
      httpPost.addHeader("Content-Type", "application/json");
      httpPost.setEntity(new StringEntity(trip.toString(), "UTF-8"));
      
      HttpResponse response = client.execute(httpPost);
      Log.d(TAG, "post status: " + response.getStatusLine());
  }
}




Java Source Code List

fi.aalto.tripchain.BackgroundService.java
fi.aalto.tripchain.Configuration.java
fi.aalto.tripchain.LoginActivity.java
fi.aalto.tripchain.MainActivity.java
fi.aalto.tripchain.StartFragment.java
fi.aalto.tripchain.TripFragment.java
fi.aalto.tripchain.receivers.ActivityReceiver.java
fi.aalto.tripchain.receivers.EventDispatcher.java
fi.aalto.tripchain.receivers.EventListener.java
fi.aalto.tripchain.receivers.LocationReceiver.java
fi.aalto.tripchain.receivers.Receiver.java
fi.aalto.tripchain.route.ActivityListener.java
fi.aalto.tripchain.route.Activity.java
fi.aalto.tripchain.route.LocationListener.java
fi.aalto.tripchain.route.RoadSegment.java
fi.aalto.tripchain.route.Roads.java
fi.aalto.tripchain.route.RouteSegment.java
fi.aalto.tripchain.route.Route.java
fi.aalto.tripchain.route.TripRecorder.java
fi.aalto.tripchain.route.Trip.java