blob: dfc190e4f50299993509d261259f20cc6bbb9f75 [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;
alision2cb99562013-05-30 17:02:20 -040047import android.widget.Toast;
alisionf76de3b2013-04-16 15:35:22 -040048
49import com.savoirfairelinux.sflphone.R;
alisionf76de3b2013-04-16 15:35:22 -040050import com.savoirfairelinux.sflphone.model.SipCall;
51import com.savoirfairelinux.sflphone.service.ISipService;
52
53/**
54 * Main list of Call Elements. We don't manage contacts ourself so they are
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
alisiona4325152013-04-19 11:10:03 -040059
alision371b77e2013-04-23 14:51:26 -040060
alision9f7a6ec2013-05-24 16:26:26 -040061 private Callbacks mCallbacks = sDummyCallbacks;
alision2cb99562013-05-30 17:02:20 -040062 Button access_calls;
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() {
68 @Override
69 public void onCallSelected(SipCall c) {
70 }
alision371b77e2013-04-23 14:51:26 -040071
alision9f7a6ec2013-05-24 16:26:26 -040072 @Override
73 public ISipService getService() {
alision2cb99562013-05-30 17:02:20 -040074 Log.i(TAG, "I'm a dummy");
alision9f7a6ec2013-05-24 16:26:26 -040075 return null;
76 }
77 };
alision371b77e2013-04-23 14:51:26 -040078
alision9f7a6ec2013-05-24 16:26:26 -040079 /**
80 * The Activity calling this fragment has to implement this interface
81 *
82 */
83 public interface Callbacks {
84 public void onCallSelected(SipCall c);
alision371b77e2013-04-23 14:51:26 -040085
alision9f7a6ec2013-05-24 16:26:26 -040086 public ISipService getService();
alision43a9b362013-05-01 16:30:15 -040087
alision9f7a6ec2013-05-24 16:26:26 -040088 }
alisionf76de3b2013-04-16 15:35:22 -040089
alision9f7a6ec2013-05-24 16:26:26 -040090 @Override
91 public void onAttach(Activity activity) {
92 super.onAttach(activity);
alisionf76de3b2013-04-16 15:35:22 -040093
alision9f7a6ec2013-05-24 16:26:26 -040094 if (!(activity instanceof Callbacks)) {
95 throw new IllegalStateException("Activity must implement fragment's callbacks.");
96 }
alisionf76de3b2013-04-16 15:35:22 -040097
alision9f7a6ec2013-05-24 16:26:26 -040098 mCallbacks = (Callbacks) activity;
alision2cb99562013-05-30 17:02:20 -040099
100
101 }
102
103 @Override
104 public void onResume(){
105 super.onResume();
alision2cb99562013-05-30 17:02:20 -0400106 if (mCallbacks.getService() != null) {
107 try {
108 HashMap<String, SipCall> calls = (HashMap<String, SipCall>) mCallbacks.getService().getCallList();
109 Log.i(TAG, "Call size "+calls.size());
110 access_calls.setText(calls.size()+" on going calls");
111
112
113 } catch (RemoteException e) {
114 Log.e(TAG, e.toString());
115 }
116 }
117
alision9f7a6ec2013-05-24 16:26:26 -0400118 }
alisionf76de3b2013-04-16 15:35:22 -0400119
alision9f7a6ec2013-05-24 16:26:26 -0400120 @Override
121 public void onDetach() {
122 super.onDetach();
123 mCallbacks = sDummyCallbacks;
124 }
alisionf76de3b2013-04-16 15:35:22 -0400125
alision9f7a6ec2013-05-24 16:26:26 -0400126 /**
127 * Runnable that fill information in a contact card asynchroniously.
128 */
129 /*
130 * public static class InfosLoader implements Runnable { private View view; private long cid; private ContentResolver cr;
131 *
132 * public InfosLoader(Context context, View element, long contact_id) { cid = contact_id; cr = context.getContentResolver(); view = element; }
133 *
134 * public static Bitmap loadContactPhoto(ContentResolver cr, long id) { Uri uri =
135 * ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id); InputStream input =
136 * ContactsContract.Contacts.openContactPhotoInputStream(cr, uri); if (input == null) { return null; } return BitmapFactory.decodeStream(input); }
137 *
138 * @Override public void run() { final Bitmap photo_bmp = loadContactPhoto(cr, cid);
139 *
140 * Cursor phones = cr.query(CommonDataKinds.Phone.CONTENT_URI, CONTACTS_PHONES_PROJECTION, CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]
141 * { Long.toString(cid) }, null);
142 *
143 * final List<String> numbers = new ArrayList<String>(); while (phones.moveToNext()) { String number =
144 * phones.getString(phones.getColumnIndex(CommonDataKinds.Phone.NUMBER)); // int type =
145 * phones.getInt(phones.getColumnIndex(CommonDataKinds.Phone.TYPE)); numbers.add(number); } phones.close(); // TODO: same for SIP adresses.
146 *
147 * final Bitmap bmp = photo_bmp; view.post(new Runnable() {
148 *
149 * @Override public void run() { } }); } }
150 */
alisionf76de3b2013-04-16 15:35:22 -0400151
alisionf76de3b2013-04-16 15:35:22 -0400152
alision9f7a6ec2013-05-24 16:26:26 -0400153 // public void removeCall(SipCall c) {
154 // Log.i(TAG, "Removing call " + c.mCallInfo.mDisplayName);
155 // mAdapter.remove(c);
156 // }
alisionf76de3b2013-04-16 15:35:22 -0400157
alision9f7a6ec2013-05-24 16:26:26 -0400158 @Override
159 public void onCreate(Bundle savedInstanceState) {
160 super.onCreate(savedInstanceState);
alision907bde72013-06-20 14:40:37 -0400161// mAdapter = new CallElementAdapter(getActivity(), new ArrayList<SipCall>());
alision2cb99562013-05-30 17:02:20 -0400162
alision9f7a6ec2013-05-24 16:26:26 -0400163 }
alisionf76de3b2013-04-16 15:35:22 -0400164
alision9f7a6ec2013-05-24 16:26:26 -0400165 @Override
166 public void onActivityCreated(Bundle savedInstanceState) {
167 super.onActivityCreated(savedInstanceState);
alisionf76de3b2013-04-16 15:35:22 -0400168
alision9f7a6ec2013-05-24 16:26:26 -0400169 // Give some text to display if there is no data. In a real
170 // application this would come from a resource.
171 // setEmptyText("No phone numbers");
alisionf76de3b2013-04-16 15:35:22 -0400172
alision9f7a6ec2013-05-24 16:26:26 -0400173 // We have a menu item to show in action bar.
174 setHasOptionsMenu(true);
alisionf76de3b2013-04-16 15:35:22 -0400175
alisionb1763882013-06-18 17:30:51 -0400176
alision371b77e2013-04-23 14:51:26 -0400177
alision9f7a6ec2013-05-24 16:26:26 -0400178 }
alision371b77e2013-04-23 14:51:26 -0400179
alision9f7a6ec2013-05-24 16:26:26 -0400180 @Override
181 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
182 inflater.inflate(R.menu.call_element_menu, menu);
alisionf76de3b2013-04-16 15:35:22 -0400183
alision9f7a6ec2013-05-24 16:26:26 -0400184 }
alisionf76de3b2013-04-16 15:35:22 -0400185
alision55c36cb2013-06-14 14:57:38 -0400186// private static final int REQUEST_CODE_PREFERENCES = 1;
187//
188// @Override
189// public boolean onOptionsItemSelected(MenuItem item) {
190// Log.i(TAG, "onOptionsItemSelected " + item.getItemId());
191// switch (item.getItemId()) {
192// case R.id.menu_settings:
193// Intent launchPreferencesIntent = new Intent().setClass(getActivity(), SFLPhonePreferenceActivity.class);
194// startActivityForResult(launchPreferencesIntent, SFLPhoneHomeActivity.REQUEST_CODE_PREFERENCES);
195// break;
196// }
197//
198// return super.onOptionsItemSelected(item);
199// }
alision43a9b362013-05-01 16:30:15 -0400200
alision907bde72013-06-20 14:40:37 -0400201
alision43a9b362013-05-01 16:30:15 -0400202
alision9f7a6ec2013-05-24 16:26:26 -0400203 @Override
204 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
205 Log.i(TAG, "onCreateView");
206 View inflatedView = inflater.inflate(R.layout.frag_call_element, container, false);
alision43a9b362013-05-01 16:30:15 -0400207
alision2cb99562013-05-30 17:02:20 -0400208 access_calls = (Button) inflatedView.findViewById(R.id.access_callactivity);
209
210 access_calls.setOnClickListener(new OnClickListener() {
211
212 @Override
213 public void onClick(View v) {
214 HashMap<String, SipCall> calls;
215 try {
216 calls = (HashMap<String, SipCall>) mCallbacks.getService().getCallList();
217 if (calls.size() == 0) {
218 Toast.makeText(getActivity(), "No calls", Toast.LENGTH_SHORT).show();
219 } else {
220 mCallbacks.onCallSelected((SipCall) calls.values().toArray()[0]);
221 }
222 } catch (RemoteException e) {
223 Log.e(TAG, e.toString());
224 }
225
226 }
227 });
228
229
230
alision9f7a6ec2013-05-24 16:26:26 -0400231 // ((Button) inflatedView.findViewById(R.id.button_attended)).setOnClickListener(new OnClickListener() {
232 //
233 // @Override
234 // public void onClick(View v) {
235 // if (mAdapter.getCount() == 2) {
236 // try {
237 // service.attendedTransfer(mAdapter.getItem(0).getCallId(), mAdapter.getItem(1).getCallId());
238 // mAdapter.clear();
239 // } catch (RemoteException e) {
240 // Log.e(TAG, e.toString());
241 // }
242 // } else {
243 // Toast.makeText(getActivity(), "You need two calls one on Hold the other current to bind them", Toast.LENGTH_LONG).show();
244 // }
245 //
246 // }
247 // });
alision43a9b362013-05-01 16:30:15 -0400248
alision9f7a6ec2013-05-24 16:26:26 -0400249 // ((Button) inflatedView.findViewById(R.id.button_conf)).setOnClickListener(new OnClickListener() {
250 //
251 // @Override
252 // public void onClick(View v) {
253 // if (mAdapter.getCount() == 2) {
254 // try {
255 // service.joinParticipant(mAdapter.getItem(0).getCallId(), mAdapter.getItem(1).getCallId());
256 // } catch (RemoteException e) {
257 // Log.e(TAG, e.toString());
258 // }
259 // } else {
260 // Toast.makeText(getActivity(), "You need two calls one on Hold the other current to create a conference", Toast.LENGTH_LONG)
261 // .show();
262 // }
263 // }
264 // });
alision43a9b362013-05-01 16:30:15 -0400265
alision9f7a6ec2013-05-24 16:26:26 -0400266 // ((ToggleButton) inflatedView.findViewById(R.id.switch_hold)).setOnCheckedChangeListener(new OnCheckedChangeListener() {
267 //
268 // @Override
269 // public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
270 // try {
271 // ArrayList<String> confList = (ArrayList<String>) service.getConferenceList();
272 // if (!confList.isEmpty()) {
273 // if (isChecked) {
274 // service.holdConference(confList.get(0));
275 // } else {
276 // service.unholdConference(confList.get(0));
277 // }
278 // }
279 // } catch (RemoteException e) {
280 // Log.e(TAG, e.toString());
281 // }
282 //
283 // }
284 // });
alision43a9b362013-05-01 16:30:15 -0400285
alision9f7a6ec2013-05-24 16:26:26 -0400286 return inflatedView;
287 }
alisione2a38e12013-04-25 14:20:20 -0400288
alision371b77e2013-04-23 14:51:26 -0400289
alisionf76de3b2013-04-16 15:35:22 -0400290}