<commit_before>package com.romainpiel.lib.ui.fragment;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.romainpiel.lib.api.ApiManager;
import com.romainpiel.lib.bus.BusManager;
import com.romainpiel.lib.bus.ChatEvent;
import com.romainpiel.lib.bus.MuteEvent;
import com.romainpiel.lib.helper.PreviewHelper;
import com.romainpiel.lib.ui.adapter.ChatAdapter;
import com.romainpiel.lib.ui.listener.OnMenuClickListener;
import com.romainpiel.lib.ui.view.CameraPreview;
import com.romainpiel.lib.utils.UIUtils;
import com.romainpiel.meatspace.R;
import com.romainpiel.meatspace.service.ChatService;
import com.romainpiel.model.Chat;
import com.romainpiel.model.ChatList;
import com.romainpiel.model.Device;
import com.squareup.otto.Subscribe;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Meatspace
 * User: romainpiel
 * Date: 01/11/2013
 * Time: 16:55
 */
public class ChatFragment extends Fragment implements PreviewHelper.OnCaptureListener, CameraPreview.PreviewReadyCallback {

    private static final String STATE_LISTVIEW = "state_listview";
    private static final String POSITION_LIST = "position_list";
    private static final String POSITION_ITEM = "position_item";

    @InjectView(R.id.fragment_chat_list) ListView listView;
    @InjectView(R.id.fragment_chat_camera_preview) CameraPreview cameraPreview;
    @InjectView(R.id.fragment_chat_input) EditText input;
    @InjectView(R.id.fragment_chat_send) ImageButton sendBtn;
    @InjectView(R.id.fragment_chat_progress_bar) ProgressBar progressBar;
    @InjectView(R.id.fragment_chat_char_count) TextView charCount;

    private ProgressDialog progressDialog;
    private AlertDialog errorDialog;
    private ChatAdapter adapter;
    private PreviewHelper previewHelper;
    private Handler uiHandler;
    private Device device;
    private Parcelable listViewState;
    private int maxCharCount;
    private int listPosition, itemPosition;

    public ChatFragment() {
        this.uiHandler = new Handler();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_chat, null);
        ButterKnife.inject(this, view);

        maxCharCount = getResources().getInteger(R.integer.input_max_char_count);

        previewHelper = new PreviewHelper(uiHandler);
        previewHelper.setOnCaptureListener(this);
        cameraPreview.setPreviewCallback(previewHelper);
        cameraPreview.setOnPreviewReady(this);
        input.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                invalidateMaxCharCount();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        invalidateMaxCharCount();

        return view;
    }

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

        if (savedInstanceState != null) {
            listViewState = savedInstanceState.getParcelable(STATE_LISTVIEW);
            listPosition = savedInstanceState.getInt(POSITION_LIST);
            itemPosition = savedInstanceState.getInt(POSITION_ITEM);
        }

        if (adapter == null) {
            adapter = new ChatAdapter(getActivity());
            adapter.setOnMuteClickListener(new OnMenuClickListener<Chat>() {
                @Override
                public void onMenuClick(Chat item) {
                    BusManager.get().getChatBus().post(new MuteEvent(true, item.getValue().getFingerprint()));
                }
            });
        }

        if (device == null) {
            device = new Device(getActivity());
        }

        listView.setAdapter(adapter);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        // Save list state
        Parcelable state = listView.onSaveInstanceState();
        outState.putParcelable(STATE_LISTVIEW, state);

        // Save position of first visible item
        listPosition = listView.getFirstVisiblePosition();
        outState.putInt(POSITION_LIST, listPosition);

        // Save scroll position of item
        View itemView = listView.getChildAt(0);
        itemPosition = itemView == null ? 0 : itemView.getTop();
        outState.putInt(POSITION_ITEM, itemPosition);
    }

    @Override
    public void onPreviewReady() {
        previewHelper.setAngle(cameraPreview.getAngle());
        previewHelper.setFrontCamera(cameraPreview.isFrontCamera());
    }

    @Override
    public void onPause() {
        super.onPause();
        cameraPreview.stopPreview();
        BusManager.get().getChatBus().unregister(this);
        previewHelper.cancelCapture();
    }

    @Override
    public void onResume() {
        super.onResume();
        cameraPreview.startPreview();
        BusManager.get().getChatBus().register(this);
    }

    @Subscribe
    public void onMessage(ChatEvent event) {
        notifyDatasetChanged(event.getChatList(), event.isFromProducer());

        switch (event.getIoState()) {
            case IDLE:
                showProgressDialog();
                break;
            case CONNECTING:
                showProgressDialog();
                break;
            case CONNECTED:
                dismissProgressDialog();
                break;
            case DISCONNECTED:
                cancelProgressDialog(<commit_msg>setup camera listeners in onResume instead of in onCreateView #19<commit_after>package com.romainpiel.lib.ui.fragment;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.romainpiel.lib.api.ApiManager;
import com.romainpiel.lib.bus.BusManager;
import com.romainpiel.lib.bus.ChatEvent;
import com.romainpiel.lib.bus.MuteEvent;
import com.romainpiel.lib.helper.PreviewHelper;
import com.romainpiel.lib.ui.adapter.ChatAdapter;
import com.romainpiel.lib.ui.listener.OnMenuClickListener;
import com.romainpiel.lib.ui.view.CameraPreview;
import com.romainpiel.lib.utils.UIUtils;
import com.romainpiel.meatspace.R;
import com.romainpiel.meatspace.service.ChatService;
import com.romainpiel.model.Chat;
import com.romainpiel.model.ChatList;
import com.romainpiel.model.Device;
import com.squareup.otto.Subscribe;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Meatspace
 * User: romainpiel
 * Date: 01/11/2013
 * Time: 16:55
 */
public class ChatFragment extends Fragment implements PreviewHelper.OnCaptureListener, CameraPreview.PreviewReadyCallback {

    private static final String STATE_LISTVIEW = "state_listview";
    private static final String POSITION_LIST = "position_list";
    private static final String POSITION_ITEM = "position_item";

    @InjectView(R.id.fragment_chat_list) ListView listView;
    @InjectView(R.id.fragment_chat_camera_preview) CameraPreview cameraPreview;
    @InjectView(R.id.fragment_chat_input) EditText input;
    @InjectView(R.id.fragment_chat_send) ImageButton sendBtn;
    @InjectView(R.id.fragment_chat_progress_bar) ProgressBar progressBar;
    @InjectView(R.id.fragment_chat_char_count) TextView charCount;

    private ProgressDialog progressDialog;
    private AlertDialog errorDialog;
    private ChatAdapter adapter;
    private PreviewHelper previewHelper;
    private Handler uiHandler;
    private Device device;
    private Parcelable listViewState;
    private int maxCharCount;
    private int listPosition, itemPosition;

    public ChatFragment() {
        this.uiHandler = new Handler();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_chat, null);
        ButterKnife.inject(this, view);

        maxCharCount = getResources().getInteger(R.integer.input_max_char_count);

        previewHelper = new PreviewHelper(uiHandler);
        previewHelper.setOnCaptureListener(this);
        input.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                invalidateMaxCharCount();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        invalidateMaxCharCount();

        return view;
    }

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

        if (savedInstanceState != null) {
            listViewState = savedInstanceState.getParcelable(STATE_LISTVIEW);
            listPosition = savedInstanceState.getInt(POSITION_LIST);
            itemPosition = savedInstanceState.getInt(POSITION_ITEM);
        }

        if (adapter == null) {
            adapter = new ChatAdapter(getActivity());
            adapter.setOnMuteClickListener(new OnMenuClickListener<Chat>() {
                @Override
                public void onMenuClick(Chat item) {
                    BusManager.get().getChatBus().post(new MuteEvent(true, item.getValue().getFingerprint()));
                }
            });
        }

        if (device == null) {
            device = new Device(getActivity());
        }

        listView.setAdapter(adapter);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        // Save list state
        Parcelable state = listView.onSaveInstanceState();
        outState.putParcelable(STATE_LISTVIEW, state);

        // Save position of first visible item
        listPosition = listView.getFirstVisiblePosition();
        outState.putInt(POSITION_LIST, listPosition);

        // Save scroll position of item
        View itemView = listView.getChildAt(0);
        itemPosition = itemView == null ? 0 : itemView.getTop();
        outState.putInt(POSITION_ITEM, itemPosition);
    }

    @Override
    public void onPreviewReady() {
        previewHelper.setAngle(cameraPreview.getAngle());
        previewHelper.setFrontCamera(cameraPreview.isFrontCamera());
    }

    @Override
    public void onPause() {
        super.onPause();
        cameraPreview.stopPreview();
        BusManager.get().getChatBus().unregister(this);
        previewHelper.cancelCapture();
    }

    @Override
    public void onResume() {
        super.onResume();
        cameraPreview.startPreview();
        cameraPreview.setPreviewCallback(previewHelper);
        cameraPreview.setOnPreviewReady(this);
        BusManager.get().getChatBus().register(this);
    }

    @Subscribe
    public void onMessage(ChatEvent event) {
        notifyDatasetChanged(event.getChatList(), event.isFromProducer());

        switch (event.getIoState()) {
            case IDLE:
                showProgressDialog();
                break;
            case CONNECTING:
                showProgressDialog();
                break;
            case CONNECTED:
                dismissProgressDialog();
                break;
            case DISCONNECTED:
                cancelProgressDialog(
{'fraction_non_alphanumeric': 0.08944954128440367, 'fraction_numerical': 0.002530844669408415, 'mean_word_length': 3.071152607855763, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3317614', 'n_tokens_mistral': 3538, 'n_tokens_neox': 3402, 'n_words': 684}