PageRenderTime 43ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/sofurry/activities/ViewPMActivity.java

http://sofurry-android-app.googlecode.com/
Java | 300 lines | 182 code | 57 blank | 61 comment | 18 complexity | e320a8d772e7aa49eece6d5b0f0a58a2 MD5 | raw file
  1. package com.sofurry.activities;
  2. import android.content.Context;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.inputmethod.InputMethodManager;
  6. import android.webkit.WebView;
  7. import android.widget.ImageView;
  8. import android.widget.SlidingDrawer;
  9. import android.widget.TextView;
  10. import android.widget.Toast;
  11. import com.sofurry.AppConstants;
  12. import com.sofurry.R;
  13. import com.sofurry.adapters.NamesAcDbAdapter;
  14. import com.sofurry.base.classes.ActivityWithRequests;
  15. import com.sofurry.mobileapi.ApiFactory;
  16. import com.sofurry.mobileapi.core.Request;
  17. import com.sofurry.requests.AndroidRequestWrapper;
  18. import com.sofurry.requests.DataCall;
  19. import org.json.JSONArray;
  20. import org.json.JSONObject;
  21. /**
  22. * Class description
  23. *
  24. *
  25. * @author Author Name
  26. */
  27. public class ViewPMActivity
  28. extends ActivityWithRequests
  29. implements SlidingDrawer.OnDrawerCloseListener, SlidingDrawer.OnDrawerOpenListener {
  30. private ImageView drawerHandle_;
  31. private SlidingDrawer replyDrawer_;
  32. private String content_ = null;
  33. private String fromUserId_;
  34. private String fromUserName_;
  35. private String msgDate_;
  36. private String msgSubject_;
  37. private TextView messageText_;
  38. private TextView subject_;
  39. private WebView webview_;
  40. private int pmId_;
  41. public void buttonClick(View v) {
  42. try {
  43. if (v.getId() == R.id.reply) {
  44. pbh.showProgressDialog("Sending reply...");
  45. String messageText = messageText_.getText().toString();
  46. // Prepare message text, because line-breaks aren't transmitted correctly
  47. messageText = messageText.replaceAll("\n", "<br />").replaceAll("\r", "");
  48. Request req = ApiFactory.createSendPM(fromUserName_,Integer.parseInt(fromUserId_),subject_.getText().toString(),messageText,pmId_);
  49. AndroidRequestWrapper arw = new AndroidRequestWrapper(requesthandler, req);
  50. arw.exec(new DataCall() { public void call() { handleSendData((JSONObject)arg1); } });
  51. }
  52. } catch (Exception e) {
  53. onError(e);
  54. }
  55. }
  56. protected String formatContents(String contents, String fromUserName, String subject, String date) {
  57. StringBuilder contentBuilder;
  58. contents = contents.replaceAll(AppConstants.PM_CONTENTS_URL_REGEX, AppConstants.PM_CONTENTS_URL_TEMPLATE);
  59. contents = contents.replace("\n", "<br/>");
  60. // Create object
  61. contentBuilder = new StringBuilder();
  62. // Insert prefix and postfix information to get the right colour
  63. contentBuilder.append(AppConstants.PM_CONTENTS_PREFIX);
  64. contentBuilder.append("<b>From:</b> </i>");
  65. contentBuilder.append(fromUserName);
  66. contentBuilder.append("</i><br/>");
  67. contentBuilder.append("<b>Subject:</b> </i>");
  68. contentBuilder.append(subject);
  69. contentBuilder.append("</i><br/>");
  70. contentBuilder.append("<b>Date:</b> </i>");
  71. contentBuilder.append(date);
  72. contentBuilder.append("</i><br/><br/>");
  73. contentBuilder.append(contents);
  74. contentBuilder.append(AppConstants.PM_CONTENTS_POSTFIX);
  75. // Return result, replacing line breaks
  76. return contentBuilder.toString();
  77. }
  78. public void onCreate(Bundle savedInstanceState) {
  79. super.onCreate(savedInstanceState);
  80. setContentView(R.layout.viewpm);
  81. // Find the needed views
  82. drawerHandle_ = (ImageView) findViewById(R.id.handle);
  83. messageText_ = (TextView) findViewById(R.id.message_text);
  84. replyDrawer_ = (SlidingDrawer) findViewById(R.id.drawer);
  85. subject_ = (TextView) findViewById(R.id.subject);
  86. webview_ = (WebView) findViewById(R.id.browser);
  87. // Set the WebView's background colour to black
  88. webview_.setBackgroundColor(0);
  89. // Attach SlidingDrawer listeners
  90. replyDrawer_.setOnDrawerOpenListener(this);
  91. replyDrawer_.setOnDrawerCloseListener(this);
  92. // Check if we have a saved instance
  93. if (savedInstanceState == null) {
  94. Bundle extras = getIntent().getExtras();
  95. if (extras != null) {
  96. pmId_ = extras.getInt("PMID");
  97. pbh.showProgressDialog("Fetching data...");
  98. Request req = ApiFactory.createGetPmContent(pmId_);
  99. AndroidRequestWrapper arw = new AndroidRequestWrapper(requesthandler, req);
  100. arw.exec(new DataCall() { public void call() { handlePMContent((JSONObject)arg1); } });
  101. }
  102. } else {
  103. boolean drawerOpened;
  104. // Retrieve objects stored in the instance state object
  105. content_ = (String) retrieveObject("content");
  106. drawerOpened = (Boolean) retrieveObject("drawerOpened");
  107. fromUserId_ = (String) retrieveObject("fromUserId");
  108. fromUserName_ = (String) retrieveObject("fromUserName");
  109. msgDate_ = (String) retrieveObject("msgDate");
  110. msgSubject_ = (String) retrieveObject("msgSubject");
  111. pmId_ = (Integer) retrieveObject("pmId");
  112. // Also set text fields with contents stores in the instance state object
  113. messageText_.setText((CharSequence) retrieveObject("messageText"));
  114. subject_.setText((CharSequence) retrieveObject("subject"));
  115. // Open the drawer if it was open when the instance was saved
  116. if (drawerOpened) {
  117. replyDrawer_.open();
  118. }
  119. // Display the contents
  120. showContent();
  121. }
  122. }
  123. /**
  124. * Handles the feedback data from fetch PM Content
  125. * @param obj
  126. */
  127. public void handlePMContent(JSONObject obj) {
  128. try {
  129. JSONArray items;
  130. JSONObject jsonItem;
  131. // Hide the progress dialog
  132. pbh.hideProgressDialog();
  133. // Parse message from retrieved array
  134. items = new JSONArray(obj.getString("items"));
  135. jsonItem = items.getJSONObject(0);
  136. // Retrieve the data we need to use later
  137. msgSubject_ = jsonItem.getString("subject");
  138. fromUserId_ = jsonItem.getString("fromUserId");
  139. fromUserName_ = jsonItem.getString("fromUserName");
  140. msgDate_ = jsonItem.getString("date");
  141. // Generate and show contents
  142. content_ = formatContents(jsonItem.getString("message"), fromUserName_, msgSubject_, msgDate_);
  143. showContent();
  144. // Also set the subject if need be
  145. if (subject_.getText().length() == 0) {
  146. // If there's no 'Re:' in front of the subject, add one
  147. if (!msgSubject_.substring(0, 3).equalsIgnoreCase("re:")) {
  148. subject_.setText("Re: " + msgSubject_);
  149. } else {
  150. subject_.setText(msgSubject_);
  151. }
  152. }
  153. } catch (Exception e) {
  154. onError(e);
  155. }
  156. }
  157. /**
  158. * Handles the date Returned from SendPM
  159. * @param obj
  160. */
  161. public void handleSendData(JSONObject obj) {
  162. try {
  163. Boolean hadSuccess;
  164. // Hide progress dialog
  165. pbh.hideProgressDialog();
  166. // Figure out if we were successful in sending
  167. hadSuccess = obj.getBoolean("success");
  168. // Now act on it
  169. if (hadSuccess) {
  170. // Add the name to the name cache, because well.. Why not?
  171. NamesAcDbAdapter dbHelper = new NamesAcDbAdapter(this).open();
  172. dbHelper.names_.addName(fromUserName_);
  173. dbHelper.close();
  174. // Display 'toast' message about the success
  175. Toast.makeText(this, "Message sent!", Toast.LENGTH_LONG).show();
  176. // Close the activity
  177. this.finish();
  178. } else {
  179. errorTitle_ = "Error";
  180. errorMessage_ = obj.getString("message");
  181. showDialog(AppConstants.DIALOG_ERROR_ID);
  182. }
  183. } catch (Exception e) {
  184. onError(e);
  185. }
  186. }
  187. /**
  188. * Changes the drawer's handle graphics, as well as closes the soft keyboard
  189. *
  190. */
  191. public void onDrawerClosed() {
  192. if (isOrientationLandscape()) {
  193. drawerHandle_.setImageResource(R.drawable.reply_land);
  194. } else {
  195. drawerHandle_.setImageResource(R.drawable.reply);
  196. }
  197. // Hide the soft keyboard, if it's visible
  198. ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
  199. subject_.getWindowToken(),
  200. 0);
  201. }
  202. /**
  203. * Changes the drawer's handle graphics
  204. *
  205. */
  206. public void onDrawerOpened() {
  207. if (isOrientationLandscape()) {
  208. drawerHandle_.setImageResource(R.drawable.reply_land_close);
  209. } else {
  210. drawerHandle_.setImageResource(R.drawable.reply_close);
  211. }
  212. }
  213. /**
  214. * Primarily when the phone is rotated, storing all the important data so it can be restored
  215. * and a proper user experience is ensured
  216. *
  217. *
  218. * @param outState
  219. */
  220. @Override
  221. protected void onSaveInstanceState(Bundle outState) {
  222. super.onSaveInstanceState(outState);
  223. // Save various contents so we can restore it again
  224. // Starting with returned data
  225. storeObject("content", content_);
  226. storeObject("fromUserId", fromUserId_);
  227. storeObject("fromUserName", fromUserName_);
  228. storeObject("msgDate", msgDate_);
  229. storeObject("msgSubject", msgSubject_);
  230. storeObject("pmId", (Integer) pmId_);
  231. // And the state of the drawer
  232. storeObject("drawerOpened", (Boolean) replyDrawer_.isOpened());
  233. // And of course the various views
  234. storeObject("messageText", messageText_.getText());
  235. storeObject("subject", subject_.getText());
  236. }
  237. /**
  238. * Displays the contents in the WebView
  239. */
  240. public void showContent() {
  241. webview_.loadData(content_, "text/html", "utf-8");
  242. }
  243. }