PageRenderTime 23ms CodeModel.GetById 7ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 1ms

/v3.2/nimbits-android/src/com/nimbits/android/activity/MapViewActivity.java

http://nimbits-server.googlecode.com/
Java | 312 lines | 212 code | 71 blank | 29 comment | 18 complexity | f5f7862266f24bf7e2e867d9196b204e MD5 | raw file
  1package com.nimbits.android.activity;
  2
  3import android.app.AlertDialog;
  4import android.content.Intent;
  5import android.graphics.drawable.Drawable;
  6import android.os.Bundle;
  7import android.util.Log;
  8import android.view.GestureDetector;
  9import android.view.GestureDetector.OnGestureListener;
 10import android.view.*;
 11import com.google.android.maps.*;
 12import com.google.gson.Gson;
 13import com.google.gson.GsonBuilder;
 14import com.google.gson.reflect.TypeToken;
 15import com.nimbits.android.MapOverlay;
 16import com.nimbits.android.R;
 17import com.nimbits.android.account.OwnerAccountFactory;
 18import com.nimbits.client.model.Const;
 19import com.nimbits.client.model.common.CommonFactoryLocator;
 20import com.nimbits.client.model.point.Point;
 21import com.nimbits.client.model.point.PointModel;
 22import com.nimbits.client.model.point.PointName;
 23import com.nimbits.client.model.value.Value;
 24
 25import java.lang.reflect.Type;
 26import java.util.LinkedList;
 27import java.util.List;
 28
 29public class MapViewActivity extends MapActivity  implements OnGestureListener {
 30    private List<Overlay> mapOverlays;
 31    private Drawable drawable;
 32    private Drawable drawableAlarm;
 33    private MapOverlay itemizedOverlay;
 34    private MapOverlay itemizedAlarmOverlay;
 35    private MapController mc;
 36    private String json;
 37    private Point point;
 38
 39    private final Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss Z").serializeNulls().create();
 40    private GestureDetector gestureScanner;
 41    private String baseURL;
 42
 43    private void informUser()
 44    {
 45        AlertDialog.Builder dialog = new AlertDialog.Builder(MapViewActivity.this);
 46        dialog.setTitle("No GPS Data to Show");
 47        dialog.setMessage("In order to show your point data on a map, you must record at least one value with GPS Coordinates. Please visit www.nimbits.com to learn more");
 48        dialog.show();
 49    }
 50
 51    public void onCreate(Bundle savedInstanceState) {
 52        super.onCreate(savedInstanceState);
 53        setContentView(R.layout.map_view);
 54        gestureScanner = new GestureDetector(this);
 55        Bundle b = getIntent().getExtras();
 56        PointName pointName = (PointName) CommonFactoryLocator.getInstance().createPointName(b.getString(Const.PARAM_POINT));
 57
 58        json = b.getString(Const.PARAM_JSON);
 59        //String Category = b.getString("Category");
 60        String type = b.getString(Const.PARAM_TYPE);
 61        baseURL = b.getString(baseURL);
 62        MapView mapView = (MapView) findViewById(R.id.mapview1);
 63        mapView.setBuiltInZoomControls(true);
 64        mapView.setClickable(true);
 65        mc = mapView.getController();
 66
 67        mapOverlays = mapView.getOverlays();
 68
 69        drawable = this.getResources().getDrawable(R.drawable.greenflag);
 70
 71        drawableAlarm = this.getResources().getDrawable(R.drawable.redflag);
 72
 73        itemizedOverlay = new MapOverlay(drawable, MapViewActivity.this);
 74
 75        itemizedAlarmOverlay = new MapOverlay(drawableAlarm, MapViewActivity.this);
 76
 77        boolean additems = false;
 78        boolean addalarms  = false;
 79
 80
 81        if (type.equals("point"))
 82        {
 83            if (json != null)
 84            {
 85
 86                point = gson.fromJson(json, PointModel.class);
 87            }
 88
 89
 90            List<Value> values = null;
 91            //try {
 92            values = OwnerAccountFactory.getInstance().getNimbitsClient(MapViewActivity.this, baseURL).getSeries(pointName, 10);
 93            if (values != null)
 94            {
 95                if (values.size() > 0)
 96                {
 97                    for (Value v: values)
 98                    {
 99                        Log.v("V","" + v.getValue() + v.getLatitude() + v.getLongitude());
100                        if (v.getLatitude() != 0 && v.getLongitude() !=0)
101                        {
102                            Log.v("map", "adding to map");
103                            int lat = (int) (v.getLatitude() * 1e6);
104                            int lng = (int) (v.getLongitude() * 1e6);
105
106                            GeoPoint geopoint = new GeoPoint(lat,lng);
107                            OverlayItem overlayitem = new OverlayItem(geopoint,v.getTimestamp().toString(),v.getValue() + " " + v.getNote());
108
109                            //if (! point.i
110                            {
111                                overlayitem.setMarker(drawable);
112
113                            }
114//							else
115//							{
116//								Log.v("map", "adding alarm to map");
117//								overlayitem.setMarker(drawableAlarm);
118//							}
119                            itemizedOverlay.addOverlay(overlayitem);
120                            mc.animateTo(geopoint);
121                            mc.setCenter(geopoint);
122                            mc.setZoom(15);
123
124
125
126                        }
127
128                    }
129
130                    mapOverlays.add(itemizedOverlay);
131                }
132                else
133                {
134                    informUser();
135                }
136
137
138
139            }
140            else
141            {
142                Type pointListType = new TypeToken<LinkedList<Point>>() {}.getType();
143                LinkedList<Point> points = gson.fromJson(json, pointListType);
144
145                Log.v("j",json);
146                if (points.size() > 0)
147                {
148                    for (Point p : points)
149                    {
150                        Log.v("map", "adding to map");
151                        Log.v("map",""+p.getLastLat());
152                        Log.v("map",""+ p.getLastLng());
153                        Value value;
154                        value =  OwnerAccountFactory.getInstance().getNimbitsClient(MapViewActivity.this, baseURL).getCurrentRecordedValue(pointName);
155
156
157
158                        if (p.getLastLat() != 0 && p.getLastLng() !=0)
159                        {
160                            Log.v("map","lat" +  p.getLastLat() );
161                            Log.v("map","lng" +  p.getLastLng() );
162
163                            int lat = (int) (p.getLastLat() * 1e6);
164                            int lng = (int) (p.getLastLng() * 1e6);
165
166                            GeoPoint geopoint = new GeoPoint(lat,lng);
167                            OverlayItem overlayitem = new OverlayItem(geopoint,p.getName().getValue(),value.getValue() + " " + value.getNote());
168//
169//						if (! p.isAlarm())
170//						{
171//							overlayitem.setMarker(drawable );
172//							itemizedOverlay.addOverlay(overlayitem);
173//						    additems = true;
174//						}
175//						else
176//						{
177                            Log.v("map", "adding alarm to map");
178                            overlayitem.setMarker(drawableAlarm);
179                            itemizedAlarmOverlay.addOverlay(overlayitem);
180                            addalarms = true;
181                            //}
182
183                            mc.animateTo(geopoint);
184                            mc.setCenter(geopoint);
185                            mc.setZoom(15);
186                        }
187
188                    }
189                    if (additems)
190                    {
191                        mapOverlays.add(itemizedOverlay);
192
193                    }
194
195                    if (addalarms)
196                    {
197                        mapOverlays.add(itemizedAlarmOverlay);
198
199                    }
200
201                    //mapOverlays.add(itemizedOverlay);
202                }
203                else
204                {
205                    informUser();
206                }
207            }
208        }
209
210
211
212
213
214    }
215    private void loadCategories()
216    {
217        Bundle b = new Bundle();
218        Intent intent = new Intent();
219         b.putBoolean(Const.PARAM_RELOAD, false);
220        b.putString(Const.PARAM_BASE_URL,baseURL);
221        intent.putExtras( b);
222        intent.setClass( MapViewActivity.this , StartActivity.class);
223        startActivity(intent);
224        finish();
225    }
226
227    @Override
228    protected boolean isRouteDisplayed() {
229        // TODO Auto-generated method stub
230        return false;
231    }
232    @Override
233    public boolean onOptionsItemSelected(MenuItem item) {
234        // Handle item selection
235        switch (item.getItemId()) {
236
237            case R.id.main_menu:
238                loadCategories();
239                return true;
240
241
242            default:
243                return super.onOptionsItemSelected(item);
244        }
245    }
246    @Override
247    public boolean onCreateOptionsMenu(Menu menu) {
248        MenuInflater inflater = getMenuInflater();
249        inflater.inflate(R.menu.map_menu, menu);
250        return true;
251    }
252    @Override
253    public void onDestroy(){
254
255        super.onDestroy();
256
257        this.finish();
258    }
259
260
261
262
263
264    public void onTerminate() {
265
266        // clean up application global
267
268        //super.onStop();
269
270        super.onDestroy();
271
272        this.finish();          // /// close the application
273
274    }
275    @Override
276    public boolean onTouchEvent(MotionEvent event) {
277
278        return gestureScanner.onTouchEvent(event);
279
280    }
281    public boolean onDown(MotionEvent e) {
282        // TODO Auto-generated method stub
283        return false;
284    }
285
286    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
287                           float velocityY) {
288        // TODO Auto-generated method stub
289        return false;
290    }
291
292    public void onLongPress(MotionEvent e) {
293        // TODO Auto-generated method stub
294
295    }
296
297    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
298                            float distanceY) {
299        // TODO Auto-generated method stub
300        return false;
301    }
302
303    public void onShowPress(MotionEvent e) {
304        // TODO Auto-generated method stub
305
306    }
307
308    public boolean onSingleTapUp(MotionEvent e) {
309        // TODO Auto-generated method stub
310        return false;
311    }
312}