1 package com.replica.replicaisland; 2 3 import java.net.HttpURLConnection; 4 import java.net.URL; 5 import java.net.URLEncoder; 6 import java.util.ArrayList; 7 8 public class EventReporter implements Runnable { 9 public final static int EVENT_DEATH = 0; 10 public final static int EVENT_BEAT_LEVEL = 1; 11 public final static int EVENT_BEAT_GAME = 2; 12 13 public final static String REPORT_SERVER = null; // insert your server here. 14 15 private class Event { 16 public String eventType; 17 public float x; 18 public float y; 19 public float time; 20 public String level; 21 public int version; 22 public long session; 23 } 24 25 private Object mLock = new Object(); 26 private ArrayList<Event> mEvents = new ArrayList<Event>(); 27 private ArrayList<Event> mProcessedEvents = new ArrayList<Event>(); 28 private boolean mDone = false; 29 30 public void run() { 31 while (!mDone) { 32 synchronized(mLock) { 33 if (mEvents.isEmpty()) { 34 while (mEvents.isEmpty() && !mDone) { 35 try { 36 mLock.wait(); 37 } catch (InterruptedException e) { 38 } 39 } 40 } 41 42 mProcessedEvents.addAll(mEvents); 43 mEvents.clear(); 44 } 45 46 final int count = mProcessedEvents.size(); 47 for (int x = 0; x < count; x++) { 48 recordEvent(mProcessedEvents.get(x)); 49 } 50 51 mProcessedEvents.clear(); 52 } 53 } 54 55 public void addEvent(int eventType, float x, float y, float time, String level, int version, long session) { 56 Event event = new Event(); 57 event.x = x; 58 event.y = y; 59 event.time = time; 60 event.level = level; 61 event.version = version; 62 event.session = session; 63 switch (eventType) { 64 case EVENT_DEATH: 65 event.eventType = "death"; 66 break; 67 case EVENT_BEAT_LEVEL: 68 event.eventType = "beatLevel"; 69 break; 70 case EVENT_BEAT_GAME: 71 event.eventType = "beatGame"; 72 break; 73 } 74 synchronized(mLock) { 75 mEvents.add(event); 76 mLock.notifyAll(); 77 } 78 } 79 80 public void recordEvent(Event event) { 81 URL serverAddress = null; 82 HttpURLConnection connection = null; 83 84 if (REPORT_SERVER != null) { 85 try { 86 serverAddress = new URL(REPORT_SERVER + "?" 87 + "event=" + event.eventType 88 + "&x=" + event.x 89 + "&y=" + event.y 90 + "&time=" + event.time 91 + "&level=" + URLEncoder.encode(event.level, "UTF-8") 92 + "&version=" + event.version 93 + "&session=" + event.session); 94 //set up out communications stuff 95 connection = null; 96 97 //Set up the initial connection 98 connection = (HttpURLConnection)serverAddress.openConnection(); 99 100 connection.setRequestMethod("GET"); 101 connection.setDoOutput(true); 102 connection.setReadTimeout(0); 103 104 connection.connect(); 105 final int response = connection.getResponseCode(); 106 DebugLog.d("Report Event", event.eventType + " " + response + ":" + connection.getURL().toString()); 107 108 109 110 } catch (Exception e) { 111 // This code can silently fail. 112 //e.printStackTrace(); 113 } 114 finally 115 { 116 //close the connection 117 connection.disconnect(); 118 connection = null; 119 } 120 } 121 122 } 123 124 public void stop() { 125 synchronized(mLock) { 126 mDone = true; 127 mLock.notifyAll(); 128 } 129 } 130 131 } 132