blob: a3dcdd7c1b35fde19b663f769412a4a3b52b95e7 [file] [log] [blame]
alisionf76de3b2013-04-16 15:35:22 -04001/*
2 * Copyright (C) 2004-2012 Savoir-Faire Linux Inc.
3 *
4 * Author: Adrien Beraud <adrien.beraud@gmail.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 */
31package com.savoirfairelinux.sflphone.fragments;
32
alisionf76de3b2013-04-16 15:35:22 -040033import java.util.ArrayList;
alision2cb99562013-05-30 17:02:20 -040034import java.util.HashMap;
alisionf76de3b2013-04-16 15:35:22 -040035
36import android.app.Activity;
37import android.app.AlertDialog;
38import android.app.ListFragment;
alisionf76de3b2013-04-16 15:35:22 -040039import android.content.Context;
alisionf76de3b2013-04-16 15:35:22 -040040import android.content.DialogInterface;
alision371b77e2013-04-23 14:51:26 -040041import android.content.Intent;
alisionf76de3b2013-04-16 15:35:22 -040042import android.os.Bundle;
alision2cb99562013-05-30 17:02:20 -040043import android.os.RemoteException;
alisionf76de3b2013-04-16 15:35:22 -040044import android.util.Log;
45import android.view.LayoutInflater;
alisiond8c83882013-05-17 17:00:42 -040046import android.view.Menu;
47import android.view.MenuInflater;
48import android.view.MenuItem;
alisionf76de3b2013-04-16 15:35:22 -040049import android.view.View;
alision2cb99562013-05-30 17:02:20 -040050import android.view.View.OnClickListener;
alision43a9b362013-05-01 16:30:15 -040051import android.view.ViewGroup;
alisionf76de3b2013-04-16 15:35:22 -040052import android.widget.AdapterView;
alision371b77e2013-04-23 14:51:26 -040053import android.widget.AdapterView.OnItemClickListener;
alisionf76de3b2013-04-16 15:35:22 -040054import android.widget.AdapterView.OnItemLongClickListener;
alision43a9b362013-05-01 16:30:15 -040055import android.widget.Button;
alisionf76de3b2013-04-16 15:35:22 -040056import android.widget.ListView;
alision2cb99562013-05-30 17:02:20 -040057import android.widget.Toast;
alisionf76de3b2013-04-16 15:35:22 -040058
59import com.savoirfairelinux.sflphone.R;
alisiona4325152013-04-19 11:10:03 -040060import com.savoirfairelinux.sflphone.adapters.CallElementAdapter;
alision2cb99562013-05-30 17:02:20 -040061import com.savoirfairelinux.sflphone.client.SFLPhoneHomeActivity;
alisiond8c83882013-05-17 17:00:42 -040062import com.savoirfairelinux.sflphone.client.SFLPhonePreferenceActivity;
alisionf76de3b2013-04-16 15:35:22 -040063import com.savoirfairelinux.sflphone.model.SipCall;
64import com.savoirfairelinux.sflphone.service.ISipService;
65
66/**
67 * Main list of Call Elements. We don't manage contacts ourself so they are
68 */
alision55c36cb2013-06-14 14:57:38 -040069public class HomeFragment extends ListFragment {
70 private static final String TAG = HomeFragment.class.getSimpleName();
alision9f7a6ec2013-05-24 16:26:26 -040071 private CallElementAdapter mAdapter;
alisiona4325152013-04-19 11:10:03 -040072
alision371b77e2013-04-23 14:51:26 -040073
alision9f7a6ec2013-05-24 16:26:26 -040074 private Callbacks mCallbacks = sDummyCallbacks;
alision2cb99562013-05-30 17:02:20 -040075 Button access_calls;
alision43a9b362013-05-01 16:30:15 -040076
alision9f7a6ec2013-05-24 16:26:26 -040077 /**
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
82 public void onCallSelected(SipCall c) {
83 }
alision371b77e2013-04-23 14:51:26 -040084
alision9f7a6ec2013-05-24 16:26:26 -040085 @Override
86 public ISipService getService() {
alision2cb99562013-05-30 17:02:20 -040087 Log.i(TAG, "I'm a dummy");
alision9f7a6ec2013-05-24 16:26:26 -040088 return null;
89 }
90 };
alision371b77e2013-04-23 14:51:26 -040091
alision9f7a6ec2013-05-24 16:26:26 -040092 /**
93 * The Activity calling this fragment has to implement this interface
94 *
95 */
96 public interface Callbacks {
97 public void onCallSelected(SipCall c);
alision371b77e2013-04-23 14:51:26 -040098
alision9f7a6ec2013-05-24 16:26:26 -040099 public ISipService getService();
alision43a9b362013-05-01 16:30:15 -0400100
alision9f7a6ec2013-05-24 16:26:26 -0400101 }
alisionf76de3b2013-04-16 15:35:22 -0400102
alision9f7a6ec2013-05-24 16:26:26 -0400103 @Override
104 public void onAttach(Activity activity) {
105 super.onAttach(activity);
alisionf76de3b2013-04-16 15:35:22 -0400106
alision9f7a6ec2013-05-24 16:26:26 -0400107 if (!(activity instanceof Callbacks)) {
108 throw new IllegalStateException("Activity must implement fragment's callbacks.");
109 }
alisionf76de3b2013-04-16 15:35:22 -0400110
alision9f7a6ec2013-05-24 16:26:26 -0400111 mCallbacks = (Callbacks) activity;
alision2cb99562013-05-30 17:02:20 -0400112
113
114 }
115
116 @Override
117 public void onResume(){
118 super.onResume();
alision2cb99562013-05-30 17:02:20 -0400119 if (mCallbacks.getService() != null) {
120 try {
121 HashMap<String, SipCall> calls = (HashMap<String, SipCall>) mCallbacks.getService().getCallList();
122 Log.i(TAG, "Call size "+calls.size());
123 access_calls.setText(calls.size()+" on going calls");
124
125
126 } catch (RemoteException e) {
127 Log.e(TAG, e.toString());
128 }
129 }
130
alision9f7a6ec2013-05-24 16:26:26 -0400131 }
alisionf76de3b2013-04-16 15:35:22 -0400132
alision9f7a6ec2013-05-24 16:26:26 -0400133 @Override
134 public void onDetach() {
135 super.onDetach();
136 mCallbacks = sDummyCallbacks;
137 }
alisionf76de3b2013-04-16 15:35:22 -0400138
alision9f7a6ec2013-05-24 16:26:26 -0400139 /**
140 * Runnable that fill information in a contact card asynchroniously.
141 */
142 /*
143 * public static class InfosLoader implements Runnable { private View view; private long cid; private ContentResolver cr;
144 *
145 * public InfosLoader(Context context, View element, long contact_id) { cid = contact_id; cr = context.getContentResolver(); view = element; }
146 *
147 * public static Bitmap loadContactPhoto(ContentResolver cr, long id) { Uri uri =
148 * ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id); InputStream input =
149 * ContactsContract.Contacts.openContactPhotoInputStream(cr, uri); if (input == null) { return null; } return BitmapFactory.decodeStream(input); }
150 *
151 * @Override public void run() { final Bitmap photo_bmp = loadContactPhoto(cr, cid);
152 *
153 * Cursor phones = cr.query(CommonDataKinds.Phone.CONTENT_URI, CONTACTS_PHONES_PROJECTION, CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]
154 * { Long.toString(cid) }, null);
155 *
156 * final List<String> numbers = new ArrayList<String>(); while (phones.moveToNext()) { String number =
157 * phones.getString(phones.getColumnIndex(CommonDataKinds.Phone.NUMBER)); // int type =
158 * phones.getInt(phones.getColumnIndex(CommonDataKinds.Phone.TYPE)); numbers.add(number); } phones.close(); // TODO: same for SIP adresses.
159 *
160 * final Bitmap bmp = photo_bmp; view.post(new Runnable() {
161 *
162 * @Override public void run() { } }); } }
163 */
alisionf76de3b2013-04-16 15:35:22 -0400164
alision9f7a6ec2013-05-24 16:26:26 -0400165 public void addCall(SipCall c) {
alision2cb99562013-05-30 17:02:20 -0400166 // Log.i(TAG, "Adding call " + c.mCallInfo.mDisplayName);
alision9f7a6ec2013-05-24 16:26:26 -0400167 if (mAdapter == null) {
168 Log.w(TAG, "mAdapter is null");
169 return;
170 }
171 mAdapter.add(c);
172 }
alisionf76de3b2013-04-16 15:35:22 -0400173
alision9f7a6ec2013-05-24 16:26:26 -0400174 // public void removeCall(SipCall c) {
175 // Log.i(TAG, "Removing call " + c.mCallInfo.mDisplayName);
176 // mAdapter.remove(c);
177 // }
alisionf76de3b2013-04-16 15:35:22 -0400178
alision9f7a6ec2013-05-24 16:26:26 -0400179 @Override
180 public void onCreate(Bundle savedInstanceState) {
181 super.onCreate(savedInstanceState);
182 mAdapter = new CallElementAdapter(getActivity(), new ArrayList<SipCall>());
alision2cb99562013-05-30 17:02:20 -0400183
alision9f7a6ec2013-05-24 16:26:26 -0400184 }
alisionf76de3b2013-04-16 15:35:22 -0400185
alision9f7a6ec2013-05-24 16:26:26 -0400186 @Override
187 public void onActivityCreated(Bundle savedInstanceState) {
188 super.onActivityCreated(savedInstanceState);
alisionf76de3b2013-04-16 15:35:22 -0400189
alision9f7a6ec2013-05-24 16:26:26 -0400190 // Give some text to display if there is no data. In a real
191 // application this would come from a resource.
192 // setEmptyText("No phone numbers");
alisionf76de3b2013-04-16 15:35:22 -0400193
alision9f7a6ec2013-05-24 16:26:26 -0400194 // We have a menu item to show in action bar.
195 setHasOptionsMenu(true);
alisionf76de3b2013-04-16 15:35:22 -0400196
alision9f7a6ec2013-05-24 16:26:26 -0400197 final Context context = getActivity();
198 ListView lv = getListView();
199 lv.setAdapter(mAdapter);
200 lv.setOnItemLongClickListener(new OnItemLongClickListener() {
201 @Override
202 public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
203 Log.i(TAG, "On Long Click");
204 final CharSequence[] items = { "Hang up Call", "Send Message", "Add to Conference" };
205 final SipCall call = mAdapter.getItem(pos);
206 // // FIXME
207 // service = sflphoneApplication.getSipService();
208 AlertDialog.Builder builder = new AlertDialog.Builder(context);
alisionfde875f2013-05-28 17:01:54 -0400209 builder.setTitle("Action to perform with " + call.getContacts().get(0).getmDisplayName()).setCancelable(true)
alision9f7a6ec2013-05-24 16:26:26 -0400210 .setItems(items, new DialogInterface.OnClickListener() {
211 @Override
212 public void onClick(DialogInterface dialog, int item) {
213 Log.i(TAG, "Selected " + items[item]);
alision2cb99562013-05-30 17:02:20 -0400214 // switch (item) {
215 // case 0:
216 // call.notifyServiceHangup(service);
217 // break;
218 // case 1:
219 // call.sendTextMessage();
220 // // Need to hangup this call immediately since no way to do it after this action
221 // call.notifyServiceHangup(service);
222 // break;
223 // case 2:
224 // call.addToConference();
225 // // Need to hangup this call immediately since no way to do it after this action
226 // call.notifyServiceHangup(service);
227 // break;
228 // default:
229 // break;
230 // }
alision9f7a6ec2013-05-24 16:26:26 -0400231 }
232 });
233 AlertDialog alert = builder.create();
234 alert.show();
alisionf76de3b2013-04-16 15:35:22 -0400235
alision9f7a6ec2013-05-24 16:26:26 -0400236 return true;
237 }
238 });
alision371b77e2013-04-23 14:51:26 -0400239
alision9f7a6ec2013-05-24 16:26:26 -0400240 lv.setOnItemClickListener(new OnItemClickListener() {
alision371b77e2013-04-23 14:51:26 -0400241
alision9f7a6ec2013-05-24 16:26:26 -0400242 @Override
243 public void onItemClick(AdapterView<?> arg0, View v, int pos, long arg3) {
244 mCallbacks.onCallSelected(mAdapter.getItem(pos));
alision371b77e2013-04-23 14:51:26 -0400245
alision9f7a6ec2013-05-24 16:26:26 -0400246 }
alision371b77e2013-04-23 14:51:26 -0400247
alision9f7a6ec2013-05-24 16:26:26 -0400248 });
249 }
alision371b77e2013-04-23 14:51:26 -0400250
alision9f7a6ec2013-05-24 16:26:26 -0400251 @Override
252 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
253 inflater.inflate(R.menu.call_element_menu, menu);
alisionf76de3b2013-04-16 15:35:22 -0400254
alision9f7a6ec2013-05-24 16:26:26 -0400255 }
alisionf76de3b2013-04-16 15:35:22 -0400256
alision55c36cb2013-06-14 14:57:38 -0400257// private static final int REQUEST_CODE_PREFERENCES = 1;
258//
259// @Override
260// public boolean onOptionsItemSelected(MenuItem item) {
261// Log.i(TAG, "onOptionsItemSelected " + item.getItemId());
262// switch (item.getItemId()) {
263// case R.id.menu_settings:
264// Intent launchPreferencesIntent = new Intent().setClass(getActivity(), SFLPhonePreferenceActivity.class);
265// startActivityForResult(launchPreferencesIntent, SFLPhoneHomeActivity.REQUEST_CODE_PREFERENCES);
266// break;
267// }
268//
269// return super.onOptionsItemSelected(item);
270// }
alision43a9b362013-05-01 16:30:15 -0400271
alision9f7a6ec2013-05-24 16:26:26 -0400272 public void updateCall(String iD, String newState) {
273 if (mAdapter == null) {
274 Log.w(TAG, "mAdapter is null");
275 return;
276 }
277 mAdapter.update(iD, newState);
278 }
alision43a9b362013-05-01 16:30:15 -0400279
alision9f7a6ec2013-05-24 16:26:26 -0400280 @Override
281 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
282 Log.i(TAG, "onCreateView");
283 View inflatedView = inflater.inflate(R.layout.frag_call_element, container, false);
alision43a9b362013-05-01 16:30:15 -0400284
alision2cb99562013-05-30 17:02:20 -0400285 access_calls = (Button) inflatedView.findViewById(R.id.access_callactivity);
286
287 access_calls.setOnClickListener(new OnClickListener() {
288
289 @Override
290 public void onClick(View v) {
291 HashMap<String, SipCall> calls;
292 try {
293 calls = (HashMap<String, SipCall>) mCallbacks.getService().getCallList();
294 if (calls.size() == 0) {
295 Toast.makeText(getActivity(), "No calls", Toast.LENGTH_SHORT).show();
296 } else {
297 mCallbacks.onCallSelected((SipCall) calls.values().toArray()[0]);
298 }
299 } catch (RemoteException e) {
300 Log.e(TAG, e.toString());
301 }
302
303 }
304 });
305
306
307
alision9f7a6ec2013-05-24 16:26:26 -0400308 // ((Button) inflatedView.findViewById(R.id.button_attended)).setOnClickListener(new OnClickListener() {
309 //
310 // @Override
311 // public void onClick(View v) {
312 // if (mAdapter.getCount() == 2) {
313 // try {
314 // service.attendedTransfer(mAdapter.getItem(0).getCallId(), mAdapter.getItem(1).getCallId());
315 // mAdapter.clear();
316 // } catch (RemoteException e) {
317 // Log.e(TAG, e.toString());
318 // }
319 // } else {
320 // Toast.makeText(getActivity(), "You need two calls one on Hold the other current to bind them", Toast.LENGTH_LONG).show();
321 // }
322 //
323 // }
324 // });
alision43a9b362013-05-01 16:30:15 -0400325
alision9f7a6ec2013-05-24 16:26:26 -0400326 // ((Button) inflatedView.findViewById(R.id.button_conf)).setOnClickListener(new OnClickListener() {
327 //
328 // @Override
329 // public void onClick(View v) {
330 // if (mAdapter.getCount() == 2) {
331 // try {
332 // service.joinParticipant(mAdapter.getItem(0).getCallId(), mAdapter.getItem(1).getCallId());
333 // } catch (RemoteException e) {
334 // Log.e(TAG, e.toString());
335 // }
336 // } else {
337 // Toast.makeText(getActivity(), "You need two calls one on Hold the other current to create a conference", Toast.LENGTH_LONG)
338 // .show();
339 // }
340 // }
341 // });
alision43a9b362013-05-01 16:30:15 -0400342
alision9f7a6ec2013-05-24 16:26:26 -0400343 // ((ToggleButton) inflatedView.findViewById(R.id.switch_hold)).setOnCheckedChangeListener(new OnCheckedChangeListener() {
344 //
345 // @Override
346 // public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
347 // try {
348 // ArrayList<String> confList = (ArrayList<String>) service.getConferenceList();
349 // if (!confList.isEmpty()) {
350 // if (isChecked) {
351 // service.holdConference(confList.get(0));
352 // } else {
353 // service.unholdConference(confList.get(0));
354 // }
355 // }
356 // } catch (RemoteException e) {
357 // Log.e(TAG, e.toString());
358 // }
359 //
360 // }
361 // });
alision43a9b362013-05-01 16:30:15 -0400362
alision9f7a6ec2013-05-24 16:26:26 -0400363 return inflatedView;
364 }
alisione2a38e12013-04-25 14:20:20 -0400365
alision371b77e2013-04-23 14:51:26 -0400366
alisionf76de3b2013-04-16 15:35:22 -0400367}