<commit_before>package de.fau.cs.mad.yasme.android.ui.fragments;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
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.ListView;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

import de.fau.cs.mad.yasme.android.EditTextWithImage;
import de.fau.cs.mad.yasme.android.R;
import de.fau.cs.mad.yasme.android.asyncTasks.database.GetNewMessagesForChatTask;
import de.fau.cs.mad.yasme.android.asyncTasks.server.GetMessageTask;
import de.fau.cs.mad.yasme.android.asyncTasks.server.SendMessageTask;
import de.fau.cs.mad.yasme.android.controller.FragmentObservable;
import de.fau.cs.mad.yasme.android.controller.Log;
import de.fau.cs.mad.yasme.android.controller.NotifiableFragment;
import de.fau.cs.mad.yasme.android.controller.ObservableRegistry;
import de.fau.cs.mad.yasme.android.controller.Toaster;
import de.fau.cs.mad.yasme.android.encryption.MessageEncryption;
import de.fau.cs.mad.yasme.android.entities.Chat;
import de.fau.cs.mad.yasme.android.entities.Message;
import de.fau.cs.mad.yasme.android.entities.User;
import de.fau.cs.mad.yasme.android.storage.DatabaseManager;
import de.fau.cs.mad.yasme.android.storage.PictureManager;
import de.fau.cs.mad.yasme.android.ui.AbstractYasmeActivity;
import de.fau.cs.mad.yasme.android.ui.ChatAdapter;
import de.fau.cs.mad.yasme.android.ui.activities.ChatActivity;
import de.fau.cs.mad.yasme.android.ui.activities.ChatSettingsActivity;

/**
 * Created by <NAME> <<EMAIL>> on 21.06.2014.
 */
public class ChatFragment extends Fragment implements NotifiableFragment<List<Message>> {
    public static final String RESTORE_LATEST_MESSAGE_ON_DISPLAY = "LATEST_MESSAGE_ON_DISPLAY";
    public static final String RESTORE_CHAT_ID = "CHAT_ID";
    private ChatAdapter mAdapter;
    private Chat chat;
    private AtomicLong latestMessageOnDisplay;
    private Context mContext;

    //UI references
    private Button imageCancel;
    private ImageView imageView;
    private EditText editMessage;
    private ListView list;

    private static final int RESULT_LOAD_IMAGE = 100;
    private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 200;
    private Uri imageUri;
    private Bitmap bitmap;

    public ChatFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Retrain instance variables!
        // setRetainInstance(true);

        ChatActivity activity = (ChatActivity) getActivity();
        mContext = DatabaseManager.INSTANCE.getContext();

        Intent intent = activity.getIntent();
        long chatId = intent.getLongExtra(AbstractYasmeActivity.CHAT_ID, -1);
        if (chatId <= 0) {
            throw new ExceptionInInitializerError("chatId <= 0");
        }

        //add the fragments own menu items
        setHasOptionsMenu(true);

        //trying to get chat with chatId from local DB
        try {
            chat = DatabaseManager.INSTANCE.getChatDAO().get(chatId);
            // Assuming that the messages are sorted by id
            latestMessageOnDisplay = new AtomicLong(0);
        } catch (NullPointerException e) {
            // Occurs when new chat has been generated, but id hasn't been returned by the server yet
            chat = null;
            Log.e(this.getClass().getSimpleName(), "get chat from DB failed");
        }
        if (chat == null) {
            Toaster.getInstance().toast(R.string.unable_open_chat, Toast.LENGTH_SHORT);
            activity.finish();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_chat, container, false);

        // Restore saved state if member variables are null
        if (null != savedInstanceState && null == latestMessageOnDisplay) {
            long latestMessageOnDisplayId = savedInstanceState.getLong(RESTORE_LATEST_MESSAGE_ON_DISPLAY);
            latestMessageOnDisplay = new AtomicLong(latestMessageOnDisplayId);
        }
        if (null != savedInstanceState && null == chat) {
            long chatId = savedInstanceState.getLong(RESTORE_CHAT_ID);
            chat = DatabaseManager.INSTANCE.getChatDAO().get(chatId);
            if (null == chat) {
                Log.e(this.getClass().getSimpleName(), "Oh no, looks like this chat has been deleted in the meantime");
                return rootView;
            }
        }
        list = (ListView) rootView.findViewById(R.id.chat_messageList);
        imageCancel = (Button) rootView.findViewById(R.id.button_cancel_image);
        imageCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                imageView.setImageResource(android.R.color.transparent);
                imageCancel.setVisibility(View.GONE);
                imageView.setVisibility(View.GONE);
                editMessage.setVisibility(View.VISIBLE);
                editMessage.requestFocus();
            }
        });

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        LinearLayout layoutTextView = (LinearLayout) rootView.findViewById(R.id.text_view_layout);
        final EditTextWithImage ownEdit = new EditTextWithImage(DatabaseManager.INSTANCE.getContext());
        editMessage = ownEdit.getEditText();
        imageView = ownEdit.getImageView();
        imageView.setVisibility(View.GONE);

        editMessage.setOnTouchListener(new View.OnTouchListener() {<commit_msg>reduced maxHeight and maxWidth of imageView<commit_after>package de.fau.cs.mad.yasme.android.ui.fragments;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
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.ListView;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

import de.fau.cs.mad.yasme.android.EditTextWithImage;
import de.fau.cs.mad.yasme.android.R;
import de.fau.cs.mad.yasme.android.asyncTasks.database.GetNewMessagesForChatTask;
import de.fau.cs.mad.yasme.android.asyncTasks.server.GetMessageTask;
import de.fau.cs.mad.yasme.android.asyncTasks.server.SendMessageTask;
import de.fau.cs.mad.yasme.android.controller.FragmentObservable;
import de.fau.cs.mad.yasme.android.controller.Log;
import de.fau.cs.mad.yasme.android.controller.NotifiableFragment;
import de.fau.cs.mad.yasme.android.controller.ObservableRegistry;
import de.fau.cs.mad.yasme.android.controller.Toaster;
import de.fau.cs.mad.yasme.android.encryption.MessageEncryption;
import de.fau.cs.mad.yasme.android.entities.Chat;
import de.fau.cs.mad.yasme.android.entities.Message;
import de.fau.cs.mad.yasme.android.entities.User;
import de.fau.cs.mad.yasme.android.storage.DatabaseManager;
import de.fau.cs.mad.yasme.android.storage.PictureManager;
import de.fau.cs.mad.yasme.android.ui.AbstractYasmeActivity;
import de.fau.cs.mad.yasme.android.ui.ChatAdapter;
import de.fau.cs.mad.yasme.android.ui.activities.ChatActivity;
import de.fau.cs.mad.yasme.android.ui.activities.ChatSettingsActivity;

/**
 * Created by <NAME> <<EMAIL>> on 21.06.2014.
 */
public class ChatFragment extends Fragment implements NotifiableFragment<List<Message>> {
    public static final String RESTORE_LATEST_MESSAGE_ON_DISPLAY = "LATEST_MESSAGE_ON_DISPLAY";
    public static final String RESTORE_CHAT_ID = "CHAT_ID";
    private ChatAdapter mAdapter;
    private Chat chat;
    private AtomicLong latestMessageOnDisplay;
    private Context mContext;

    //UI references
    private Button imageCancel;
    private ImageView imageView;
    private EditText editMessage;
    private ListView list;

    private static final int RESULT_LOAD_IMAGE = 100;
    private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 200;
    private Uri imageUri;
    private Bitmap bitmap;

    public ChatFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Retrain instance variables!
        // setRetainInstance(true);

        ChatActivity activity = (ChatActivity) getActivity();
        mContext = DatabaseManager.INSTANCE.getContext();

        Intent intent = activity.getIntent();
        long chatId = intent.getLongExtra(AbstractYasmeActivity.CHAT_ID, -1);
        if (chatId <= 0) {
            throw new ExceptionInInitializerError("chatId <= 0");
        }

        //add the fragments own menu items
        setHasOptionsMenu(true);

        //trying to get chat with chatId from local DB
        try {
            chat = DatabaseManager.INSTANCE.getChatDAO().get(chatId);
            // Assuming that the messages are sorted by id
            latestMessageOnDisplay = new AtomicLong(0);
        } catch (NullPointerException e) {
            // Occurs when new chat has been generated, but id hasn't been returned by the server yet
            chat = null;
            Log.e(this.getClass().getSimpleName(), "get chat from DB failed");
        }
        if (chat == null) {
            Toaster.getInstance().toast(R.string.unable_open_chat, Toast.LENGTH_SHORT);
            activity.finish();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_chat, container, false);

        // Restore saved state if member variables are null
        if (null != savedInstanceState && null == latestMessageOnDisplay) {
            long latestMessageOnDisplayId = savedInstanceState.getLong(RESTORE_LATEST_MESSAGE_ON_DISPLAY);
            latestMessageOnDisplay = new AtomicLong(latestMessageOnDisplayId);
        }
        if (null != savedInstanceState && null == chat) {
            long chatId = savedInstanceState.getLong(RESTORE_CHAT_ID);
            chat = DatabaseManager.INSTANCE.getChatDAO().get(chatId);
            if (null == chat) {
                Log.e(this.getClass().getSimpleName(), "Oh no, looks like this chat has been deleted in the meantime");
                return rootView;
            }
        }
        list = (ListView) rootView.findViewById(R.id.chat_messageList);
        imageCancel = (Button) rootView.findViewById(R.id.button_cancel_image);
        imageCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                imageView.setImageResource(android.R.color.transparent);
                imageCancel.setVisibility(View.GONE);
                imageView.setVisibility(View.GONE);
                editMessage.setVisibility(View.VISIBLE);
                editMessage.requestFocus();
            }
        });

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        LinearLayout layoutTextView = (LinearLayout) rootView.findViewById(R.id.text_view_layout);
        final EditTextWithImage ownEdit = new EditTextWithImage(DatabaseManager.INSTANCE.getContext());
        editMessage = ownEdit.getEditText();
        imageView = ownEdit.getImageView();
        imageView.setMaxHeight(256);
        imageView.setMaxWidth(256);
        imageView.setVisibility(View.GONE);

        editMessage.setOnTouchListener(new View.OnTouchListener() {
{'fraction_non_alphanumeric': 0.09648380395475879, 'fraction_numerical': 0.003231514964991921, 'mean_word_length': 3.303973509933775, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1568652', 'n_tokens_mistral': 3824, 'n_tokens_neox': 3656, 'n_words': 737}