alision | fde875f | 2013-05-28 17:01:54 -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 | |
| 32 | package com.savoirfairelinux.sflphone.fragments; |
| 33 | |
| 34 | import java.util.ArrayList; |
| 35 | import java.util.HashMap; |
| 36 | |
| 37 | import android.app.Activity; |
| 38 | import android.app.Fragment; |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 39 | import android.os.Bundle; |
| 40 | import android.os.RemoteException; |
| 41 | import android.util.Log; |
| 42 | import android.view.LayoutInflater; |
| 43 | import android.view.View; |
| 44 | import android.view.ViewGroup; |
alision | 8599211 | 2013-05-29 12:18:08 -0400 | [diff] [blame] | 45 | import android.widget.AdapterView; |
| 46 | import android.widget.AdapterView.OnItemClickListener; |
| 47 | import android.widget.ListView; |
alision | 8570418 | 2013-05-29 15:23:03 -0400 | [diff] [blame] | 48 | import android.widget.Toast; |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 49 | |
| 50 | import com.savoirfairelinux.sflphone.R; |
| 51 | import com.savoirfairelinux.sflphone.adapters.CallListAdapter; |
| 52 | import com.savoirfairelinux.sflphone.model.SipCall; |
| 53 | import com.savoirfairelinux.sflphone.service.ISipService; |
| 54 | |
| 55 | public class CallListFragment extends Fragment { |
| 56 | static final String TAG = "CallFragment"; |
| 57 | |
| 58 | private Callbacks mCallbacks = sDummyCallbacks; |
| 59 | |
| 60 | CallListAdapter mAdapter; |
| 61 | |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 62 | |
| 63 | @Override |
| 64 | public void onCreate(Bundle savedBundle) { |
| 65 | super.onCreate(savedBundle); |
| 66 | |
alision | 8599211 | 2013-05-29 12:18:08 -0400 | [diff] [blame] | 67 | mAdapter = new CallListAdapter(getActivity(), new ArrayList<SipCall>()); |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 68 | |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity. |
| 73 | */ |
| 74 | private static Callbacks sDummyCallbacks = new Callbacks() { |
| 75 | |
| 76 | @Override |
| 77 | public ISipService getService() { |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 78 | return null; |
| 79 | } |
alision | 8599211 | 2013-05-29 12:18:08 -0400 | [diff] [blame] | 80 | |
| 81 | @Override |
| 82 | public void onCallSelected(SipCall call) { |
| 83 | } |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | /** |
| 87 | * The Activity calling this fragment has to implement this interface |
| 88 | * |
| 89 | */ |
| 90 | public interface Callbacks { |
| 91 | public ISipService getService(); |
alision | 8599211 | 2013-05-29 12:18:08 -0400 | [diff] [blame] | 92 | public void onCallSelected(SipCall call); |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 93 | |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void onAttach(Activity activity) { |
| 98 | super.onAttach(activity); |
| 99 | |
| 100 | if (!(activity instanceof Callbacks)) { |
| 101 | throw new IllegalStateException("Activity must implement fragment's callbacks."); |
| 102 | } |
| 103 | |
| 104 | mCallbacks = (Callbacks) activity; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void onDetach() { |
| 109 | super.onDetach(); |
| 110 | mCallbacks = sDummyCallbacks; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 115 | ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.frag_call_list, container, false); |
| 116 | |
alision | 8599211 | 2013-05-29 12:18:08 -0400 | [diff] [blame] | 117 | ListView list = (ListView) rootView.findViewById(R.id.call_list); |
| 118 | list.setDividerHeight(2); |
| 119 | // expandbleLis.setGroupIndicator(null); |
| 120 | list.setClickable(true); |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 121 | |
alision | 8599211 | 2013-05-29 12:18:08 -0400 | [diff] [blame] | 122 | list.setAdapter(mAdapter); |
| 123 | |
| 124 | list.setOnItemClickListener(itemClickListener); |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 125 | return rootView; |
| 126 | } |
| 127 | |
| 128 | public void update() { |
| 129 | try { |
| 130 | HashMap<String, SipCall> list = (HashMap<String, SipCall>) mCallbacks.getService().getCallList(); |
alision | 8599211 | 2013-05-29 12:18:08 -0400 | [diff] [blame] | 131 | mAdapter.update(list); |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 132 | } catch (RemoteException e) { |
| 133 | Log.e(TAG, e.toString()); |
| 134 | } |
| 135 | |
| 136 | } |
alision | 8599211 | 2013-05-29 12:18:08 -0400 | [diff] [blame] | 137 | |
| 138 | OnItemClickListener itemClickListener = new OnItemClickListener() { |
| 139 | |
| 140 | @Override |
| 141 | public void onItemClick(AdapterView<?> arg0, View view, int pos, long arg3) { |
alision | 8570418 | 2013-05-29 15:23:03 -0400 | [diff] [blame] | 142 | Toast.makeText(getActivity(), "ItemClicked", Toast.LENGTH_SHORT).show(); |
alision | 8599211 | 2013-05-29 12:18:08 -0400 | [diff] [blame] | 143 | mCallbacks.onCallSelected(mAdapter.getItem(pos)); |
| 144 | |
| 145 | } |
| 146 | }; |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 147 | |
| 148 | } |