blob: 10d6300a8c3a540a9fd3d8c98c61d3b456800aa3 [file] [log] [blame]
alision11e8e162013-05-28 10:33:14 -04001/*
2 * Copyright (C) 2004-2012 Savoir-Faire Linux Inc.
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
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;
alision11e8e162013-05-28 10:33:14 -040040import android.content.Loader;
alision9f7a6ec2013-05-24 16:26:26 -040041import android.os.Bundle;
alisiond295ec22013-05-17 10:12:13 -040042import android.util.Log;
43import android.view.LayoutInflater;
44import android.view.View;
alision9f7a6ec2013-05-24 16:26:26 -040045import android.view.View.OnClickListener;
alisiond295ec22013-05-17 10:12:13 -040046import android.view.ViewGroup;
alision9f7a6ec2013-05-24 16:26:26 -040047import android.view.inputmethod.EditorInfo;
48import android.view.inputmethod.InputMethodManager;
alision11e8e162013-05-28 10:33:14 -040049import android.widget.AdapterView;
50import android.widget.AdapterView.OnItemSelectedListener;
alision9f7a6ec2013-05-24 16:26:26 -040051import android.widget.Button;
52import android.widget.ImageButton;
alision11e8e162013-05-28 10:33:14 -040053import android.widget.RadioButton;
54import android.widget.Spinner;
alisiond295ec22013-05-17 10:12:13 -040055
56import com.savoirfairelinux.sflphone.R;
alision11e8e162013-05-28 10:33:14 -040057import com.savoirfairelinux.sflphone.adapters.AccountSelectionAdapter;
58import com.savoirfairelinux.sflphone.loaders.AccountsLoader;
59import com.savoirfairelinux.sflphone.model.Account;
alisiond295ec22013-05-17 10:12:13 -040060import com.savoirfairelinux.sflphone.service.ISipService;
alision9f7a6ec2013-05-24 16:26:26 -040061import com.savoirfairelinux.sflphone.views.ClearableEditText;
alisiond295ec22013-05-17 10:12:13 -040062
alision11e8e162013-05-28 10:33:14 -040063public class DialingFragment extends Fragment implements LoaderCallbacks<ArrayList<Account>> {
alision9f7a6ec2013-05-24 16:26:26 -040064
alisiond295ec22013-05-17 10:12:13 -040065 private static final String TAG = HistoryFragment.class.getSimpleName();
66 public static final String ARG_SECTION_NUMBER = "section_number";
67 private boolean isReady;
68 private ISipService service;
alisiond295ec22013-05-17 10:12:13 -040069
alision9f7a6ec2013-05-24 16:26:26 -040070 ClearableEditText textField;
alision11e8e162013-05-28 10:33:14 -040071 // private AccountSelectionSpinner mAccountSelectionSpinner;
72
73 AccountSelectionAdapter mAdapter;
alision9f7a6ec2013-05-24 16:26:26 -040074 private Callbacks mCallbacks = sDummyCallbacks;
alision11e8e162013-05-28 10:33:14 -040075 private Spinner spinnerAccounts;
alision9f7a6ec2013-05-24 16:26:26 -040076
77 /**
78 * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity.
79 */
80 private static Callbacks sDummyCallbacks = new Callbacks() {
81 @Override
alision84813a12013-05-27 17:40:39 -040082 public void onCallDialed(String account, String to) {
alision9f7a6ec2013-05-24 16:26:26 -040083 }
84
85 @Override
86 public ISipService getService() {
87 // TODO Auto-generated method stub
88 return null;
89 }
90 };
91
92 /**
93 * The Activity calling this fragment has to implement this interface
94 *
95 */
96 public interface Callbacks {
alision84813a12013-05-27 17:40:39 -040097 void onCallDialed(String account, String to);
alision9f7a6ec2013-05-24 16:26:26 -040098
99 public ISipService getService();
100
101 }
alisiond295ec22013-05-17 10:12:13 -0400102
103 @Override
104 public void onAttach(Activity activity) {
105 super.onAttach(activity);
alision9f7a6ec2013-05-24 16:26:26 -0400106
107 if (!(activity instanceof Callbacks)) {
108 throw new IllegalStateException("Activity must implement fragment's callbacks.");
109 }
110
111 mCallbacks = (Callbacks) activity;
alision85704182013-05-29 15:23:03 -0400112
alisiond295ec22013-05-17 10:12:13 -0400113 }
114
115 @Override
116 public void onDetach() {
117 super.onDetach();
118 }
119
120 @Override
121 public void onCreate(Bundle savedInstanceState) {
122 super.onCreate(savedInstanceState);
123 // mAdapter = new HistoryAdapter(getActivity(),new ArrayList<HashMap<String, String>>());
124 }
125
126 @Override
127 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
128 View inflatedView = inflater.inflate(R.layout.frag_dialing, parent, false);
alision11e8e162013-05-28 10:33:14 -0400129
130 spinnerAccounts = (Spinner) inflatedView.findViewById(R.id.account_selection);
alisiond295ec22013-05-17 10:12:13 -0400131
alision11e8e162013-05-28 10:33:14 -0400132 spinnerAccounts.setOnItemSelectedListener(new OnItemSelectedListener() {
133
134 @Override
135 public void onItemSelected(AdapterView<?> arg0, View view, int pos, long arg3) {
136 // public void onClick(DialogInterface dialog, int which) {
137
138 Log.i(TAG, "Selected Account: " + mAdapter.getItem(pos));
139 if (null != view) {
140 ((RadioButton) view.findViewById(R.id.account_checked)).toggle();
141 }
142 mAdapter.setSelectedAccount(pos);
143 // accountSelectedNotifyAccountList(mAdapter.getItem(pos));
144 // setSelection(cursor.getPosition(),true);
145
146 }
147
148 @Override
149 public void onNothingSelected(AdapterView<?> arg0) {
150 // TODO Auto-generated method stub
151
152 }
153 });
alision9f7a6ec2013-05-24 16:26:26 -0400154
155 textField = (ClearableEditText) inflatedView.findViewById(R.id.textField);
156 ((ImageButton) inflatedView.findViewById(R.id.buttonCall)).setOnClickListener(new OnClickListener() {
157 @Override
158 public void onClick(View v) {
alision11e8e162013-05-28 10:33:14 -0400159
160 Account account = mAdapter.getSelectedAccount();
alision84813a12013-05-27 17:40:39 -0400161 String to = textField.getText().toString();
alision11e8e162013-05-28 10:33:14 -0400162 mCallbacks.onCallDialed(account.getAccountID(), to);
alision9f7a6ec2013-05-24 16:26:26 -0400163 }
164 });
165
166 ((Button) inflatedView.findViewById(R.id.alphabetic_keyboard)).setOnClickListener(new OnClickListener() {
167
168 @Override
169 public void onClick(View v) {
170 textField.setInputType(EditorInfo.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
alision11e8e162013-05-28 10:33:14 -0400171 InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
alision9f7a6ec2013-05-24 16:26:26 -0400172 lManager.showSoftInput(textField.getEdit_text(), 0);
173 }
174 });
175
176 ((Button) inflatedView.findViewById(R.id.numeric_keyboard)).setOnClickListener(new OnClickListener() {
177
178 @Override
179 public void onClick(View v) {
180 textField.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
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 isReady = true;
alision85704182013-05-29 15:23:03 -0400187
188 return inflatedView;
189 }
190
191 @Override
192 public void onResume(){
193 super.onResume();
alision9f7a6ec2013-05-24 16:26:26 -0400194 if (mCallbacks.getService() != null) {
195
196 onServiceSipBinded(mCallbacks.getService());
197 }
alisiond295ec22013-05-17 10:12:13 -0400198 }
199
200 @Override
201 public void onStart() {
202 super.onStart();
203
204 }
205
alision11e8e162013-05-28 10:33:14 -0400206 public Account getSelectedAccount() {
207 return mAdapter.getSelectedAccount();
alision9f7a6ec2013-05-24 16:26:26 -0400208 }
209
210 /**
211 * Called by activity to pass a reference to sipservice to Fragment.
212 *
213 * @param isip
214 */
215 public void onServiceSipBinded(ISipService isip) {
216
217 if (isReady) {
218 service = isip;
alision11e8e162013-05-28 10:33:14 -0400219
220 mAdapter = new AccountSelectionAdapter(getActivity(), service, new ArrayList<Account>());
221 spinnerAccounts.setAdapter(mAdapter);
222 getActivity().getLoaderManager().initLoader(555, null, this);
alision9f7a6ec2013-05-24 16:26:26 -0400223 }
224
225 }
alisiond295ec22013-05-17 10:12:13 -0400226
alision11e8e162013-05-28 10:33:14 -0400227 @Override
228 public Loader<ArrayList<Account>> onCreateLoader(int id, Bundle args) {
229 AccountsLoader l = new AccountsLoader(getActivity(), service);
230 l.forceLoad();
231 return l;
232 }
233
234 @Override
235 public void onLoadFinished(Loader<ArrayList<Account>> loader, ArrayList<Account> results) {
236 mAdapter.removeAll();
237 mAdapter.addAll(results);
238
239 }
240
241 @Override
242 public void onLoaderReset(Loader<ArrayList<Account>> arg0) {
243 // TODO Auto-generated method stub
244
245 }
246
alisiond295ec22013-05-17 10:12:13 -0400247}