AlbumLayout.java 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package com.ivalentin.gm;
  2. import android.annotation.SuppressLint;
  3. import android.content.Context;
  4. import android.database.Cursor;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.graphics.Bitmap;
  7. import android.graphics.BitmapFactory;
  8. import android.os.Bundle;
  9. import android.app.Fragment;
  10. import android.app.FragmentManager;
  11. import android.app.FragmentTransaction;
  12. import android.util.Log;
  13. import android.view.LayoutInflater;
  14. import android.view.View;
  15. import android.view.ViewGroup;
  16. import android.webkit.WebView;
  17. import android.widget.ImageView;
  18. import android.widget.LinearLayout;
  19. import android.widget.TextView;
  20. import java.io.File;
  21. import java.util.Locale;
  22. /**
  23. * Fragment to display photos of a selected album.
  24. * Album id is passed in a bundle.
  25. * Album name is also passed, to be displayed on photos with no title.
  26. * Everything is done when the view is inflated.
  27. *
  28. * @author Iñigo Valentin
  29. *
  30. * @see Fragment
  31. *
  32. */
  33. public class AlbumLayout extends Fragment {
  34. //Main View
  35. //private View view;
  36. //private String currLang;
  37. //private String albumName;
  38. @SuppressLint("InflateParams") //Throws unknown error when done properly.
  39. @Override
  40. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  41. //Load the layout
  42. View view = inflater.inflate(R.layout.fragment_layout_album, null);
  43. //Get bundled id
  44. Bundle bundle = this.getArguments();
  45. int id = bundle.getInt("album", -1);
  46. if (id == -1){
  47. Log.e("Album error", "No such album: " + id);
  48. this.getActivity().onBackPressed();
  49. }
  50. //Get data from database
  51. SQLiteDatabase db = getActivity().openOrCreateDatabase(GM.DB_NAME, Context.MODE_PRIVATE, null);
  52. final Cursor cursor;
  53. String currLang = Locale.getDefault().getDisplayLanguage();
  54. if (!currLang.equals("es") && !currLang.equals("eu")){
  55. currLang = "en";
  56. }
  57. cursor = db.rawQuery("SELECT id, name_" + currLang + " AS name, description_" + currLang + " AS description FROM album WHERE id = " + id + ";", null);
  58. cursor.moveToFirst();
  59. //Set album elements
  60. TextView tvTitle = (TextView) view.findViewById(R.id.tv_album_title);
  61. WebView tvDescription = (WebView) view.findViewById(R.id.wv_album_description);
  62. tvTitle.setText(cursor.getString(1));
  63. final String albumName = cursor.getString(1);
  64. if (cursor.getString(2).length() < 1){
  65. tvDescription.setVisibility(View.GONE);
  66. }
  67. else {
  68. tvDescription.loadDataWithBaseURL(null, cursor.getString(2), "text/html", "utf-8", null);
  69. }
  70. cursor.close();
  71. //Assign elements
  72. LinearLayout llList = (LinearLayout) view.findViewById(R.id.ll_album_list);
  73. LinearLayout entry;
  74. //An inflater
  75. LayoutInflater factory = LayoutInflater.from(getActivity());
  76. String image;
  77. Cursor imageCursor = db.rawQuery("SELECT id, title_" + currLang + " AS title, file, width, height FROM photo, photo_album WHERE photo = id AND album = " + id + " ORDER BY uploaded DESC;", null);
  78. while (imageCursor.moveToNext()){
  79. //Create a new row
  80. entry = (LinearLayout) factory.inflate(R.layout.row_album, null);
  81. //Set left and right layouts
  82. LinearLayout left = (LinearLayout) entry.findViewById(R.id.ll_row_album_left);
  83. //Set title
  84. TextView tvTitleLeft = (TextView) entry.findViewById(R.id.tv_row_album_title_left);
  85. if (imageCursor.getString(1).length() > 0) {
  86. tvTitleLeft.setText(imageCursor.getString(1));
  87. }
  88. else{
  89. tvTitleLeft.setVisibility(View.GONE);
  90. }
  91. //Set id
  92. TextView tvHiddenLeft = (TextView) entry.findViewById(R.id.tv_row_album_hidden_left);
  93. tvHiddenLeft.setText(imageCursor.getString(0));
  94. //Set image
  95. ImageView ivLeft = (ImageView) entry.findViewById(R.id.iv_row_album_left);
  96. image = imageCursor.getString(2);
  97. //Check if image exists
  98. File f;
  99. f = new File(this.getActivity().getFilesDir().toString() + "/img/galeria/preview/" + image);
  100. if (f.exists()){
  101. //If the image exists, set it.
  102. Bitmap myBitmap = BitmapFactory.decodeFile(this.getActivity().getFilesDir().toString() + "/img/galeria/preview/" + image);
  103. ivLeft.setImageBitmap(myBitmap);
  104. }
  105. else {
  106. //If not, create directories and download asynchronously
  107. File fpath;
  108. fpath = new File(this.getActivity().getFilesDir().toString() + "/img/galeria/preview/");
  109. if(fpath.mkdirs()) {
  110. new DownloadImage(GM.SERVER + "/img/galeria/preview/" + image, this.getActivity().getFilesDir().toString() + "/img/galeria/preview/" + image, ivLeft).execute();
  111. }
  112. }
  113. //Count comments
  114. Cursor cursorComments = db.rawQuery("SELECT id FROM photo_comment WHERE photo = " + imageCursor.getString(0) + ";", null);
  115. TextView tvCommentsLeft = (TextView) entry.findViewById(R.id.tv_row_album_comments_left);
  116. tvCommentsLeft.setText(cursorComments.getCount());
  117. cursorComments.close();
  118. //Set onClickListener
  119. left.setOnClickListener(new View.OnClickListener(){
  120. @Override
  121. public void onClick(View v) {
  122. //Toast.makeText(getActivity(), "OPENING POST", Toast.LENGTH_LONG).show();
  123. Fragment fragment = new PhotoLayout();
  124. Bundle bundle = new Bundle();
  125. //Pass post id
  126. int id = Integer.parseInt(((TextView) v.findViewById(R.id.tv_row_album_hidden_left)).getText().toString());
  127. bundle.putInt("photo", id);
  128. bundle.putString("albumName", albumName);
  129. fragment.setArguments(bundle);
  130. FragmentManager fm = AlbumLayout.this.getActivity().getFragmentManager();
  131. FragmentTransaction ft = fm.beginTransaction();
  132. ft.replace(R.id.activity_main_content_fragment, fragment);
  133. ft.addToBackStack("photo_" + id);
  134. ft.commit();
  135. }
  136. });
  137. //If odd pass, to the right line
  138. if (imageCursor.moveToNext()){
  139. LinearLayout right = (LinearLayout) entry.findViewById(R.id.ll_row_album_right);
  140. right.setVisibility(View.VISIBLE);
  141. //Set title
  142. TextView tvTitleRight = (TextView) entry.findViewById(R.id.tv_row_album_title_right);
  143. if (imageCursor.getString(1).length() > 0) {
  144. tvTitleRight.setText(imageCursor.getString(1));
  145. }
  146. else{
  147. tvTitleRight.setVisibility(View.GONE);
  148. }
  149. //Set id
  150. TextView tvHiddenRight = (TextView) entry.findViewById(R.id.tv_row_album_hidden_right);
  151. tvHiddenRight.setText(imageCursor.getString(0));
  152. //Set image
  153. ImageView ivRight = (ImageView) entry.findViewById(R.id.iv_row_album_right);
  154. image = imageCursor.getString(2);
  155. //Check if image exists
  156. f = new File(this.getActivity().getFilesDir().toString() + "/img/galeria/preview/" + image);
  157. if (f.exists()){
  158. //If the image exists, set it.
  159. Bitmap myBitmap = BitmapFactory.decodeFile(this.getActivity().getFilesDir().toString() + "/img/galeria/preview/" + image);
  160. ivRight.setImageBitmap(myBitmap);
  161. }
  162. else {
  163. //If not, create directories and download asynchronously
  164. File fpath;
  165. fpath = new File(this.getActivity().getFilesDir().toString() + "/img/galeria/preview/");
  166. if(fpath.mkdirs()) {
  167. new DownloadImage(GM.SERVER + "/img/galeria/preview/" + image, this.getActivity().getFilesDir().toString() + "/img/galeria/preview/" + image, ivRight).execute();
  168. }
  169. }
  170. //Count comments
  171. cursorComments = db.rawQuery("SELECT id FROM photo_comment WHERE photo = " + imageCursor.getString(0) + ";", null);
  172. TextView tvCommentsRight = (TextView) entry.findViewById(R.id.tv_row_album_comments_right);
  173. tvCommentsRight.setText(cursorComments.getCount());
  174. cursorComments.close();
  175. //Set onClickListener
  176. right.setOnClickListener(new View.OnClickListener(){
  177. @Override
  178. public void onClick(View v) {
  179. Fragment fragment = new PhotoLayout();
  180. Bundle bundle = new Bundle();
  181. //Pass post id
  182. int id = Integer.parseInt(((TextView) v.findViewById(R.id.tv_row_album_hidden_right)).getText().toString());
  183. bundle.putInt("photo", id);
  184. bundle.putString("albumName", albumName);
  185. fragment.setArguments(bundle);
  186. FragmentManager fm = AlbumLayout.this.getActivity().getFragmentManager();
  187. FragmentTransaction ft = fm.beginTransaction();
  188. ft.replace(R.id.activity_main_content_fragment, fragment);
  189. ft.addToBackStack("photo_" + id);
  190. ft.commit();
  191. }
  192. });
  193. }
  194. llList.addView(entry);
  195. }
  196. imageCursor.close();
  197. db.close();
  198. return view;
  199. }
  200. }