blob: 888a92f5ff9844ee59e539a2fd7d7775bdd6b4ed [file] [log] [blame]
alisionf76de3b2013-04-16 15:35:22 -04001/*
alision2ec64f92013-06-17 17:28:58 -04002 * Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
alisionf76de3b2013-04-16 15:35:22 -04003 *
alision2ec64f92013-06-17 17:28:58 -04004 * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
alisionf76de3b2013-04-16 15:35:22 -04005 *
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 */
31package com.savoirfairelinux.sflphone.fragments;
32
alision2cb99562013-05-30 17:02:20 -040033import java.util.HashMap;
alisionf76de3b2013-04-16 15:35:22 -040034
35import android.app.Activity;
alisionb1763882013-06-18 17:30:51 -040036import android.app.Fragment;
alisionf76de3b2013-04-16 15:35:22 -040037import android.os.Bundle;
alision2cb99562013-05-30 17:02:20 -040038import android.os.RemoteException;
alisionf76de3b2013-04-16 15:35:22 -040039import android.util.Log;
40import android.view.LayoutInflater;
alisiond8c83882013-05-17 17:00:42 -040041import android.view.Menu;
42import android.view.MenuInflater;
alisionf76de3b2013-04-16 15:35:22 -040043import android.view.View;
alision2cb99562013-05-30 17:02:20 -040044import android.view.View.OnClickListener;
alision43a9b362013-05-01 16:30:15 -040045import android.view.ViewGroup;
alision43a9b362013-05-01 16:30:15 -040046import android.widget.Button;
Alexandre Lision573045c2013-09-11 17:20:25 -040047import android.widget.ListView;
Alexandre Lisionc51ccb12013-09-11 16:00:30 -040048import android.widget.TextView;
alision2cb99562013-05-30 17:02:20 -040049import android.widget.Toast;
alisionf76de3b2013-04-16 15:35:22 -040050
51import com.savoirfairelinux.sflphone.R;
alisiondf1dac92013-06-27 17:35:53 -040052import com.savoirfairelinux.sflphone.model.Conference;
alisionf76de3b2013-04-16 15:35:22 -040053import com.savoirfairelinux.sflphone.model.SipCall;
54import com.savoirfairelinux.sflphone.service.ISipService;
55
alisionb1763882013-06-18 17:30:51 -040056public class HomeFragment extends Fragment {
alision55c36cb2013-06-14 14:57:38 -040057 private static final String TAG = HomeFragment.class.getSimpleName();
alision907bde72013-06-20 14:40:37 -040058
alision9f7a6ec2013-05-24 16:26:26 -040059 private Callbacks mCallbacks = sDummyCallbacks;
alision2cb99562013-05-30 17:02:20 -040060 Button access_calls;
Alexandre Lisionc51ccb12013-09-11 16:00:30 -040061 TextView nb_calls, nb_confs;
Alexandre Lision573045c2013-09-11 17:20:25 -040062 ListView list_calls, list_confs;
alision43a9b362013-05-01 16:30:15 -040063
alision9f7a6ec2013-05-24 16:26:26 -040064 /**
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() {
alision371b77e2013-04-23 14:51:26 -040068
alision9f7a6ec2013-05-24 16:26:26 -040069 @Override
70 public ISipService getService() {
alision2cb99562013-05-30 17:02:20 -040071 Log.i(TAG, "I'm a dummy");
alision9f7a6ec2013-05-24 16:26:26 -040072 return null;
73 }
alisiondf1dac92013-06-27 17:35:53 -040074
75 @Override
76 public void resumeCallActivity() {
77 }
alision9f7a6ec2013-05-24 16:26:26 -040078 };
alision371b77e2013-04-23 14:51:26 -040079
alision9f7a6ec2013-05-24 16:26:26 -040080 /**
81 * The Activity calling this fragment has to implement this interface
82 *
83 */
84 public interface Callbacks {
alision371b77e2013-04-23 14:51:26 -040085
alision9f7a6ec2013-05-24 16:26:26 -040086 public ISipService getService();
alision43a9b362013-05-01 16:30:15 -040087
alisiondf1dac92013-06-27 17:35:53 -040088 public void resumeCallActivity();
89
alision9f7a6ec2013-05-24 16:26:26 -040090 }
alisionf76de3b2013-04-16 15:35:22 -040091
alision9f7a6ec2013-05-24 16:26:26 -040092 @Override
93 public void onAttach(Activity activity) {
94 super.onAttach(activity);
alisionf76de3b2013-04-16 15:35:22 -040095
alision9f7a6ec2013-05-24 16:26:26 -040096 if (!(activity instanceof Callbacks)) {
97 throw new IllegalStateException("Activity must implement fragment's callbacks.");
98 }
alisionf76de3b2013-04-16 15:35:22 -040099
alision9f7a6ec2013-05-24 16:26:26 -0400100 mCallbacks = (Callbacks) activity;
alisiondf1dac92013-06-27 17:35:53 -0400101
alision2cb99562013-05-30 17:02:20 -0400102 }
alisiondf1dac92013-06-27 17:35:53 -0400103
alision2cb99562013-05-30 17:02:20 -0400104 @Override
alisiondf1dac92013-06-27 17:35:53 -0400105 public void onResume() {
alision2cb99562013-05-30 17:02:20 -0400106 super.onResume();
alision2cb99562013-05-30 17:02:20 -0400107 if (mCallbacks.getService() != null) {
108 try {
109 HashMap<String, SipCall> calls = (HashMap<String, SipCall>) mCallbacks.getService().getCallList();
alisiondf1dac92013-06-27 17:35:53 -0400110 HashMap<String, Conference> confs = (HashMap<String, Conference>) mCallbacks.getService().getConferenceList();
Alexandre Lision573045c2013-09-11 17:20:25 -0400111
112 updateCallList(calls);
113 updateConferenceList(confs);
114
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400115 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");
alision2cb99562013-05-30 17:02:20 -0400121
alision2cb99562013-05-30 17:02:20 -0400122 } catch (RemoteException e) {
123 Log.e(TAG, e.toString());
124 }
125 }
alisiondf1dac92013-06-27 17:35:53 -0400126
alision9f7a6ec2013-05-24 16:26:26 -0400127 }
alisionf76de3b2013-04-16 15:35:22 -0400128
Alexandre Lision573045c2013-09-11 17:20:25 -0400129 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
alision9f7a6ec2013-05-24 16:26:26 -0400138 @Override
139 public void onDetach() {
140 super.onDetach();
141 mCallbacks = sDummyCallbacks;
142 }
alisionf76de3b2013-04-16 15:35:22 -0400143
alision9f7a6ec2013-05-24 16:26:26 -0400144 @Override
145 public void onCreate(Bundle savedInstanceState) {
146 super.onCreate(savedInstanceState);
alisiondf1dac92013-06-27 17:35:53 -0400147 // mAdapter = new CallElementAdapter(getActivity(), new ArrayList<SipCall>());
148
alision9f7a6ec2013-05-24 16:26:26 -0400149 }
alisionf76de3b2013-04-16 15:35:22 -0400150
alision9f7a6ec2013-05-24 16:26:26 -0400151 @Override
152 public void onActivityCreated(Bundle savedInstanceState) {
153 super.onActivityCreated(savedInstanceState);
alisionf76de3b2013-04-16 15:35:22 -0400154
alision9f7a6ec2013-05-24 16:26:26 -0400155 // 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");
alisionf76de3b2013-04-16 15:35:22 -0400158
alision9f7a6ec2013-05-24 16:26:26 -0400159 // We have a menu item to show in action bar.
160 setHasOptionsMenu(true);
alisionf76de3b2013-04-16 15:35:22 -0400161
alision9f7a6ec2013-05-24 16:26:26 -0400162 }
alision371b77e2013-04-23 14:51:26 -0400163
alision9f7a6ec2013-05-24 16:26:26 -0400164 @Override
165 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
166 inflater.inflate(R.menu.call_element_menu, menu);
alisionf76de3b2013-04-16 15:35:22 -0400167
alision9f7a6ec2013-05-24 16:26:26 -0400168 }
alisionf76de3b2013-04-16 15:35:22 -0400169
alision9f7a6ec2013-05-24 16:26:26 -0400170 @Override
171 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
172 Log.i(TAG, "onCreateView");
alision465ceba2013-07-04 09:24:30 -0400173 View inflatedView = inflater.inflate(R.layout.frag_home, container, false);
alision43a9b362013-05-01 16:30:15 -0400174
alision2cb99562013-05-30 17:02:20 -0400175 access_calls = (Button) inflatedView.findViewById(R.id.access_callactivity);
176
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400177 nb_calls = (TextView) inflatedView.findViewById(R.id.calls_counter);
178 nb_confs = (TextView) inflatedView.findViewById(R.id.confs_counter);
Alexandre Lision573045c2013-09-11 17:20:25 -0400179 list_calls = (ListView) inflatedView.findViewById(R.id.calls_list);
180 list_confs = (ListView) inflatedView.findViewById(R.id.confs_list);
181
alision2cb99562013-05-30 17:02:20 -0400182 access_calls.setOnClickListener(new OnClickListener() {
183
184 @Override
185 public void onClick(View v) {
alisiondf1dac92013-06-27 17:35:53 -0400186
alision2cb99562013-05-30 17:02:20 -0400187 try {
alisiondf1dac92013-06-27 17:35:53 -0400188 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()) {
alision2cb99562013-05-30 17:02:20 -0400191 Toast.makeText(getActivity(), "No calls", Toast.LENGTH_SHORT).show();
192 } else {
alisiondf1dac92013-06-27 17:35:53 -0400193
194 mCallbacks.resumeCallActivity();
195
alision2cb99562013-05-30 17:02:20 -0400196 }
197 } catch (RemoteException e) {
198 Log.e(TAG, e.toString());
199 }
200
201 }
202 });
alision43a9b362013-05-01 16:30:15 -0400203
alision9f7a6ec2013-05-24 16:26:26 -0400204 return inflatedView;
205 }
alisione2a38e12013-04-25 14:20:20 -0400206
alisionf76de3b2013-04-16 15:35:22 -0400207}