Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Stuff in the rest of the thing... Really, this doesn't do anything ye…

…t beyond "work as expected".
  • Loading branch information...
commit 0fc9e7cc3eceeee1d506907fd9a2f754df2d5430 1 parent 7354b90
Don Kelly authored April 14, 2012
6  AndroidManifest.xml
@@ -5,6 +5,7 @@
5 5
     android:versionName="1.0" >
6 6
 
7 7
     <uses-sdk android:minSdkVersion="14" />
  8
+    <uses-permission android:name="android.permission.INTERNET"/>
8 9
 
9 10
     <application
10 11
         android:icon="@drawable/ic_launcher"
@@ -18,6 +19,11 @@
18 19
                 <category android:name="android.intent.category.LAUNCHER" />
19 20
             </intent-filter>
20 21
         </activity>
  22
+        <!--
  23
+        <receiver android:name=".LoggingReceiver" android:enabled="true">
  24
+            <intent-filter><action android:name="ca.strangeware.transit.answers.nearby" /></intent-filter>
  25
+        </receiver>
  26
+        -->
21 27
         <service android:name=".TransitService">
22 28
             <intent-filter><action android:name="ca.strangeware.transit.arrivals" /></intent-filter>
23 29
             <intent-filter><action android:name="ca.strangeware.transit.arrivals.watch" /></intent-filter>
10  res/layout/main.xml
@@ -8,11 +8,17 @@
8 8
         android:id="@+id/button1"
9 9
         android:layout_width="wrap_content"
10 10
         android:layout_height="wrap_content"
11  
-        android:text="Make it go" />
  11
+        android:text="ARRIVALS" />
12 12
     <Button
13 13
         android:id="@+id/button2"
14 14
         android:layout_width="wrap_content"
15 15
         android:layout_height="wrap_content"
16  
-        android:text="Another intent" />
  16
+        android:text="NEARBY" />
  17
+
  18
+    <ListView
  19
+        android:id="@+id/listView1"
  20
+        android:layout_width="match_parent"
  21
+        android:layout_height="wrap_content" >
  22
+    </ListView>
17 23
     
18 24
 </LinearLayout>
76  src/ca/strangeware/transit/TransitActivity.java
... ...
@@ -1,25 +1,88 @@
1 1
 package ca.strangeware.transit;
2 2
 
  3
+import java.util.Date;
  4
+import java.util.ArrayList;
  5
+import java.util.HashMap;
  6
+import java.util.Map;
  7
+import java.util.UUID;
  8
+
3 9
 import android.app.Activity;
4 10
 import android.content.Intent;
5 11
 import android.os.Bundle;
6 12
 import android.view.View;
7 13
 import android.view.View.OnClickListener;
8 14
 import android.widget.Button;
  15
+import android.widget.ListView;
  16
+import android.widget.SimpleAdapter;
9 17
 
10  
-public class TransitActivity extends Activity {
  18
+public class TransitActivity extends Activity implements TransitHelper.Callback {
  19
+	public void onAnswer(Intent intent) {
  20
+		if (intent.getAction().equals(TransitHelper.NEARBY_INTENT)) {
  21
+			showStop(intent);
  22
+		} else if (intent.getAction().equals(TransitHelper.ARRIVALS_INTENT)) {
  23
+			showTrip(intent);
  24
+		}
  25
+	}
  26
+	
  27
+	private TransitHelper _helper;
  28
+	private ArrayList<Map<String, String>> _list = new ArrayList<Map<String,String>>();
  29
+	private SimpleAdapter _adapter;
  30
+	private ListView _listView;
  31
+	
  32
+	private void showStop(Intent intent) {
  33
+		Bundle extras = intent.getExtras();
  34
+		HashMap<String, String> item = new HashMap<String, String>();
  35
+		item.put("title", extras.getString("stop.name"));
  36
+		_list.add(item);
  37
+		_adapter.notifyDataSetChanged();
  38
+	}
  39
+	
  40
+	private int minutesFromNow(int secondsSinceMidnight) {
  41
+		Date now = new Date();
  42
+		int elapsedSinceMidnight = now.getHours() * 3600 + now.getMinutes() * 60 + now.getSeconds();
  43
+		return (secondsSinceMidnight - elapsedSinceMidnight) / 60;
  44
+	}
  45
+	
  46
+	private void showTrip(Intent intent) {
  47
+		Bundle extras = intent.getExtras();
  48
+		HashMap<String, String> item = new HashMap<String, String>();
  49
+		item.put("title", String.format("%d %s (%d minutes)",
  50
+				extras.getInt("trip.route"),
  51
+				extras.getString("trip.headsign"),
  52
+				minutesFromNow(extras.getInt("arrival"))));
  53
+		_list.add(item);
  54
+		_adapter.notifyDataSetChanged();
  55
+	}
  56
+	
11 57
     /** Called when the activity is first created. */
12 58
     @Override
13 59
     public void onCreate(Bundle savedInstanceState) {
14 60
         super.onCreate(savedInstanceState);
  61
+
  62
+        _helper = new TransitHelper(this);
  63
+        _helper.register(this, new String[] { TransitHelper.ARRIVALS_INTENT });
  64
+        
15 65
         setContentView(R.layout.main);
16 66
         
17  
-        Button b = (Button) findViewById(R.id.button1);
  67
+        _adapter = new SimpleAdapter(this, _list, android.R.layout.simple_list_item_1, new String[] {"title"}, new int[] {android.R.id.text1});
  68
+        
  69
+        _listView = (ListView) findViewById(R.id.listView1);
  70
+        _listView.setAdapter(_adapter);
  71
+        
  72
+        registerListeners();
  73
+    }
  74
+
  75
+	private void registerListeners() {
  76
+		Button b = (Button) findViewById(R.id.button1);
18 77
         b.setOnClickListener(new OnClickListener() {
19 78
 			
20 79
 			@Override
21 80
 			public void onClick(View v) {
22  
-				Intent i = new Intent("ca.strangeware.transit.TransitService");
  81
+				Intent i = new Intent("ca.strangeware.transit.arrivals");
  82
+				i.putExtra("request_id", UUID.randomUUID().toString());
  83
+				i.putExtra("stop.number", 3010);
  84
+				_list.clear();
  85
+				_adapter.notifyDataSetInvalidated();
23 86
 				startService(i);
24 87
 			}
25 88
 		});
@@ -29,8 +92,13 @@ public void onClick(View v) {
29 92
 			@Override
30 93
 			public void onClick(View v) {
31 94
 				Intent i = new Intent("ca.strangeware.transit.nearby");
  95
+				i.putExtra("request_id", UUID.randomUUID().toString());
  96
+				i.putExtra("location.lat", 45.40412);
  97
+				i.putExtra("location.lon", -75.7405);
  98
+				_list.clear();
  99
+				_adapter.notifyDataSetInvalidated();
32 100
 				startService(i);
33 101
 			}
34 102
 		});
35  
-    }
  103
+	}
36 104
 }
43  src/ca/strangeware/transit/TransitHelper.java
... ...
@@ -0,0 +1,43 @@
  1
+package ca.strangeware.transit;
  2
+
  3
+import android.app.Activity;
  4
+import android.content.BroadcastReceiver;
  5
+import android.content.Context;
  6
+import android.content.Intent;
  7
+import android.content.IntentFilter;
  8
+
  9
+public class TransitHelper {
  10
+	public interface Callback {
  11
+		void onAnswer(Intent i);
  12
+	}
  13
+	
  14
+	private Callback _callback;
  15
+	private BroadcastReceiver _receiver = new BroadcastReceiver() {
  16
+		@Override
  17
+		public void onReceive(Context context, Intent intent) {
  18
+			_callback.onAnswer(intent);
  19
+		}
  20
+	};
  21
+	private Activity _activity;
  22
+		
  23
+	public TransitHelper(Callback c) {
  24
+		_callback = c;
  25
+	}
  26
+	
  27
+	public static final String NEARBY_INTENT = "ca.strangeware.transit.answers.nearby";
  28
+	public static final String ARRIVALS_INTENT = "ca.strangeware.transit.answers.arrivals";
  29
+	
  30
+	public void register(Activity act, String[] intents) {
  31
+		_activity = act;
  32
+		for (String k : intents) {
  33
+			IntentFilter filt = new IntentFilter(k);
  34
+			_activity.registerReceiver(_receiver, filt);
  35
+		}
  36
+	}
  37
+	
  38
+	public void unregister() {
  39
+		if (null != _activity) {
  40
+			_activity.unregisterReceiver(_receiver);
  41
+		}
  42
+	}
  43
+}
135  src/ca/strangeware/transit/TransitService.java
... ...
@@ -1,26 +1,146 @@
1 1
 package ca.strangeware.transit;
2 2
 
  3
+import java.io.BufferedReader;
  4
+import java.io.IOException;
  5
+import java.io.InputStreamReader;
  6
+import java.util.HashMap;
  7
+
  8
+import org.apache.http.HttpResponse;
  9
+import org.apache.http.client.ClientProtocolException;
  10
+import org.apache.http.client.HttpClient;
  11
+import org.apache.http.client.methods.HttpGet;
  12
+import org.apache.http.impl.client.DefaultHttpClient;
  13
+import org.json.JSONArray;
  14
+import org.json.JSONException;
  15
+import org.json.JSONObject;
  16
+
3 17
 import android.app.Service;
4 18
 import android.content.Intent;
  19
+import android.os.Bundle;
5 20
 import android.os.Handler;
6 21
 import android.os.HandlerThread;
7 22
 import android.os.IBinder;
8 23
 import android.os.Looper;
9 24
 import android.os.Message;
10 25
 import android.os.Process;
  26
+import android.util.Log;
11 27
 
12 28
 public class TransitService extends Service {
  29
+	private static final String API = "http://octranspo-api.heroku.com/";
  30
+	
  31
+	private static final String ACTION_NEARBY = "ca.strangeware.transit.nearby";
  32
+	private static final String ACTION_ARRIVALS = "ca.strangeware.transit.arrivals";
  33
+	private static final String ACTION_ARRIVALS_WATCH = "ca.strangeware.transit.arrivals.watch";
  34
+	
  35
+	private static final int WHAT_UNKNOWN = 0;
  36
+	private static final int WHAT_NEARBY= 1;
  37
+	private static final int WHAT_ARRIVALS = 2;
  38
+	private static final int WHAT_ARRIVALS_WATCH = 3;	
  39
+	
13 40
 	private Looper _looper;
14 41
 	private OurHandler _handler;
15  
-	
  42
+	private HashMap<String, Integer> _whats = new HashMap<String, Integer>(){{
  43
+		put(ACTION_NEARBY, WHAT_NEARBY);
  44
+		put(ACTION_ARRIVALS, WHAT_ARRIVALS);
  45
+		put(ACTION_ARRIVALS_WATCH, WHAT_ARRIVALS_WATCH);
  46
+	}};
  47
+		
16 48
 	private final class OurHandler extends Handler {
  49
+		private HttpClient _client;
17 50
 		public OurHandler(Looper l) {
18 51
 			super(l);
  52
+			_client = new DefaultHttpClient();
19 53
 		}
20 54
 		
21 55
 		@Override
22 56
 		public void handleMessage(Message m) {
  57
+			try {
  58
+				switch (m.what) {
  59
+				case WHAT_NEARBY:
  60
+					nearby(m.getData());
  61
+					break;
  62
+				case WHAT_ARRIVALS:
  63
+					arrivals(m.getData());
  64
+					break;
  65
+				case WHAT_ARRIVALS_WATCH:
  66
+					//nearbyStopsRepeat();
  67
+					break;
  68
+				default:
  69
+					return;
  70
+				}
  71
+			} catch (ClientProtocolException e) {
  72
+				e.printStackTrace();
  73
+			} catch (IOException e) {
  74
+				e.printStackTrace();
  75
+			} catch (JSONException e){
  76
+				e.printStackTrace();
  77
+			}
  78
+		}
  79
+		
  80
+		private JSONArray getArray(String url) throws JSONException, IOException {
  81
+			String endpoint = TransitService.API + url;
  82
+			HttpGet get = new HttpGet(endpoint);
23 83
 			
  84
+			Log.d("Starting", "Starting the request");
  85
+			HttpResponse response = _client.execute(get);
  86
+			BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
  87
+			Log.d("Received Data", "Data received");
  88
+			
  89
+			return new JSONArray(reader.readLine());			
  90
+		}
  91
+		
  92
+		public void nearby(Bundle args) throws ClientProtocolException, IOException, JSONException {
  93
+			String url = String.format("stops_nearby/%f/%f", args.getDouble("location.lat"), args.getDouble("location.lon"));
  94
+			Log.d("Service", "URL: " + url);
  95
+			JSONArray json = getArray(url);
  96
+			for(int i = 0, len = json.length(); i < len; i++) {
  97
+				Bundle extras = new Bundle();
  98
+				Intent intent = new Intent("ca.strangeware.transit.answers.nearby");
  99
+				
  100
+				JSONObject o = json.getJSONObject(i);
  101
+				JSONObject stop = o.getJSONObject("stop");
  102
+
  103
+				extras.putString("request_id", args.getString("request_id"));
  104
+				extras.putInt("distance", o.getInt("distance"));
  105
+
  106
+				extras.putInt("stop.number", stop.getInt("number"));
  107
+				extras.putString("stop.name", stop.getString("name"));
  108
+				extras.putDouble("stop.lat", stop.getDouble("lat"));
  109
+				extras.putDouble("stop.lon", stop.getDouble("lon"));	
  110
+				
  111
+				intent.putExtras(extras);
  112
+				Log.d("Nearby", "Broadcasting!");
  113
+				sendBroadcast(intent);
  114
+			}
  115
+		}
  116
+		
  117
+		public void arrivals(Bundle args) throws JSONException, IOException  {
  118
+			String url = String.format("arrivals/%d", args.getInt("stop.number"));
  119
+			JSONArray json = getArray(url);
  120
+			for (int i = 0, len = json.length(); i < len; i++) {
  121
+				Bundle extras = new Bundle();
  122
+				Intent intent = new Intent("ca.strangeware.transit.answers.arrivals");
  123
+				JSONObject o = json.getJSONObject(i);
  124
+				JSONObject stop = o.getJSONObject("stop");
  125
+				JSONObject trip = o.getJSONObject("trip");
  126
+				
  127
+				extras.putString("request_id", args.getString("request_id"));
  128
+				
  129
+				extras.putInt("arrival", o.getInt("arrival"));
  130
+				extras.putInt("departure", o.getInt("departure"));
  131
+				
  132
+				extras.putInt("stop.number", stop.getInt("number"));
  133
+				extras.putString("stop.name", stop.getString("name"));
  134
+				
  135
+				extras.putInt("trip.route", trip.getInt("route"));
  136
+				extras.putString("trip.headsign", trip.getString("headsign"));
  137
+				
  138
+				intent.putExtras(extras);
  139
+				sendBroadcast(intent);
  140
+			}
  141
+		}
  142
+		
  143
+		public void nearbyStopsRepeat() throws IOException, ClientProtocolException, JSONException{
24 144
 		}
25 145
 	}
26 146
 	
@@ -30,6 +150,8 @@ public TransitService() {
30 150
 	@Override
31 151
 	public void onCreate() {
32 152
 		HandlerThread th = new HandlerThread("ServiceStartArguments", Process.THREAD_PRIORITY_BACKGROUND);
  153
+		th.start();
  154
+		
33 155
 		_looper = th.getLooper();
34 156
 		_handler = new OurHandler(_looper);
35 157
 	}
@@ -39,10 +161,19 @@ public IBinder onBind(Intent intent) {
39 161
 		return null;
40 162
 	}
41 163
 	
  164
+	private int makeWhatFromAction(String act) {
  165
+		int rv = WHAT_UNKNOWN;
  166
+		if ( _whats.containsKey(act) ) {
  167
+			rv = _whats.get(act);
  168
+		}
  169
+		return rv;
  170
+	}
  171
+	
42 172
 	@Override
43 173
 	public int onStartCommand(Intent i, int flags, int startId) {
44 174
 		Message m = _handler.obtainMessage();
45  
-		// TODO: setup stuff
  175
+		m.what = makeWhatFromAction(i.getAction());
  176
+		m.setData(i.getExtras());
46 177
 		_handler.sendMessage(m);
47 178
 		return START_STICKY;
48 179
 	}

0 notes on commit 0fc9e7c

Please sign in to comment.
Something went wrong with that request. Please try again.