ActivityLayout.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package com.ivalentin.margolariak;
  2. import android.annotation.SuppressLint;
  3. import android.database.Cursor;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.graphics.Bitmap;
  6. import android.graphics.BitmapFactory;
  7. import android.os.Bundle;
  8. import android.app.Fragment;
  9. import android.app.FragmentManager;
  10. import android.app.FragmentTransaction;
  11. import android.text.Html;
  12. import android.util.Log;
  13. import android.view.LayoutInflater;
  14. import android.view.View;
  15. import android.view.ViewGroup;
  16. import android.widget.ImageView;
  17. import android.widget.LinearLayout;
  18. import android.widget.TextView;
  19. import java.io.File;
  20. /**
  21. * Fragment to display activities.
  22. *
  23. * @author Iñigo Valentin
  24. *
  25. * @see Fragment
  26. *
  27. */
  28. @SuppressLint("ResultOfMethodCallIgnored")
  29. public class ActivityLayout extends Fragment{
  30. /**
  31. * Run when the fragment is inflated.
  32. * Assigns views, gets the date and does the first call to the {@link #populate} function.
  33. *
  34. * @param inflater A LayoutInflater to manage views
  35. * @param container The container View
  36. * @param savedInstanceState Bundle containing the state
  37. *
  38. * @return The fragment view
  39. *
  40. * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
  41. */
  42. @SuppressLint("InflateParams") //Throws unknown error when done properly.
  43. @Override
  44. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  45. //Load the layout
  46. View view = inflater.inflate(R.layout.fragment_layout_activities, null);
  47. //Set the title
  48. ((MainActivity) getActivity()).setSectionTitle(view.getContext().getString(R.string.menu_activities));
  49. populate(view);
  50. return view;
  51. }
  52. /**
  53. * Populates the list of activities.
  54. * It uses the default layout as parent.
  55. */
  56. @SuppressWarnings("ResultOfMethodCallIgnored")
  57. @SuppressLint("InflateParams") //Throws unknown error when done properly.
  58. private void populate(View view){
  59. LinearLayout llListFuture = (LinearLayout) view.findViewById(R.id.ll_activities_future_list);
  60. LinearLayout llListPast = (LinearLayout) view.findViewById(R.id.ll_activities_past_list);
  61. LinearLayout entry;
  62. //An inflater
  63. LayoutInflater factory = LayoutInflater.from(getActivity());
  64. SQLiteDatabase db = SQLiteDatabase.openDatabase(getActivity().getDatabasePath(GM.DB.NAME).getAbsolutePath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY);
  65. final Cursor cursor;
  66. String lang = GM.getLang();
  67. //Get data from the database of the future activities
  68. cursor = db.rawQuery("SELECT id, date, city, title_" + lang+ " AS title, text_" + lang + " AS text, price FROM activity WHERE date >= date('now') ORDER BY date DESC;", null);
  69. if (cursor.getCount() > 0) {
  70. TextView tvNoFuture = (TextView) view.findViewById(R.id.tv_activities_no_future);
  71. tvNoFuture.setVisibility(View.GONE);
  72. //Print rows
  73. while (cursor.moveToNext()) {
  74. //Create a new row
  75. entry = (LinearLayout) factory.inflate(R.layout.row_activity_future, null);
  76. //Set margins TODO: I dont know why it doesnt read margins from the xml
  77. LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
  78. int margin = (int) (9 * getActivity().getResources().getDisplayMetrics().density);
  79. layoutParams.setMargins(margin, margin, margin, margin);
  80. entry.setLayoutParams(layoutParams);
  81. //Set title
  82. TextView tvTitle = (TextView) entry.findViewById(R.id.tv_row_activity_future_title);
  83. tvTitle.setText(cursor.getString(3));
  84. //Set city
  85. TextView tvCity = (TextView) entry.findViewById(R.id.tv_row_activity_future_city);
  86. tvCity.setText(cursor.getString(2));
  87. //Set price
  88. TextView tvPrice = (TextView) entry.findViewById(R.id.tv_row_activity_future_price);
  89. tvPrice.setText(String.format(getString(R.string.price), cursor.getInt(5)));
  90. //Set text
  91. String text = Html.fromHtml(cursor.getString(4)).toString();
  92. TextView tvText = (TextView) entry.findViewById(R.id.tv_row_activity_future_text);
  93. tvText.setText(text);
  94. //Set date
  95. TextView tvDate = (TextView) entry.findViewById(R.id.tv_row_activity_future_date);
  96. tvDate.setText(GM.formatDate(cursor.getString(1) + " 00:00:00", lang, true, false, false));
  97. //Set hidden id
  98. TextView tvId = (TextView) entry.findViewById(R.id.tv_row_activity_future_hidden);
  99. tvId.setText(cursor.getString(0));
  100. //Get image
  101. ImageView iv = (ImageView) entry.findViewById(R.id.iv_row_activity_future);
  102. Cursor cursorImage = db.rawQuery("SELECT image FROM activity_image WHERE activity = " + cursor.getString(0) + " ORDER BY idx LIMIT 1;", null);
  103. if (cursorImage.getCount() > 0) {
  104. cursorImage.moveToFirst();
  105. String image = cursorImage.getString(0);
  106. //Check if image exists
  107. File f;
  108. f = new File(this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/" + image);
  109. if (f.exists()) {
  110. //If the image exists, set it.
  111. Bitmap myBitmap = BitmapFactory.decodeFile(this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/" + image);
  112. iv.setImageBitmap(myBitmap);
  113. } else {
  114. //If not, create directories and download asynchronously
  115. File fpath;
  116. fpath = new File(this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/");
  117. fpath.mkdirs();
  118. new DownloadImage(GM.API.SERVER + "/img/actividades/miniature/" + image, this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/" + image, iv, GM.IMG.SIZE.MINIATURE).execute();
  119. }
  120. }
  121. cursorImage.close();
  122. //Set onCLickListener
  123. entry.setOnClickListener(new View.OnClickListener() {
  124. @Override
  125. public void onClick(View v) {
  126. //Toast.makeText(getActivity(), "OPENING POST", Toast.LENGTH_LONG).show();
  127. Fragment fragment = new ActivityFutureLayout();
  128. Bundle bundle = new Bundle();
  129. //Pass post id
  130. int id = Integer.parseInt(((TextView) v.findViewById(R.id.tv_row_activity_future_hidden)).getText().toString());
  131. bundle.putInt("activity", id);
  132. fragment.setArguments(bundle);
  133. FragmentManager fm = ActivityLayout.this.getActivity().getFragmentManager();
  134. FragmentTransaction ft = fm.beginTransaction();
  135. ft.replace(R.id.activity_main_content_fragment, fragment);
  136. ft.addToBackStack("activity_" + id);
  137. ft.commit();
  138. }
  139. });
  140. //Add to the list
  141. llListFuture.addView(entry);
  142. }
  143. cursor.close();
  144. }
  145. //Get data from the database of the past activities
  146. Cursor cursorPast = db.rawQuery("SELECT id, date, city, title_" + lang + " AS title, text_" + lang + " AS text, after_" + lang + " AS after FROM activity WHERE date < date('now') ORDER BY date DESC;", null);
  147. //Print rows
  148. while (cursorPast.moveToNext()) {
  149. //Create a new row
  150. entry = (LinearLayout) factory.inflate(R.layout.row_activity_past, null);
  151. //Set margins TODO: I dont know why it doesnt read margins from the xml
  152. LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
  153. int margin = (int) (9 * getActivity().getResources().getDisplayMetrics().density);
  154. layoutParams.setMargins(margin, margin, margin, margin);
  155. entry.setLayoutParams(layoutParams);
  156. //Set title
  157. TextView tvTitle = (TextView) entry.findViewById(R.id.tv_row_activity_past_title);
  158. tvTitle.setText(cursorPast.getString(3));
  159. //Set text
  160. String text;
  161. if (cursorPast.getString(5) == null){
  162. text = Html.fromHtml(cursorPast.getString(4)).toString();
  163. }
  164. else {
  165. if (cursorPast.getString(5) == null || cursorPast.getString(5).length() < 1) {
  166. text = Html.fromHtml(cursorPast.getString(4)).toString();
  167. } else {
  168. text = Html.fromHtml(cursorPast.getString(5)).toString();
  169. }
  170. }
  171. TextView tvText = (TextView) entry.findViewById(R.id.tv_row_activity_past_text);
  172. tvText.setText(text);
  173. //Set date
  174. TextView tvDate = (TextView) entry.findViewById(R.id.tv_row_activity_past_date);
  175. tvDate.setText(GM.formatDate(cursorPast.getString(1) + " 00:00:00", lang, true, true, false));
  176. //Set hidden id
  177. TextView tvId = (TextView) entry.findViewById(R.id.tv_row_activity_past_hidden);
  178. tvId.setText(cursorPast.getString(0));
  179. //Get image
  180. ImageView iv = (ImageView) entry.findViewById(R.id.iv_row_activity_past);
  181. Cursor cursorImage = db.rawQuery("SELECT image FROM activity_image WHERE activity = " + cursorPast.getString(0) + " ORDER BY idx LIMIT 1;", null);
  182. if (cursorImage.getCount() > 0) {
  183. cursorImage.moveToFirst();
  184. String image = cursorImage.getString(0);
  185. //Check if image exists
  186. File f;
  187. f = new File(this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/" + image);
  188. if (f.exists()) {
  189. //If the image exists, set it.
  190. Bitmap myBitmap = BitmapFactory.decodeFile(this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/" + image);
  191. iv.setImageBitmap(myBitmap);
  192. } else {
  193. //If not, create directories and download asynchronously
  194. File fpath;
  195. fpath = new File(this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/");
  196. fpath.mkdirs();
  197. new DownloadImage(GM.API.SERVER + "/img/actividades/miniature/" + image, this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/" + image, iv, GM.IMG.SIZE.MINIATURE).execute();
  198. }
  199. }
  200. cursorImage.close();
  201. //Set onCLickListener
  202. entry.setOnClickListener(new View.OnClickListener() {
  203. @Override
  204. public void onClick(View v) {
  205. //Toast.makeText(getActivity(), "OPENING POST", Toast.LENGTH_LONG).show();
  206. Fragment fragment = new ActivityPastLayout();
  207. Bundle bundle = new Bundle();
  208. //Pass post id
  209. int id = Integer.parseInt(((TextView) v.findViewById(R.id.tv_row_activity_past_hidden)).getText().toString());
  210. bundle.putInt("activity", id);
  211. fragment.setArguments(bundle);
  212. FragmentManager fm = ActivityLayout.this.getActivity().getFragmentManager();
  213. FragmentTransaction ft = fm.beginTransaction();
  214. ft.replace(R.id.activity_main_content_fragment, fragment);
  215. ft.addToBackStack("activity_" + id);
  216. ft.commit();
  217. }
  218. });
  219. //Add to the list
  220. llListPast.addView(entry);
  221. }
  222. cursorPast.close();
  223. db.close();
  224. }
  225. }