alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 1 | /* |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 2 | * Copyright (C) 2004-2013 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 | |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 32 | package com.savoirfairelinux.sflphone.fragments; |
| 33 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 34 | import java.util.ArrayList; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 35 | |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 36 | import android.app.Activity; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 37 | import android.app.Fragment; |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 38 | import android.app.LoaderManager.LoaderCallbacks; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 39 | import android.content.Context; |
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 40 | import android.content.Intent; |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 41 | import android.content.Loader; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 42 | import android.os.Bundle; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 43 | import android.util.Log; |
| 44 | import android.view.LayoutInflater; |
| 45 | import android.view.View; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 46 | import android.view.View.OnClickListener; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 47 | import android.view.ViewGroup; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 48 | import android.view.inputmethod.EditorInfo; |
| 49 | import android.view.inputmethod.InputMethodManager; |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 50 | import android.widget.AdapterView; |
| 51 | import android.widget.AdapterView.OnItemSelectedListener; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 52 | import android.widget.Button; |
| 53 | import android.widget.ImageButton; |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 54 | import android.widget.RadioButton; |
| 55 | import android.widget.Spinner; |
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 56 | import android.widget.Toast; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 57 | |
| 58 | import com.savoirfairelinux.sflphone.R; |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 59 | import com.savoirfairelinux.sflphone.adapters.AccountSelectionAdapter; |
| 60 | import com.savoirfairelinux.sflphone.loaders.AccountsLoader; |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 61 | import com.savoirfairelinux.sflphone.loaders.LoaderConstants; |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 62 | import com.savoirfairelinux.sflphone.model.Account; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 63 | import com.savoirfairelinux.sflphone.service.ISipService; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 64 | import com.savoirfairelinux.sflphone.views.ClearableEditText; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 65 | |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 66 | public class DialingFragment extends Fragment implements LoaderCallbacks<ArrayList<Account>> { |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 67 | |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 68 | private static final String TAG = HistoryFragment.class.getSimpleName(); |
| 69 | public static final String ARG_SECTION_NUMBER = "section_number"; |
| 70 | private boolean isReady; |
| 71 | private ISipService service; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 72 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 73 | ClearableEditText textField; |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 74 | // private AccountSelectionSpinner mAccountSelectionSpinner; |
| 75 | |
| 76 | AccountSelectionAdapter mAdapter; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 77 | private Callbacks mCallbacks = sDummyCallbacks; |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 78 | private Spinner spinnerAccounts; |
alision | 55c36cb | 2013-06-14 14:57:38 -0400 | [diff] [blame] | 79 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 80 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 81 | |
| 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 |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 87 | public void onCallDialed(String account, String to) { |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 88 | } |
| 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 { |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 102 | void onCallDialed(String account, String to); |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 103 | |
| 104 | public ISipService getService(); |
| 105 | |
| 106 | } |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 107 | |
| 108 | @Override |
| 109 | public void onAttach(Activity activity) { |
| 110 | super.onAttach(activity); |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 111 | |
| 112 | if (!(activity instanceof Callbacks)) { |
| 113 | throw new IllegalStateException("Activity must implement fragment's callbacks."); |
| 114 | } |
| 115 | |
| 116 | mCallbacks = (Callbacks) activity; |
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 117 | |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public void onDetach() { |
| 122 | super.onDetach(); |
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 123 | mCallbacks = sDummyCallbacks; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 124 | } |
| 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); |
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 135 | |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 136 | spinnerAccounts = (Spinner) inflatedView.findViewById(R.id.account_selection); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 137 | |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 138 | 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 | }); |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 160 | |
| 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) { |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 165 | |
| 166 | Account account = mAdapter.getSelectedAccount(); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 167 | String to = textField.getText().toString(); |
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 168 | if (to.contentEquals("")) { |
| 169 | Toast.makeText(getActivity(), "Enter a number", Toast.LENGTH_LONG).show(); |
| 170 | } else { |
| 171 | mCallbacks.onCallDialed(account.getAccountID(), to); |
| 172 | } |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 173 | } |
| 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); |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 181 | InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 182 | 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); |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 191 | InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 192 | lManager.showSoftInput(textField.getEdit_text(), 0); |
| 193 | } |
| 194 | }); |
| 195 | |
| 196 | isReady = true; |
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 197 | |
alision | 8570418 | 2013-05-29 15:23:03 -0400 | [diff] [blame] | 198 | return inflatedView; |
| 199 | } |
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 200 | |
alision | 8570418 | 2013-05-29 15:23:03 -0400 | [diff] [blame] | 201 | @Override |
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 202 | public void onResume() { |
alision | 8570418 | 2013-05-29 15:23:03 -0400 | [diff] [blame] | 203 | super.onResume(); |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 204 | if (mCallbacks.getService() != null) { |
| 205 | |
| 206 | onServiceSipBinded(mCallbacks.getService()); |
| 207 | } |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public void onStart() { |
| 212 | super.onStart(); |
| 213 | |
| 214 | } |
| 215 | |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 216 | public Account getSelectedAccount() { |
| 217 | return mAdapter.getSelectedAccount(); |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 218 | } |
| 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; |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 229 | |
| 230 | mAdapter = new AccountSelectionAdapter(getActivity(), service, new ArrayList<Account>()); |
| 231 | spinnerAccounts.setAdapter(mAdapter); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 232 | getActivity().getLoaderManager().initLoader(LoaderConstants.ACCOUNTS_LOADER, null, this); |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | } |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 236 | |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 237 | @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 | |
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 257 | public void updateAllAccounts() { |
alision | 55c36cb | 2013-06-14 14:57:38 -0400 | [diff] [blame] | 258 | if (getActivity() != null) |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 259 | getActivity().getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this); |
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 260 | |
| 261 | } |
| 262 | |
| 263 | public void updateAccount(Intent accountState) { |
| 264 | |
| 265 | if (mAdapter != null) |
| 266 | mAdapter.updateAccount(accountState); |
| 267 | |
| 268 | } |
| 269 | |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 270 | } |