ActivityLayout.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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
  77. LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
  78. layoutParams.setMargins(10, 10, 10, 25);
  79. entry.setLayoutParams(layoutParams);
  80. //Set title
  81. TextView tvTitle = (TextView) entry.findViewById(R.id.tv_row_activity_future_title);
  82. tvTitle.setText(cursor.getString(3));
  83. //Set city
  84. TextView tvCity = (TextView) entry.findViewById(R.id.tv_row_activity_future_city);
  85. tvCity.setText(cursor.getString(2));
  86. //Set price
  87. TextView tvPrice = (TextView) entry.findViewById(R.id.tv_row_activity_future_price);
  88. tvPrice.setText(String.format(getString(R.string.price), cursor.getInt(5)));
  89. //Set text
  90. String text = Html.fromHtml(cursor.getString(4)).toString();
  91. TextView tvText = (TextView) entry.findViewById(R.id.tv_row_activity_future_text);
  92. tvText.setText(text);
  93. //Set date
  94. TextView tvDate = (TextView) entry.findViewById(R.id.tv_row_activity_future_date);
  95. tvDate.setText(GM.formatDate(cursor.getString(1) + " 00:00:00", lang, false));
  96. //Set hidden id
  97. TextView tvId = (TextView) entry.findViewById(R.id.tv_row_activity_future_hidden);
  98. tvId.setText(cursor.getString(0));
  99. //Get image
  100. ImageView iv = (ImageView) entry.findViewById(R.id.iv_row_activity_future);
  101. Cursor cursorImage = db.rawQuery("SELECT image FROM activity_image WHERE activity = " + cursor.getString(0) + " ORDER BY idx LIMIT 1;", null);
  102. if (cursorImage.getCount() > 0) {
  103. cursorImage.moveToFirst();
  104. String image = cursorImage.getString(0);
  105. //Check if image exists
  106. File f;
  107. f = new File(this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/" + image);
  108. if (f.exists()) {
  109. //If the image exists, set it.
  110. Bitmap myBitmap = BitmapFactory.decodeFile(this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/" + image);
  111. iv.setImageBitmap(myBitmap);
  112. } else {
  113. //If not, create directories and download asynchronously
  114. File fpath;
  115. fpath = new File(this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/");
  116. fpath.mkdirs();
  117. new DownloadImage(GM.API.SERVER + "/img/actividades/miniature/" + image, this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/" + image, iv, GM.IMG.MINIATURE).execute();
  118. }
  119. }
  120. cursorImage.close();
  121. //Set onCLickListener
  122. entry.setOnClickListener(new View.OnClickListener() {
  123. @Override
  124. public void onClick(View v) {
  125. //Toast.makeText(getActivity(), "OPENING POST", Toast.LENGTH_LONG).show();
  126. Fragment fragment = new ActivityFutureLayout();
  127. Bundle bundle = new Bundle();
  128. //Pass post id
  129. int id = Integer.parseInt(((TextView) v.findViewById(R.id.tv_row_activity_future_hidden)).getText().toString());
  130. bundle.putInt("activity", id);
  131. fragment.setArguments(bundle);
  132. FragmentManager fm = ActivityLayout.this.getActivity().getFragmentManager();
  133. FragmentTransaction ft = fm.beginTransaction();
  134. ft.replace(R.id.activity_main_content_fragment, fragment);
  135. ft.addToBackStack("activity_" + id);
  136. ft.commit();
  137. }
  138. });
  139. //Add to the list
  140. llListFuture.addView(entry);
  141. }
  142. cursor.close();
  143. }
  144. //Get data from the database of the past activities
  145. 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);
  146. //Print rows
  147. while (cursorPast.moveToNext()) {
  148. //Create a new row
  149. entry = (LinearLayout) factory.inflate(R.layout.row_activity_past, null);
  150. //Set margins
  151. LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
  152. layoutParams.setMargins(10, 10, 10, 25);
  153. entry.setLayoutParams(layoutParams);
  154. //Set title
  155. TextView tvTitle = (TextView) entry.findViewById(R.id.tv_row_activity_past_title);
  156. tvTitle.setText(cursorPast.getString(3));
  157. //Set text
  158. String text;
  159. if (cursorPast.getString(5) == null){
  160. text = Html.fromHtml(cursorPast.getString(4)).toString();
  161. }
  162. else {
  163. if (cursorPast.getString(5).length() < 1) {
  164. text = Html.fromHtml(cursorPast.getString(4)).toString();
  165. } else {
  166. text = Html.fromHtml(cursorPast.getString(5)).toString();
  167. }
  168. }
  169. TextView tvText = (TextView) entry.findViewById(R.id.tv_row_activity_past_text);
  170. tvText.setText(text);
  171. //Set date
  172. TextView tvDate = (TextView) entry.findViewById(R.id.tv_row_activity_past_date);
  173. tvDate.setText(GM.formatDate(cursorPast.getString(1) + " 00:00:00", lang, false));
  174. //Set hidden id
  175. TextView tvId = (TextView) entry.findViewById(R.id.tv_row_activity_past_hidden);
  176. tvId.setText(cursorPast.getString(0));
  177. //Get image
  178. ImageView iv = (ImageView) entry.findViewById(R.id.iv_row_activity_past);
  179. Cursor cursorImage = db.rawQuery("SELECT image FROM activity_image WHERE activity = " + cursorPast.getString(0) + " ORDER BY idx LIMIT 1;", null);
  180. if (cursorImage.getCount() > 0) {
  181. cursorImage.moveToFirst();
  182. String image = cursorImage.getString(0);
  183. //Check if image exists
  184. File f;
  185. f = new File(this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/" + image);
  186. if (f.exists()) {
  187. //If the image exists, set it.
  188. Bitmap myBitmap = BitmapFactory.decodeFile(this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/" + image);
  189. iv.setImageBitmap(myBitmap);
  190. } else {
  191. //If not, create directories and download asynchronously
  192. File fpath;
  193. fpath = new File(this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/");
  194. fpath.mkdirs();
  195. new DownloadImage(GM.API.SERVER + "/img/actividades/miniature/" + image, this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/" + image, iv, GM.IMG.MINIATURE).execute();
  196. }
  197. }
  198. cursorImage.close();
  199. //Set onCLickListener
  200. entry.setOnClickListener(new View.OnClickListener() {
  201. @Override
  202. public void onClick(View v) {
  203. //Toast.makeText(getActivity(), "OPENING POST", Toast.LENGTH_LONG).show();
  204. Fragment fragment = new ActivityPastLayout();
  205. Bundle bundle = new Bundle();
  206. //Pass post id
  207. int id = Integer.parseInt(((TextView) v.findViewById(R.id.tv_row_activity_past_hidden)).getText().toString());
  208. bundle.putInt("activity", id);
  209. fragment.setArguments(bundle);
  210. FragmentManager fm = ActivityLayout.this.getActivity().getFragmentManager();
  211. FragmentTransaction ft = fm.beginTransaction();
  212. ft.replace(R.id.activity_main_content_fragment, fragment);
  213. ft.addToBackStack("activity_" + id);
  214. ft.commit();
  215. }
  216. });
  217. //Add to the list
  218. llListPast.addView(entry);
  219. }
  220. cursorPast.close();
  221. db.close();
  222. }
  223. }