blob: 0c09181846a91006cabd1d5df7b80b5489eb2f23 [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 Lisiona7ab2e32014-02-14 15:33:33 -050071 public void incomingText(Conference updated, String ID, String from, String msg) {
72 mCallbacks.updateDisplayedConference(updated);
73 if(updated.equals(mCallbacks.getDisplayedConference())){
Alexandre Lision48b49eb2014-02-11 13:37:33 -050074 SipMessage sipMsg = new SipMessage(true, msg);
75 putMessage(sipMsg);
76 }
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -050077
Alexandre Lision5f144b82014-02-11 09:59:36 -050078 }
79
80
Alexandre Lision666b3772013-10-28 17:42:48 -040081 /**
82 * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity.
83 */
84 private static Callbacks sDummyCallbacks = new Callbacks() {
85
86 @Override
87 public ISipService getService() {
88 return null;
89 }
90
Alexandre Lisiond5686032013-10-29 11:09:21 -040091 @Override
Alexandre Lision48b49eb2014-02-11 13:37:33 -050092 public Conference getDisplayedConference() {
93 return null;
94 }
95
96 @Override
Alexandre Lisiond5686032013-10-29 11:09:21 -040097 public boolean sendIM(SipMessage msg) {
98 return false;
99 }
100
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500101 @Override
102 public void updateDisplayedConference(Conference c) {
103
104 }
105
Alexandre Lision666b3772013-10-28 17:42:48 -0400106 };
107
108 /**
109 * The Activity calling this fragment has to implement this interface
Alexandre Lision666b3772013-10-28 17:42:48 -0400110 */
111 public interface Callbacks {
112 public ISipService getService();
113
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500114 public Conference getDisplayedConference();
115
Alexandre Lisiond5686032013-10-29 11:09:21 -0400116 public boolean sendIM(SipMessage msg);
Alexandre Lisiona7ab2e32014-02-14 15:33:33 -0500117
118 public void updateDisplayedConference(Conference c);
Alexandre Lision666b3772013-10-28 17:42:48 -0400119 }
120
121 @Override
122 public void onAttach(Activity activity) {
123 super.onAttach(activity);
124
125 if (!(activity instanceof Callbacks)) {
126 throw new IllegalStateException("Activity must implement fragment's callbacks.");
127 }
128
129 mCallbacks = (Callbacks) activity;
130 }
131
132 @Override
133 public void onDetach() {
134 super.onDetach();
135 mCallbacks = sDummyCallbacks;
136 }
137
Alexandre Lision666b3772013-10-28 17:42:48 -0400138 @Override
139 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
140 ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.frag_imessaging, container, false);
141
142 list = (ListView) rootView.findViewById(R.id.message_list);
143 list.setAdapter(mAdapter);
144
Alexandre Lisiond5686032013-10-29 11:09:21 -0400145 sendTextField = (EditText) rootView.findViewById(R.id.send_im_edittext);
146
147 sendTextField.setOnEditorActionListener(new OnEditorActionListener() {
148
149 @Override
150 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
151
152 if (actionId == EditorInfo.IME_ACTION_SEND) {
Alexandre Lisionb97c9512014-01-07 17:14:03 -0500153 sendMessage();
Alexandre Lisiond5686032013-10-29 11:09:21 -0400154 }
155 return true;
156 }
157 });
158
Alexandre Lisionb97c9512014-01-07 17:14:03 -0500159 rootView.findViewById(R.id.send_im_button).setOnClickListener(new OnClickListener() {
Alexandre Lisiond5686032013-10-29 11:09:21 -0400160
161 @Override
162 public void onClick(View v) {
Alexandre Lisionb97c9512014-01-07 17:14:03 -0500163 sendMessage();
Alexandre Lisiond5686032013-10-29 11:09:21 -0400164 }
165 });
166
Alexandre Lisionb97c9512014-01-07 17:14:03 -0500167
Alexandre Lision666b3772013-10-28 17:42:48 -0400168 return rootView;
169 }
Alexandre Lisiond5686032013-10-29 11:09:21 -0400170
Alexandre Lisionb97c9512014-01-07 17:14:03 -0500171 private void sendMessage() {
172 if (sendTextField.getText().toString().length() > 0) {
173 SipMessage toSend = new SipMessage(false, sendTextField.getText().toString());
Alexandre Lision5f144b82014-02-11 09:59:36 -0500174 if (mCallbacks.sendIM(toSend)) {
Alexandre Lisionb97c9512014-01-07 17:14:03 -0500175 putMessage(toSend);
176 sendTextField.setText("");
177 } else {
178 Toast.makeText(getActivity(), "Error sending message", Toast.LENGTH_SHORT).show();
179 }
180 }
181 }
182
183
Alexandre Lision666b3772013-10-28 17:42:48 -0400184 @Override
185 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Alexandre Lisiond5686032013-10-29 11:09:21 -0400186 super.onActivityResult(requestCode, resultCode, data);
Alexandre Lision666b3772013-10-28 17:42:48 -0400187 }
188
189 public void putMessage(SipMessage msg) {
190 mAdapter.add(msg);
Alexandre Lisiond5686032013-10-29 11:09:21 -0400191 Log.i(TAG, "Messages" + mAdapter.getCount());
Alexandre Lision666b3772013-10-28 17:42:48 -0400192 }
193}