PhotoLayout.java 8.8 KB

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