Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 1 | package org.sflphone.fragments; |
| 2 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 3 | import java.util.ArrayList; |
| 4 | |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 5 | import org.sflphone.R; |
| 6 | import org.sflphone.adapters.DiscussArrayAdapter; |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 7 | import org.sflphone.model.SipMessage; |
| 8 | import org.sflphone.service.ISipService; |
| 9 | |
| 10 | import android.app.Activity; |
| 11 | import android.app.Fragment; |
| 12 | import android.content.Intent; |
| 13 | import android.os.Bundle; |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 14 | import android.util.Log; |
| 15 | import android.view.KeyEvent; |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 16 | import android.view.LayoutInflater; |
| 17 | import android.view.View; |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 18 | import android.view.View.OnClickListener; |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 19 | import android.view.ViewGroup; |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 20 | import android.view.inputmethod.EditorInfo; |
| 21 | import android.widget.Button; |
| 22 | import android.widget.EditText; |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 23 | import android.widget.ListView; |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 24 | import android.widget.TextView; |
| 25 | import android.widget.TextView.OnEditorActionListener; |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 26 | |
| 27 | public class IMFragment extends Fragment { |
| 28 | static final String TAG = CallListFragment.class.getSimpleName(); |
| 29 | |
| 30 | private Callbacks mCallbacks = sDummyCallbacks; |
| 31 | |
| 32 | DiscussArrayAdapter mAdapter; |
| 33 | ListView list; |
| 34 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 35 | private EditText sendTextField; |
| 36 | |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 37 | public static final int REQUEST_TRANSFER = 10; |
| 38 | public static final int REQUEST_CONF = 20; |
| 39 | |
| 40 | @Override |
| 41 | public void onCreate(Bundle savedBundle) { |
| 42 | super.onCreate(savedBundle); |
| 43 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 44 | mAdapter = new DiscussArrayAdapter(getActivity(), getArguments()); |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 45 | |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity. |
| 50 | */ |
| 51 | private static Callbacks sDummyCallbacks = new Callbacks() { |
| 52 | |
| 53 | @Override |
| 54 | public ISipService getService() { |
| 55 | return null; |
| 56 | } |
| 57 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 58 | @Override |
| 59 | public boolean sendIM(SipMessage msg) { |
| 60 | return false; |
| 61 | } |
| 62 | |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | /** |
| 66 | * The Activity calling this fragment has to implement this interface |
| 67 | * |
| 68 | */ |
| 69 | public interface Callbacks { |
| 70 | public ISipService getService(); |
| 71 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 72 | public boolean sendIM(SipMessage msg); |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public void onAttach(Activity activity) { |
| 77 | super.onAttach(activity); |
| 78 | |
| 79 | if (!(activity instanceof Callbacks)) { |
| 80 | throw new IllegalStateException("Activity must implement fragment's callbacks."); |
| 81 | } |
| 82 | |
| 83 | mCallbacks = (Callbacks) activity; |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public void onDetach() { |
| 88 | super.onDetach(); |
| 89 | mCallbacks = sDummyCallbacks; |
| 90 | } |
| 91 | |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 92 | @Override |
| 93 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 94 | ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.frag_imessaging, container, false); |
| 95 | |
| 96 | list = (ListView) rootView.findViewById(R.id.message_list); |
| 97 | list.setAdapter(mAdapter); |
| 98 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 99 | sendTextField = (EditText) rootView.findViewById(R.id.send_im_edittext); |
| 100 | |
| 101 | sendTextField.setOnEditorActionListener(new OnEditorActionListener() { |
| 102 | |
| 103 | @Override |
| 104 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
| 105 | |
| 106 | if (actionId == EditorInfo.IME_ACTION_SEND) { |
| 107 | if (sendTextField.getText().toString().length() > 0) { |
| 108 | SipMessage toSend = new SipMessage(false, sendTextField.getText().toString()); |
| 109 | putMessage(toSend); |
| 110 | sendTextField.setText(""); |
| 111 | mCallbacks.sendIM(toSend); |
| 112 | } |
| 113 | } |
| 114 | return true; |
| 115 | } |
| 116 | }); |
| 117 | |
| 118 | ((Button) rootView.findViewById(R.id.send_im_button)).setOnClickListener(new OnClickListener() { |
| 119 | |
| 120 | @Override |
| 121 | public void onClick(View v) { |
| 122 | if (sendTextField.getText().toString().length() > 0) { |
| 123 | SipMessage toSend = new SipMessage(false, sendTextField.getText().toString()); |
| 124 | putMessage(toSend); |
| 125 | sendTextField.setText(""); |
| 126 | mCallbacks.sendIM(toSend); |
| 127 | } |
| 128 | } |
| 129 | }); |
| 130 | |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 131 | return rootView; |
| 132 | } |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 133 | |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 134 | @Override |
| 135 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 136 | super.onActivityResult(requestCode, resultCode, data); |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | public void putMessage(SipMessage msg) { |
| 140 | mAdapter.add(msg); |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 141 | Log.i(TAG, "Messages" + mAdapter.getCount()); |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 142 | } |
| 143 | } |