alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
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 | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 40 | import android.content.Loader; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 41 | import android.os.Bundle; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 42 | import android.util.Log; |
| 43 | import android.view.LayoutInflater; |
| 44 | import android.view.View; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 45 | import android.view.View.OnClickListener; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 46 | import android.view.ViewGroup; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 47 | import android.view.inputmethod.EditorInfo; |
| 48 | import android.view.inputmethod.InputMethodManager; |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 49 | import android.widget.AdapterView; |
| 50 | import android.widget.AdapterView.OnItemSelectedListener; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 51 | import android.widget.Button; |
| 52 | import android.widget.ImageButton; |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 53 | import android.widget.RadioButton; |
| 54 | import android.widget.Spinner; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 55 | |
| 56 | import com.savoirfairelinux.sflphone.R; |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 57 | import com.savoirfairelinux.sflphone.adapters.AccountSelectionAdapter; |
| 58 | import com.savoirfairelinux.sflphone.loaders.AccountsLoader; |
| 59 | import com.savoirfairelinux.sflphone.model.Account; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 60 | import com.savoirfairelinux.sflphone.service.ISipService; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 61 | import com.savoirfairelinux.sflphone.views.ClearableEditText; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 62 | |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 63 | public class DialingFragment extends Fragment implements LoaderCallbacks<ArrayList<Account>> { |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 64 | |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 65 | 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; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 69 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 70 | ClearableEditText textField; |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 71 | // private AccountSelectionSpinner mAccountSelectionSpinner; |
| 72 | |
| 73 | AccountSelectionAdapter mAdapter; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 74 | private Callbacks mCallbacks = sDummyCallbacks; |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 75 | private Spinner spinnerAccounts; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 76 | |
| 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 |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 82 | public void onCallDialed(String account, String to) { |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 83 | } |
| 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 { |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 97 | void onCallDialed(String account, String to); |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 98 | |
| 99 | public ISipService getService(); |
| 100 | |
| 101 | } |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 102 | |
| 103 | @Override |
| 104 | public void onAttach(Activity activity) { |
| 105 | super.onAttach(activity); |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 106 | |
| 107 | if (!(activity instanceof Callbacks)) { |
| 108 | throw new IllegalStateException("Activity must implement fragment's callbacks."); |
| 109 | } |
| 110 | |
| 111 | mCallbacks = (Callbacks) activity; |
alision | 8570418 | 2013-05-29 15:23:03 -0400 | [diff] [blame] | 112 | |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 113 | } |
| 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); |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 129 | |
| 130 | spinnerAccounts = (Spinner) inflatedView.findViewById(R.id.account_selection); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 131 | |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 132 | 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 | }); |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 154 | |
| 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) { |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 159 | |
| 160 | Account account = mAdapter.getSelectedAccount(); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 161 | String to = textField.getText().toString(); |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 162 | mCallbacks.onCallDialed(account.getAccountID(), to); |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 163 | } |
| 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); |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 171 | InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 172 | 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); |
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 | isReady = true; |
alision | 8570418 | 2013-05-29 15:23:03 -0400 | [diff] [blame] | 187 | |
| 188 | return inflatedView; |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public void onResume(){ |
| 193 | super.onResume(); |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 194 | if (mCallbacks.getService() != null) { |
| 195 | |
| 196 | onServiceSipBinded(mCallbacks.getService()); |
| 197 | } |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public void onStart() { |
| 202 | super.onStart(); |
| 203 | |
| 204 | } |
| 205 | |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 206 | public Account getSelectedAccount() { |
| 207 | return mAdapter.getSelectedAccount(); |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 208 | } |
| 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; |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 219 | |
| 220 | mAdapter = new AccountSelectionAdapter(getActivity(), service, new ArrayList<Account>()); |
| 221 | spinnerAccounts.setAdapter(mAdapter); |
| 222 | getActivity().getLoaderManager().initLoader(555, null, this); |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | } |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 226 | |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 227 | @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 | |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 247 | } |