GalleryLayout.java 7.0 KB

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