Advertisement
Guest User

Untitled

a guest
Jun 27th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.61 KB | None | 0 0
  1. package ru.mityaresh.cadromonitor;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.content.res.Resources;
  6. import android.graphics.Bitmap;
  7. import android.graphics.BitmapFactory;
  8. import android.os.AsyncTask;
  9. import android.os.Bundle;
  10. import android.util.Log;
  11. import android.widget.ImageView;
  12. import org.json.JSONException;
  13. import org.json.JSONObject;
  14. import java.io.IOException;
  15. import java.util.Timer;
  16. import java.util.TimerTask;
  17.  
  18. public class Snapshot extends Activity {
  19.  
  20.     private ImageView mImageView;
  21.  
  22.     @Override
  23.     protected void onCreate(Bundle savedInstanceState) {
  24.         super.onCreate(savedInstanceState);
  25.         setContentView(R.layout.activity_snapshot);
  26.  
  27.         mImageView = (ImageView) findViewById(R.id.snapshotfrc);
  28.  
  29.         TimerTask timerTask = new TimerTask() {
  30.             @Override
  31.             public void run() {
  32.                 GetPic getPicTask = new GetPic();
  33.                 getPicTask.execute();
  34.             }
  35.         };
  36.  
  37.         Timer timer = new Timer();
  38.  
  39.         timer.schedule(timerTask, 0, 1000);
  40.  
  41.     }
  42.  
  43.     class GetPic extends AsyncTask<Void, Void, Bitmap> {
  44.  
  45.         @Override
  46.         protected Bitmap doInBackground(Void... voids) {
  47.             return showPic();
  48.         }
  49.  
  50.         @Override
  51.         protected void onPostExecute(final Bitmap result) {
  52.             try {
  53.                 mImageView.setImageBitmap(result);
  54.             } catch (NullPointerException e) {
  55.                 e.printStackTrace();
  56.             }
  57.         }
  58.     }
  59.  
  60.  
  61.     Bitmap showPic() {
  62.         Context context = getApplicationContext();
  63.         if (NetUtils.isNetworkConnected(context) && NetUtils.isInternetAvailable()) {
  64.             String result = "";
  65.             try {
  66.                 result = NetUtils.httpGet(NetUtils.SPACEAPI_ENDPOINT);
  67.             } catch (IOException e) {
  68.                 Log.e("Snapshot", e.getMessage());
  69.             }
  70.  
  71.             Resources res = context.getResources();
  72.  
  73.             Boolean isOpen = false;
  74.             Bitmap snap = BitmapFactory.decodeResource(res, R.drawable.nosnap);
  75.             try {
  76.                 JSONObject jObject = new JSONObject(result);
  77.                 isOpen = jObject.getJSONObject("state").getBoolean("open");
  78.                 if (isOpen) {
  79.                     String camUrl = jObject.getJSONArray("cam").get(0).toString();
  80.                     snap = NetUtils.getCamImage(camUrl);
  81.                 }
  82.                 return snap;
  83.             } catch (JSONException e) {
  84.                 Log.e("Snapshot", e.getMessage());
  85.             }
  86.         }
  87.         return null;
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement