Alexandre Lision | a8b7872 | 2013-12-13 10:18:33 -0500 | [diff] [blame] | 1 | /* |
Alexandre Lision | c1024c0 | 2014-01-06 11:12:53 -0500 | [diff] [blame] | 2 | * Copyright (C) 2004-2014 Savoir-Faire Linux Inc. |
Alexandre Lision | a8b7872 | 2013-12-13 10:18:33 -0500 | [diff] [blame] | 3 | * |
| 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 Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 31 | package org.sflphone.fragments; |
| 32 | |
Alexandre Lision | b97c951 | 2014-01-07 17:14:03 -0500 | [diff] [blame] | 33 | import android.widget.*; |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 34 | import org.sflphone.R; |
| 35 | import org.sflphone.adapters.DiscussArrayAdapter; |
Alexandre Lision | 48b49eb | 2014-02-11 13:37:33 -0500 | [diff] [blame] | 36 | import org.sflphone.model.Conference; |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 37 | import org.sflphone.model.SipMessage; |
| 38 | import org.sflphone.service.ISipService; |
| 39 | |
| 40 | import android.app.Activity; |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 41 | import android.content.Intent; |
| 42 | import android.os.Bundle; |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 43 | import android.util.Log; |
| 44 | import android.view.KeyEvent; |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 45 | import android.view.LayoutInflater; |
| 46 | import android.view.View; |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 47 | import android.view.View.OnClickListener; |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 48 | import android.view.ViewGroup; |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 49 | import android.view.inputmethod.EditorInfo; |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 50 | import android.widget.TextView.OnEditorActionListener; |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 51 | |
Alexandre Lision | 5f144b8 | 2014-02-11 09:59:36 -0500 | [diff] [blame] | 52 | public class IMFragment extends CallableWrapperFragment { |
Alexandre Lision | f02190d | 2013-12-12 17:26:12 -0500 | [diff] [blame] | 53 | static final String TAG = IMFragment.class.getSimpleName(); |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 54 | |
| 55 | private Callbacks mCallbacks = sDummyCallbacks; |
| 56 | |
| 57 | DiscussArrayAdapter mAdapter; |
| 58 | ListView list; |
| 59 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 60 | private EditText sendTextField; |
| 61 | |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 62 | @Override |
| 63 | public void onCreate(Bundle savedBundle) { |
| 64 | super.onCreate(savedBundle); |
| 65 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 66 | mAdapter = new DiscussArrayAdapter(getActivity(), getArguments()); |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 67 | |
| 68 | } |
| 69 | |
Alexandre Lision | 5f144b8 | 2014-02-11 09:59:36 -0500 | [diff] [blame] | 70 | @Override |
Alexandre Lision | a7ab2e3 | 2014-02-14 15:33:33 -0500 | [diff] [blame] | 71 | public void incomingText(Conference updated, String ID, String from, String msg) { |
| 72 | mCallbacks.updateDisplayedConference(updated); |
| 73 | if(updated.equals(mCallbacks.getDisplayedConference())){ |
Alexandre Lision | 48b49eb | 2014-02-11 13:37:33 -0500 | [diff] [blame] | 74 | SipMessage sipMsg = new SipMessage(true, msg); |
| 75 | putMessage(sipMsg); |
| 76 | } |
Alexandre Lision | a7ab2e3 | 2014-02-14 15:33:33 -0500 | [diff] [blame] | 77 | |
Alexandre Lision | 5f144b8 | 2014-02-11 09:59:36 -0500 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 81 | /** |
| 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 Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 91 | @Override |
Alexandre Lision | 48b49eb | 2014-02-11 13:37:33 -0500 | [diff] [blame] | 92 | public Conference getDisplayedConference() { |
| 93 | return null; |
| 94 | } |
| 95 | |
| 96 | @Override |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 97 | public boolean sendIM(SipMessage msg) { |
| 98 | return false; |
| 99 | } |
| 100 | |
Alexandre Lision | a7ab2e3 | 2014-02-14 15:33:33 -0500 | [diff] [blame] | 101 | @Override |
| 102 | public void updateDisplayedConference(Conference c) { |
| 103 | |
| 104 | } |
| 105 | |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | /** |
| 109 | * The Activity calling this fragment has to implement this interface |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 110 | */ |
| 111 | public interface Callbacks { |
| 112 | public ISipService getService(); |
| 113 | |
Alexandre Lision | 48b49eb | 2014-02-11 13:37:33 -0500 | [diff] [blame] | 114 | public Conference getDisplayedConference(); |
| 115 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 116 | public boolean sendIM(SipMessage msg); |
Alexandre Lision | a7ab2e3 | 2014-02-14 15:33:33 -0500 | [diff] [blame] | 117 | |
| 118 | public void updateDisplayedConference(Conference c); |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 119 | } |
| 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 Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 138 | @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 Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 145 | 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 Lision | b97c951 | 2014-01-07 17:14:03 -0500 | [diff] [blame] | 153 | sendMessage(); |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 154 | } |
| 155 | return true; |
| 156 | } |
| 157 | }); |
| 158 | |
Alexandre Lision | b97c951 | 2014-01-07 17:14:03 -0500 | [diff] [blame] | 159 | rootView.findViewById(R.id.send_im_button).setOnClickListener(new OnClickListener() { |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 160 | |
| 161 | @Override |
| 162 | public void onClick(View v) { |
Alexandre Lision | b97c951 | 2014-01-07 17:14:03 -0500 | [diff] [blame] | 163 | sendMessage(); |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 164 | } |
| 165 | }); |
| 166 | |
Alexandre Lision | b97c951 | 2014-01-07 17:14:03 -0500 | [diff] [blame] | 167 | |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 168 | return rootView; |
| 169 | } |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 170 | |
Alexandre Lision | b97c951 | 2014-01-07 17:14:03 -0500 | [diff] [blame] | 171 | private void sendMessage() { |
| 172 | if (sendTextField.getText().toString().length() > 0) { |
| 173 | SipMessage toSend = new SipMessage(false, sendTextField.getText().toString()); |
Alexandre Lision | 5f144b8 | 2014-02-11 09:59:36 -0500 | [diff] [blame] | 174 | if (mCallbacks.sendIM(toSend)) { |
Alexandre Lision | b97c951 | 2014-01-07 17:14:03 -0500 | [diff] [blame] | 175 | putMessage(toSend); |
| 176 | sendTextField.setText(""); |
| 177 | } else { |
| 178 | Toast.makeText(getActivity(), "Error sending message", Toast.LENGTH_SHORT).show(); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 184 | @Override |
| 185 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 186 | super.onActivityResult(requestCode, resultCode, data); |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | public void putMessage(SipMessage msg) { |
| 190 | mAdapter.add(msg); |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 191 | Log.i(TAG, "Messages" + mAdapter.getCount()); |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 192 | } |
| 193 | } |