blob: f9e63b88083352c573433bb0ed0b1d8c33aea343 [file] [log] [blame]
Alexandre Lisiona8b78722013-12-13 10:18:33 -05001/*
Alexandre Lisionc1024c02014-01-06 11:12:53 -05002 * Copyright (C) 2004-2014 Savoir-Faire Linux Inc.
Alexandre Lisiona8b78722013-12-13 10:18:33 -05003 *
4 * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 * Additional permission under GNU GPL version 3 section 7:
21 *
22 * If you modify this program, or any covered work, by linking or
23 * combining it with the OpenSSL project's OpenSSL library (or a
24 * modified version of that library), containing parts covered by the
25 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
26 * grants you additional permission to convey the resulting work.
27 * Corresponding Source for a non-source form of such a combination
28 * shall include the source code for the parts of OpenSSL used as well
29 * as that of the covered work.
30 */
Alexandre Lision666b3772013-10-28 17:42:48 -040031package org.sflphone.fragments;
32
Alexandre Lisionb97c9512014-01-07 17:14:03 -050033import android.widget.*;
Alexandre Lision666b3772013-10-28 17:42:48 -040034import org.sflphone.R;
35import org.sflphone.adapters.DiscussArrayAdapter;
Alexandre Lision48b49eb2014-02-11 13:37:33 -050036import org.sflphone.model.Conference;
Alexandre Lision666b3772013-10-28 17:42:48 -040037import org.sflphone.model.SipMessage;
38import org.sflphone.service.ISipService;
39
40import android.app.Activity;
Alexandre Lision666b3772013-10-28 17:42:48 -040041import android.content.Intent;
42import android.os.Bundle;
Alexandre Lisiond5686032013-10-29 11:09:21 -040043import android.util.Log;
44import android.view.KeyEvent;
Alexandre Lision666b3772013-10-28 17:42:48 -040045import android.view.LayoutInflater;
46import android.view.View;
Alexandre Lisiond5686032013-10-29 11:09:21 -040047import android.view.View.OnClickListener;
Alexandre Lision666b3772013-10-28 17:42:48 -040048import android.view.ViewGroup;
Alexandre Lisiond5686032013-10-29 11:09:21 -040049import android.view.inputmethod.EditorInfo;
Alexandre Lisiond5686032013-10-29 11:09:21 -040050import android.widget.TextView.OnEditorActionListener;
Alexandre Lision666b3772013-10-28 17:42:48 -040051
Alexandre Lision5f144b82014-02-11 09:59:36 -050052public class IMFragment extends CallableWrapperFragment {
Alexandre Lisionf02190d2013-12-12 17:26:12 -050053 static final String TAG = IMFragment.class.getSimpleName();
Alexandre Lision666b3772013-10-28 17:42:48 -040054
55 private Callbacks mCallbacks = sDummyCallbacks;
56
57 DiscussArrayAdapter mAdapter;
58 ListView list;
59
Alexandre Lisiond5686032013-10-29 11:09:21 -040060 private EditText sendTextField;
61
Alexandre Lision666b3772013-10-28 17:42:48 -040062 @Override
63 public void onCreate(Bundle savedBundle) {
64 super.onCreate(savedBundle);
65
Alexandre Lisiond5686032013-10-29 11:09:21 -040066 mAdapter = new DiscussArrayAdapter(getActivity(), getArguments());
Alexandre Lision666b3772013-10-28 17:42:48 -040067
68 }
69
Alexandre Lision5f144b82014-02-11 09:59:36 -050070 @Override
Alexandre Lision48b49eb2014-02-11 13:37:33 -050071 public void incomingText(String ID, String from, String msg) {
72 if (mCallbacks.getDisplayedConference().getId().contentEquals(ID)) {
73 SipMessage sipMsg = new SipMessage(true, msg);
74 putMessage(sipMsg);
75 }
Alexandre Lision5f144b82014-02-11 09:59:36 -050076 }
77
78
Alexandre Lision666b3772013-10-28 17:42:48 -040079 /**
80 * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity.
81 */
82 private static Callbacks sDummyCallbacks = new Callbacks() {
83
84 @Override
85 public ISipService getService() {
86 return null;
87 }
88
Alexandre Lisiond5686032013-10-29 11:09:21 -040089 @Override
Alexandre Lision48b49eb2014-02-11 13:37:33 -050090 public Conference getDisplayedConference() {
91 return null;
92 }
93
94 @Override
Alexandre Lisiond5686032013-10-29 11:09:21 -040095 public boolean sendIM(SipMessage msg) {
96 return false;
97 }
98
Alexandre Lision666b3772013-10-28 17:42:48 -040099 };
100
101 /**
102 * The Activity calling this fragment has to implement this interface
Alexandre Lision666b3772013-10-28 17:42:48 -0400103 */
104 public interface Callbacks {
105 public ISipService getService();
106
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500107 public Conference getDisplayedConference();
108
Alexandre Lisiond5686032013-10-29 11:09:21 -0400109 public boolean sendIM(SipMessage msg);
Alexandre Lision666b3772013-10-28 17:42:48 -0400110 }
111
112 @Override
113 public void onAttach(Activity activity) {
114 super.onAttach(activity);
115
116 if (!(activity instanceof Callbacks)) {
117 throw new IllegalStateException("Activity must implement fragment's callbacks.");
118 }
119
120 mCallbacks = (Callbacks) activity;
121 }
122
123 @Override
124 public void onDetach() {
125 super.onDetach();
126 mCallbacks = sDummyCallbacks;
127 }
128
Alexandre Lision666b3772013-10-28 17:42:48 -0400129 @Override
130 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
131 ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.frag_imessaging, container, false);
132
133 list = (ListView) rootView.findViewById(R.id.message_list);
134 list.setAdapter(mAdapter);
135
Alexandre Lisiond5686032013-10-29 11:09:21 -0400136 sendTextField = (EditText) rootView.findViewById(R.id.send_im_edittext);
137
138 sendTextField.setOnEditorActionListener(new OnEditorActionListener() {
139
140 @Override
141 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
142
143 if (actionId == EditorInfo.IME_ACTION_SEND) {
Alexandre Lisionb97c9512014-01-07 17:14:03 -0500144 sendMessage();
Alexandre Lisiond5686032013-10-29 11:09:21 -0400145 }
146 return true;
147 }
148 });
149
Alexandre Lisionb97c9512014-01-07 17:14:03 -0500150 rootView.findViewById(R.id.send_im_button).setOnClickListener(new OnClickListener() {
Alexandre Lisiond5686032013-10-29 11:09:21 -0400151
152 @Override
153 public void onClick(View v) {
Alexandre Lisionb97c9512014-01-07 17:14:03 -0500154 sendMessage();
Alexandre Lisiond5686032013-10-29 11:09:21 -0400155 }
156 });
157
Alexandre Lisionb97c9512014-01-07 17:14:03 -0500158
Alexandre Lision666b3772013-10-28 17:42:48 -0400159 return rootView;
160 }
Alexandre Lisiond5686032013-10-29 11:09:21 -0400161
Alexandre Lisionb97c9512014-01-07 17:14:03 -0500162 private void sendMessage() {
163 if (sendTextField.getText().toString().length() > 0) {
164 SipMessage toSend = new SipMessage(false, sendTextField.getText().toString());
Alexandre Lision5f144b82014-02-11 09:59:36 -0500165 if (mCallbacks.sendIM(toSend)) {
Alexandre Lisionb97c9512014-01-07 17:14:03 -0500166 putMessage(toSend);
167 sendTextField.setText("");
168 } else {
169 Toast.makeText(getActivity(), "Error sending message", Toast.LENGTH_SHORT).show();
170 }
171 }
172 }
173
174
Alexandre Lision666b3772013-10-28 17:42:48 -0400175 @Override
176 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Alexandre Lisiond5686032013-10-29 11:09:21 -0400177 super.onActivityResult(requestCode, resultCode, data);
Alexandre Lision666b3772013-10-28 17:42:48 -0400178 }
179
180 public void putMessage(SipMessage msg) {
181 mAdapter.add(msg);
Alexandre Lisiond5686032013-10-29 11:09:21 -0400182 Log.i(TAG, "Messages" + mAdapter.getCount());
Alexandre Lision666b3772013-10-28 17:42:48 -0400183 }
184}