blob: cc57d3f09eabca95a7e0600b77abbf35ad4eda50 [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 Lision666b3772013-10-28 17:42:48 -040036import org.sflphone.model.SipMessage;
37import org.sflphone.service.ISipService;
38
39import android.app.Activity;
40import android.app.Fragment;
41import 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
52public class IMFragment extends Fragment {
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
70 /**
71 * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity.
72 */
73 private static Callbacks sDummyCallbacks = new Callbacks() {
74
75 @Override
76 public ISipService getService() {
77 return null;
78 }
79
Alexandre Lisiond5686032013-10-29 11:09:21 -040080 @Override
81 public boolean sendIM(SipMessage msg) {
82 return false;
83 }
84
Alexandre Lision666b3772013-10-28 17:42:48 -040085 };
86
87 /**
88 * The Activity calling this fragment has to implement this interface
89 *
90 */
91 public interface Callbacks {
92 public ISipService getService();
93
Alexandre Lisiond5686032013-10-29 11:09:21 -040094 public boolean sendIM(SipMessage msg);
Alexandre Lision666b3772013-10-28 17:42:48 -040095 }
96
97 @Override
98 public void onAttach(Activity activity) {
99 super.onAttach(activity);
100
101 if (!(activity instanceof Callbacks)) {
102 throw new IllegalStateException("Activity must implement fragment's callbacks.");
103 }
104
105 mCallbacks = (Callbacks) activity;
106 }
107
108 @Override
109 public void onDetach() {
110 super.onDetach();
111 mCallbacks = sDummyCallbacks;
112 }
113
Alexandre Lision666b3772013-10-28 17:42:48 -0400114 @Override
115 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
116 ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.frag_imessaging, container, false);
117
118 list = (ListView) rootView.findViewById(R.id.message_list);
119 list.setAdapter(mAdapter);
120
Alexandre Lisiond5686032013-10-29 11:09:21 -0400121 sendTextField = (EditText) rootView.findViewById(R.id.send_im_edittext);
122
123 sendTextField.setOnEditorActionListener(new OnEditorActionListener() {
124
125 @Override
126 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
127
128 if (actionId == EditorInfo.IME_ACTION_SEND) {
Alexandre Lisionb97c9512014-01-07 17:14:03 -0500129 sendMessage();
Alexandre Lisiond5686032013-10-29 11:09:21 -0400130 }
131 return true;
132 }
133 });
134
Alexandre Lisionb97c9512014-01-07 17:14:03 -0500135 rootView.findViewById(R.id.send_im_button).setOnClickListener(new OnClickListener() {
Alexandre Lisiond5686032013-10-29 11:09:21 -0400136
137 @Override
138 public void onClick(View v) {
Alexandre Lisionb97c9512014-01-07 17:14:03 -0500139 sendMessage();
Alexandre Lisiond5686032013-10-29 11:09:21 -0400140 }
141 });
142
Alexandre Lisionb97c9512014-01-07 17:14:03 -0500143
144
Alexandre Lision666b3772013-10-28 17:42:48 -0400145 return rootView;
146 }
Alexandre Lisiond5686032013-10-29 11:09:21 -0400147
Alexandre Lisionb97c9512014-01-07 17:14:03 -0500148 private void sendMessage() {
149 if (sendTextField.getText().toString().length() > 0) {
150 SipMessage toSend = new SipMessage(false, sendTextField.getText().toString());
151 if(mCallbacks.sendIM(toSend)){
152 putMessage(toSend);
153 sendTextField.setText("");
154 } else {
155 Toast.makeText(getActivity(), "Error sending message", Toast.LENGTH_SHORT).show();
156 }
157 }
158 }
159
160
161
Alexandre Lision666b3772013-10-28 17:42:48 -0400162 @Override
163 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Alexandre Lisiond5686032013-10-29 11:09:21 -0400164 super.onActivityResult(requestCode, resultCode, data);
Alexandre Lision666b3772013-10-28 17:42:48 -0400165 }
166
167 public void putMessage(SipMessage msg) {
168 mAdapter.add(msg);
Alexandre Lisiond5686032013-10-29 11:09:21 -0400169 Log.i(TAG, "Messages" + mAdapter.getCount());
Alexandre Lision666b3772013-10-28 17:42:48 -0400170 }
171}