| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- package com.ivalentin.gm;
- import android.annotation.SuppressLint;
- import android.app.Fragment;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.io.File;
- import java.util.Locale;
- /**
- * Section that shows a photo of the gallery, with details and comments.
- * Contains info from almost every other section.
- *
- * @author Iñigo Valentin
- *
- * @see Fragment
- *
- */
- public class PhotoLayout extends Fragment {
- //Main View
- private View view;
- private String currLang;
- private String albumName;
- Integer photos[];
- int position;
- Context context;
- @SuppressLint("InflateParams") //Throws unknown error when done properly.
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- //Load the layout
- view = inflater.inflate(R.layout.fragment_layout_photo, null);
- context = view.getContext();
- //Get bundled id
- Bundle bundle = this.getArguments();
- albumName = bundle.getString("albumName", "");
- int id = bundle.getInt("photo", -1);
- if (id == -1) {
- Log.e("Photo error", "No such photo: " + id);
- this.getActivity().onBackPressed();
- }
- photos = loadPhotos(id);
- position = getPosition(photos, id);
- //Assign next/previous buttons listener
- Button btnPrevious = (Button) view.findViewById(R.id.bt_photo_previous);
- Button btnNext = (Button) view.findViewById(R.id.bt_photo_next);
- btnPrevious.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (position > 0){
- position --;
- populate(photos[position]);
- }
- }
- });
- btnNext.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (position < photos.length - 1){
- position ++;
- populate(photos[position]);
- }
- }
- });
- populate(id, view);
- return view;
- }
- /**
- * Given a photo id array and a photo id, tells the position of the photo in the array.
- *
- * @param list Array withe the ids of photos.
- * @param id Id of the reference photo.
- *
- * @return The position of the photo in the array.
- */
- public int getPosition(Integer[] list, int id){
- int i;
- try{
- i = 0;
- while (list[i] != id){
- i ++;
- }
- }
- catch(Exception ex){
- i = -1;
- }
- return i;
- }
- /**
- * Given a photo, builds an ordered array with the ids of all photos in the same album.
- *
- * @param id Id of the reference photo.
- *
- * @return A sorted Integer[] with the ids of the photos in the same album.
- */
- private Integer[] loadPhotos(int id){
- SQLiteDatabase db = getActivity().openOrCreateDatabase(GM.DB_NAME, Context.MODE_PRIVATE, null);
- //Get album id for the photo
- Cursor cAlbum = db.rawQuery("SELECT album FROM photo_album WHERE photo = " + id + ";", null);
- cAlbum.moveToFirst();
- int album = cAlbum.getInt(0);
- cAlbum.close();
- //Get photos of the album
- 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);
- Integer list[] = new Integer[cursor.getCount()];
- int i = 0;
- while(cursor.moveToNext()){
- list[i] = cursor.getInt(0);
- i ++;
- }
- cursor.close();
- db.close();
- return list;
- }
- private void populate(int id){
- populate(id, view);
- }
- @SuppressLint("InflateParams") //Throws unknown error when done properly.
- private void populate(int id, View v){
- final int photoId = id;
- //Get data from database
- SQLiteDatabase db = getActivity().openOrCreateDatabase(GM.DB_NAME, Context.MODE_PRIVATE, null);
- final Cursor cursor;
- currLang = Locale.getDefault().getDisplayLanguage();
- if (!currLang.equals("es") && !currLang.equals("eu")){
- currLang = "en";
- }
- cursor = db.rawQuery("SELECT id, file, title_" + currLang+ " AS title, description_" + currLang + " AS description, uploaded, width, height, size FROM photo WHERE id = " + id + ";", null);
- cursor.moveToFirst();
- //Get display elements
- ImageView imageView = (ImageView) v.findViewById(R.id.iv_photo);
- TextView tvTitle = (TextView) v.findViewById(R.id.tv_photo_title);
- TextView tvDate = (TextView) v.findViewById(R.id.tv_photo_date);
- final TextView tvComments = (TextView) v.findViewById(R.id.tv_comments);
- TextView tvDescription = (TextView) v.findViewById(R.id.tv_photo_description);
- LinearLayout llCommentList = (LinearLayout) v.findViewById(R.id.ll_comment_list);
- //Set fields
- if (cursor.getString(2).length() > 0) {
- tvTitle.setVisibility(View.VISIBLE);
- tvTitle.setText(cursor.getString(2));
- ((MainActivity) getActivity()).setSectionTitle(cursor.getString(2));
- }
- else{
- tvTitle.setVisibility(View.GONE);
- ((MainActivity) getActivity()).setSectionTitle(albumName);
- }
- if (cursor.getString(3).length() > 0) {
- tvDescription.setVisibility(View.VISIBLE);
- tvDescription.setText(cursor.getString(3));
- }
- else{
- tvDescription.setVisibility(View.GONE);
- }
- tvDate.setText(GM.formatDate(cursor.getString(4), currLang, true));
- //Get image
- String image = cursor.getString(1);
- //Check if image exists
- File f;
- f = new File(this.getActivity().getFilesDir().toString() + "/img/galeria/preview/" + image);
- if (f.exists()){
- //If the image exists, set it.
- Bitmap myBitmap = BitmapFactory.decodeFile(this.getActivity().getFilesDir().toString() + "/img/galeria/preview/" + image);
- imageView.setImageBitmap(myBitmap);
- }
- else {
- //If not, create directories and download asynchronously
- File fpath;
- fpath = new File(this.getActivity().getFilesDir().toString() + "/img/galeria/preview/");
- if (fpath.mkdirs()) {
- new DownloadImage(GM.SERVER + "/img/galeria/preview/" + image, this.getActivity().getFilesDir().toString() + "/img/galeria/preview/" + image, imageView).execute();
- }
- }
- //Hide or show comments, as needed
- Button btNext = (Button) v.findViewById(R.id.bt_photo_next);
- Button btPrevious = (Button) v.findViewById(R.id.bt_photo_previous);
- if (position == 0){
- btPrevious.setVisibility(View.INVISIBLE);
- }
- else{
- btPrevious.setVisibility(View.VISIBLE);
- }
- if (position == photos.length - 1){
- btNext.setVisibility(View.INVISIBLE);
- }
- else{
- btNext.setVisibility(View.VISIBLE);
- }
- //Get comments
- Cursor commentCursor = db.rawQuery("SELECT text, dtime, username FROM photo_comment WHERE photo = " + id + ";", null);
- tvComments.setText(String.format(getResources().getQuantityString(R.plurals.comment_comments, commentCursor.getCount()), commentCursor.getCount()));
- final int commentCount = commentCursor.getCount();
- cursor.moveToFirst();
- LinearLayout entry;
- LayoutInflater factory = LayoutInflater.from(getActivity());
- llCommentList.removeAllViews();
- while (commentCursor.moveToNext()) {
- entry = (LinearLayout) factory.inflate(R.layout.row_comment, null);
- //Set user
- TextView tvUser = (TextView) entry.findViewById(R.id.tv_row_comment_user);
- tvUser.setText(commentCursor.getString(2));
- TextView tvCDate = (TextView) entry.findViewById(R.id.tv_row_comment_date);
- tvCDate.setText(GM.formatDate(commentCursor.getString(1), currLang, true));
- TextView tvText = (TextView) entry.findViewById(R.id.tv_row_comment_text);
- tvText.setText(commentCursor.getString(0));
- //Add to the list
- llCommentList.addView(entry);
- }
- commentCursor.close();
- //Set up comment form
- final EditText etCommentText = (EditText) view.findViewById(R.id.et_comment_text);
- final EditText etCommentName = (EditText) view.findViewById(R.id.et_comment_name);
- Button btSendComment = (Button) view.findViewById(R.id.bt_comment_send);
- btSendComment.setOnClickListener(new View.OnClickListener(){
- @Override
- public void onClick(View v) {
- String user = etCommentName.getText().toString();
- String text = etCommentText.getText().toString();
- if (user.length() < 1){
- Toast.makeText(getActivity(), getString(R.string.comment_toast_user), Toast.LENGTH_LONG).show();
- etCommentName.requestFocus();
- return;
- }
- if (text.length() < 1){
- Toast.makeText(getActivity(), getString(R.string.comment_toast_text), Toast.LENGTH_LONG).show();
- etCommentText.requestFocus();
- return;
- }
- //Start async task
- LinearLayout form = (LinearLayout) view.findViewById(R.id.ll_new_comment);
- LinearLayout list = (LinearLayout) view.findViewById(R.id.ll_comment_list);
- new PostComment("galeria", user, text, currLang, photoId, form, list, commentCount, tvComments, context).execute();
- }
- });
- cursor.close();
- db.close();
- }
- }
|