ActivityLayout.java 12 KB

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