blob: 6f6ddb210f8474e63f14d3b10a27fa0cd889dc91 [file] [log] [blame]
Alexandre Lision666b3772013-10-28 17:42:48 -04001package org.sflphone.fragments;
2
Alexandre Lisiond5686032013-10-29 11:09:21 -04003import java.util.ArrayList;
4
Alexandre Lision666b3772013-10-28 17:42:48 -04005import org.sflphone.R;
6import org.sflphone.adapters.DiscussArrayAdapter;
Alexandre Lision666b3772013-10-28 17:42:48 -04007import org.sflphone.model.SipMessage;
8import org.sflphone.service.ISipService;
9
10import android.app.Activity;
11import android.app.Fragment;
12import android.content.Intent;
13import android.os.Bundle;
Alexandre Lisiond5686032013-10-29 11:09:21 -040014import android.util.Log;
15import android.view.KeyEvent;
Alexandre Lision666b3772013-10-28 17:42:48 -040016import android.view.LayoutInflater;
17import android.view.View;
Alexandre Lisiond5686032013-10-29 11:09:21 -040018import android.view.View.OnClickListener;
Alexandre Lision666b3772013-10-28 17:42:48 -040019import android.view.ViewGroup;
Alexandre Lisiond5686032013-10-29 11:09:21 -040020import android.view.inputmethod.EditorInfo;
21import android.widget.Button;
22import android.widget.EditText;
Alexandre Lision666b3772013-10-28 17:42:48 -040023import android.widget.ListView;
Alexandre Lisiond5686032013-10-29 11:09:21 -040024import android.widget.TextView;
25import android.widget.TextView.OnEditorActionListener;
Alexandre Lision666b3772013-10-28 17:42:48 -040026
27public 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 Lisiond5686032013-10-29 11:09:21 -040035 private EditText sendTextField;
36
Alexandre Lision666b3772013-10-28 17:42:48 -040037 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 Lisiond5686032013-10-29 11:09:21 -040044 mAdapter = new DiscussArrayAdapter(getActivity(), getArguments());
Alexandre Lision666b3772013-10-28 17:42:48 -040045
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 Lisiond5686032013-10-29 11:09:21 -040058 @Override
59 public boolean sendIM(SipMessage msg) {
60 return false;
61 }
62
Alexandre Lision666b3772013-10-28 17:42:48 -040063 };
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 Lisiond5686032013-10-29 11:09:21 -040072 public boolean sendIM(SipMessage msg);
Alexandre Lision666b3772013-10-28 17:42:48 -040073 }
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 Lision666b3772013-10-28 17:42:48 -040092 @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 Lisiond5686032013-10-29 11:09:21 -040099 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 Lision666b3772013-10-28 17:42:48 -0400131 return rootView;
132 }
Alexandre Lisiond5686032013-10-29 11:09:21 -0400133
Alexandre Lision666b3772013-10-28 17:42:48 -0400134 @Override
135 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Alexandre Lisiond5686032013-10-29 11:09:21 -0400136 super.onActivityResult(requestCode, resultCode, data);
Alexandre Lision666b3772013-10-28 17:42:48 -0400137 }
138
139 public void putMessage(SipMessage msg) {
140 mAdapter.add(msg);
Alexandre Lisiond5686032013-10-29 11:09:21 -0400141 Log.i(TAG, "Messages" + mAdapter.getCount());
Alexandre Lision666b3772013-10-28 17:42:48 -0400142 }
143}