GalleryLayout.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.view.LayoutInflater;
  12. import android.view.View;
  13. import android.view.ViewGroup;
  14. import android.widget.ImageView;
  15. import android.widget.LinearLayout;
  16. import android.widget.TextView;
  17. import java.io.File;
  18. /**
  19. * Fragment of the gallery section.
  20. *
  21. * @see Fragment
  22. *
  23. * @author Iñigo Valentin
  24. *
  25. */
  26. public class GalleryLayout extends Fragment{
  27. /**
  28. * Run when the fragment is inflated.
  29. * Assigns views, gets the date and does the first call to the {@link @populate()} function.
  30. *
  31. * @param inflater A LayoutInflater to manage views
  32. * @param container The container View
  33. * @param savedInstanceState Bundle containing the state
  34. *
  35. * @return The fragment view
  36. *
  37. * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
  38. */
  39. @SuppressLint("InflateParams") //Throws unknown error when done properly.
  40. @Override
  41. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  42. //Load the layout
  43. View view = inflater.inflate(R.layout.fragment_layout_gallery, null);
  44. //Set the title
  45. ((MainActivity) getActivity()).setSectionTitle(view.getContext().getString(R.string.menu_gallery));
  46. populate(view);
  47. return view;
  48. }
  49. /**
  50. * Populates the list of activities around.
  51. * It uses the default layout as parent.
  52. */
  53. @SuppressLint("InflateParams") //Throws unknown error when done properly.
  54. @SuppressWarnings("ResultOfMethodCallIgnored")
  55. private void populate(View view){
  56. //Assign elements
  57. LinearLayout llList = (LinearLayout) view.findViewById(R.id.ll_gallery_album_list);
  58. LinearLayout entry;
  59. //An inflater
  60. LayoutInflater factory = LayoutInflater.from(getActivity());
  61. SQLiteDatabase db = SQLiteDatabase.openDatabase(getActivity().getDatabasePath(GM.DB_NAME).getAbsolutePath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY);
  62. final Cursor cursor;
  63. String lang = GM.getLang();
  64. //Get data from the database
  65. cursor = db.rawQuery("SELECT DISTINCT album.id AS id, album.title_" + lang + " AS name FROM photo, album, photo_album WHERE photo.id = photo AND album.id = album ORDER BY uploaded DESC;", null);
  66. //Loop
  67. while (cursor.moveToNext()){
  68. //Create a new row
  69. entry = (LinearLayout) factory.inflate(R.layout.row_gallery, null);
  70. //Set margins
  71. LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
  72. layoutParams.setMargins(10, 10, 10, 25);
  73. entry.setLayoutParams(layoutParams);
  74. //Set title
  75. TextView tvTitle = (TextView) entry.findViewById(R.id.tv_row_gallery_title);
  76. tvTitle.setText(cursor.getString(1));
  77. //Set hidden id
  78. TextView tvId = (TextView) entry.findViewById(R.id.tv_row_gallery_hidden);
  79. tvId.setText(cursor.getString(0));
  80. //Get images
  81. ImageView preview[] = new ImageView[4];
  82. preview[0] = (ImageView) entry.findViewById(R.id.iv_row_gallery_0);
  83. preview[1] = (ImageView) entry.findViewById(R.id.iv_row_gallery_1);
  84. preview[2] = (ImageView) entry.findViewById(R.id.iv_row_gallery_2);
  85. preview[3] = (ImageView) entry.findViewById(R.id.iv_row_gallery_3);
  86. //Get db rows
  87. Cursor cursorImage = db.rawQuery("SELECT id, file, width, height FROM photo, photo_album WHERE photo = id AND album = " + cursor.getString(0) + " ORDER BY random() LIMIT 4;", null);
  88. //Loop
  89. int i = 0;
  90. while (cursorImage.moveToNext()){
  91. String image = cursorImage.getString(1);
  92. //Check if image exists
  93. File f;
  94. f = new File(this.getActivity().getFilesDir().toString() + "/img/galeria/miniature/" + image);
  95. if (f.exists()){
  96. //If the image exists, set it.
  97. Bitmap myBitmap = BitmapFactory.decodeFile(this.getActivity().getFilesDir().toString() + "/img/galeria/miniature/" + image);
  98. preview[i].setImageBitmap(myBitmap);
  99. }
  100. else {
  101. //If not, create directories and download asynchronously
  102. File fpath;
  103. fpath = new File(this.getActivity().getFilesDir().toString() + "/img/galeria/miniature/");
  104. fpath.mkdirs();
  105. new DownloadImage(GM.SERVER + "/img/galeria/miniature/" + image, this.getActivity().getFilesDir().toString() + "/img/galeria/miniature/" + image, preview[i], GM.IMG_MINIATURE).execute();
  106. }
  107. i ++;
  108. }
  109. //Set photo counter
  110. Cursor cursorCounter = db.rawQuery("SELECT id FROM photo, photo_album WHERE photo = id AND album = " + cursor.getString(0) + ";", null);
  111. TextView tvPhotoCounter = (TextView) entry.findViewById(R.id.tv_row_gallery_counter);
  112. tvPhotoCounter.setText(getResources().getQuantityString(R.plurals.gallery_photo_count, cursorCounter.getCount(), cursorCounter.getCount()));
  113. //Close cursors
  114. cursorCounter.close();
  115. cursorImage.close();
  116. //Set onCLickListener
  117. entry.setOnClickListener(new View.OnClickListener(){
  118. @Override
  119. public void onClick(View v) {
  120. Fragment fragment = new AlbumLayout();
  121. Bundle bundle = new Bundle();
  122. //Pass post id
  123. int id = Integer.parseInt(((TextView) v.findViewById(R.id.tv_row_gallery_hidden)).getText().toString());
  124. bundle.putInt("album", id);
  125. fragment.setArguments(bundle);
  126. FragmentManager fm = GalleryLayout.this.getActivity().getFragmentManager();
  127. FragmentTransaction ft = fm.beginTransaction();
  128. ft.replace(R.id.activity_main_content_fragment, fragment);
  129. ft.addToBackStack("album_" + id);
  130. ft.commit();
  131. }
  132. });
  133. //Add to the list
  134. llList.addView(entry);
  135. }
  136. //Close database
  137. cursor.close();
  138. db.close();
  139. }
  140. }