alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 1 | /* |
Alexandre Lision | c1024c0 | 2014-01-06 11:12:53 -0500 | [diff] [blame] | 2 | * Copyright (C) 2004-2014 Savoir-Faire Linux Inc. |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [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 | */ |
| 31 | |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 32 | package org.sflphone.fragments; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 33 | |
Alexandre Lision | 31f46fc | 2013-09-26 11:19:54 -0400 | [diff] [blame] | 34 | import java.util.Locale; |
| 35 | |
Alexandre Lision | be54c34 | 2014-01-21 17:10:33 -0500 | [diff] [blame] | 36 | import android.support.v4.app.Fragment; |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 37 | import org.sflphone.R; |
| 38 | import org.sflphone.service.ISipService; |
| 39 | import org.sflphone.views.ClearableEditText; |
| 40 | |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 41 | import android.app.Activity; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 42 | import android.content.Context; |
| 43 | import android.os.Bundle; |
Alexandre Lision | 64dc8c0 | 2013-09-25 15:32:25 -0400 | [diff] [blame] | 44 | import android.os.RemoteException; |
Alexandre Lision | 31f46fc | 2013-09-26 11:19:54 -0400 | [diff] [blame] | 45 | import android.text.Editable; |
| 46 | import android.text.TextWatcher; |
Alexandre Lision | 7f0bba5 | 2013-10-16 14:43:11 -0400 | [diff] [blame] | 47 | import android.view.KeyEvent; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 48 | import android.view.LayoutInflater; |
Alexandre Lision | 6e8931e | 2013-09-19 16:49:34 -0400 | [diff] [blame] | 49 | import android.view.MotionEvent; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 50 | import android.view.View; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 51 | import android.view.View.OnClickListener; |
Alexandre Lision | 6e8931e | 2013-09-19 16:49:34 -0400 | [diff] [blame] | 52 | import android.view.View.OnTouchListener; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 53 | import android.view.ViewGroup; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 54 | import android.view.inputmethod.EditorInfo; |
| 55 | import android.view.inputmethod.InputMethodManager; |
Alexandre Lision | 30e35d1 | 2013-11-26 16:18:19 -0500 | [diff] [blame] | 56 | import android.widget.Button; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 57 | import android.widget.ImageButton; |
Alexandre Lision | 7f0bba5 | 2013-10-16 14:43:11 -0400 | [diff] [blame] | 58 | import android.widget.TextView; |
| 59 | import android.widget.TextView.OnEditorActionListener; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 60 | |
Alexandre Lision | 6e8931e | 2013-09-19 16:49:34 -0400 | [diff] [blame] | 61 | public class DialingFragment extends Fragment implements OnTouchListener { |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 62 | |
Alexandre Lision | f30ff85 | 2013-12-09 17:08:40 -0500 | [diff] [blame] | 63 | @SuppressWarnings("unused") |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 64 | private static final String TAG = DialingFragment.class.getSimpleName(); |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 65 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 66 | ClearableEditText textField; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 67 | private Callbacks mCallbacks = sDummyCallbacks; |
| 68 | |
| 69 | /** |
| 70 | * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity. |
| 71 | */ |
| 72 | private static Callbacks sDummyCallbacks = new Callbacks() { |
| 73 | @Override |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 74 | public void onCallDialed(String to) { |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public ISipService getService() { |
| 79 | // TODO Auto-generated method stub |
| 80 | return null; |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | /** |
| 85 | * The Activity calling this fragment has to implement this interface |
| 86 | * |
| 87 | */ |
| 88 | public interface Callbacks { |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 89 | void onCallDialed(String account); |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 90 | |
| 91 | public ISipService getService(); |
| 92 | |
| 93 | } |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 94 | |
| 95 | @Override |
| 96 | public void onAttach(Activity activity) { |
| 97 | super.onAttach(activity); |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 98 | |
| 99 | if (!(activity instanceof Callbacks)) { |
| 100 | throw new IllegalStateException("Activity must implement fragment's callbacks."); |
| 101 | } |
| 102 | |
| 103 | mCallbacks = (Callbacks) activity; |
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 104 | |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void onDetach() { |
| 109 | super.onDetach(); |
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 110 | mCallbacks = sDummyCallbacks; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void onCreate(Bundle savedInstanceState) { |
| 115 | super.onCreate(savedInstanceState); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { |
| 120 | View inflatedView = inflater.inflate(R.layout.frag_dialing, parent, false); |
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 121 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 122 | textField = (ClearableEditText) inflatedView.findViewById(R.id.textField); |
| 123 | ((ImageButton) inflatedView.findViewById(R.id.buttonCall)).setOnClickListener(new OnClickListener() { |
| 124 | @Override |
| 125 | public void onClick(View v) { |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 126 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 127 | String to = textField.getText().toString(); |
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 128 | if (to.contentEquals("")) { |
Alexandre Lision | c2bd7b6 | 2013-09-30 10:45:00 -0400 | [diff] [blame] | 129 | textField.setError(getString(R.string.dial_error_no_number_dialed)); |
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 130 | } else { |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 131 | mCallbacks.onCallDialed(to); |
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 132 | } |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 133 | } |
| 134 | }); |
| 135 | |
Alexandre Lision | 6e8931e | 2013-09-19 16:49:34 -0400 | [diff] [blame] | 136 | inflatedView.setOnTouchListener(this); |
| 137 | |
Alexandre Lision | 30e35d1 | 2013-11-26 16:18:19 -0500 | [diff] [blame] | 138 | ((Button) inflatedView.findViewById(R.id.alphabetic_keyboard)).setOnClickListener(new OnClickListener() { |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 139 | |
| 140 | @Override |
| 141 | public void onClick(View v) { |
| 142 | textField.setInputType(EditorInfo.TYPE_TEXT_FLAG_NO_SUGGESTIONS); |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 143 | InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 144 | lManager.showSoftInput(textField.getEdit_text(), 0); |
| 145 | } |
| 146 | }); |
| 147 | |
Alexandre Lision | 30e35d1 | 2013-11-26 16:18:19 -0500 | [diff] [blame] | 148 | ((Button) inflatedView.findViewById(R.id.numeric_keyboard)).setOnClickListener(new OnClickListener() { |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 149 | |
| 150 | @Override |
| 151 | public void onClick(View v) { |
Alexandre Lision | 6e8931e | 2013-09-19 16:49:34 -0400 | [diff] [blame] | 152 | textField.setInputType(EditorInfo.TYPE_CLASS_PHONE); |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 153 | InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 154 | lManager.showSoftInput(textField.getEdit_text(), 0); |
| 155 | } |
| 156 | }); |
Alexandre Lision | 7f0bba5 | 2013-10-16 14:43:11 -0400 | [diff] [blame] | 157 | |
| 158 | textField.setOnEditorActionListener(new OnEditorActionListener() { |
| 159 | |
| 160 | @Override |
| 161 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
| 162 | String to = textField.getText().toString(); |
| 163 | if (to.contentEquals("")) { |
| 164 | textField.setError(getString(R.string.dial_error_no_number_dialed)); |
| 165 | } else { |
| 166 | mCallbacks.onCallDialed(to); |
| 167 | } |
| 168 | return true; |
| 169 | } |
| 170 | }); |
alision | 8570418 | 2013-05-29 15:23:03 -0400 | [diff] [blame] | 171 | return inflatedView; |
| 172 | } |
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 173 | |
alision | 8570418 | 2013-05-29 15:23:03 -0400 | [diff] [blame] | 174 | @Override |
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 175 | public void onResume() { |
alision | 8570418 | 2013-05-29 15:23:03 -0400 | [diff] [blame] | 176 | super.onResume(); |
Alexandre Lision | 31f46fc | 2013-09-26 11:19:54 -0400 | [diff] [blame] | 177 | textField.setTextWatcher(dtmfKeyListener); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | @Override |
Alexandre Lision | 64dc8c0 | 2013-09-25 15:32:25 -0400 | [diff] [blame] | 181 | public void onPause() { |
| 182 | super.onPause(); |
Alexandre Lision | 31f46fc | 2013-09-26 11:19:54 -0400 | [diff] [blame] | 183 | textField.unsetTextWatcher(); |
Alexandre Lision | 64dc8c0 | 2013-09-25 15:32:25 -0400 | [diff] [blame] | 184 | } |
| 185 | |
Alexandre Lision | 31f46fc | 2013-09-26 11:19:54 -0400 | [diff] [blame] | 186 | TextWatcher dtmfKeyListener = new TextWatcher() { |
Alexandre Lision | 64dc8c0 | 2013-09-25 15:32:25 -0400 | [diff] [blame] | 187 | |
| 188 | @Override |
Alexandre Lision | 31f46fc | 2013-09-26 11:19:54 -0400 | [diff] [blame] | 189 | public void afterTextChanged(Editable s) { |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public void onTextChanged(CharSequence s, int start, int before, int count) { |
| 198 | if (count - before > 1 || count == 0) |
| 199 | return; // pasted a number (not implemented yet) |
| 200 | |
| 201 | try { |
| 202 | String toSend = Character.toString(s.charAt(start)); |
Alexandre Lision | 31f46fc | 2013-09-26 11:19:54 -0400 | [diff] [blame] | 203 | toSend.toUpperCase(Locale.getDefault()); |
| 204 | mCallbacks.getService().playDtmf(toSend); |
| 205 | } catch (RemoteException e) { |
| 206 | e.printStackTrace(); |
Alexandre Lision | 64dc8c0 | 2013-09-25 15:32:25 -0400 | [diff] [blame] | 207 | } |
Alexandre Lision | 64dc8c0 | 2013-09-25 15:32:25 -0400 | [diff] [blame] | 208 | } |
| 209 | }; |
| 210 | |
| 211 | @Override |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 212 | public void onStart() { |
| 213 | super.onStart(); |
Alexandre Lision | 6e8931e | 2013-09-19 16:49:34 -0400 | [diff] [blame] | 214 | } |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 215 | |
Alexandre Lision | 6e8931e | 2013-09-19 16:49:34 -0400 | [diff] [blame] | 216 | @Override |
| 217 | public boolean onTouch(View v, MotionEvent event) { |
| 218 | InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); |
Alexandre Lision | 6e8931e | 2013-09-19 16:49:34 -0400 | [diff] [blame] | 219 | textField.setError(null); |
| 220 | lManager.hideSoftInputFromWindow(textField.getWindowToken(), 0); |
| 221 | return false; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 222 | } |
| 223 | |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 224 | } |