blob: 9994e2acb7b266ecc96acd0041478645b978845b [file] [log] [blame]
alision11e8e162013-05-28 10:33:14 -04001/*
alision2ec64f92013-06-17 17:28:58 -04002 * Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
alision11e8e162013-05-28 10:33:14 -04003 *
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 Lision064e1e02013-10-01 16:18:42 -040032package org.sflphone.fragments;
alisiond295ec22013-05-17 10:12:13 -040033
Alexandre Lision31f46fc2013-09-26 11:19:54 -040034import java.util.Locale;
35
Alexandre Lision064e1e02013-10-01 16:18:42 -040036import org.sflphone.R;
37import org.sflphone.service.ISipService;
38import org.sflphone.views.ClearableEditText;
39
alisiond295ec22013-05-17 10:12:13 -040040import android.app.Activity;
alisiond295ec22013-05-17 10:12:13 -040041import android.app.Fragment;
alision9f7a6ec2013-05-24 16:26:26 -040042import android.content.Context;
43import android.os.Bundle;
Alexandre Lision64dc8c02013-09-25 15:32:25 -040044import android.os.RemoteException;
Alexandre Lision31f46fc2013-09-26 11:19:54 -040045import android.text.Editable;
46import android.text.TextWatcher;
alisiond295ec22013-05-17 10:12:13 -040047import android.view.LayoutInflater;
Alexandre Lision6e8931e2013-09-19 16:49:34 -040048import android.view.MotionEvent;
alisiond295ec22013-05-17 10:12:13 -040049import android.view.View;
alision9f7a6ec2013-05-24 16:26:26 -040050import android.view.View.OnClickListener;
Alexandre Lision6e8931e2013-09-19 16:49:34 -040051import android.view.View.OnTouchListener;
alisiond295ec22013-05-17 10:12:13 -040052import android.view.ViewGroup;
alision9f7a6ec2013-05-24 16:26:26 -040053import android.view.inputmethod.EditorInfo;
54import android.view.inputmethod.InputMethodManager;
55import android.widget.Button;
56import android.widget.ImageButton;
alisiond295ec22013-05-17 10:12:13 -040057
Alexandre Lision6e8931e2013-09-19 16:49:34 -040058public class DialingFragment extends Fragment implements OnTouchListener {
alision9f7a6ec2013-05-24 16:26:26 -040059
alision1005ba12013-06-19 13:52:44 -040060 private static final String TAG = DialingFragment.class.getSimpleName();
alision907bde72013-06-20 14:40:37 -040061
alision9f7a6ec2013-05-24 16:26:26 -040062 ClearableEditText textField;
alision9f7a6ec2013-05-24 16:26:26 -040063 private Callbacks mCallbacks = sDummyCallbacks;
64
65 /**
66 * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity.
67 */
68 private static Callbacks sDummyCallbacks = new Callbacks() {
69 @Override
alision907bde72013-06-20 14:40:37 -040070 public void onCallDialed(String to) {
alision9f7a6ec2013-05-24 16:26:26 -040071 }
72
73 @Override
74 public ISipService getService() {
75 // TODO Auto-generated method stub
76 return null;
77 }
78 };
79
80 /**
81 * The Activity calling this fragment has to implement this interface
82 *
83 */
84 public interface Callbacks {
alision907bde72013-06-20 14:40:37 -040085 void onCallDialed(String account);
alision9f7a6ec2013-05-24 16:26:26 -040086
87 public ISipService getService();
88
89 }
alisiond295ec22013-05-17 10:12:13 -040090
91 @Override
92 public void onAttach(Activity activity) {
93 super.onAttach(activity);
alision9f7a6ec2013-05-24 16:26:26 -040094
95 if (!(activity instanceof Callbacks)) {
96 throw new IllegalStateException("Activity must implement fragment's callbacks.");
97 }
98
99 mCallbacks = (Callbacks) activity;
alision58356b72013-06-03 17:13:36 -0400100
alisiond295ec22013-05-17 10:12:13 -0400101 }
102
103 @Override
104 public void onDetach() {
105 super.onDetach();
alision58356b72013-06-03 17:13:36 -0400106 mCallbacks = sDummyCallbacks;
alisiond295ec22013-05-17 10:12:13 -0400107 }
108
109 @Override
110 public void onCreate(Bundle savedInstanceState) {
111 super.onCreate(savedInstanceState);
alisiond295ec22013-05-17 10:12:13 -0400112 }
113
114 @Override
115 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
116 View inflatedView = inflater.inflate(R.layout.frag_dialing, parent, false);
alision58356b72013-06-03 17:13:36 -0400117
alision9f7a6ec2013-05-24 16:26:26 -0400118 textField = (ClearableEditText) inflatedView.findViewById(R.id.textField);
119 ((ImageButton) inflatedView.findViewById(R.id.buttonCall)).setOnClickListener(new OnClickListener() {
120 @Override
121 public void onClick(View v) {
alision11e8e162013-05-28 10:33:14 -0400122
alision84813a12013-05-27 17:40:39 -0400123 String to = textField.getText().toString();
alision58356b72013-06-03 17:13:36 -0400124 if (to.contentEquals("")) {
Alexandre Lisionc2bd7b62013-09-30 10:45:00 -0400125 textField.setError(getString(R.string.dial_error_no_number_dialed));
alision58356b72013-06-03 17:13:36 -0400126 } else {
alision907bde72013-06-20 14:40:37 -0400127 mCallbacks.onCallDialed(to);
alision58356b72013-06-03 17:13:36 -0400128 }
alision9f7a6ec2013-05-24 16:26:26 -0400129 }
130 });
131
Alexandre Lision6e8931e2013-09-19 16:49:34 -0400132 inflatedView.setOnTouchListener(this);
133
alision9f7a6ec2013-05-24 16:26:26 -0400134 ((Button) inflatedView.findViewById(R.id.alphabetic_keyboard)).setOnClickListener(new OnClickListener() {
135
136 @Override
137 public void onClick(View v) {
138 textField.setInputType(EditorInfo.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
alision11e8e162013-05-28 10:33:14 -0400139 InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
alision9f7a6ec2013-05-24 16:26:26 -0400140 lManager.showSoftInput(textField.getEdit_text(), 0);
141 }
142 });
143
144 ((Button) inflatedView.findViewById(R.id.numeric_keyboard)).setOnClickListener(new OnClickListener() {
145
146 @Override
147 public void onClick(View v) {
Alexandre Lision6e8931e2013-09-19 16:49:34 -0400148 textField.setInputType(EditorInfo.TYPE_CLASS_PHONE);
alision11e8e162013-05-28 10:33:14 -0400149 InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
alision9f7a6ec2013-05-24 16:26:26 -0400150 lManager.showSoftInput(textField.getEdit_text(), 0);
151 }
152 });
alision85704182013-05-29 15:23:03 -0400153 return inflatedView;
154 }
alision58356b72013-06-03 17:13:36 -0400155
alision85704182013-05-29 15:23:03 -0400156 @Override
alision58356b72013-06-03 17:13:36 -0400157 public void onResume() {
alision85704182013-05-29 15:23:03 -0400158 super.onResume();
Alexandre Lision31f46fc2013-09-26 11:19:54 -0400159 textField.setTextWatcher(dtmfKeyListener);
alisiond295ec22013-05-17 10:12:13 -0400160 }
161
162 @Override
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400163 public void onPause() {
164 super.onPause();
Alexandre Lision31f46fc2013-09-26 11:19:54 -0400165 textField.unsetTextWatcher();
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400166 }
167
Alexandre Lision31f46fc2013-09-26 11:19:54 -0400168 TextWatcher dtmfKeyListener = new TextWatcher() {
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400169
170 @Override
Alexandre Lision31f46fc2013-09-26 11:19:54 -0400171 public void afterTextChanged(Editable s) {
172 }
173
174 @Override
175 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
176 }
177
178 @Override
179 public void onTextChanged(CharSequence s, int start, int before, int count) {
180 if (count - before > 1 || count == 0)
181 return; // pasted a number (not implemented yet)
182
183 try {
184 String toSend = Character.toString(s.charAt(start));
Alexandre Lision31f46fc2013-09-26 11:19:54 -0400185 toSend.toUpperCase(Locale.getDefault());
186 mCallbacks.getService().playDtmf(toSend);
187 } catch (RemoteException e) {
188 e.printStackTrace();
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400189 }
Alexandre Lision64dc8c02013-09-25 15:32:25 -0400190 }
191 };
192
193 @Override
alisiond295ec22013-05-17 10:12:13 -0400194 public void onStart() {
195 super.onStart();
Alexandre Lision6e8931e2013-09-19 16:49:34 -0400196 }
alisiond295ec22013-05-17 10:12:13 -0400197
Alexandre Lision6e8931e2013-09-19 16:49:34 -0400198 @Override
199 public boolean onTouch(View v, MotionEvent event) {
200 InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
Alexandre Lision6e8931e2013-09-19 16:49:34 -0400201 textField.setError(null);
202 lManager.hideSoftInputFromWindow(textField.getWindowToken(), 0);
203 return false;
alisiond295ec22013-05-17 10:12:13 -0400204 }
205
alisiond295ec22013-05-17 10:12:13 -0400206}