blob: fb9f83b7b1e34c3bbea04183203d7130cb206ae5 [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
33import java.io.InputStream;
34import java.util.ArrayList;
35import java.util.List;
alisionf76de3b2013-04-16 15:35:22 -040036
37import android.app.Activity;
38import android.app.AlertDialog;
39import android.app.ListFragment;
40import android.app.LoaderManager;
41import android.content.ContentResolver;
42import android.content.ContentUris;
43import android.content.Context;
44import android.content.CursorLoader;
45import android.content.DialogInterface;
46import android.content.Loader;
47import android.database.Cursor;
48import android.graphics.Bitmap;
49import android.graphics.BitmapFactory;
50import android.net.Uri;
51import android.os.Bundle;
alisiona4325152013-04-19 11:10:03 -040052import android.os.RemoteException;
alisionf76de3b2013-04-16 15:35:22 -040053import android.provider.ContactsContract;
54import android.provider.ContactsContract.CommonDataKinds;
55import android.provider.ContactsContract.CommonDataKinds.Phone;
56import android.provider.ContactsContract.CommonDataKinds.SipAddress;
57import android.provider.ContactsContract.Contacts;
58import android.util.Log;
59import android.view.LayoutInflater;
60import android.view.View;
61import android.view.ViewGroup;
62import android.widget.AdapterView;
63import android.widget.AdapterView.OnItemLongClickListener;
alisionf76de3b2013-04-16 15:35:22 -040064import android.widget.ListView;
alisionf76de3b2013-04-16 15:35:22 -040065
66import com.savoirfairelinux.sflphone.R;
alisionf76de3b2013-04-16 15:35:22 -040067import com.savoirfairelinux.sflphone.account.AccountSelectionSpinner;
alisiona4325152013-04-19 11:10:03 -040068import com.savoirfairelinux.sflphone.adapters.CallElementAdapter;
alisionf76de3b2013-04-16 15:35:22 -040069import com.savoirfairelinux.sflphone.client.SFLPhoneHomeActivity;
70import com.savoirfairelinux.sflphone.client.SFLphoneApplication;
alisiond9e29442013-04-17 16:10:18 -040071import com.savoirfairelinux.sflphone.client.receiver.AccountListReceiver;
alisionf76de3b2013-04-16 15:35:22 -040072import com.savoirfairelinux.sflphone.model.SipCall;
73import com.savoirfairelinux.sflphone.service.ISipService;
74
75/**
76 * Main list of Call Elements. We don't manage contacts ourself so they are
77 */
78public class CallElementListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {
alisiond9e29442013-04-17 16:10:18 -040079 private static final String TAG = CallElementListFragment.class.getSimpleName();
alisiona4325152013-04-19 11:10:03 -040080
81 // private ContactManager mContactManager;
alisionf76de3b2013-04-16 15:35:22 -040082 private CallElementAdapter mAdapter;
83 private String mCurFilter;
84 private SFLPhoneHomeActivity sflphoneHome;
alisiona4325152013-04-19 11:10:03 -040085 // private SFLphoneApplication sflphoneApplication;
alisionf76de3b2013-04-16 15:35:22 -040086 private ISipService service;
alisiona4325152013-04-19 11:10:03 -040087 private AccountSelectionSpinner mAccountSelectionSpinner;
88
alisionf76de3b2013-04-16 15:35:22 -040089 private AccountListReceiver mAccountList;
90
alisiona4325152013-04-19 11:10:03 -040091 private boolean isReady = false;
92
alisionf76de3b2013-04-16 15:35:22 -040093 static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { Contacts._ID, Contacts.DISPLAY_NAME, Contacts.PHOTO_ID, Contacts.LOOKUP_KEY };
94 static final String[] CONTACTS_PHONES_PROJECTION = new String[] { Phone.NUMBER, Phone.TYPE };
95 static final String[] CONTACTS_SIP_PROJECTION = new String[] { SipAddress.SIP_ADDRESS, SipAddress.TYPE };
96
97 @Override
98 public void onAttach(Activity activity) {
99 super.onAttach(activity);
100 sflphoneHome = (SFLPhoneHomeActivity) activity;
101 service = ((SFLphoneApplication) sflphoneHome.getApplication()).getSipService();
102 mAccountList = ((SFLphoneApplication) sflphoneHome.getApplication()).getAccountList();
103 Log.w(TAG, "onAttach() service=" + service + ", accountList=" + mAccountList);
104 }
105
106 public String getSelectedAccount() {
alisiond9e29442013-04-17 16:10:18 -0400107 // return mAccountSelectionButton.getText().toString();
alisionf76de3b2013-04-16 15:35:22 -0400108 return "CIOUCOU";
109 }
110
111 /**
112 * Runnable that fill information in a contact card asynchroniously.
113 */
114 public static class InfosLoader implements Runnable {
115 private View view;
116 private long cid;
117 private ContentResolver cr;
118
119 public InfosLoader(Context context, View element, long contact_id) {
120 cid = contact_id;
121 cr = context.getContentResolver();
122 view = element;
123 }
124
125 public static Bitmap loadContactPhoto(ContentResolver cr, long id) {
126 Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
127 InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
128 if (input == null) {
129 return null;
130 }
131 return BitmapFactory.decodeStream(input);
132 }
133
134 @Override
135 public void run() {
136 final Bitmap photo_bmp = loadContactPhoto(cr, cid);
137
138 Cursor phones = cr.query(CommonDataKinds.Phone.CONTENT_URI, CONTACTS_PHONES_PROJECTION, CommonDataKinds.Phone.CONTACT_ID + " = ?",
139 new String[] { Long.toString(cid) }, null);
140
141 final List<String> numbers = new ArrayList<String>();
142 while (phones.moveToNext()) {
143 String number = phones.getString(phones.getColumnIndex(CommonDataKinds.Phone.NUMBER));
144 // int type = phones.getInt(phones.getColumnIndex(CommonDataKinds.Phone.TYPE));
145 numbers.add(number);
146 }
147 phones.close();
148 // TODO: same for SIP adresses.
149
150 final Bitmap bmp = photo_bmp;
151 view.post(new Runnable() {
152 @Override
153 public void run() {
154 /*
155 * ImageView photo_view = (ImageView) view.findViewById(R.id.photo); TextView phones_txt = (TextView)
156 * view.findViewById(R.id.phones);
157 *
158 * if (photo_bmp != null) { photo_view.setImageBitmap(bmp); photo_view.setVisibility(View.VISIBLE); } else {
159 * photo_view.setVisibility(View.GONE); }
160 *
161 * if (numbers.size() > 0) { String phonestxt = numbers.get(0); for (int i = 1, n = numbers.size(); i < n; i++) phonestxt += "\n"
162 * + numbers.get(i); phones_txt.setText(phonestxt); phones_txt.setVisibility(View.VISIBLE); } else
163 * phones_txt.setVisibility(View.GONE);
164 */
165 }
166 });
167 }
168 }
169
alisionf76de3b2013-04-16 15:35:22 -0400170 public CallElementListFragment() {
171 super();
172 }
173
174 public void setAccountList(AccountListReceiver accountList) {
175 mAccountList = accountList;
176 }
177
178 public void addCall(SipCall c) {
179 Log.i(TAG, "Adding call " + c.mCallInfo.mDisplayName);
180 mAdapter.add(c);
181 }
182
183 public void removeCall(SipCall c) {
184 Log.i(TAG, "Removing call " + c.mCallInfo.mDisplayName);
185 mAdapter.remove(c);
186 }
187
188 @Override
189 public void onActivityCreated(Bundle savedInstanceState) {
190 super.onActivityCreated(savedInstanceState);
191
192 // Give some text to display if there is no data. In a real
193 // application this would come from a resource.
194 // setEmptyText("No phone numbers");
195
196 // We have a menu item to show in action bar.
197 setHasOptionsMenu(true);
198
199 // Create an empty adapter we will use to display the loaded data.
200 ArrayList calls = new ArrayList();
201 mAdapter = new CallElementAdapter(getActivity(), calls);
202 setListAdapter(mAdapter);
203
204 // Start out with a progress indicator.
205 // setListShown(false);
206
207 // Prepare the loader. Either re-connect with an existing one,
208 // or start a new one.
209 // getLoaderManager().initLoader(0, null, this)
210
211 final Context context = getActivity();
212 ListView lv = getListView();
213 lv.setOnItemLongClickListener(new OnItemLongClickListener() {
214 @Override
215 public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
216 Log.i(TAG, "On Long Click");
217 final CharSequence[] items = { "Hang up Call", "Send Message", "Add to Conference" };
218 final SipCall call = (SipCall) mAdapter.getItem(pos);
alisiona4325152013-04-19 11:10:03 -0400219 // // FIXME
220 // service = sflphoneApplication.getSipService();
alisionf76de3b2013-04-16 15:35:22 -0400221 AlertDialog.Builder builder = new AlertDialog.Builder(context);
222 builder.setTitle("Action to perform with " + call.mCallInfo.mDisplayName).setCancelable(true)
223 .setItems(items, new DialogInterface.OnClickListener() {
224 public void onClick(DialogInterface dialog, int item) {
225 Log.i(TAG, "Selected " + items[item]);
226 switch (item) {
227 case 0:
228 call.notifyServiceHangup(service);
229 break;
230 case 1:
231 call.sendTextMessage();
232 // Need to hangup this call immediately since no way to do it after this action
233 call.notifyServiceHangup(service);
234 break;
235 case 2:
236 call.addToConference();
237 // Need to hangup this call immediately since no way to do it after this action
238 call.notifyServiceHangup(service);
239 break;
240 default:
241 break;
242 }
243 }
244 });
245 AlertDialog alert = builder.create();
246 alert.show();
247
248 return true;
249 }
250 });
251 }
252
253 @Override
254 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
alisiona4325152013-04-19 11:10:03 -0400255 Log.i(TAG, "onCreateView");
alisionf76de3b2013-04-16 15:35:22 -0400256 View inflatedView = inflater.inflate(R.layout.frag_call_element, container, false);
257
alisiona4325152013-04-19 11:10:03 -0400258 mAccountSelectionSpinner = (AccountSelectionSpinner) inflatedView.findViewById(R.id.account_selection_button);
259 mAccountList.addManagementUI(mAccountSelectionSpinner);
260 mAccountSelectionSpinner.setAccountList(mAccountList);
alisionf76de3b2013-04-16 15:35:22 -0400261
alisiona4325152013-04-19 11:10:03 -0400262 isReady = true;
263 if (service != null) {
264 onServiceSipBinded(service);
265 }
alisionf76de3b2013-04-16 15:35:22 -0400266 return inflatedView;
267 }
268
269 public void onListItemClick(ListView l, View v, int position, long id) {
270 // Insert desired behavior here.
271 SipCall call = (SipCall) mAdapter.getItem(position);
272 Log.i(TAG, "Call Clicked: " + call.getCallId());
273
274 call.launchCallActivity(getActivity());
275
276 sflphoneHome.onSelectedCallAction(call);
277 }
278
279 @Override
280 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
281
alisiona4325152013-04-19 11:10:03 -0400282 Log.i(TAG, "onCreateLoader");
alisionf76de3b2013-04-16 15:35:22 -0400283 // return new CursorLoader(getActivity(), CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
284
285 // This is called when a new Loader needs to be created. This
286 // sample only has one Loader, so we don't care about the ID.
287 // First, pick the base URI to use depending on whether we are
288 // currently filtering.
289 Uri baseUri;
290
291 if (mCurFilter != null) {
292 baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(mCurFilter));
293 } else {
294 baseUri = Contacts.CONTENT_URI;
295 }
296
297 // Now create and return a CursorLoader that will take care of
298 // creating a Cursor for the data being displayed.
299 String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND (" + Contacts.HAS_PHONE_NUMBER + "=1) AND (" + Contacts.DISPLAY_NAME
300 + " != '' ))";
301 // String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND (" + Contacts.DISPLAY_NAME + " != '' ))";
302
303 return new CursorLoader(getActivity(), baseUri, CONTACTS_SUMMARY_PROJECTION, select, null, Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
304 }
305
306 @Override
307 public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
alisiona4325152013-04-19 11:10:03 -0400308 Log.i(TAG, "onLoadFinished");
alisionf76de3b2013-04-16 15:35:22 -0400309 // Swap the new cursor in. (The framework will take care of closing the
310 // old cursor once we return.)
311 // mAdapter.swapCursor(data);
312
313 // The list should now be shown.
314 /*
315 * if (isResumed()) { setListShown(true); } else { setListShownNoAnimation(true); }
316 */
317 }
318
319 @Override
320 public void onLoaderReset(Loader<Cursor> loader) {
321 // This is called when the last Cursor provided to onLoadFinished()
322 // above is about to be closed. We need to make sure we are no
323 // longer using it.
324 // mAdapter.swapCursor(null);
325 }
alisiona4325152013-04-19 11:10:03 -0400326
327 /**
328 * Called by activity to pass a reference to sipservice to Fragment.
329 *
330 * @param isip
331 */
332 public void onServiceSipBinded(ISipService isip) {
333
334 if (isReady) {
335 service = isip;
336 ArrayList<String> accountList;
337 try {
338 accountList = (ArrayList<String>) service.getAccountList();
339 mAccountSelectionSpinner.populate(service, accountList);
340 } catch (RemoteException e) {
341 Log.i(TAG, e.toString());
342 }
343 }
344
345 }
346
alisionf76de3b2013-04-16 15:35:22 -0400347}