Advertisement
Guest User

Stackoverflow question

a guest
Sep 29th, 2013
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 25.56 KB | None | 0 0
  1. package com.example.free;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.util.ArrayList;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Locale;
  11.  
  12. import org.apache.http.HttpEntity;
  13. import org.apache.http.HttpResponse;
  14. import org.apache.http.HttpStatus;
  15. import org.apache.http.client.HttpClient;
  16. import org.apache.http.client.methods.HttpGet;
  17. import org.apache.http.client.methods.HttpPost;
  18. import org.apache.http.impl.client.DefaultHttpClient;
  19. import org.json.JSONArray;
  20. import org.json.JSONException;
  21. import org.json.JSONObject;
  22.  
  23. import android.content.Context;
  24. import android.content.Intent;
  25. import android.graphics.Bitmap;
  26. import android.graphics.BitmapFactory;
  27. import android.location.Address;
  28. import android.location.Criteria;
  29. import android.location.Geocoder;
  30. import android.location.Location;
  31. import android.location.LocationListener;
  32. import android.location.LocationManager;
  33. import android.net.Uri;
  34. import android.os.AsyncTask;
  35. import android.os.Bundle;
  36. import android.support.v4.app.Fragment;
  37. import android.util.Log;
  38. import android.view.LayoutInflater;
  39. import android.view.View;
  40. import android.view.View.OnClickListener;
  41. import android.view.ViewGroup;
  42. import android.widget.ImageView;
  43. import android.widget.ProgressBar;
  44. import android.widget.ScrollView;
  45. import android.widget.TextView;
  46. import android.widget.Toast;
  47.  
  48. import com.google.android.gms.maps.CameraUpdateFactory;
  49. import com.google.android.gms.maps.GoogleMap;
  50. import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
  51. import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
  52. import com.google.android.gms.maps.MapFragment;
  53. import com.google.android.gms.maps.model.BitmapDescriptorFactory;
  54. import com.google.android.gms.maps.model.LatLng;
  55. import com.google.android.gms.maps.model.Marker;
  56. import com.google.android.gms.maps.model.MarkerOptions;
  57.  
  58. public class MainActivity extends Fragment implements OnMapLongClickListener {
  59.  
  60.     public void ToastLoadShout(String msg) {
  61.         Toast.makeText(getActivity(), msg.toString(), Toast.LENGTH_LONG).show();
  62.     }
  63.  
  64.     // GoogleMap variables
  65.     // static final LatLng gps = new LatLng(0, 0);
  66.     static double lat = 0.0;
  67.     static double lon = 0.0;
  68.     private GoogleMap map;
  69.     Marker loc;
  70.     Marker lastOpened = null;
  71.     private HashMap<Marker, Integer> markerMap = new HashMap<Marker, Integer>();
  72.  
  73.     // For checking server's json output
  74.     // String datadata;
  75.  
  76.     // http request variables
  77.     HttpClient client;
  78.     HttpPost httppost;
  79.     HttpGet httpget;
  80.     JSONObject json;
  81.  
  82.     // items holding variables
  83.     List<List<String>> items;
  84.     List<item> markers = new ArrayList<item>();
  85.  
  86.     // other variables
  87.     // private ProgressDialog m_ProgressDialog = null;
  88.     static int tries = 0;
  89.     TextView loader;
  90.     View line1;
  91.     View line2;
  92.     View line3;
  93.     ProgressBar loadbar;
  94.  
  95.     ImageView Load;
  96.     ImageView Add;
  97.  
  98.     ScrollView itemView;
  99.     ImageView closeItemView;
  100.     String addrString = "";
  101.  
  102.     ProgressBar Loadimg;
  103.  
  104.     TextView title;
  105.     TextView category;
  106.     TextView addr;
  107.     TextView desc;
  108.     TextView pub;
  109.     TextView positive;
  110.     TextView negative;
  111.     TextView idfeed;
  112.  
  113.     ImageView view;
  114.  
  115.     double itemLat;
  116.     double itemLon;
  117.  
  118.     @Override
  119.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
  120.             Bundle savedInstanceState) {
  121.         return inflater.inflate(R.layout.activity_main, container, false);
  122.     }
  123.  
  124.     @Override
  125.     public void onStart() {
  126.         super.onStart();
  127.  
  128. //      getActivity().getActionBar().setDisplayOptions(
  129. //              ActionBar.DISPLAY_SHOW_CUSTOM);
  130. //      getActivity().getActionBar().setCustomView(R.layout.titlebar);
  131. //
  132. //      getActivity().getActionBar().setHomeButtonEnabled(true);
  133. //      getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
  134.  
  135.         // converting int to string and vice versa
  136.         /*
  137.          * String num="4321"; int n = Integer.parseInt(num); String after=
  138.          * Integer.toString(n); ToastLoadShout(after);
  139.          */
  140.  
  141.         // itemView Scrollview views
  142.         title = (TextView) getView().findViewById(R.id.title);
  143.         category = (TextView) getView().findViewById(R.id.category);
  144.         Loadimg = (ProgressBar) getView().findViewById(R.id.Loadimg);
  145.         addr = (TextView) getView().findViewById(R.id.addr);
  146.         desc = (TextView) getView().findViewById(R.id.desc);
  147.         pub = (TextView) getView().findViewById(R.id.pub);
  148.         view = (ImageView) getView().findViewById(R.id.imageView1);
  149.         positive = (TextView) getView().findViewById(R.id.positive);
  150.         negative = (TextView) getView().findViewById(R.id.negative);
  151.         idfeed = (TextView) getView().findViewById(R.id.idfeed);
  152.  
  153.         itemView = (ScrollView) getView().findViewById(R.id.itemwin);
  154.         itemView.setVisibility(View.INVISIBLE);
  155.         closeItemView = (ImageView) getView().findViewById(R.id.closeitem);
  156.         closeItemView.setVisibility(View.INVISIBLE);
  157.  
  158.         Loadimg.setVisibility(View.INVISIBLE);
  159.  
  160.         turnGPSOn();
  161.  
  162.         map = ((MapFragment) getActivity().getFragmentManager()
  163.                 .findFragmentById(R.id.map)).getMap();
  164.  
  165.         ImageView locate;
  166.         locate = (ImageView) getView().findViewById(R.id.locate);
  167.         locate.setOnClickListener(new OnClickListener() {
  168.             public void onClick(View v) {
  169.                 tries++;
  170.                 ReNewCoordinates();
  171.                 ReNewCoordinates();
  172.                 if (lat == 0 && lon == 0)
  173.                     getLocation();
  174.                 if (tries > 2 && lat == 0 && lon == 0) {
  175.                     ToastLoadShout("Unable to determine location.\nPlease enable Wireless and GPS satelites.");
  176.                     startActivity(new Intent(
  177.                             android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
  178.                 }
  179.                 if (loc != null)
  180.                     loc.remove();
  181.  
  182.                 if (lat == 0 && lon == 0)
  183.                     ToastLoadShout("Try again in a few seconds. (#" + tries
  184.                             + ")");
  185.                 else {
  186.                     map.moveCamera(CameraUpdateFactory.newLatLngZoom(
  187.                             new LatLng(lat, lon), 10));
  188.                     ToastLoadShout("your location is: " + lat + " , " + lon);
  189.                     map.animateCamera(CameraUpdateFactory.zoomTo(14), 3000,
  190.                             null);
  191.  
  192.                     loc = map.addMarker(new MarkerOptions().position(
  193.                             new LatLng(lat, lon)).icon(
  194.                             BitmapDescriptorFactory
  195.                                     .fromResource(R.drawable.loc)));
  196.                 }
  197.             }
  198.         });
  199.  
  200.         loadbar = (ProgressBar) getView().findViewById(R.id.loadbar);
  201.         loadbar.setVisibility(View.INVISIBLE);
  202.  
  203.         line1 = (View) getView().findViewById(R.id.line1);
  204.         line2 = (View) getView().findViewById(R.id.line2);
  205.         line3 = (View) getView().findViewById(R.id.line3);
  206.         loader = (TextView) getView().findViewById(R.id.loader);
  207.         loader.setVisibility(View.INVISIBLE);
  208.         line1.setVisibility(View.INVISIBLE);
  209.         line2.setVisibility(View.INVISIBLE);
  210.         line3.setVisibility(View.INVISIBLE);
  211.  
  212.         Load = (ImageView) getView().findViewById(R.id.refresh);
  213.         Load.setOnClickListener(new OnClickListener() {
  214.             public void onClick(View v) {
  215.                 map.clear();
  216.                 markers.clear();
  217.                 new Load().execute();
  218.             }
  219.         });
  220.  
  221.         // Add = (ImageView) getView().findViewById(R.id.add);
  222.         // Add.setOnClickListener(new OnClickListener() {
  223.         // public void onClick(View v) {
  224.         // Intent Intent = new Intent(MainActivity.this,Add.class);
  225.         // Bundle bndlanimation =
  226.         // ActivityOptions.makeCustomAnimation(getApplicationContext(),
  227.         // R.anim.animation,R.anim.animation2).toBundle();
  228.         // startActivity(Intent, bndlanimation);
  229.         // }
  230.         // });
  231.  
  232.         closeItemView.setOnClickListener(new OnClickListener() {
  233.             public void onClick(View v) {
  234.                 itemView.setVisibility(View.INVISIBLE);
  235.                 closeItemView.setVisibility(View.INVISIBLE);
  236.                 line3.setVisibility(View.INVISIBLE);
  237.             }
  238.         });
  239.  
  240.         map.getUiSettings().setZoomControlsEnabled(false);
  241.         map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(50.909474,
  242.                 13.917618), 10000));
  243.         map.animateCamera(CameraUpdateFactory.zoomTo(1), 20, null);
  244.  
  245.         // Move the camera instantly to hamburg with a zoom of 15.
  246.         // map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat,lon),
  247.         // 15));
  248.  
  249.         // Zoom in, animating the camera.
  250.         // map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
  251.         // ToastLoadShout("your location is: "+lat+" , "+lon);
  252.  
  253.         map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
  254.             public void onInfoWindowClick(Marker marker) {
  255.                 // Intent Intent = new Intent(MainActivity.this,ItemView.class);
  256.                 for (int i = 0; i < markers.size(); i++) {
  257.                     if (markerMap.get(marker) == markers.get(i).getId()) {
  258.                         /*
  259.                          * Intent.putExtra("thing",markers.get(i));
  260.                          * startActivity(Intent);
  261.                          */
  262.                         view.setImageDrawable(null);
  263.                         itemView.fullScroll(View.FOCUS_UP);
  264.                         itemView.setVisibility(View.VISIBLE);
  265.                         closeItemView.setVisibility(View.VISIBLE);
  266.                         line3.setVisibility(View.VISIBLE);
  267.                         Loadimg.setVisibility(View.VISIBLE);
  268.                         title.setText(markers.get(i).getTitle());
  269.                         category.setText(markers.get(i).getCat() + "");
  270.                         desc.setText(markers.get(i).getDesc());
  271.                         addr.setText(markers.get(i).getAddr());
  272.                         pub.setText(markers.get(i).getPub());
  273.                         positive.setText(markers.get(i).getP() + "");
  274.                         negative.setText(markers.get(i).getN() + "");
  275.                         idfeed.setText("№" + markers.get(i).getId() + "");
  276.                         // itemLat=markers.get(i).getLat();
  277.                         // itemLon=markers.get(i).getLon();
  278.                         // new LoadAddress().execute();
  279.                         new ImageDownloader()
  280.                                 .execute("http://apple.sourcherry.tk/freeapp/resize.php?h=400&img=upload/id"
  281.                                         + markers.get(i).getId() + ".jpg");
  282.                         break;
  283.                     }
  284.                 }
  285.             }
  286.         });
  287.  
  288.         map.setOnMapLongClickListener(this);
  289.     }
  290.  
  291.     public void onMapLongClick(LatLng point) {
  292.         if (loc != null)
  293.             loc.remove();
  294.         loc = map.addMarker(new MarkerOptions().position(point).icon(
  295.                 BitmapDescriptorFactory.fromResource(R.drawable.loc)));
  296.         lat = point.latitude;
  297.         lon = point.longitude;
  298.     }
  299.  
  300.     public void ArrangePutMarkers() {
  301.         if (items != null) {
  302.             for (int i = 0; i < items.get(0).size(); i++) {
  303.                 // My old method //markers.add(new
  304.                 // item(Integer.parseInt(items.get(0).get(i))/*id*/,
  305.                 // items.get(1).get(i)/*title*/,items.get(2).get(i)/*snippet*/,
  306.                 // Integer.parseInt(items.get(3).get(i))/*cat*/,items.get(4).get(i)/*desc*/,
  307.                 // Double.parseDouble(items.get(5).get(i))/*lat*/,
  308.                 // Double.parseDouble(items.get(6).get(i))/*lon*/,
  309.                 // items.get(7).get(i)/*pub*/,
  310.                 // Integer.parseInt(items.get(8).get(i))/*p*/,
  311.                 // Integer.parseInt(items.get(9).get(i))/*n*/,Integer.parseInt(items.get(10).get(i))/*program*/,items.get(11).get(i)/*address*/));
  312.                 markers.add(new item(
  313.                         Integer.parseInt(items.get(0).get(i))/* id */, items
  314.                                 .get(1).get(i)/* title */,
  315.                         items.get(2).get(i)/* snippet */, Double
  316.                                 .parseDouble(items.get(3).get(i))/* lat */,
  317.                         Double.parseDouble(items.get(4).get(i))/* lon */,
  318.                         Integer.parseInt(items.get(5).get(i))/* program */));
  319.             }
  320.  
  321.             for (int i = 0; i < markers.size(); i++) {
  322.                 LatLng ll = new LatLng(markers.get(i).getLat(), markers.get(i)
  323.                         .getLon());
  324.                 String title = markers.get(i).getTitle();
  325.                 String snippet = markers.get(i).getSnippet();
  326.                 // String desc=markers.get(i).getDesc();
  327.  
  328.                 if (markers.get(i).getProgram() == 0)
  329.                     this.markerMap.put(this.map.addMarker(new MarkerOptions()
  330.                             .position(ll)
  331.                             .title(title)
  332.                             .snippet(snippet)
  333.                             .icon(BitmapDescriptorFactory
  334.                                     .fromResource(R.drawable.bronze))), markers
  335.                             .get(i).getId());
  336.  
  337.                 else if (markers.get(i).getProgram() == 1)
  338.                     this.markerMap.put(this.map.addMarker(new MarkerOptions()
  339.                             .position(ll)
  340.                             .title(title)
  341.                             .snippet(snippet)
  342.                             .icon(BitmapDescriptorFactory
  343.                                     .fromResource(R.drawable.silver))), markers
  344.                             .get(i).getId());
  345.  
  346.                 else if (markers.get(i).getProgram() == 2)
  347.                     this.markerMap.put(this.map.addMarker(new MarkerOptions()
  348.                             .position(ll)
  349.                             .title(title)
  350.                             .snippet(snippet)
  351.                             .icon(BitmapDescriptorFactory
  352.                                     .fromResource(R.drawable.gold))), markers
  353.                             .get(i).getId());
  354.             }
  355.         }
  356.         // loader.setText("Done.");
  357.     }
  358.  
  359.     class Load extends AsyncTask<String, Integer, Boolean> {
  360.         @Override
  361.         protected void onPreExecute() {
  362.             loadbar.setVisibility(View.VISIBLE);
  363.             Load.setVisibility(View.INVISIBLE);
  364.             loader.setVisibility(View.VISIBLE);
  365.             line1.setVisibility(View.VISIBLE);
  366.             line2.setVisibility(View.VISIBLE);
  367.             // TODO Auto-generated method stub
  368.             loader.setText("Getting those attractions..");
  369.         }
  370.  
  371.         @Override
  372.         protected Boolean doInBackground(String... params) {
  373.  
  374.             try {
  375.                 items = DownloadList();
  376.                 if (items != null)
  377.                     return true;
  378.  
  379.             } catch (Exception e) {
  380.                 e.printStackTrace();
  381.                 return false;
  382.             }
  383.             return false;
  384.         }
  385.  
  386.         @Override
  387.         protected void onPostExecute(Boolean res) {
  388.             // TODO Auto-generated method stub
  389.             if (res) {
  390.                 // m_ProgressDialog.dismiss();
  391.                 ArrangePutMarkers();
  392.                 loader.setVisibility(View.INVISIBLE);
  393.                 line1.setVisibility(View.INVISIBLE);
  394.                 line2.setVisibility(View.INVISIBLE);
  395.  
  396.                 loadbar.setVisibility(View.INVISIBLE);
  397.                 Load.setVisibility(View.VISIBLE);
  398.                 // ToastLoadShout(datadata);
  399.             } else {
  400.                 // m_ProgressDialog.dismiss();
  401.                 loader.setText("Oh snap. There was an error..");
  402.                 ToastLoadShout("Error");
  403.  
  404.                 loadbar.setVisibility(View.INVISIBLE);
  405.                 Load.setVisibility(View.VISIBLE);
  406.                 // ToastLoadShout(datadata);
  407.             }
  408.         }
  409.     }
  410.  
  411.     public List<List<String>> Download() {
  412.         String data = null;
  413.         // String res = "";
  414.         try {
  415.             client = new DefaultHttpClient();// Reference to the Internet
  416.             httppost = new HttpPost(
  417.                     "http://apple.sourcherry.tk/freeapp/json.php");
  418.             HttpResponse response = client.execute(httppost);
  419.             HttpEntity entity = response.getEntity();// get the content of the
  420.                                                         // message
  421.             InputStream webs = entity.getContent();
  422.  
  423.             BufferedReader in = new BufferedReader(new InputStreamReader(webs,
  424.                     "iso-8859-1"));
  425.  
  426.             StringBuffer sb = new StringBuffer("");
  427.  
  428.             String l = " ";
  429.             // String nl=System.getProperty("line.separator");
  430.             while ((l = in.readLine()) != null) {
  431.                 sb.append(l + "\n");
  432.             }
  433.             data = sb.toString();
  434.             webs.close();
  435.  
  436.             List<List<String>> all = new ArrayList<List<String>>();
  437.             all.add(new ArrayList<String>());// id
  438.             all.add(new ArrayList<String>());// title
  439.             all.add(new ArrayList<String>());// snippet
  440.             all.add(new ArrayList<String>());// category
  441.             all.add(new ArrayList<String>());// desc
  442.             all.add(new ArrayList<String>());// lat
  443.             all.add(new ArrayList<String>());// lon
  444.             all.add(new ArrayList<String>());// pub
  445.             all.add(new ArrayList<String>());// p
  446.             all.add(new ArrayList<String>());// n
  447.             all.add(new ArrayList<String>());// program
  448.             all.add(new ArrayList<String>());// addr
  449.  
  450.             try {
  451.  
  452.                 JSONObject json = new JSONObject(data);
  453.                 JSONArray jArray = json.getJSONArray("item");
  454.                 // id
  455.                 for (int i = 0; i < jArray.length(); i++) {
  456.  
  457.                     JSONObject json_data = jArray.getJSONObject(i);
  458.                     all.get(0).add(json_data.getString("id"));
  459.                 }
  460.                 // title
  461.                 for (int i = 0; i < jArray.length(); i++) {
  462.  
  463.                     JSONObject json_data = jArray.getJSONObject(i);
  464.                     all.get(1).add(json_data.getString("title"));
  465.  
  466.                 }
  467.                 // snippet
  468.                 for (int i = 0; i < jArray.length(); i++) {
  469.  
  470.                     JSONObject json_data = jArray.getJSONObject(i);
  471.                     all.get(2).add(json_data.getString("snippet"));
  472.  
  473.                 }
  474.                 // category
  475.                 for (int i = 0; i < jArray.length(); i++) {
  476.  
  477.                     JSONObject json_data = jArray.getJSONObject(i);
  478.                     all.get(3).add(json_data.getString("cat"));
  479.  
  480.                 }
  481.                 // desc
  482.                 for (int i = 0; i < jArray.length(); i++) {
  483.  
  484.                     JSONObject json_data = jArray.getJSONObject(i);
  485.                     all.get(4).add(json_data.getString("desc"));
  486.  
  487.                 }
  488.                 // lat
  489.                 for (int i = 0; i < jArray.length(); i++) {
  490.  
  491.                     JSONObject json_data = jArray.getJSONObject(i);
  492.                     all.get(5).add(json_data.getString("lat"));
  493.  
  494.                 }
  495.                 // lon
  496.                 for (int i = 0; i < jArray.length(); i++) {
  497.  
  498.                     JSONObject json_data = jArray.getJSONObject(i);
  499.                     all.get(6).add(json_data.getString("lon"));
  500.  
  501.                 }
  502.                 // pub
  503.                 for (int i = 0; i < jArray.length(); i++) {
  504.  
  505.                     JSONObject json_data = jArray.getJSONObject(i);
  506.                     all.get(7).add(json_data.getString("pub"));
  507.  
  508.                 }
  509.                 // p
  510.                 for (int i = 0; i < jArray.length(); i++) {
  511.  
  512.                     JSONObject json_data = jArray.getJSONObject(i);
  513.                     all.get(8).add(json_data.getString("p"));
  514.  
  515.                 }
  516.                 // n
  517.                 for (int i = 0; i < jArray.length(); i++) {
  518.  
  519.                     JSONObject json_data = jArray.getJSONObject(i);
  520.                     all.get(9).add(json_data.getString("n"));
  521.  
  522.                 }
  523.                 // program
  524.                 for (int i = 0; i < jArray.length(); i++) {
  525.  
  526.                     JSONObject json_data = jArray.getJSONObject(i);
  527.                     all.get(10).add(json_data.getString("program"));
  528.  
  529.                 }
  530.                 // address
  531.                 for (int i = 0; i < jArray.length(); i++) {
  532.  
  533.                     JSONObject json_data = jArray.getJSONObject(i);
  534.                     all.get(11).add(json_data.getString("addr"));
  535.  
  536.                 }
  537.  
  538.                 return all;
  539.  
  540.             } catch (JSONException e) {
  541.  
  542.             }
  543.  
  544.         } catch (Exception e) {
  545.             int x = 3;
  546.             // TODO: handle exception
  547.         }
  548.  
  549.         return null;
  550.     }
  551.  
  552.     public List<List<String>> DownloadList() {
  553.         String data = null;
  554.         // String res = "";
  555.         try {
  556.             client = new DefaultHttpClient();// Reference to the Internet
  557.             httpget = new HttpGet(
  558.                     "http://apple.sourcherry.tk/freeapp/distance.php?lat="
  559.                             + lat + "&lon=" + lon + "&rad=5");
  560.             HttpResponse response = client.execute(httpget);
  561.             HttpEntity entity = response.getEntity();// get the content of the
  562.                                                         // message
  563.             InputStream webs = entity.getContent();
  564.  
  565.             BufferedReader in = new BufferedReader(new InputStreamReader(webs,
  566.                     "UTF-8"));
  567.  
  568.             StringBuffer sb = new StringBuffer("");
  569.  
  570.             String l = " ";
  571.             // String nl=System.getProperty("line.separator");
  572.             while ((l = in.readLine()) != null) {
  573.                 sb.append(l + "\n");
  574.             }
  575.             data = sb.toString();
  576.             // datadata=data;
  577.             webs.close();
  578.  
  579.             List<List<String>> all = new ArrayList<List<String>>();
  580.             all.add(new ArrayList<String>());// id
  581.             all.add(new ArrayList<String>());// title
  582.             all.add(new ArrayList<String>());// snippet
  583.             all.add(new ArrayList<String>());// lat
  584.             all.add(new ArrayList<String>());// lon
  585.             all.add(new ArrayList<String>());// program
  586.  
  587.             try {
  588.  
  589.                 JSONObject json = new JSONObject(data);
  590.                 JSONArray jArray = json.getJSONArray("item");
  591.                 // id
  592.                 for (int i = 0; i < jArray.length(); i++) {
  593.  
  594.                     JSONObject json_data = jArray.getJSONObject(i);
  595.                     all.get(0).add(json_data.getString("id"));
  596.                 }
  597.                 // title
  598.                 for (int i = 0; i < jArray.length(); i++) {
  599.  
  600.                     JSONObject json_data = jArray.getJSONObject(i);
  601.                     all.get(1).add(json_data.getString("title"));
  602.  
  603.                 }
  604.                 // snippet
  605.                 for (int i = 0; i < jArray.length(); i++) {
  606.  
  607.                     JSONObject json_data = jArray.getJSONObject(i);
  608.                     all.get(2).add(json_data.getString("snippet"));
  609.  
  610.                 }
  611.                 // lat
  612.                 for (int i = 0; i < jArray.length(); i++) {
  613.  
  614.                     JSONObject json_data = jArray.getJSONObject(i);
  615.                     all.get(3).add(json_data.getString("lat"));
  616.  
  617.                 }
  618.                 // lon
  619.                 for (int i = 0; i < jArray.length(); i++) {
  620.  
  621.                     JSONObject json_data = jArray.getJSONObject(i);
  622.                     all.get(4).add(json_data.getString("lon"));
  623.  
  624.                 }
  625.                 // program
  626.                 for (int i = 0; i < jArray.length(); i++) {
  627.  
  628.                     JSONObject json_data = jArray.getJSONObject(i);
  629.                     all.get(5).add(json_data.getString("program"));
  630.  
  631.                 }
  632.  
  633.                 return all;
  634.  
  635.             } catch (JSONException e) {
  636.  
  637.             }
  638.  
  639.         } catch (Exception e) {
  640.             int x = 3;
  641.             // TODO: handle exception
  642.         }
  643.  
  644.         return null;
  645.     }
  646.  
  647.     private void turnGPSOn() {
  648.  
  649.         String provider = android.provider.Settings.Secure.getString(
  650.                 getActivity().getContentResolver(),
  651.                 android.provider.Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
  652.         if (!provider.contains("gps")) { // if gps is disabled
  653.             final Intent poke = new Intent();
  654.             poke.setClassName("com.android.settings",
  655.                     "com.android.settings.widget.SettingsAppWidgetProvider");
  656.             poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
  657.             poke.setData(Uri.parse("3"));
  658.             getActivity().sendBroadcast(poke);
  659.             ToastLoadShout("Turning GPS on");
  660.         }
  661.     }
  662.  
  663.     private void getLocation() {
  664.         // Get the location manager
  665.         LocationManager locationManager = (LocationManager) getActivity()
  666.                 .getSystemService(getActivity().LOCATION_SERVICE);
  667.         Criteria criteria = new Criteria();
  668.         String bestProvider = locationManager.getBestProvider(criteria, false);
  669.         Location location = locationManager.getLastKnownLocation(bestProvider);
  670.         LocationListener loc_listener = new LocationListener() {
  671.  
  672.             public void onLocationChanged(Location l) {
  673.             }
  674.  
  675.             public void onProviderEnabled(String p) {
  676.             }
  677.  
  678.             public void onProviderDisabled(String p) {
  679.             }
  680.  
  681.             public void onStatusChanged(String p, int status, Bundle extras) {
  682.             }
  683.         };
  684.         locationManager
  685.                 .requestLocationUpdates(bestProvider, 0, 0, loc_listener);
  686.         location = locationManager.getLastKnownLocation(bestProvider);
  687.         try {
  688.             lat = location.getLatitude();
  689.             lon = location.getLongitude();
  690.         } catch (NullPointerException e) {
  691.             // lat = 0;
  692.             // lon = 0;
  693.         }
  694.     }
  695.  
  696.     public void ReNewCoordinates() {
  697.         LocationManager locationManager = (LocationManager) getActivity()
  698.                 .getSystemService(Context.LOCATION_SERVICE);
  699.         // Define a listener that responds to location updates
  700.         LocationListener locationListener = new LocationListener() {
  701.             public void onLocationChanged(Location location) {
  702.                 // Called when a new location is found by the network location
  703.                 // provider.
  704.                 lat = (location.getLatitude());
  705.                 lon = (location.getLongitude());
  706.             }
  707.  
  708.             public void onProviderDisabled(String provider) {
  709.             }
  710.  
  711.             public void onProviderEnabled(String provider) {
  712.             }
  713.  
  714.             public void onStatusChanged(String provider, int status,
  715.                     Bundle extras) {
  716.             }
  717.         };
  718.         locationManager.requestLocationUpdates(
  719.                 LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
  720.     }
  721.  
  722.     private class ImageDownloader extends AsyncTask<String, String, Bitmap> {
  723.  
  724.         protected void onPreExecute(String res) {
  725.             Log.i("Async-Example", "onPreExecute Called");
  726.  
  727.         }
  728.  
  729.         protected Bitmap doInBackground(String... param) {
  730.             // TODO Auto-generated method stub
  731.             return downloadBitmap(param[0]);
  732.         }
  733.  
  734.         protected void onPostExecute(Bitmap result) {
  735.             Log.i("Async-Example", "onPostExecute Called");
  736.             view.setImageBitmap(result);
  737.             Loadimg.setVisibility(View.INVISIBLE);
  738.  
  739.         }
  740.  
  741.         private Bitmap downloadBitmap(String url) {
  742.             // initilize the default HTTP client object
  743.             final DefaultHttpClient client = new DefaultHttpClient();
  744.  
  745.             // forming a HttoGet request
  746.             final HttpGet getRequest = new HttpGet(url);
  747.             try {
  748.  
  749.                 HttpResponse response = client.execute(getRequest);
  750.  
  751.                 // check 200 OK for success
  752.                 final int statusCode = response.getStatusLine().getStatusCode();
  753.  
  754.                 if (statusCode != HttpStatus.SC_OK) {
  755.                     Log.w("ImageDownloader", "Error " + statusCode
  756.                             + " while retrieving bitmap from " + url);
  757.                     return null;
  758.  
  759.                 }
  760.  
  761.                 final HttpEntity entity = response.getEntity();
  762.                 if (entity != null) {
  763.                     InputStream inputStream = null;
  764.                     try {
  765.                         // getting contents from the stream
  766.                         inputStream = entity.getContent();
  767.  
  768.                         // decoding stream data back into image Bitmap that
  769.                         // android understands
  770.                         final Bitmap bitmap = BitmapFactory
  771.                                 .decodeStream(inputStream);
  772.  
  773.                         return bitmap;
  774.                     } finally {
  775.                         if (inputStream != null) {
  776.                             inputStream.close();
  777.                         }
  778.                         entity.consumeContent();
  779.                     }
  780.                 }
  781.             } catch (Exception e) {
  782.                 // You Could provide a more explicit error message for
  783.                 // IOException
  784.                 getRequest.abort();
  785.                 Log.e("ImageDownloader", "Something went wrong while"
  786.                         + " retrieving bitmap from " + url + e.toString());
  787.             }
  788.  
  789.             return null;
  790.         }
  791.  
  792.     }
  793.  
  794.     // class LoadAddress extends AsyncTask<String, Integer, Boolean>
  795.     // {
  796.     // @Override
  797.     // protected void onPreExecute() {
  798.     //
  799.     // }
  800.     // @Override
  801.     // protected Boolean doInBackground(String... params) {
  802.     //
  803.     // try {
  804.     // addrString = GetAddress(itemLat+"",itemLon+"");
  805.     // if (addrString!="")
  806.     // return true;
  807.     // } catch (Exception e) {
  808.     // e.printStackTrace();
  809.     // return false;
  810.     // }
  811.     // return false;
  812.     // }
  813.     // @Override
  814.     // protected void onPostExecute(Boolean res) {
  815.     // // TODO Auto-generated method stub
  816.     // if (res){
  817.     // addr.setText(addrString);
  818.     // }
  819.     // else
  820.     // addr.setText("Unable to fetch address.");
  821.     // }
  822.     // }
  823.  
  824.     public String GetAddress(String lat, String lon) {
  825.         Geocoder geocoder = new Geocoder(getActivity(), Locale.ENGLISH);
  826.         String ret = "";
  827.         try {
  828.             List<Address> addresses = geocoder.getFromLocation(
  829.                     Double.parseDouble(lat), Double.parseDouble(lon), 1);
  830.             if (addresses != null) {
  831.                 Address returnedAddress = addresses.get(0);
  832.                 StringBuilder strReturnedAddress = new StringBuilder("");
  833.                 for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
  834.                     strReturnedAddress
  835.                             .append(returnedAddress.getAddressLine(i)).append(
  836.                                     "\n");
  837.                 }
  838.                 ret = strReturnedAddress.toString();
  839.                 ret = ret.substring(0, ret.length() - 1);
  840.             } else {
  841.                 ret = "Unknown Address";
  842.             }
  843.         } catch (IOException e) {
  844.             // TODO Auto-generated catch block
  845.             e.printStackTrace();
  846.             ret = "Couldn't get Address";
  847.         }
  848.         return ret;
  849.     }
  850.  
  851.     public void onBackPressed() {
  852.         if (itemView.getVisibility() == View.VISIBLE) {
  853.             itemView.setVisibility(View.INVISIBLE);
  854.             closeItemView.setVisibility(View.INVISIBLE);
  855.             line3.setVisibility(View.INVISIBLE);
  856.             ImageDownloader loadimg = new ImageDownloader();
  857.             if (loadimg.getStatus() == AsyncTask.Status.RUNNING) {
  858.                 loadimg.cancel(true);
  859.             }
  860.         } else {
  861.             getActivity().finish();
  862.         }
  863.     }
  864.     // <!!!!--------------------------------Adding To
  865.     // database----------------------------------------!!!!>
  866. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement