Przeglądaj źródła

Add journeys to places

master
Chris Smith 14 lat temu
rodzic
commit
cb536e750a

+ 1
- 0
code/PlacesDisplay/AndroidManifest.xml Wyświetl plik

@@ -15,6 +15,7 @@
15 15
 
16 16
     <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
17 17
     <uses-permission android:name="uk.co.md87.android.contextanalyser.READ_PLACES"/>
18
+    <uses-permission android:name="uk.co.md87.android.contextanalyser.READ_JOURNEYS"/>
18 19
     <uses-permission android:name="android.permission.INTERNET"/>
19 20
     <uses-sdk android:minSdkVersion="3" />
20 21
 

BIN
code/PlacesDisplay/dist/PlacesDisplay.apk Wyświetl plik


+ 111
- 0
code/PlacesDisplay/src/uk/co/md87/android/placesdisplay/JourneysOverlay.java Wyświetl plik

@@ -0,0 +1,111 @@
1
+/*
2
+ * Copyright (c) 2009-2010 Chris Smith
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package uk.co.md87.android.placesdisplay;
24
+
25
+import android.content.Context;
26
+import android.database.Cursor;
27
+import android.graphics.Canvas;
28
+import android.graphics.Paint;
29
+import android.graphics.Path;
30
+import android.graphics.Point;
31
+import android.util.Log;
32
+import com.google.android.maps.MapView;
33
+import com.google.android.maps.Overlay;
34
+import com.google.android.maps.OverlayItem;
35
+import java.util.ArrayList;
36
+import java.util.HashMap;
37
+import java.util.List;
38
+import java.util.Map;
39
+import uk.co.md87.android.contextapi.ContextApi;
40
+import uk.co.md87.android.contextapi.ContextApi.Journeys.ColumnNames;
41
+
42
+/**
43
+ * An overlay for showing journeys between places.
44
+ *
45
+ * @author chris
46
+ */
47
+public class JourneysOverlay extends Overlay {
48
+
49
+    private final Map<OverlayItem, Map<OverlayItem, Integer>> journeys
50
+            = new HashMap<OverlayItem, Map<OverlayItem, Integer>>();
51
+    private int max = 0;
52
+
53
+    public JourneysOverlay(final Context context, final Map<Integer, OverlayItem> places) {
54
+        final Cursor cursor = context.getContentResolver().query(ContextApi.Journeys.CONTENT_URI,
55
+                new String[] { ColumnNames.START, ColumnNames.END, ColumnNames.NUMBER },
56
+                null, null, null);
57
+
58
+        if (cursor.moveToFirst()) {
59
+            final int startColumn = cursor.getColumnIndex(ColumnNames.START);
60
+            final int endColumn = cursor.getColumnIndex(ColumnNames.END);
61
+            final int numberColumn = cursor.getColumnIndex(ColumnNames.NUMBER);
62
+
63
+            do {
64
+                final int start = cursor.getInt(startColumn);
65
+                final int end = cursor.getInt(endColumn);
66
+                final OverlayItem a = places.get(Math.min(start, end)),
67
+                        b = places.get(Math.max(start, end));
68
+
69
+                if (!journeys.containsKey(a)) {
70
+                    journeys.put(a, new HashMap<OverlayItem, Integer>());
71
+                }
72
+
73
+                final Map<OverlayItem, Integer> map = journeys.get(a);
74
+
75
+                if (!map.containsKey(b)) {
76
+                    map.put(b, 0);
77
+                }
78
+
79
+                final int total = map.get(b) + cursor.getInt(numberColumn);
80
+                map.put(b, total);
81
+                max = Math.max(max, total);
82
+            } while (cursor.moveToNext());
83
+        }
84
+    }
85
+
86
+    @Override
87
+    public void draw(Canvas canvas, MapView map, boolean shadow) {
88
+        super.draw(canvas, map, shadow);
89
+
90
+        final Paint paint = new Paint();
91
+        paint.setARGB(255, 255, 0, 0);
92
+        paint.setStyle(Paint.Style.FILL_AND_STROKE);
93
+
94
+        for (Map.Entry<OverlayItem, Map<OverlayItem, Integer>> start
95
+                : journeys.entrySet()) {
96
+            final Point spoint = map.getProjection().toPixels(start.getKey().getPoint(), null);
97
+            for (Map.Entry<OverlayItem, Integer> entry : start.getValue().entrySet()) {
98
+                final Point epoint = map.getProjection().toPixels(entry.getKey().getPoint(), null);
99
+
100
+                final Paint mypaint = new Paint(paint);
101
+
102
+                mypaint.setStrokeJoin(Paint.Join.ROUND);
103
+                mypaint.setStrokeCap(Paint.Cap.ROUND);
104
+                mypaint.setStrokeWidth(10 * entry.getValue() / (float) max);
105
+
106
+                canvas.drawLine(spoint.x, spoint.y, epoint.x, epoint.y, mypaint);
107
+            }
108
+        }
109
+    }
110
+
111
+}

+ 14
- 3
code/PlacesDisplay/src/uk/co/md87/android/placesdisplay/PlacesDisplay.java Wyświetl plik

@@ -41,7 +41,9 @@ import com.google.android.maps.Overlay;
41 41
 import com.google.android.maps.OverlayItem;
42 42
 
43 43
 import java.util.Date;
44
+import java.util.HashMap;
44 45
 import java.util.List;
46
+import java.util.Map;
45 47
 
46 48
 import uk.co.md87.android.contextapi.ContextApi;
47 49
 import uk.co.md87.android.contextapi.ContextApi.Places.ColumnNames;
@@ -104,8 +106,8 @@ public class PlacesDisplay extends MapActivity {
104 106
 
105 107
         final Cursor cursor = managedQuery(ContextApi.Places.CONTENT_URI,
106 108
                 new String[] { ColumnNames.LATITUDE, ColumnNames.LONGITUDE,
107
-                ColumnNames.NAME, ColumnNames.LAST_VISIT, ColumnNames.VISIT_COUNT },
108
-                null, null, null);
109
+                ColumnNames._ID, ColumnNames.NAME, ColumnNames.LAST_VISIT,
110
+                ColumnNames.VISIT_COUNT }, null, null, null);
109 111
 
110 112
         final java.text.DateFormat dateFormat = DateFormat.getDateFormat(this);
111 113
         final java.text.DateFormat timeFormat = DateFormat.getTimeFormat(this);
@@ -114,15 +116,20 @@ public class PlacesDisplay extends MapActivity {
114 116
             final int latitudeColumn = cursor.getColumnIndex(ColumnNames.LATITUDE);
115 117
             final int longitudeColumn = cursor.getColumnIndex(ColumnNames.LONGITUDE);
116 118
             final int nameColumn = cursor.getColumnIndex(ColumnNames.NAME);
119
+            final int idColumn = cursor.getColumnIndex(ColumnNames._ID);
117 120
             final int lastVisitColumn = cursor.getColumnIndex(ColumnNames.LAST_VISIT);
118 121
             final int visitCountColumn = cursor.getColumnIndex(ColumnNames.VISIT_COUNT);
119 122
 
123
+            final Map<Integer, OverlayItem> places
124
+                    = new HashMap<Integer, OverlayItem>(cursor.getCount());
125
+
120 126
             do {
121 127
                 final double latitude = cursor.getDouble(latitudeColumn);
122 128
                 final double longitude = cursor.getDouble(longitudeColumn);
123 129
                 final String name = cursor.getString(nameColumn);
124 130
                 final long lastVisit = cursor.getLong(lastVisitColumn);
125 131
                 final int visitCount = cursor.getInt(visitCountColumn);
132
+                final int id = cursor.getInt(idColumn);
126 133
 
127 134
                 final GeoPoint point = new GeoPoint((int) (latitude * 1000000),
128 135
                         (int) (longitude * 1000000));
@@ -135,9 +142,13 @@ public class PlacesDisplay extends MapActivity {
135 142
                         + " at " + timeFormat.format(date));
136 143
 
137 144
                 overlay.addOverlay(overlayitem);
145
+
146
+                places.put(id, overlayitem);
138 147
             } while (cursor.moveToNext());
148
+
149
+            mapOverlays.add(new JourneysOverlay(this, places));
150
+            mapOverlays.add(overlay);
139 151
         }
140
-        mapOverlays.add(overlay);
141 152
     }
142 153
 
143 154
     @Override

Ładowanie…
Anuluj
Zapisz