PostComment.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package com.ivalentin.margolariak;
  2. import android.annotation.SuppressLint;
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.content.SharedPreferences;
  6. import android.database.sqlite.SQLiteDatabase;
  7. import android.os.AsyncTask;
  8. import android.text.format.DateFormat;
  9. import android.util.Log;
  10. import android.view.LayoutInflater;
  11. import android.view.View;
  12. import android.widget.Button;
  13. import android.widget.EditText;
  14. import android.widget.LinearLayout;
  15. import android.widget.ProgressBar;
  16. import android.widget.TextView;
  17. import java.io.IOException;
  18. import java.io.UnsupportedEncodingException;
  19. import java.net.HttpURLConnection;
  20. import java.net.MalformedURLException;
  21. import java.net.ProtocolException;
  22. import java.net.URL;
  23. import java.net.URLEncoder;
  24. import java.util.Date;
  25. class PostComment extends AsyncTask<String, String, Integer> {
  26. //Elements passed from fragments
  27. private final String type, user, text, language;
  28. private String userCode;
  29. private final LinearLayout list, form;
  30. private final int id;
  31. private final Context context;
  32. //Elements calculated here
  33. private Button btSend;
  34. private ProgressBar pb;
  35. private int code = 404;
  36. PostComment(String type, String user, String text, String language, int id, LinearLayout form, LinearLayout list, Context context) {
  37. super();
  38. this.type = type;
  39. this.user = user;
  40. this.text = text;
  41. this.form = form;
  42. this.list = list;
  43. this.language = language;
  44. this.id = id;
  45. this.context = context;
  46. }
  47. /**
  48. * Before starting background thread
  49. * */
  50. @Override
  51. protected void onPreExecute() {
  52. super.onPreExecute();
  53. btSend = (Button) form.findViewById(R.id.bt_comment_send);
  54. pb = (ProgressBar) form.findViewById(R.id.pb_comment);
  55. btSend.setVisibility(View.GONE);
  56. pb.setVisibility(View.VISIBLE);
  57. //Get user code
  58. SharedPreferences sharedData = context.getSharedPreferences(GM.DATA.DATA, Context.MODE_PRIVATE);
  59. userCode = sharedData.getString(GM.DATA.KEY.USER, GM.DATA.DEFAULT.USER);
  60. }
  61. /**
  62. * Downloading file in background thread
  63. * */
  64. @Override
  65. @SuppressWarnings("deprecation")
  66. protected Integer doInBackground(String... f_url) {
  67. URL url;
  68. String urlParams;
  69. try {
  70. urlParams = "username=" + URLEncoder.encode(user) + "&text=" + URLEncoder.encode(text);
  71. urlParams = urlParams + "&id=" + id;
  72. switch (type) {
  73. case "blog":
  74. urlParams = urlParams + "&target=post";
  75. break;
  76. case "galeria":
  77. urlParams = urlParams + "&target=photo";
  78. break;
  79. case "actividades":
  80. urlParams = urlParams + "&target=activity";
  81. break;
  82. default:
  83. Log.e("Comment error", "Unknown section: " + type);
  84. return -1;
  85. }
  86. switch (language) {
  87. case "es":
  88. urlParams = urlParams + "&lang=es";
  89. break;
  90. case "eu":
  91. urlParams = urlParams + "&lang=eu";
  92. break;
  93. default:
  94. urlParams = urlParams + "&lang=en";
  95. }
  96. urlParams = urlParams + "&client=" + GM.API.CLIENT + "&user=" + userCode;
  97. String uri = GM.API.SERVER + GM.API.COMMENT.PATH + "?" + urlParams;
  98. url = new URL(uri);
  99. Log.e("URI", uri);
  100. HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
  101. code = urlConnection.getResponseCode();
  102. switch (code) {
  103. case 400: //Client error: Bad request
  104. Log.e("COMMENT", "The server returned a 400 code (Client Error: Bad request) for the url \"" + uri + "\"");
  105. return(-400);
  106. case 403: //Client error: Forbidden
  107. Log.e("COMMENT", "The server returned a 403 code (Client Error: Forbidden) for the url \"" + uri + "\"");
  108. return(-403);
  109. case 200: //Success: OK
  110. Log.d("COMMENT", "The server returned a 200 code (Success: OK) for the url \"" + uri + "\".");
  111. break;
  112. default:
  113. Log.e("COMMENT", "The server returned a " + code + " code for the url \"" + uri + "\".");
  114. return(-6);
  115. }
  116. }
  117. catch (UnsupportedEncodingException e) {
  118. Log.e("COMMENT", "Unable to post comment (UnsupportedEncodingException): " + e.toString());
  119. return -2;
  120. }
  121. catch (ProtocolException e) {
  122. Log.e("COMMENT", "Unable to post comment (ProtocolException): " + e.toString());
  123. return -3;
  124. }
  125. catch (MalformedURLException e) {
  126. Log.e("COMMENT", "Unable to post comment (MalformedURLException): " + e.toString());
  127. return -4;
  128. }
  129. catch (IOException e) {
  130. Log.e("COMMENT", "Unable to post comment (IOException): " + e.toString());
  131. return -5;
  132. }
  133. catch (Exception e) {
  134. Log.e("COMMENT", "Unable to post comment: " + e.toString());
  135. return -1;
  136. }
  137. return 0;
  138. }
  139. /**
  140. * After completing background task
  141. */
  142. @SuppressLint("InflateParams") //Throws unknown error when done properly.
  143. @Override
  144. protected void onPostExecute(Integer a) {
  145. if (code == 200) {
  146. //Clear form
  147. EditText formText = (EditText) form.findViewById(R.id.et_comment_text);
  148. EditText formUser = (EditText) form.findViewById(R.id.et_comment_name);
  149. formUser.setText("");
  150. formText.setText("");
  151. //TODO: Update counter
  152. //Perform a sync
  153. ((MainActivity) context).bgSync();
  154. //Insert comment in list
  155. LayoutInflater factory = LayoutInflater.from(context);
  156. LinearLayout entry = (LinearLayout) factory.inflate(R.layout.row_comment, null);
  157. //Set user
  158. TextView tvUser = (TextView) entry.findViewById(R.id.tv_row_comment_user);
  159. tvUser.setText(user);
  160. Date d = new Date();
  161. String date = DateFormat.format("yyyy-MM-dd hh:mm:ss", d.getTime()).toString();
  162. TextView tvCDate = (TextView) entry.findViewById(R.id.tv_row_comment_date);
  163. tvCDate.setText(GM.formatDate(date, language, true));
  164. TextView tvText = (TextView) entry.findViewById(R.id.tv_row_comment_text);
  165. tvText.setText(text);
  166. //Add to the list
  167. list.addView(entry);
  168. }
  169. //Show button again
  170. btSend.setVisibility(View.VISIBLE);
  171. pb.setVisibility(View.GONE);
  172. }
  173. }