GalleryLayout.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package com.ivalentin.margolariak;
  2. import android.database.Cursor;
  3. import android.database.sqlite.SQLiteDatabase;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.os.Bundle;
  7. import android.app.Fragment;
  8. import android.app.FragmentManager;
  9. import android.app.FragmentTransaction;
  10. import android.view.LayoutInflater;
  11. import android.view.View;
  12. import android.view.ViewGroup;
  13. import android.widget.ImageView;
  14. import android.widget.LinearLayout;
  15. import android.widget.TextView;
  16. import java.io.File;
  17. import java.util.Random;
  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 Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
  38. */
  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_gallery, container, false);
  43. //Set the title
  44. ((MainActivity) getActivity()).setSectionTitle(view.getContext().getString(R.string.menu_gallery));
  45. populate(view);
  46. return view;
  47. }
  48. /**
  49. * Populates the list of activities around.
  50. * It uses the default layout as parent.
  51. */
  52. private void populate(View view){
  53. //Assign elements
  54. LinearLayout llList = (LinearLayout) view.findViewById(R.id.ll_gallery_album_list);
  55. LinearLayout entry;
  56. //An inflater
  57. LayoutInflater factory = LayoutInflater.from(getActivity());
  58. SQLiteDatabase db = SQLiteDatabase.openDatabase(getActivity().getDatabasePath(GM.DB.NAME).getAbsolutePath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY);
  59. final Cursor cursor;
  60. String lang = GM.getLang();
  61. //Get data from the database
  62. 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);
  63. //Loop
  64. while (cursor.moveToNext()){
  65. //Create a new row
  66. entry = (LinearLayout) factory.inflate(R.layout.row_gallery, llList, false);
  67. //Set margins
  68. LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
  69. int margin = (int) (9 * getActivity().getResources().getDisplayMetrics().density);
  70. layoutParams.setMargins(margin, margin, margin, margin);
  71. entry.setLayoutParams(layoutParams);
  72. //Set title
  73. TextView tvTitle = (TextView) entry.findViewById(R.id.tv_row_gallery_title);
  74. tvTitle.setText(cursor.getString(1));
  75. //Set hidden id
  76. TextView tvId = (TextView) entry.findViewById(R.id.tv_row_gallery_hidden);
  77. tvId.setText(cursor.getString(0));
  78. //Get images
  79. ImageView preview[] = new ImageView[4];
  80. preview[0] = (ImageView) entry.findViewById(R.id.iv_row_gallery_0);
  81. preview[1] = (ImageView) entry.findViewById(R.id.iv_row_gallery_1);
  82. preview[2] = (ImageView) entry.findViewById(R.id.iv_row_gallery_2);
  83. preview[3] = (ImageView) entry.findViewById(R.id.iv_row_gallery_3);
  84. //Get db rows
  85. 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);
  86. //Loop
  87. int i = 0;
  88. Random r;
  89. while (cursorImage.moveToNext()){
  90. String image = cursorImage.getString(1);
  91. //Check if image exists
  92. File f;
  93. f = new File(this.getActivity().getFilesDir().toString() + "/img/galeria/miniature/" + image);
  94. if (f.exists()){
  95. //If the image exists, set it.
  96. Bitmap myBitmap = BitmapFactory.decodeFile(this.getActivity().getFilesDir().toString() + "/img/galeria/miniature/" + image);
  97. preview[i].setImageBitmap(myBitmap);
  98. }
  99. else {
  100. //If not, create directories and download asynchronously
  101. File fpath;
  102. fpath = new File(this.getActivity().getFilesDir().toString() + "/img/galeria/miniature/");
  103. fpath.mkdirs();
  104. new DownloadImage(GM.API.SERVER + "/img/galeria/miniature/" + image, this.getActivity().getFilesDir().toString() + "/img/galeria/miniature/" + image, preview[i], GM.IMG.SIZE.MINIATURE).execute();
  105. }
  106. //Rotate the image
  107. r = new Random();
  108. int rot = r.nextInt(31) - 15;
  109. preview[i].setRotation(rot);
  110. i ++;
  111. }
  112. //Set photo counter
  113. Cursor cursorCounter = db.rawQuery("SELECT id FROM photo, photo_album WHERE photo = id AND album = " + cursor.getString(0) + ";", null);
  114. TextView tvPhotoCounter = (TextView) entry.findViewById(R.id.tv_row_gallery_counter);
  115. tvPhotoCounter.setText(getResources().getQuantityString(R.plurals.gallery_photo_count, cursorCounter.getCount(), cursorCounter.getCount()));
  116. //Close cursors
  117. cursorCounter.close();
  118. cursorImage.close();
  119. //Set onCLickListener
  120. entry.setOnClickListener(new View.OnClickListener(){
  121. @Override
  122. public void onClick(View v) {
  123. Fragment fragment = new AlbumLayout();
  124. Bundle bundle = new Bundle();
  125. //Pass post id
  126. int id = Integer.parseInt(((TextView) v.findViewById(R.id.tv_row_gallery_hidden)).getText().toString());
  127. bundle.putInt("album", id);
  128. fragment.setArguments(bundle);
  129. FragmentManager fm = GalleryLayout.this.getActivity().getFragmentManager();
  130. FragmentTransaction ft = fm.beginTransaction();
  131. ft.replace(R.id.activity_main_content_fragment, fragment);
  132. ft.addToBackStack("album_" + id);
  133. ft.commit();
  134. }
  135. });
  136. llList.addView(entry);
  137. }
  138. //Close database
  139. cursor.close();
  140. db.close();
  141. }
  142. }