alision | f76de3b | 2013-04-16 15:35:22 -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 | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 3 | * |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 4 | * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com> |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 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 | package com.savoirfairelinux.sflphone.fragments; |
| 32 | |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 33 | import java.util.HashMap; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 34 | |
| 35 | import android.app.Activity; |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 36 | import android.app.Fragment; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 37 | import android.os.Bundle; |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 38 | import android.os.RemoteException; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 39 | import android.util.Log; |
| 40 | import android.view.LayoutInflater; |
alision | d8c8388 | 2013-05-17 17:00:42 -0400 | [diff] [blame] | 41 | import android.view.Menu; |
| 42 | import android.view.MenuInflater; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 43 | import android.view.View; |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 44 | import android.view.View.OnClickListener; |
alision | 43a9b36 | 2013-05-01 16:30:15 -0400 | [diff] [blame] | 45 | import android.view.ViewGroup; |
alision | 43a9b36 | 2013-05-01 16:30:15 -0400 | [diff] [blame] | 46 | import android.widget.Button; |
Alexandre Lision | 573045c | 2013-09-11 17:20:25 -0400 | [diff] [blame^] | 47 | import android.widget.ListView; |
Alexandre Lision | c51ccb1 | 2013-09-11 16:00:30 -0400 | [diff] [blame] | 48 | import android.widget.TextView; |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 49 | import android.widget.Toast; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 50 | |
| 51 | import com.savoirfairelinux.sflphone.R; |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 52 | import com.savoirfairelinux.sflphone.model.Conference; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 53 | import com.savoirfairelinux.sflphone.model.SipCall; |
| 54 | import com.savoirfairelinux.sflphone.service.ISipService; |
| 55 | |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 56 | public class HomeFragment extends Fragment { |
alision | 55c36cb | 2013-06-14 14:57:38 -0400 | [diff] [blame] | 57 | private static final String TAG = HomeFragment.class.getSimpleName(); |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 58 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 59 | private Callbacks mCallbacks = sDummyCallbacks; |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 60 | Button access_calls; |
Alexandre Lision | c51ccb1 | 2013-09-11 16:00:30 -0400 | [diff] [blame] | 61 | TextView nb_calls, nb_confs; |
Alexandre Lision | 573045c | 2013-09-11 17:20:25 -0400 | [diff] [blame^] | 62 | ListView list_calls, list_confs; |
alision | 43a9b36 | 2013-05-01 16:30:15 -0400 | [diff] [blame] | 63 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 64 | /** |
| 65 | * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity. |
| 66 | */ |
| 67 | private static Callbacks sDummyCallbacks = new Callbacks() { |
alision | 371b77e | 2013-04-23 14:51:26 -0400 | [diff] [blame] | 68 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 69 | @Override |
| 70 | public ISipService getService() { |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 71 | Log.i(TAG, "I'm a dummy"); |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 72 | return null; |
| 73 | } |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 74 | |
| 75 | @Override |
| 76 | public void resumeCallActivity() { |
| 77 | } |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 78 | }; |
alision | 371b77e | 2013-04-23 14:51:26 -0400 | [diff] [blame] | 79 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 80 | /** |
| 81 | * The Activity calling this fragment has to implement this interface |
| 82 | * |
| 83 | */ |
| 84 | public interface Callbacks { |
alision | 371b77e | 2013-04-23 14:51:26 -0400 | [diff] [blame] | 85 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 86 | public ISipService getService(); |
alision | 43a9b36 | 2013-05-01 16:30:15 -0400 | [diff] [blame] | 87 | |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 88 | public void resumeCallActivity(); |
| 89 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 90 | } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 91 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 92 | @Override |
| 93 | public void onAttach(Activity activity) { |
| 94 | super.onAttach(activity); |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 95 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 96 | if (!(activity instanceof Callbacks)) { |
| 97 | throw new IllegalStateException("Activity must implement fragment's callbacks."); |
| 98 | } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 99 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 100 | mCallbacks = (Callbacks) activity; |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 101 | |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 102 | } |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 103 | |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 104 | @Override |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 105 | public void onResume() { |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 106 | super.onResume(); |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 107 | if (mCallbacks.getService() != null) { |
| 108 | try { |
| 109 | HashMap<String, SipCall> calls = (HashMap<String, SipCall>) mCallbacks.getService().getCallList(); |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 110 | HashMap<String, Conference> confs = (HashMap<String, Conference>) mCallbacks.getService().getConferenceList(); |
Alexandre Lision | 573045c | 2013-09-11 17:20:25 -0400 | [diff] [blame^] | 111 | |
| 112 | updateCallList(calls); |
| 113 | updateConferenceList(confs); |
| 114 | |
Alexandre Lision | c51ccb1 | 2013-09-11 16:00:30 -0400 | [diff] [blame] | 115 | if(!calls.isEmpty() || !confs.isEmpty()){ |
| 116 | access_calls.setVisibility(View.VISIBLE); |
| 117 | } else { |
| 118 | access_calls.setVisibility(View.GONE); |
| 119 | } |
| 120 | // access_calls.setText(calls.size() + " on going calls and "+confs.size()+" conferences"); |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 121 | |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 122 | } catch (RemoteException e) { |
| 123 | Log.e(TAG, e.toString()); |
| 124 | } |
| 125 | } |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 126 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 127 | } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 128 | |
Alexandre Lision | 573045c | 2013-09-11 17:20:25 -0400 | [diff] [blame^] | 129 | private void updateConferenceList(HashMap<String, Conference> confs) { |
| 130 | nb_confs.setText(""+confs.size()); |
| 131 | } |
| 132 | |
| 133 | private void updateCallList(HashMap<String, SipCall> calls) { |
| 134 | nb_calls.setText(""+calls.size()); |
| 135 | |
| 136 | } |
| 137 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 138 | @Override |
| 139 | public void onDetach() { |
| 140 | super.onDetach(); |
| 141 | mCallbacks = sDummyCallbacks; |
| 142 | } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 143 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 144 | @Override |
| 145 | public void onCreate(Bundle savedInstanceState) { |
| 146 | super.onCreate(savedInstanceState); |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 147 | // mAdapter = new CallElementAdapter(getActivity(), new ArrayList<SipCall>()); |
| 148 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 149 | } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 150 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 151 | @Override |
| 152 | public void onActivityCreated(Bundle savedInstanceState) { |
| 153 | super.onActivityCreated(savedInstanceState); |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 154 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 155 | // Give some text to display if there is no data. In a real |
| 156 | // application this would come from a resource. |
| 157 | // setEmptyText("No phone numbers"); |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 158 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 159 | // We have a menu item to show in action bar. |
| 160 | setHasOptionsMenu(true); |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 161 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 162 | } |
alision | 371b77e | 2013-04-23 14:51:26 -0400 | [diff] [blame] | 163 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 164 | @Override |
| 165 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
| 166 | inflater.inflate(R.menu.call_element_menu, menu); |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 167 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 168 | } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 169 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 170 | @Override |
| 171 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 172 | Log.i(TAG, "onCreateView"); |
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 173 | View inflatedView = inflater.inflate(R.layout.frag_home, container, false); |
alision | 43a9b36 | 2013-05-01 16:30:15 -0400 | [diff] [blame] | 174 | |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 175 | access_calls = (Button) inflatedView.findViewById(R.id.access_callactivity); |
| 176 | |
Alexandre Lision | c51ccb1 | 2013-09-11 16:00:30 -0400 | [diff] [blame] | 177 | nb_calls = (TextView) inflatedView.findViewById(R.id.calls_counter); |
| 178 | nb_confs = (TextView) inflatedView.findViewById(R.id.confs_counter); |
Alexandre Lision | 573045c | 2013-09-11 17:20:25 -0400 | [diff] [blame^] | 179 | list_calls = (ListView) inflatedView.findViewById(R.id.calls_list); |
| 180 | list_confs = (ListView) inflatedView.findViewById(R.id.confs_list); |
| 181 | |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 182 | access_calls.setOnClickListener(new OnClickListener() { |
| 183 | |
| 184 | @Override |
| 185 | public void onClick(View v) { |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 186 | |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 187 | try { |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 188 | HashMap<String, SipCall> calls = (HashMap<String, SipCall>) mCallbacks.getService().getCallList(); |
| 189 | HashMap<String, Conference> confs = (HashMap<String, Conference>) mCallbacks.getService().getConferenceList(); |
| 190 | if (calls.isEmpty() && confs.isEmpty()) { |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 191 | Toast.makeText(getActivity(), "No calls", Toast.LENGTH_SHORT).show(); |
| 192 | } else { |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 193 | |
| 194 | mCallbacks.resumeCallActivity(); |
| 195 | |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 196 | } |
| 197 | } catch (RemoteException e) { |
| 198 | Log.e(TAG, e.toString()); |
| 199 | } |
| 200 | |
| 201 | } |
| 202 | }); |
alision | 43a9b36 | 2013-05-01 16:30:15 -0400 | [diff] [blame] | 203 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 204 | return inflatedView; |
| 205 | } |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 206 | |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 207 | } |