LablancaLayout.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package com.ivalentin.margolariak;
  2. import android.annotation.SuppressLint;
  3. import android.app.Fragment;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.database.sqlite.SQLiteDatabase;
  7. import android.graphics.Bitmap;
  8. import android.graphics.BitmapFactory;
  9. import android.os.Build;
  10. import android.os.Bundle;
  11. import android.util.Log;
  12. import android.view.LayoutInflater;
  13. import android.view.View;
  14. import android.view.ViewGroup;
  15. import android.webkit.WebView;
  16. import android.widget.Button;
  17. import android.widget.ImageView;
  18. import android.widget.LinearLayout;
  19. import android.widget.TextView;
  20. import java.io.File;
  21. import java.text.DateFormat;
  22. import java.text.SimpleDateFormat;
  23. import java.util.Calendar;
  24. import java.util.Date;
  25. import java.util.Locale;
  26. /**
  27. * Fragment openen for La Blanca sections while the festivals are not close.
  28. *
  29. * @see Fragment
  30. *
  31. * @author Iñigo Valentin
  32. *
  33. */
  34. public class LablancaLayout extends Fragment {
  35. /**
  36. * Run when the fragment is inflated.
  37. *
  38. * @param inflater A LayoutInflater to manage views
  39. * @param container The container View
  40. * @param savedInstanceState Bundle containing the state
  41. * @return The fragment view
  42. * @see android.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
  43. */
  44. @Override
  45. @SuppressLint("InflateParams")
  46. @SuppressWarnings("ResultOfMethodCallIgnored")
  47. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  48. //Load the layout
  49. View view = inflater.inflate(R.layout.fragment_layout_lablanca, null);
  50. //Set the title
  51. ((MainActivity) getActivity()).setSectionTitle(view.getContext().getString(R.string.menu_lablanca));
  52. //Get database info from the database
  53. String lang = GM.getLang();
  54. int year = Calendar.getInstance().get(Calendar.YEAR);
  55. SQLiteDatabase db = SQLiteDatabase.openDatabase(getActivity().getDatabasePath(GM.DB_NAME).getAbsolutePath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY);
  56. Cursor cursor = db.rawQuery("SELECT text_" + lang + ", img FROM festival WHERE year = " + year + ";", null);
  57. cursor.moveToFirst();
  58. //Set text
  59. WebView headerText = (WebView) view.findViewById(R.id.wv_lablanca_header);
  60. if (Build.VERSION.SDK_INT >= 19) {
  61. headerText.setLayerType(View.LAYER_TYPE_HARDWARE, null);
  62. }
  63. else {
  64. headerText.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
  65. }
  66. headerText.loadDataWithBaseURL(null, cursor.getString(0), "text/html", "utf-8", null);
  67. //Set image
  68. ImageView headerImage = (ImageView) view.findViewById(R.id.iv_lablanca_header);
  69. String image = cursor.getString(1);
  70. if (image.length() > 0){
  71. //Check if image exists
  72. File f;
  73. f = new File(this.getActivity().getFilesDir().toString() + "/img/fiestas/preview/" + image);
  74. if (f.exists()) {
  75. //If the image exists, set it.
  76. Bitmap myBitmap = BitmapFactory.decodeFile(this.getActivity().getFilesDir().toString() + "/img/fiestas/preview/" + image);
  77. headerImage.setImageBitmap(myBitmap);
  78. } else {
  79. //If not, create directories and download asynchronously
  80. File fpath;
  81. fpath = new File(this.getActivity().getFilesDir().toString() + "/img/fiestas/preview/");
  82. fpath.mkdirs();
  83. new DownloadImage(GM.SERVER + "/img/fiestas/preview/" + image, this.getActivity().getFilesDir().toString() + "/img/fiestas/preview/" + image, headerImage).execute();
  84. }
  85. }
  86. else{
  87. headerImage.setVisibility(View.GONE);
  88. }
  89. //Get prices for single days
  90. Cursor cursorDays = db.rawQuery("SELECT date, name_" + lang + ", price FROM festival_day WHERE strftime('%Y', date) = '" + year + "' ORDER BY date;", null);
  91. LinearLayout list = (LinearLayout) view.findViewById(R.id.ll_lablanca_prices_days_list);
  92. LinearLayout entry;
  93. LayoutInflater factory = LayoutInflater.from(getActivity());
  94. while (cursorDays.moveToNext()){
  95. //Create a new row
  96. entry = (LinearLayout) factory.inflate(R.layout.row_festival_price_day, null);
  97. //Set margins
  98. LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
  99. layoutParams.setMargins(10, 10, 10, 25);
  100. entry.setLayoutParams(layoutParams);
  101. //Set title
  102. TextView tvName = (TextView) entry.findViewById(R.id.tv_row_festival_price_day_name);
  103. tvName.setText(cursorDays.getString(1));
  104. //Set price
  105. TextView tvPrice = (TextView) entry.findViewById(R.id.tv_row_festival_price_day_price);
  106. String price = cursorDays.getString(2) + " " + getString(R.string.eur);
  107. tvPrice.setText(price);
  108. //Set date
  109. TextView tvDate = (TextView) entry.findViewById(R.id.tv_row_festival_price_day_date);
  110. DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.ENGLISH);
  111. Date date;
  112. try {
  113. date = format.parse(cursorDays.getString(0));
  114. Calendar calendar = Calendar.getInstance();
  115. calendar.setTime(date);
  116. int day = calendar.get(Calendar.DAY_OF_MONTH);
  117. int month = calendar.get(Calendar.MONTH);
  118. switch (month){
  119. case 6:
  120. tvDate.setText(String.format(getString(R.string.lablanca_date_july), day));
  121. break;
  122. case 7:
  123. tvDate.setText(String.format(getString(R.string.lablanca_date_august), day));
  124. break;
  125. }
  126. }
  127. catch(Exception ex){
  128. Log.e("Date format error", ex.toString());
  129. }
  130. list.addView(entry);
  131. }
  132. //Get prices for single days
  133. Cursor cursorPacks = db.rawQuery("SELECT name_" + lang + ", description_" + lang + ", price FROM festival_offer WHERE year = " + year + " ORDER BY days;", null);
  134. list = (LinearLayout) view.findViewById(R.id.ll_lablanca_prices_packs_list);
  135. while (cursorPacks.moveToNext()){
  136. //Create a new row
  137. entry = (LinearLayout) factory.inflate(R.layout.row_festival_price_pack, null);
  138. //Set margins
  139. LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
  140. layoutParams.setMargins(10, 10, 10, 25);
  141. entry.setLayoutParams(layoutParams);
  142. //Set title
  143. TextView tvName = (TextView) entry.findViewById(R.id.tv_row_festival_price_pack_name);
  144. tvName.setText(cursorPacks.getString(0));
  145. //Set text
  146. TextView tvText = (TextView) entry.findViewById(R.id.tv_row_festival_price_pack_text);
  147. tvText.setText(cursorPacks.getString(1));
  148. //Set price
  149. TextView tvPrice = (TextView) entry.findViewById(R.id.tv_row_festival_price_pack_price);
  150. String price = cursorPacks.getString(2) + " " + getString(R.string.eur);
  151. tvPrice.setText(price);
  152. list.addView(entry);
  153. }
  154. cursor.close();
  155. cursorDays.close();
  156. cursorPacks.close();
  157. db.close();
  158. //Set listeners for the schedule buttons.
  159. Button gmSchedule = (Button) view.findViewById(R.id.bt_lablanca_schedule_gm);
  160. Button citySchedule = (Button) view.findViewById(R.id.bt_lablanca_schedule_city);
  161. gmSchedule.setOnClickListener(new View.OnClickListener() {
  162. @Override
  163. public void onClick(View v) {
  164. ((MainActivity) getActivity()).loadSection(GM.SECTION_LABLANCA_GM_SCHEDULE, false);
  165. }
  166. });
  167. citySchedule.setOnClickListener(new View.OnClickListener() {
  168. @Override
  169. public void onClick(View v) {
  170. ((MainActivity) getActivity()).loadSection(GM.SECTION_LABLANCA_SCHEDULE, false);
  171. }
  172. });
  173. return view;
  174. }
  175. }