PageRenderTime 62ms CodeModel.GetById 10ms app.highlight 47ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://nimbits-server.googlecode.com/
Java | 333 lines | 235 code | 88 blank | 10 comment | 12 complexity | 5904eb7edd0c6e9b63211e5a445f437d MD5 | raw file
  1package com.nimbits.android.activity;
  2
  3import android.app.Activity;
  4import android.app.Dialog;
  5import android.app.ProgressDialog;
  6import android.content.Intent;
  7import android.graphics.Bitmap;
  8import android.graphics.BitmapFactory;
  9import android.os.Bundle;
 10import android.os.Handler;
 11import android.os.Message;
 12import android.util.DisplayMetrics;
 13import android.util.Log;
 14import android.view.*;
 15import android.view.GestureDetector.OnGestureListener;
 16import android.widget.ImageView;
 17import com.google.gson.Gson;
 18import com.google.gson.GsonBuilder;
 19import com.google.gson.reflect.TypeToken;
 20import com.nimbits.android.R;
 21import com.nimbits.android.account.OwnerAccountFactory;
 22import com.nimbits.client.model.Const;
 23import com.nimbits.client.model.point.Point;
 24import com.nimbits.client.model.point.PointModel;
 25
 26import java.io.UnsupportedEncodingException;
 27import java.lang.reflect.Type;
 28import java.net.URLEncoder;
 29import java.util.LinkedList;
 30
 31
 32
 33public class ChartActivity  extends Activity   implements OnGestureListener {
 34
 35
 36    private final Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss Z").serializeNulls().create();
 37    private String pointlist ="";
 38
 39    private String category = "";
 40    private String chartTitle = "";
 41    private GestureDetector gestureScanner;
 42    private String baseURL;
 43    private  static final int timerInterval = 15000;
 44
 45    private static final int LoadDialogID = 1;
 46    private LoadThread loadThread;
 47    private ProgressDialog loadDialog;
 48    private final Handler timerHandler = new Handler();
 49    private String type;
 50    private String json;
 51    private int count = 10;
 52
 53    private final Runnable timerTask = new Runnable() {
 54        public void run() {
 55            Log.v("chart view timer", "tick");
 56            loadThread = new LoadThread(loadHandler);
 57            loadThread.start();
 58        }
 59    };
 60
 61    protected Dialog onCreateDialog(int id) {
 62        Dialog dialog = null;
 63
 64        switch(id) {
 65            case LoadDialogID:
 66                loadDialog = new ProgressDialog(ChartActivity.this);
 67                loadDialog.setMessage("Loading Chart");
 68                loadThread = new LoadThread(loadHandler);
 69                loadThread.start();
 70                return loadDialog;
 71            default:
 72                dialog = null;
 73        }
 74
 75
 76        return dialog;
 77    }
 78    private final Handler loadHandler = new Handler() {
 79
 80
 81        public void handleMessage(Message msg) {
 82
 83
 84            byte[] chartData = msg.getData().getByteArray(Const.PARAM_CHART_DATA);
 85            ImageView imgViewer = (ImageView) findViewById(R.id.chart_image);
 86            Bitmap bm = BitmapFactory.decodeByteArray(chartData, 0, chartData.length);
 87            DisplayMetrics dm = new DisplayMetrics();
 88            getWindowManager().getDefaultDisplay().getMetrics(dm);
 89
 90            imgViewer.setMinimumHeight(dm.heightPixels);
 91            imgViewer.setMinimumWidth(dm.widthPixels);
 92            imgViewer.setImageBitmap(bm);
 93
 94
 95            try {
 96                dismissDialog(LoadDialogID);
 97                removeDialog(LoadDialogID);
 98            } catch (Exception e) {
 99
100            }
101
102            timerHandler.removeCallbacks(timerTask);
103            timerHandler.postDelayed(timerTask, timerInterval);
104
105        }
106    };
107    private class LoadThread extends Thread {
108        final Handler m;
109        final static int STATE_DONE = 0;
110        final static int STATE_RUNNING = 1;
111        @SuppressWarnings("unused")
112        int mState;
113        @SuppressWarnings("unused")
114        int total;
115
116        LoadThread(Handler h) {
117            m = h;
118        }
119
120        private void update(byte[] chartData)
121        {
122            Message msg = m.obtainMessage();
123            Bundle b = new Bundle();
124            b.putByteArray(Const.PARAM_CHART_DATA, chartData);
125            msg.setData(b);
126
127            m.sendMessage(msg);
128        }
129
130
131
132
133        public void run() {
134            byte[] chartData = null;
135            try {
136                if (type.equals(Const.PARAM_POINT))
137                {
138                    Point point;
139                    if (json != null)
140                    {
141
142                        point = gson.fromJson(json, PointModel.class);
143                        pointlist = URLEncoder.encode(point.getName().getValue(),Const.CONST_ENCODING);
144                        chartTitle = URLEncoder.encode(point.getName().getValue(),Const.CONST_ENCODING);
145                    }
146
147
148                }
149                else
150                {
151                    Type pointListType = new TypeToken<LinkedList<Point>>() {}.getType();
152                    LinkedList<Point> points = gson.fromJson(json, pointListType);
153                    chartTitle =URLEncoder.encode(category,Const.CONST_ENCODING);
154
155                    if (points.size() > 0)
156                    {
157                        for (Point p : points)
158                        {
159
160
161                            pointlist += URLEncoder.encode(p.getName().getValue(),Const.CONST_ENCODING) +"%2C";
162
163
164
165                        }
166                        pointlist = pointlist.substring(0,pointlist.length()-1);
167
168                    }
169
170                }
171            } catch (UnsupportedEncodingException e) {
172                // TODO Auto-generated catch block
173                e.printStackTrace();
174            }
175
176            Log.v("chartact points", pointlist);
177
178            String params;
179
180            params = "points=" +   pointlist +
181                    "&cht=lc&chs=400x200" +
182                    "&chxt=y&autoscale=true" +
183                    "&chco=000000,00FF00,0000FF,FF0000,FF0066,FFCC33,663333,003333" +
184                    "&chtt=" + chartTitle +
185                    "&chdl=" +  pointlist.replace("%2C", "%7C") ;
186
187
188
189            try {
190
191                chartData=  OwnerAccountFactory.getInstance().getNimbitsClient(ChartActivity.this, baseURL).getBinaryFile(baseURL  + "/service/chartapi", params);
192            } catch (Exception e) {
193                e.printStackTrace();
194            }
195
196
197
198            update(chartData);
199
200        }
201    }
202
203
204    public void onCreate(Bundle savedInstanceState) {
205        super.onCreate(savedInstanceState);
206        setContentView(R.layout.chart_layout);
207        gestureScanner = new GestureDetector(this);
208        Bundle b = getIntent().getExtras();
209
210        json = b.getString(Const.PARAM_JSON);
211        category = b.getString(Const.PARAM_CATEGORY);
212        baseURL = b.getString(Const.PARAM_BASE_URL);
213        type = b.getString(Const.PARAM_TYPE);
214
215        showDialog(LoadDialogID);
216
217    }
218    @Override
219    public boolean onKeyDown(int keyCode, KeyEvent event)  {
220        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
221            loadCategories();
222            return true;
223
224
225        }
226
227        return super.onKeyDown(keyCode, event);
228    }
229    private void loadCategories() {
230        final Bundle b = new Bundle();
231        final Intent intent = new Intent();
232        b.putBoolean(Const.PARAM_RELOAD, false);
233        b.putString(Const.PARAM_CATEGORY,category);
234        b.putString(Const.PARAM_BASE_URL,baseURL);
235        intent.putExtras( b);
236        intent.setClass( ChartActivity.this , StartActivity.class);
237        startActivity(intent);
238        finish();
239    }
240
241    @Override
242    public boolean onOptionsItemSelected(final MenuItem item) {
243        // Handle item selection
244        switch (item.getItemId()) {
245
246            case R.id.main_menu:
247                loadCategories();
248                return true;
249
250
251            default:
252                return super.onOptionsItemSelected(item);
253        }
254    }
255    @Override
256    public boolean onCreateOptionsMenu(Menu menu) {
257        MenuInflater inflater = getMenuInflater();
258        inflater.inflate(R.menu.map_menu, menu);
259        return true;
260    }
261    @Override
262    public void onDestroy(){
263
264        super.onDestroy();
265
266        this.finish();
267    }
268
269
270
271    @Override
272    public void finish()
273    {
274        super.finish();
275        if (timerHandler != null)
276        {
277            timerHandler.removeCallbacks(timerTask);
278        }
279    }
280
281
282
283    public void onTerminate() {
284
285        // clean up application global
286
287        //super.onStop();
288
289        super.onDestroy();
290
291        this.finish();          // /// close the application
292
293    }
294
295    @Override
296    public boolean onTouchEvent(MotionEvent event) {
297
298        return gestureScanner.onTouchEvent(event);
299
300    }
301    public boolean onDown(MotionEvent e) {
302        // TODO Auto-generated method stub
303        return false;
304    }
305    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
306                           float velocityY) {
307        // TODO Auto-generated method stub
308
309
310        loadCategories();
311
312
313
314        return false;
315    }
316    public void onLongPress(MotionEvent e) {
317        // TODO Auto-generated method stub
318
319    }
320    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
321                            float distanceY) {
322        // TODO Auto-generated method stub
323        return false;
324    }
325    public void onShowPress(MotionEvent e) {
326        // TODO Auto-generated method stub
327
328    }
329    public boolean onSingleTapUp(MotionEvent e) {
330        // TODO Auto-generated method stub
331        return false;
332    }
333}