Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.bk.lrandom.realestate;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import org.apache.http.HttpEntity;
  6. import org.apache.http.HttpResponse;
  7. import org.apache.http.client.HttpClient;
  8. import org.apache.http.client.methods.HttpPost;
  9. import org.apache.http.entity.mime.MultipartEntity;
  10. import org.apache.http.entity.mime.content.StringBody;
  11. import org.apache.http.impl.client.DefaultHttpClient;
  12. import org.apache.http.util.EntityUtils;
  13. import org.json.JSONArray;
  14. import org.json.JSONObject;
  15.  
  16. import android.annotation.SuppressLint;
  17. import android.app.AlertDialog;
  18. import android.app.ProgressDialog;
  19. import android.content.DialogInterface;
  20. import android.content.Intent;
  21. import android.content.res.Resources;
  22. import android.graphics.Bitmap;
  23. import android.net.Uri;
  24. import android.os.AsyncTask;
  25. import android.os.Bundle;
  26. import android.os.Handler;
  27. import android.util.Log;
  28. import android.util.TypedValue;
  29. import android.view.LayoutInflater;
  30. import android.view.View;
  31. import android.widget.ArrayAdapter;
  32. import android.widget.EditText;
  33. import android.widget.GridView;
  34. import android.widget.ImageButton;
  35. import android.widget.TextView;
  36.  
  37. import com.NYXDigital.NiceSupportMapFragment;
  38. import com.bk.lrandom.realestate.adapters.GalleryAdapter;
  39. import com.bk.lrandom.realestate.business.JSONFetchTask;
  40. import com.bk.lrandom.realestate.business.Ultils;
  41. import com.bk.lrandom.realestate.business.UserSessionManager;
  42. import com.bk.lrandom.realestate.confs.constants;
  43. import com.bk.lrandom.realestate.fragments.DetailContentFragment;
  44. import com.bk.lrandom.realestate.models.Properties;
  45. import com.bk.lrandom.realestate.models.User;
  46. import com.google.android.gms.maps.CameraUpdate;
  47. import com.google.android.gms.maps.CameraUpdateFactory;
  48. import com.google.android.gms.maps.GoogleMap;
  49. import com.google.android.gms.maps.model.BitmapDescriptorFactory;
  50. import com.google.android.gms.maps.model.LatLng;
  51. import com.google.android.gms.maps.model.Marker;
  52. import com.google.android.gms.maps.model.MarkerOptions;
  53. import com.koushikdutta.async.future.FutureCallback;
  54. import com.koushikdutta.ion.Ion;
  55.  
  56. @SuppressLint({ "NewApi", "HandlerLeak" })
  57. public class DetailActivity extends ActionBarParentActivity {
  58.     TextView type, purpose, county, cities, address, bathroom, bedroom, title,
  59.             price, area;
  60.     ProgressDialog dialog;
  61.     public static final String IMAGES_RESPONSE = "images_feed";
  62.     int properties_id;
  63.     String user_id, email, phone_text;
  64.     ImageButton btnEmail, btnSMS, btnCall, btnProfile;
  65.     EditText message;
  66.     User logedUser;
  67.     GridView gallery, amenities;
  68.     int columnWidth;
  69.     ArrayList<String> paths;
  70.     ArrayList<String> amenitiesText;
  71.     private GoogleMap gmap;
  72.     Marker gpin = null;
  73.  
  74.     public static final String TAG = "DetailActivity";
  75.  
  76.     private void initGallery() {
  77.         Resources r = getResources();
  78.         float padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
  79.                 constants.GRID_PADDING, r.getDisplayMetrics());
  80.  
  81.         columnWidth = (int) ((Ultils.getScreenWidth(DetailActivity.this) - ((constants.NUM_OF_COLUMNS + 1) * padding)) / constants.NUM_OF_COLUMNS);
  82.  
  83.         gallery.setNumColumns(constants.NUM_OF_COLUMNS);
  84.         gallery.setColumnWidth(columnWidth);
  85.         gallery.setStretchMode(GridView.NO_STRETCH);
  86.         gallery.setHorizontalSpacing((int) padding);
  87.         gallery.setVerticalSpacing((int) padding);
  88.     }
  89.  
  90.     Handler handlerImages = new Handler() {
  91.         public void handleMessage(android.os.Message msg) {
  92.             Bundle bundle = msg.getData();
  93.             if (bundle.containsKey(IMAGES_RESPONSE)) {
  94.                 String jsonString = bundle.getString(IMAGES_RESPONSE);
  95.                 new JSONFetchTask(getResources().getString(
  96.                         R.string.estate_json_url)
  97.                         + "amenities?id=" + properties_id, handlerAmenities)
  98.                         .execute();
  99.                 try {
  100.                     JSONArray jsonArray = new JSONArray(jsonString);
  101.                     for (int i = 0; i < jsonArray.length(); i++) {
  102.                         JSONObject jsonObj = jsonArray.getJSONObject(i);
  103.                         String path = getResources().getString(
  104.                                 R.string.domain_url)
  105.                                 + jsonObj.getString("path");
  106.                         paths.add(path);
  107.                     }
  108.                     GalleryAdapter adapter = new GalleryAdapter(
  109.                             DetailActivity.this, paths, columnWidth);
  110.                     gallery.setAdapter(adapter);
  111.                 } catch (Exception e) {
  112.                 }
  113.             }
  114.         };
  115.     };
  116.  
  117.     Handler handlerAmenities = new Handler() {
  118.         public void handleMessage(android.os.Message msg) {
  119.             Bundle bundle = msg.getData();
  120.             if (bundle.containsKey(JSONFetchTask.KEY_RESPONSE)) {
  121.                 String jsonString = bundle
  122.                         .getString(JSONFetchTask.KEY_RESPONSE);
  123.                 try {
  124.                     JSONArray jsonArray = new JSONArray(jsonString);
  125.                     amenitiesText = new ArrayList<String>();
  126.                     for (int i = 0; i < jsonArray.length(); i++) {
  127.                         JSONObject jsonObj = jsonArray.getJSONObject(i);
  128.                         String amenitiesJSON = jsonObj.getString("name");
  129.                         amenitiesText.add(amenitiesJSON);
  130.                     }
  131.                     ArrayAdapter<String> adapter = new ArrayAdapter<String>(
  132.                             DetailActivity.this,
  133.                             R.layout.amenities_grid_item_layout, amenitiesText);
  134.                     amenities.setAdapter(adapter);
  135.                 } catch (Exception e) {
  136.                     e.printStackTrace();
  137.                 }
  138.             }
  139.         };
  140.     };
  141.  
  142.     @Override
  143.     protected void onCreate(Bundle savedInstanceState) {
  144.         // TODO Auto-generated method stub
  145.         super.onCreate(savedInstanceState);
  146.         setContentView(R.layout.detail_layout);
  147.         gallery = (GridView) findViewById(R.id.gallery);
  148.         amenities = (GridView) findViewById(R.id.amenities);
  149.         initGallery();
  150.  
  151.         if (getIntent().getExtras() != null
  152.                 && getIntent().getExtras().containsKey(constants.COMMON_KEY)) {
  153.             btnSMS = (ImageButton) findViewById(R.id.btnSmS);
  154.             btnEmail = (ImageButton) findViewById(R.id.btnEmail);
  155.             btnCall = (ImageButton) findViewById(R.id.btnPhone);
  156.             btnProfile = (ImageButton) findViewById(R.id.btnProfile);
  157.  
  158.             title = (TextView) findViewById(R.id.title);
  159.             type = (TextView) findViewById(R.id.type);
  160.             county = (TextView) findViewById(R.id.county);
  161.             cities = (TextView) findViewById(R.id.cities);
  162.             purpose = (TextView) findViewById(R.id.purpose);
  163.             address = (TextView) findViewById(R.id.address);
  164.             bathroom = (TextView) findViewById(R.id.bathroom);
  165.             paths = new ArrayList<String>();
  166.             bedroom = (TextView) findViewById(R.id.bedroom);
  167.             price = (TextView) findViewById(R.id.price);
  168.             area = (TextView) findViewById(R.id.area);
  169.             properties_id = getIntent().getExtras()
  170.                     .getInt(constants.COMMON_KEY);
  171.             new JSONFetchTask(getResources()
  172.                     .getString(R.string.estate_json_url)
  173.                     + "estates?estate_id="
  174.                     + properties_id, handler).execute();
  175.         }
  176.  
  177.         dialog = new ProgressDialog(this);
  178.         dialog.setMessage(getResources().getString(R.string.loading));
  179.         dialog.setCanceledOnTouchOutside(false);
  180.         dialog.show();
  181.         getSupportActionBar().setHomeButtonEnabled(true);
  182.         getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  183.         setTitle(getResources().getString(R.string.detail_label));
  184.     }
  185.  
  186.     Handler handler = new Handler() {
  187.         public void handleMessage(android.os.Message msg) {
  188.             Bundle bundle = msg.getData();
  189.             if (bundle.containsKey(JSONFetchTask.KEY_RESPONSE)) {
  190.                 dialog.dismiss();
  191.                 String jsonString = bundle
  192.                         .getString(JSONFetchTask.KEY_RESPONSE);
  193.                 try {
  194.                     JSONArray jsonArray = new JSONArray(jsonString);
  195.                     if (jsonArray.length() == 1) {
  196.                         JSONObject obj = jsonArray.getJSONObject(0);
  197.                         parseProperties(obj);
  198.                     }
  199.                 } catch (Exception e) {
  200.                     Log.e(TAG, "error parse properties");
  201.                     // TODO: handle exception
  202.                 }
  203.             }
  204.         };
  205.     };
  206.  
  207.     public void parseProperties(JSONObject jsonObj) {
  208.         new JSONFetchTask(getResources().getString(R.string.images_json_url)
  209.                 + "images/estate_id/" + properties_id, handlerImages,
  210.                 IMAGES_RESPONSE).execute();
  211.         try {
  212.             user_id = jsonObj.getString("user_id");
  213.             email = jsonObj.getString("email");
  214.             phone_text = jsonObj.getString("phone");
  215.  
  216.             btnSMS.setOnClickListener(new View.OnClickListener() {
  217.                 @Override
  218.                 public void onClick(View v) {
  219.                     // TODO Auto-generated method stub
  220.                     if (!Ultils.isConnectingToInternet(DetailActivity.this)) {
  221.                         showMsg(getResources().getString(R.string.open_network));
  222.                         return;
  223.                     }
  224.  
  225.                     UserSessionManager userSession = new UserSessionManager(
  226.                             DetailActivity.this);
  227.  
  228.                     if (userSession.getUserSession() == null) {
  229.                         Intent intent = new Intent(DetailActivity.this,
  230.                                 AuthenticationActivity.class);
  231.                         startActivity(intent);
  232.                     } else {
  233.                         logedUser = userSession.getUserSession();
  234.                         if (logedUser.getPhone() == null
  235.                                 || phone_text.equals("")) {
  236.                             showDialog(getResources().getString(
  237.                                     R.string.this_user_not_share_phone_number));
  238.                         } else {
  239.                             startActivity(new Intent(Intent.ACTION_VIEW, Uri
  240.                                     .parse("sms:" + phone_text)));
  241.                         }
  242.                     }
  243.                 }
  244.  
  245.             });
  246.  
  247.             btnCall.setOnClickListener(new View.OnClickListener() {
  248.                 @Override
  249.                 public void onClick(View v) {
  250.                     // TODO Auto-generated method stub
  251.                     if (!Ultils.isConnectingToInternet(DetailActivity.this)) {
  252.                         showMsg(getResources().getString(R.string.open_network));
  253.                         return;
  254.                     }
  255.  
  256.                     UserSessionManager userSession = new UserSessionManager(
  257.                             DetailActivity.this);
  258.  
  259.                     if (userSession.getUserSession() == null) {
  260.                         Intent intent = new Intent(DetailActivity.this,
  261.                                 AuthenticationActivity.class);
  262.                         startActivity(intent);
  263.                     } else {
  264.                         logedUser = userSession.getUserSession();
  265.                         if (logedUser.getPhone() == null
  266.                                 || phone_text.equals("")) {
  267.                             showDialog(getResources().getString(
  268.                                     R.string.this_user_not_share_phone_number));
  269.                         } else {
  270.                             startActivity(new Intent(Intent.ACTION_CALL, Uri
  271.                                     .parse("tel:" + phone_text)));
  272.                         }
  273.                     }
  274.                 }
  275.             });
  276.  
  277.             btnEmail.setOnClickListener(new View.OnClickListener() {
  278.  
  279.                 @Override
  280.                 public void onClick(View v) {
  281.                     // TODO Auto-generated method stub
  282.                     if (!Ultils.isConnectingToInternet(DetailActivity.this)) {
  283.                         showMsg(getResources().getString(R.string.open_network));
  284.                         return;
  285.                     }
  286.  
  287.                     UserSessionManager userSession = new UserSessionManager(
  288.                             DetailActivity.this);
  289.  
  290.                     if (userSession.getUserSession() == null) {
  291.                         Intent intent = new Intent(DetailActivity.this,
  292.                                 AuthenticationActivity.class);
  293.                         startActivity(intent);
  294.                     } else {
  295.                         logedUser = userSession.getUserSession();
  296.                         if (logedUser.getId() == Integer.parseInt(user_id)) {
  297.                             showDialog(getResources().getString(
  298.                                     R.string.enquiry_alert));
  299.                             return;
  300.                         }
  301.  
  302.                         LayoutInflater inflater = LayoutInflater
  303.                                 .from(DetailActivity.this);
  304.                         View promptsView = inflater.inflate(
  305.                                 R.layout.enquiry_prompts_layout, null);
  306.  
  307.                         AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
  308.                                 DetailActivity.this);
  309.  
  310.                         alertDialogBuilder.setView(promptsView);
  311.  
  312.                         message = (EditText) promptsView
  313.                                 .findViewById(R.id.editTextDialogUserInput);
  314.                         alertDialogBuilder.setMessage(getResources().getString(
  315.                                 R.string.send_enquiry));
  316.                         alertDialogBuilder
  317.                                 .setCancelable(false)
  318.                                 .setPositiveButton(
  319.                                         getResources().getString(
  320.                                                 R.string.ok_label),
  321.                                         new DialogInterface.OnClickListener() {
  322.                                             public void onClick(
  323.  
  324.                                             DialogInterface dialog, int id) {
  325.                                                 if (message.getText()
  326.                                                         .toString()
  327.                                                         .equalsIgnoreCase("")) {
  328.                                                     showDialog(getResources()
  329.                                                             .getString(
  330.                                                                     R.string.type_message));
  331.                                                     return;
  332.                                                 }
  333.                                                 new SendEnquiry().execute();
  334.                                             }
  335.                                         })
  336.                                 .setNegativeButton(
  337.                                         getResources().getString(
  338.                                                 R.string.cancel_label),
  339.                                         new DialogInterface.OnClickListener() {
  340.                                             public void onClick(
  341.                                                     DialogInterface dialog,
  342.                                                     int id) {
  343.                                                 dialog.cancel();
  344.                                             }
  345.                                         });
  346.  
  347.                         // create alert dialog
  348.                         AlertDialog alertDialog = alertDialogBuilder.create();
  349.                         // show it
  350.                         alertDialog.show();
  351.                     }
  352.                 }
  353.             });
  354.  
  355.             btnProfile.setOnClickListener(new View.OnClickListener() {
  356.                 @Override
  357.                 public void onClick(View arg0) {
  358.                     // TODO Auto-generated method stub
  359.                     Intent intent = new Intent(DetailActivity.this,
  360.                             ProfileActivity.class);
  361.                     intent.putExtra(constants.COMMON_KEY, user_id);
  362.                     startActivity(intent);
  363.                 }
  364.             });
  365.  
  366.             String titleText = jsonObj.getString(Properties.TAG_TITLE);
  367.             title.setText(titleText);
  368.             String addressText = jsonObj.getString(Properties.TAG_ADDESS);
  369.             address.setText(" " + addressText);
  370.             String typeText = jsonObj.getString(Properties.TAG_TYPES_NAME);
  371.             type.setText(" " + typeText);
  372.             String coutyText = jsonObj.getString(Properties.TAG_COUNTY_NAME);
  373.             county.setText(" " + coutyText);
  374.             String citiesText = jsonObj.getString(Properties.TAG_CITIES);
  375.             cities.setText(" " + citiesText);
  376.             String bathroomText = jsonObj.getString(Properties.TAG_BATHROOM);
  377.             bathroom.setText(" " + bathroomText);
  378.             String bedroomText = jsonObj.getString(Properties.TAG_BEDROOM);
  379.             bedroom.setText(" " + bedroomText);
  380.             String areaText = jsonObj.getString(Properties.TAG_AREA);
  381.             area.setText(" " + areaText);
  382.             String priceText = jsonObj.getString(Properties.TAG_PRICE);
  383.             String currency = jsonObj.getString(Properties.TAG_CURRENCY);
  384.             priceText = currency + " " + priceText;
  385.             int timeRate = jsonObj.getInt("time_rate");
  386.             if (timeRate != -1) {
  387.                 Resources r = getResources();
  388.                 if (timeRate == constants.WEEK) {
  389.                     priceText += " / " + r.getString(R.string.week);
  390.                 }
  391.                 if (timeRate == constants.MONTH) {
  392.                     priceText += " / " + r.getString(R.string.month);
  393.                 }
  394.                 if (timeRate == constants.YEAR) {
  395.                     priceText += " / " + r.getString(R.string.year);
  396.                 }
  397.             }
  398.             price.setText(" " + priceText);
  399.             int purposeInt = Integer.parseInt(jsonObj
  400.                     .getString(Properties.TAG_PURPOSE));
  401.             String purposeText = "";
  402.             if (purposeInt == constants.SALES_VALUE) {
  403.                 purposeText = getResources().getString(R.string.sales_label);
  404.             }
  405.             if (purposeInt == constants.SALES_AND_RENT_VALUE) {
  406.                 purposeText = getResources().getString(
  407.                         R.string.sales_and_rent_label);
  408.             }
  409.             if (purposeInt == constants.RENT_VALUE) {
  410.                 purposeText = getResources().getString(R.string.rent_label);
  411.             }
  412.  
  413.             String lat = jsonObj.getString("lat");
  414.             String lng = jsonObj.getString("lng");
  415.             final String markerPath = jsonObj.getString("path");
  416.             gmap = ((NiceSupportMapFragment) getSupportFragmentManager()
  417.                     .findFragmentById(R.id.map)).getMap();
  418.             gmap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
  419.             CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(
  420.                     Double.parseDouble(lat), Double.parseDouble(lng)));
  421.             gmap.moveCamera(center);
  422.             gmap.animateCamera(CameraUpdateFactory.zoomTo(10.0f));
  423.             final MarkerOptions markerOpt = new MarkerOptions()
  424.                     .position(new LatLng(Double.parseDouble(lat), Double
  425.                             .parseDouble(lng)));
  426.             if (markerPath == null || markerPath.equalsIgnoreCase("")
  427.                     || markerPath == "null" || markerPath == "") {
  428.                 markerOpt.icon(BitmapDescriptorFactory
  429.                         .fromResource(R.drawable.ic_pin));
  430.                 gpin = gmap.addMarker(markerOpt);
  431.             } else {
  432.                 Ion.with(DetailActivity.this)
  433.                         .load(getResources().getString(R.string.domain_url)
  434.                                 + markerPath).asBitmap()
  435.                         .setCallback(new FutureCallback<Bitmap>() {
  436.  
  437.                             @Override
  438.                             public void onCompleted(Exception arg0, Bitmap bmp) {
  439.                                 // TODO Auto-generated method stub
  440.                                 markerOpt.icon(BitmapDescriptorFactory
  441.                                         .fromBitmap(bmp));
  442.                                 gpin = gmap.addMarker(markerOpt);
  443.                             }
  444.  
  445.                         });
  446.  
  447.             }
  448.  
  449.             purpose.setText(" " + purposeText);
  450.             String content = jsonObj.getString(Properties.TAG_CONTENT);
  451.             DetailContentFragment fragment = DetailContentFragment
  452.                     .newInstance();
  453.             Bundle bundle = new Bundle();
  454.             bundle.putString(DetailContentFragment.DETAIL_KEY, content);
  455.             fragment.setArguments(bundle);
  456.             getSupportFragmentManager().beginTransaction()
  457.                     .replace(R.id.content, fragment).commit();
  458.         } catch (Exception e) {
  459.             // TODO: handle exception
  460.             e.printStackTrace();
  461.         }
  462.     }
  463.  
  464.     private class SendEnquiry extends AsyncTask<Void, Void, Boolean> {
  465.  
  466.         public SendEnquiry() {
  467.             // TODO Auto-generated constructor stub
  468.         }
  469.  
  470.         @Override
  471.         protected void onCancelled() {
  472.             // TODO Auto-generated method stub
  473.             super.onCancelled();
  474.             dialog.dismiss();
  475.         }
  476.  
  477.         protected void onPostExecute() {
  478.             dialog.dismiss();
  479.         };
  480.  
  481.         @Override
  482.         protected void onPreExecute() {
  483.             // TODO Auto-generated method stub
  484.             super.onPreExecute();
  485.             dialog = new ProgressDialog(DetailActivity.this);
  486.             dialog.setMessage(DetailActivity.this.getResources().getString(
  487.                     R.string.please_wait_msg));
  488.             dialog.setCanceledOnTouchOutside(false);
  489.             dialog.show();
  490.         }
  491.  
  492.         @Override
  493.         protected Boolean doInBackground(Void... params) {
  494.             // TODO Auto-generated method stub
  495.             String handleInserUrl = getResources().getString(
  496.                     R.string.users_json_url)
  497.                     + "send_enquiry";
  498.             try {
  499.                 HttpClient client = new DefaultHttpClient();
  500.                 HttpPost post = new HttpPost(handleInserUrl);
  501.                 MultipartEntity reqEntity = new MultipartEntity();
  502.                 reqEntity.addPart("email", new StringBody(email));
  503.                 reqEntity.addPart("message", new StringBody(message.getText()
  504.                         .toString()));
  505.                 reqEntity.addPart("reply_to",
  506.                         new StringBody(logedUser.getEmail()));
  507.                 reqEntity.addPart("user_name",
  508.                         new StringBody(logedUser.getUserName()));
  509.                 post.setEntity(reqEntity);
  510.                 HttpResponse response = client.execute(post);
  511.                 HttpEntity resEntity = response.getEntity();
  512.                 final String response_str = EntityUtils.toString(resEntity);
  513.                 if (resEntity != null) {
  514.                     Log.i("RESPONSE", response_str);
  515.                     runOnUiThread(new Runnable() {
  516.                         public void run() {
  517.                             try {
  518.                                 dialog.dismiss();
  519.                                 JSONObject jsonObj = new JSONObject(
  520.                                         response_str);
  521.                                 if (jsonObj.getString("ok").equals("0")) {
  522.                                     showDialog(getResources().getString(
  523.                                             R.string.spam_msg));
  524.                                 }
  525.                             } catch (Exception e) {
  526.                                 e.printStackTrace();
  527.                             }
  528.                         }
  529.                     });
  530.                 }
  531.             } catch (Exception ex) {
  532.                 Log.e("Debug", "error: " + ex.getMessage(), ex);
  533.             }
  534.             return null;
  535.         }
  536.     };
  537. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement