AlbumLayout.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package com.ivalentin.margolariak;
  2. import android.annotation.SuppressLint;
  3. import android.database.Cursor;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.os.Build;
  6. import android.os.Bundle;
  7. import android.app.Fragment;
  8. import android.app.FragmentManager;
  9. import android.app.FragmentTransaction;
  10. import android.util.Log;
  11. import android.view.LayoutInflater;
  12. import android.view.View;
  13. import android.view.ViewGroup;
  14. import android.webkit.WebView;
  15. import android.widget.ImageView;
  16. import android.widget.LinearLayout;
  17. import android.widget.TextView;
  18. import java.io.File;
  19. /**
  20. * Fragment to display photos of a selected album.
  21. * Album id is passed in a bundle.
  22. * Album name is also passed, to be displayed on photos with no title.
  23. * Everything is done when the view is inflated.
  24. *
  25. * @author Iñigo Valentin
  26. *
  27. * @see Fragment
  28. *
  29. */
  30. public class AlbumLayout extends Fragment {
  31. @SuppressWarnings("ResultOfMethodCallIgnored")
  32. @SuppressLint("InflateParams") //Throws unknown error when done properly.
  33. @Override
  34. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  35. //Load the layout
  36. View view = inflater.inflate(R.layout.fragment_layout_album, null);
  37. //Get bundled id
  38. Bundle bundle = this.getArguments();
  39. int id = bundle.getInt("album", -1);
  40. if (id == -1){
  41. Log.e("Album error", "No such album: " + id);
  42. this.getActivity().onBackPressed();
  43. }
  44. //Get data from database
  45. SQLiteDatabase db = SQLiteDatabase.openDatabase(getActivity().getDatabasePath(GM.DB.NAME).getAbsolutePath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY);
  46. final Cursor cursor;
  47. String lang = GM.getLang();
  48. cursor = db.rawQuery("SELECT id, title_" + lang + " AS title, description_" + lang + " AS description, permalink FROM album WHERE id = " + id + ";", null);
  49. cursor.moveToFirst();
  50. //Set album elements
  51. TextView tvTitle = (TextView) view.findViewById(R.id.tv_album_title);
  52. WebView tvDescription = (WebView) view.findViewById(R.id.wv_album_description);
  53. if (Build.VERSION.SDK_INT >= 19) {
  54. tvDescription.setLayerType(View.LAYER_TYPE_HARDWARE, null);
  55. }
  56. else {
  57. tvDescription.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
  58. }
  59. tvTitle.setText(cursor.getString(1));
  60. ((MainActivity) getActivity()).setSectionTitle(cursor.getString(1));
  61. ((MainActivity) getActivity()).setShareLink(String.format(getString(R.string.share_with_title), cursor.getString(1)), GM.SHARE.GALLERY + cursor.getString(3));
  62. final String albumName = cursor.getString(1);
  63. final String albumPerm = cursor.getString(3);
  64. if (cursor.getString(2) == null || 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_" + lang + " 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) != null && 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. try{
  103. File file = new File(this.getActivity().getFilesDir().toString() + "/img/galeria/preview/" + image);
  104. ivLeft.setImageBitmap(GM.decodeSampledBitmapFromFile(file.getAbsolutePath(), GM.IMG.SIZE.PREVIEW));
  105. }
  106. catch (Exception ex){
  107. Log.e("Bitmap error", "Not loading image " + image + ": " + ex.toString());
  108. }
  109. }
  110. else {
  111. //If not, create directories and download asynchronously
  112. File fpath;
  113. fpath = new File(this.getActivity().getFilesDir().toString() + "/img/galeria/preview/");
  114. fpath.mkdirs();
  115. new DownloadImage(GM.API.SERVER + "/img/galeria/preview/" + image, this.getActivity().getFilesDir().toString() + "/img/galeria/preview/" + image, ivLeft, GM.IMG.SIZE.PREVIEW).execute();
  116. }
  117. //Count comments
  118. Cursor cursorComments = db.rawQuery("SELECT id FROM photo_comment WHERE photo = " + imageCursor.getString(0) + ";", null);
  119. TextView tvCommentsLeft = (TextView) entry.findViewById(R.id.tv_row_album_comments_left);
  120. tvCommentsLeft.setText(String.valueOf(cursorComments.getCount()));
  121. cursorComments.close();
  122. //Set onClickListener
  123. left.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 PhotoLayout();
  128. Bundle bundle = new Bundle();
  129. //Pass post id
  130. int id = Integer.parseInt(((TextView) v.findViewById(R.id.tv_row_album_hidden_left)).getText().toString());
  131. bundle.putInt("photo", id);
  132. bundle.putString("albumName", albumName);
  133. bundle.putString("albumPerm", albumPerm);
  134. fragment.setArguments(bundle);
  135. FragmentManager fm = AlbumLayout.this.getActivity().getFragmentManager();
  136. FragmentTransaction ft = fm.beginTransaction();
  137. ft.replace(R.id.activity_main_content_fragment, fragment);
  138. ft.addToBackStack("photo_" + id);
  139. ft.commit();
  140. }
  141. });
  142. //If odd pass, to the right line
  143. if (imageCursor.moveToNext()){
  144. LinearLayout right = (LinearLayout) entry.findViewById(R.id.ll_row_album_right);
  145. right.setVisibility(View.VISIBLE);
  146. //Set title
  147. TextView tvTitleRight = (TextView) entry.findViewById(R.id.tv_row_album_title_right);
  148. if (imageCursor.getString(1) != null && imageCursor.getString(1).length() > 0) {
  149. tvTitleRight.setText(imageCursor.getString(1));
  150. }
  151. else{
  152. tvTitleRight.setVisibility(View.GONE);
  153. }
  154. //Set id
  155. TextView tvHiddenRight = (TextView) entry.findViewById(R.id.tv_row_album_hidden_right);
  156. tvHiddenRight.setText(imageCursor.getString(0));
  157. //Set image
  158. ImageView ivRight = (ImageView) entry.findViewById(R.id.iv_row_album_right);
  159. image = imageCursor.getString(2);
  160. //Check if image exists
  161. f = new File(this.getActivity().getFilesDir().toString() + "/img/galeria/preview/" + image);
  162. if (f.exists()){
  163. //If the image exists, set it.
  164. try {
  165. File file = new File(this.getActivity().getFilesDir().toString() + "/img/galeria/preview/" + image);
  166. ivRight.setImageBitmap(GM.decodeSampledBitmapFromFile(file.getAbsolutePath(), GM.IMG.SIZE.PREVIEW));
  167. }
  168. catch (Exception ex){
  169. Log.e("Bitmap error", "Not loading image " + image + ": " + ex.toString());
  170. }
  171. }
  172. else {
  173. //If not, create directories and download asynchronously
  174. File fpath;
  175. fpath = new File(this.getActivity().getFilesDir().toString() + "/img/galeria/preview/");
  176. fpath.mkdirs();
  177. new DownloadImage(GM.API.SERVER + "/img/galeria/preview/" + image, this.getActivity().getFilesDir().toString() + "/img/galeria/preview/" + image, ivRight, GM.IMG.SIZE.PREVIEW).execute();
  178. }
  179. //Count comments
  180. cursorComments = db.rawQuery("SELECT id FROM photo_comment WHERE photo = " + imageCursor.getString(0) + ";", null);
  181. TextView tvCommentsRight = (TextView) entry.findViewById(R.id.tv_row_album_comments_right);
  182. tvCommentsRight.setText(String.valueOf(cursorComments.getCount()));
  183. cursorComments.close();
  184. //Set onClickListener
  185. right.setOnClickListener(new View.OnClickListener(){
  186. @Override
  187. public void onClick(View v) {
  188. Fragment fragment = new PhotoLayout();
  189. Bundle bundle = new Bundle();
  190. //Pass post id
  191. int id = Integer.parseInt(((TextView) v.findViewById(R.id.tv_row_album_hidden_right)).getText().toString());
  192. bundle.putInt("photo", id);
  193. bundle.putString("albumName", albumName);
  194. fragment.setArguments(bundle);
  195. FragmentManager fm = AlbumLayout.this.getActivity().getFragmentManager();
  196. FragmentTransaction ft = fm.beginTransaction();
  197. ft.replace(R.id.activity_main_content_fragment, fragment);
  198. ft.addToBackStack("photo_" + id);
  199. ft.commit();
  200. }
  201. });
  202. }
  203. llList.addView(entry);
  204. }
  205. imageCursor.close();
  206. db.close();
  207. return view;
  208. }
  209. }