blob: 73a4e76012f207f44233783086e4fabef11fb1b5 [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
alisiond295ec22013-05-17 10:12:13 -040032package com.savoirfairelinux.sflphone.fragments;
33
alision9f7a6ec2013-05-24 16:26:26 -040034import java.util.ArrayList;
alision9f7a6ec2013-05-24 16:26:26 -040035
alisiond295ec22013-05-17 10:12:13 -040036import android.app.Activity;
alisiond295ec22013-05-17 10:12:13 -040037import android.app.Fragment;
alision11e8e162013-05-28 10:33:14 -040038import android.app.LoaderManager.LoaderCallbacks;
alision9f7a6ec2013-05-24 16:26:26 -040039import android.content.Context;
alision58356b72013-06-03 17:13:36 -040040import android.content.Intent;
alision11e8e162013-05-28 10:33:14 -040041import android.content.Loader;
alision9f7a6ec2013-05-24 16:26:26 -040042import android.os.Bundle;
alisiond295ec22013-05-17 10:12:13 -040043import android.util.Log;
44import android.view.LayoutInflater;
45import android.view.View;
alision9f7a6ec2013-05-24 16:26:26 -040046import android.view.View.OnClickListener;
alisiond295ec22013-05-17 10:12:13 -040047import android.view.ViewGroup;
alision9f7a6ec2013-05-24 16:26:26 -040048import android.view.inputmethod.EditorInfo;
49import android.view.inputmethod.InputMethodManager;
alision11e8e162013-05-28 10:33:14 -040050import android.widget.AdapterView;
51import android.widget.AdapterView.OnItemSelectedListener;
alision9f7a6ec2013-05-24 16:26:26 -040052import android.widget.Button;
53import android.widget.ImageButton;
alision11e8e162013-05-28 10:33:14 -040054import android.widget.RadioButton;
55import android.widget.Spinner;
alision58356b72013-06-03 17:13:36 -040056import android.widget.Toast;
alisiond295ec22013-05-17 10:12:13 -040057
58import com.savoirfairelinux.sflphone.R;
alision11e8e162013-05-28 10:33:14 -040059import com.savoirfairelinux.sflphone.adapters.AccountSelectionAdapter;
60import com.savoirfairelinux.sflphone.loaders.AccountsLoader;
alision2ec64f92013-06-17 17:28:58 -040061import com.savoirfairelinux.sflphone.loaders.LoaderConstants;
alision11e8e162013-05-28 10:33:14 -040062import com.savoirfairelinux.sflphone.model.Account;
alisiond295ec22013-05-17 10:12:13 -040063import com.savoirfairelinux.sflphone.service.ISipService;
alision9f7a6ec2013-05-24 16:26:26 -040064import com.savoirfairelinux.sflphone.views.ClearableEditText;
alisiond295ec22013-05-17 10:12:13 -040065
alision11e8e162013-05-28 10:33:14 -040066public class DialingFragment extends Fragment implements LoaderCallbacks<ArrayList<Account>> {
alision9f7a6ec2013-05-24 16:26:26 -040067
alision1005ba12013-06-19 13:52:44 -040068 private static final String TAG = DialingFragment.class.getSimpleName();
alisiond295ec22013-05-17 10:12:13 -040069 public static final String ARG_SECTION_NUMBER = "section_number";
70 private boolean isReady;
71 private ISipService service;
alisiond295ec22013-05-17 10:12:13 -040072
alision9f7a6ec2013-05-24 16:26:26 -040073 ClearableEditText textField;
alision11e8e162013-05-28 10:33:14 -040074 // private AccountSelectionSpinner mAccountSelectionSpinner;
75
76 AccountSelectionAdapter mAdapter;
alision9f7a6ec2013-05-24 16:26:26 -040077 private Callbacks mCallbacks = sDummyCallbacks;
alision11e8e162013-05-28 10:33:14 -040078 private Spinner spinnerAccounts;
alision55c36cb2013-06-14 14:57:38 -040079
alision2ec64f92013-06-17 17:28:58 -040080
alision9f7a6ec2013-05-24 16:26:26 -040081
82 /**
83 * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity.
84 */
85 private static Callbacks sDummyCallbacks = new Callbacks() {
86 @Override
alision84813a12013-05-27 17:40:39 -040087 public void onCallDialed(String account, String to) {
alision9f7a6ec2013-05-24 16:26:26 -040088 }
89
90 @Override
91 public ISipService getService() {
92 // TODO Auto-generated method stub
93 return null;
94 }
95 };
96
97 /**
98 * The Activity calling this fragment has to implement this interface
99 *
100 */
101 public interface Callbacks {
alision84813a12013-05-27 17:40:39 -0400102 void onCallDialed(String account, String to);
alision9f7a6ec2013-05-24 16:26:26 -0400103
104 public ISipService getService();
105
106 }
alisiond295ec22013-05-17 10:12:13 -0400107
108 @Override
109 public void onAttach(Activity activity) {
110 super.onAttach(activity);
alision9f7a6ec2013-05-24 16:26:26 -0400111
112 if (!(activity instanceof Callbacks)) {
113 throw new IllegalStateException("Activity must implement fragment's callbacks.");
114 }
115
116 mCallbacks = (Callbacks) activity;
alision58356b72013-06-03 17:13:36 -0400117
alisiond295ec22013-05-17 10:12:13 -0400118 }
119
120 @Override
121 public void onDetach() {
122 super.onDetach();
alision58356b72013-06-03 17:13:36 -0400123 mCallbacks = sDummyCallbacks;
alisiond295ec22013-05-17 10:12:13 -0400124 }
125
126 @Override
127 public void onCreate(Bundle savedInstanceState) {
128 super.onCreate(savedInstanceState);
129 // mAdapter = new HistoryAdapter(getActivity(),new ArrayList<HashMap<String, String>>());
130 }
131
132 @Override
133 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
134 View inflatedView = inflater.inflate(R.layout.frag_dialing, parent, false);
alision58356b72013-06-03 17:13:36 -0400135
alision11e8e162013-05-28 10:33:14 -0400136 spinnerAccounts = (Spinner) inflatedView.findViewById(R.id.account_selection);
alisiond295ec22013-05-17 10:12:13 -0400137
alision11e8e162013-05-28 10:33:14 -0400138 spinnerAccounts.setOnItemSelectedListener(new OnItemSelectedListener() {
139
140 @Override
141 public void onItemSelected(AdapterView<?> arg0, View view, int pos, long arg3) {
142 // public void onClick(DialogInterface dialog, int which) {
143
144 Log.i(TAG, "Selected Account: " + mAdapter.getItem(pos));
145 if (null != view) {
146 ((RadioButton) view.findViewById(R.id.account_checked)).toggle();
147 }
148 mAdapter.setSelectedAccount(pos);
149 // accountSelectedNotifyAccountList(mAdapter.getItem(pos));
150 // setSelection(cursor.getPosition(),true);
151
152 }
153
154 @Override
155 public void onNothingSelected(AdapterView<?> arg0) {
156 // TODO Auto-generated method stub
157
158 }
159 });
alision9f7a6ec2013-05-24 16:26:26 -0400160
161 textField = (ClearableEditText) inflatedView.findViewById(R.id.textField);
162 ((ImageButton) inflatedView.findViewById(R.id.buttonCall)).setOnClickListener(new OnClickListener() {
163 @Override
164 public void onClick(View v) {
alision11e8e162013-05-28 10:33:14 -0400165
166 Account account = mAdapter.getSelectedAccount();
alision84813a12013-05-27 17:40:39 -0400167 String to = textField.getText().toString();
alision58356b72013-06-03 17:13:36 -0400168 if (to.contentEquals("")) {
169 Toast.makeText(getActivity(), "Enter a number", Toast.LENGTH_LONG).show();
170 } else {
171 mCallbacks.onCallDialed(account.getAccountID(), to);
172 }
alision9f7a6ec2013-05-24 16:26:26 -0400173 }
174 });
175
176 ((Button) inflatedView.findViewById(R.id.alphabetic_keyboard)).setOnClickListener(new OnClickListener() {
177
178 @Override
179 public void onClick(View v) {
180 textField.setInputType(EditorInfo.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
alision11e8e162013-05-28 10:33:14 -0400181 InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
alision9f7a6ec2013-05-24 16:26:26 -0400182 lManager.showSoftInput(textField.getEdit_text(), 0);
183 }
184 });
185
186 ((Button) inflatedView.findViewById(R.id.numeric_keyboard)).setOnClickListener(new OnClickListener() {
187
188 @Override
189 public void onClick(View v) {
190 textField.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
alision11e8e162013-05-28 10:33:14 -0400191 InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
alision9f7a6ec2013-05-24 16:26:26 -0400192 lManager.showSoftInput(textField.getEdit_text(), 0);
193 }
194 });
195
196 isReady = true;
alision58356b72013-06-03 17:13:36 -0400197
alision85704182013-05-29 15:23:03 -0400198 return inflatedView;
199 }
alision58356b72013-06-03 17:13:36 -0400200
alision85704182013-05-29 15:23:03 -0400201 @Override
alision58356b72013-06-03 17:13:36 -0400202 public void onResume() {
alision85704182013-05-29 15:23:03 -0400203 super.onResume();
alision9f7a6ec2013-05-24 16:26:26 -0400204 if (mCallbacks.getService() != null) {
205
206 onServiceSipBinded(mCallbacks.getService());
207 }
alisiond295ec22013-05-17 10:12:13 -0400208 }
209
210 @Override
211 public void onStart() {
212 super.onStart();
213
214 }
215
alision11e8e162013-05-28 10:33:14 -0400216 public Account getSelectedAccount() {
217 return mAdapter.getSelectedAccount();
alision9f7a6ec2013-05-24 16:26:26 -0400218 }
219
220 /**
221 * Called by activity to pass a reference to sipservice to Fragment.
222 *
223 * @param isip
224 */
225 public void onServiceSipBinded(ISipService isip) {
226
227 if (isReady) {
228 service = isip;
alision11e8e162013-05-28 10:33:14 -0400229
230 mAdapter = new AccountSelectionAdapter(getActivity(), service, new ArrayList<Account>());
231 spinnerAccounts.setAdapter(mAdapter);
alision2ec64f92013-06-17 17:28:58 -0400232 getActivity().getLoaderManager().initLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
alision9f7a6ec2013-05-24 16:26:26 -0400233 }
234
235 }
alisiond295ec22013-05-17 10:12:13 -0400236
alision11e8e162013-05-28 10:33:14 -0400237 @Override
238 public Loader<ArrayList<Account>> onCreateLoader(int id, Bundle args) {
239 AccountsLoader l = new AccountsLoader(getActivity(), service);
240 l.forceLoad();
241 return l;
242 }
243
244 @Override
245 public void onLoadFinished(Loader<ArrayList<Account>> loader, ArrayList<Account> results) {
246 mAdapter.removeAll();
247 mAdapter.addAll(results);
248
249 }
250
251 @Override
252 public void onLoaderReset(Loader<ArrayList<Account>> arg0) {
253 // TODO Auto-generated method stub
254
255 }
256
alision58356b72013-06-03 17:13:36 -0400257 public void updateAllAccounts() {
alision55c36cb2013-06-14 14:57:38 -0400258 if (getActivity() != null)
alision2ec64f92013-06-17 17:28:58 -0400259 getActivity().getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
alision58356b72013-06-03 17:13:36 -0400260
261 }
262
263 public void updateAccount(Intent accountState) {
264
265 if (mAdapter != null)
266 mAdapter.updateAccount(accountState);
267
268 }
269
alisiond295ec22013-05-17 10:12:13 -0400270}