PhotoLayout.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package com.ivalentin.gm;
  2. import android.annotation.SuppressLint;
  3. import android.app.Fragment;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.database.sqlite.SQLiteDatabase;
  7. import android.graphics.Bitmap;
  8. import android.graphics.BitmapFactory;
  9. import android.os.Bundle;
  10. import android.util.Log;
  11. import android.view.LayoutInflater;
  12. import android.view.View;
  13. import android.view.ViewGroup;
  14. import android.widget.Button;
  15. import android.widget.EditText;
  16. import android.widget.ImageView;
  17. import android.widget.LinearLayout;
  18. import android.widget.TextView;
  19. import android.widget.Toast;
  20. import java.io.File;
  21. import java.util.Locale;
  22. /**
  23. * Section that shows a photo of the gallery, with details and comments.
  24. * Contains info from almost every other section.
  25. *
  26. * @author Iñigo Valentin
  27. *
  28. * @see Fragment
  29. *
  30. */
  31. public class PhotoLayout extends Fragment {
  32. //Main View
  33. private View view;
  34. private String currLang;
  35. private String albumName;
  36. Integer photos[];
  37. int position;
  38. Context context;
  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 = inflater.inflate(R.layout.fragment_layout_photo, null);
  44. context = view.getContext();
  45. //Get bundled id
  46. Bundle bundle = this.getArguments();
  47. albumName = bundle.getString("albumName", "");
  48. int id = bundle.getInt("photo", -1);
  49. if (id == -1) {
  50. Log.e("Photo error", "No such photo: " + id);
  51. this.getActivity().onBackPressed();
  52. }
  53. photos = loadPhotos(id);
  54. position = getPosition(photos, id);
  55. //Assign next/previous buttons listener
  56. Button btnPrevious = (Button) view.findViewById(R.id.bt_photo_previous);
  57. Button btnNext = (Button) view.findViewById(R.id.bt_photo_next);
  58. btnPrevious.setOnClickListener(new View.OnClickListener() {
  59. @Override
  60. public void onClick(View v) {
  61. if (position > 0){
  62. position --;
  63. populate(photos[position]);
  64. }
  65. }
  66. });
  67. btnNext.setOnClickListener(new View.OnClickListener() {
  68. @Override
  69. public void onClick(View v) {
  70. if (position < photos.length - 1){
  71. position ++;
  72. populate(photos[position]);
  73. }
  74. }
  75. });
  76. populate(id, view);
  77. return view;
  78. }
  79. /**
  80. * Given a photo id array and a photo id, tells the position of the photo in the array.
  81. *
  82. * @param list Array withe the ids of photos.
  83. * @param id Id of the reference photo.
  84. *
  85. * @return The position of the photo in the array.
  86. */
  87. public int getPosition(Integer[] list, int id){
  88. int i;
  89. try{
  90. i = 0;
  91. while (list[i] != id){
  92. i ++;
  93. }
  94. }
  95. catch(Exception ex){
  96. i = -1;
  97. }
  98. return i;
  99. }
  100. /**
  101. * Given a photo, builds an ordered array with the ids of all photos in the same album.
  102. *
  103. * @param id Id of the reference photo.
  104. *
  105. * @return A sorted Integer[] with the ids of the photos in the same album.
  106. */
  107. private Integer[] loadPhotos(int id){
  108. SQLiteDatabase db = getActivity().openOrCreateDatabase(GM.DB_NAME, Context.MODE_PRIVATE, null);
  109. //Get album id for the photo
  110. Cursor cAlbum = db.rawQuery("SELECT album FROM photo_album WHERE photo = " + id + ";", null);
  111. cAlbum.moveToFirst();
  112. int album = cAlbum.getInt(0);
  113. cAlbum.close();
  114. //Get photos of the album
  115. Cursor cursor = db.rawQuery("SELECT photo.id FROM photo,album, photo_album WHERE photo.id = photo AND album.id = album AND album = " + album + " ORDER BY uploaded DESC;", null);
  116. Integer list[] = new Integer[cursor.getCount()];
  117. int i = 0;
  118. while(cursor.moveToNext()){
  119. list[i] = cursor.getInt(0);
  120. i ++;
  121. }
  122. cursor.close();
  123. db.close();
  124. return list;
  125. }
  126. private void populate(int id){
  127. populate(id, view);
  128. }
  129. @SuppressLint("InflateParams") //Throws unknown error when done properly.
  130. private void populate(int id, View v){
  131. final int photoId = id;
  132. //Get data from database
  133. SQLiteDatabase db = getActivity().openOrCreateDatabase(GM.DB_NAME, Context.MODE_PRIVATE, null);
  134. final Cursor cursor;
  135. currLang = Locale.getDefault().getDisplayLanguage();
  136. if (!currLang.equals("es") && !currLang.equals("eu")){
  137. currLang = "en";
  138. }
  139. cursor = db.rawQuery("SELECT id, file, title_" + currLang+ " AS title, description_" + currLang + " AS description, uploaded, width, height, size FROM photo WHERE id = " + id + ";", null);
  140. cursor.moveToFirst();
  141. //Get display elements
  142. ImageView imageView = (ImageView) v.findViewById(R.id.iv_photo);
  143. TextView tvTitle = (TextView) v.findViewById(R.id.tv_photo_title);
  144. TextView tvDate = (TextView) v.findViewById(R.id.tv_photo_date);
  145. final TextView tvComments = (TextView) v.findViewById(R.id.tv_comments);
  146. TextView tvDescription = (TextView) v.findViewById(R.id.tv_photo_description);
  147. LinearLayout llCommentList = (LinearLayout) v.findViewById(R.id.ll_comment_list);
  148. //Set fields
  149. if (cursor.getString(2).length() > 0) {
  150. tvTitle.setVisibility(View.VISIBLE);
  151. tvTitle.setText(cursor.getString(2));
  152. ((MainActivity) getActivity()).setSectionTitle(cursor.getString(2));
  153. }
  154. else{
  155. tvTitle.setVisibility(View.GONE);
  156. ((MainActivity) getActivity()).setSectionTitle(albumName);
  157. }
  158. if (cursor.getString(3).length() > 0) {
  159. tvDescription.setVisibility(View.VISIBLE);
  160. tvDescription.setText(cursor.getString(3));
  161. }
  162. else{
  163. tvDescription.setVisibility(View.GONE);
  164. }
  165. tvDate.setText(GM.formatDate(cursor.getString(4), currLang, true));
  166. //Get image
  167. String image = cursor.getString(1);
  168. //Check if image exists
  169. File f;
  170. f = new File(this.getActivity().getFilesDir().toString() + "/img/galeria/preview/" + image);
  171. if (f.exists()){
  172. //If the image exists, set it.
  173. Bitmap myBitmap = BitmapFactory.decodeFile(this.getActivity().getFilesDir().toString() + "/img/galeria/preview/" + image);
  174. imageView.setImageBitmap(myBitmap);
  175. }
  176. else {
  177. //If not, create directories and download asynchronously
  178. File fpath;
  179. fpath = new File(this.getActivity().getFilesDir().toString() + "/img/galeria/preview/");
  180. if (fpath.mkdirs()) {
  181. new DownloadImage(GM.SERVER + "/img/galeria/preview/" + image, this.getActivity().getFilesDir().toString() + "/img/galeria/preview/" + image, imageView).execute();
  182. }
  183. }
  184. //Hide or show comments, as needed
  185. Button btNext = (Button) v.findViewById(R.id.bt_photo_next);
  186. Button btPrevious = (Button) v.findViewById(R.id.bt_photo_previous);
  187. if (position == 0){
  188. btPrevious.setVisibility(View.INVISIBLE);
  189. }
  190. else{
  191. btPrevious.setVisibility(View.VISIBLE);
  192. }
  193. if (position == photos.length - 1){
  194. btNext.setVisibility(View.INVISIBLE);
  195. }
  196. else{
  197. btNext.setVisibility(View.VISIBLE);
  198. }
  199. //Get comments
  200. Cursor commentCursor = db.rawQuery("SELECT text, dtime, username FROM photo_comment WHERE photo = " + id + ";", null);
  201. tvComments.setText(String.format(getResources().getQuantityString(R.plurals.comment_comments, commentCursor.getCount()), commentCursor.getCount()));
  202. final int commentCount = commentCursor.getCount();
  203. cursor.moveToFirst();
  204. LinearLayout entry;
  205. LayoutInflater factory = LayoutInflater.from(getActivity());
  206. llCommentList.removeAllViews();
  207. while (commentCursor.moveToNext()) {
  208. entry = (LinearLayout) factory.inflate(R.layout.row_comment, null);
  209. //Set user
  210. TextView tvUser = (TextView) entry.findViewById(R.id.tv_row_comment_user);
  211. tvUser.setText(commentCursor.getString(2));
  212. TextView tvCDate = (TextView) entry.findViewById(R.id.tv_row_comment_date);
  213. tvCDate.setText(GM.formatDate(commentCursor.getString(1), currLang, true));
  214. TextView tvText = (TextView) entry.findViewById(R.id.tv_row_comment_text);
  215. tvText.setText(commentCursor.getString(0));
  216. //Add to the list
  217. llCommentList.addView(entry);
  218. }
  219. commentCursor.close();
  220. //Set up comment form
  221. final EditText etCommentText = (EditText) view.findViewById(R.id.et_comment_text);
  222. final EditText etCommentName = (EditText) view.findViewById(R.id.et_comment_name);
  223. Button btSendComment = (Button) view.findViewById(R.id.bt_comment_send);
  224. btSendComment.setOnClickListener(new View.OnClickListener(){
  225. @Override
  226. public void onClick(View v) {
  227. String user = etCommentName.getText().toString();
  228. String text = etCommentText.getText().toString();
  229. if (user.length() < 1){
  230. Toast.makeText(getActivity(), getString(R.string.comment_toast_user), Toast.LENGTH_LONG).show();
  231. etCommentName.requestFocus();
  232. return;
  233. }
  234. if (text.length() < 1){
  235. Toast.makeText(getActivity(), getString(R.string.comment_toast_text), Toast.LENGTH_LONG).show();
  236. etCommentText.requestFocus();
  237. return;
  238. }
  239. //Start async task
  240. LinearLayout form = (LinearLayout) view.findViewById(R.id.ll_new_comment);
  241. LinearLayout list = (LinearLayout) view.findViewById(R.id.ll_comment_list);
  242. new PostComment("galeria", user, text, currLang, photoId, form, list, commentCount, tvComments, context).execute();
  243. }
  244. });
  245. cursor.close();
  246. db.close();
  247. }
  248. }