alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 31 | package com.savoirfairelinux.sflphone.fragments; |
| 32 | |
| 33 | import java.io.InputStream; |
| 34 | import java.util.ArrayList; |
| 35 | import java.util.List; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 36 | |
| 37 | import android.app.Activity; |
| 38 | import android.app.AlertDialog; |
| 39 | import android.app.ListFragment; |
| 40 | import android.app.LoaderManager; |
| 41 | import android.content.ContentResolver; |
| 42 | import android.content.ContentUris; |
| 43 | import android.content.Context; |
| 44 | import android.content.CursorLoader; |
| 45 | import android.content.DialogInterface; |
| 46 | import android.content.Loader; |
| 47 | import android.database.Cursor; |
| 48 | import android.graphics.Bitmap; |
| 49 | import android.graphics.BitmapFactory; |
| 50 | import android.net.Uri; |
| 51 | import android.os.Bundle; |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame^] | 52 | import android.os.RemoteException; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 53 | import android.provider.ContactsContract; |
| 54 | import android.provider.ContactsContract.CommonDataKinds; |
| 55 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
| 56 | import android.provider.ContactsContract.CommonDataKinds.SipAddress; |
| 57 | import android.provider.ContactsContract.Contacts; |
| 58 | import android.util.Log; |
| 59 | import android.view.LayoutInflater; |
| 60 | import android.view.View; |
| 61 | import android.view.ViewGroup; |
| 62 | import android.widget.AdapterView; |
| 63 | import android.widget.AdapterView.OnItemLongClickListener; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 64 | import android.widget.ListView; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 65 | |
| 66 | import com.savoirfairelinux.sflphone.R; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 67 | import com.savoirfairelinux.sflphone.account.AccountSelectionSpinner; |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame^] | 68 | import com.savoirfairelinux.sflphone.adapters.CallElementAdapter; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 69 | import com.savoirfairelinux.sflphone.client.SFLPhoneHomeActivity; |
| 70 | import com.savoirfairelinux.sflphone.client.SFLphoneApplication; |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 71 | import com.savoirfairelinux.sflphone.client.receiver.AccountListReceiver; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 72 | import com.savoirfairelinux.sflphone.model.SipCall; |
| 73 | import com.savoirfairelinux.sflphone.service.ISipService; |
| 74 | |
| 75 | /** |
| 76 | * Main list of Call Elements. We don't manage contacts ourself so they are |
| 77 | */ |
| 78 | public class CallElementListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> { |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 79 | private static final String TAG = CallElementListFragment.class.getSimpleName(); |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame^] | 80 | |
| 81 | // private ContactManager mContactManager; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 82 | private CallElementAdapter mAdapter; |
| 83 | private String mCurFilter; |
| 84 | private SFLPhoneHomeActivity sflphoneHome; |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame^] | 85 | // private SFLphoneApplication sflphoneApplication; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 86 | private ISipService service; |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame^] | 87 | private AccountSelectionSpinner mAccountSelectionSpinner; |
| 88 | |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 89 | private AccountListReceiver mAccountList; |
| 90 | |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame^] | 91 | private boolean isReady = false; |
| 92 | |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 93 | 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() { |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 107 | // return mAccountSelectionButton.getText().toString(); |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 108 | 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 | |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 170 | 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); |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame^] | 219 | // // FIXME |
| 220 | // service = sflphoneApplication.getSipService(); |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 221 | 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) { |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame^] | 255 | Log.i(TAG, "onCreateView"); |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 256 | View inflatedView = inflater.inflate(R.layout.frag_call_element, container, false); |
| 257 | |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame^] | 258 | mAccountSelectionSpinner = (AccountSelectionSpinner) inflatedView.findViewById(R.id.account_selection_button); |
| 259 | mAccountList.addManagementUI(mAccountSelectionSpinner); |
| 260 | mAccountSelectionSpinner.setAccountList(mAccountList); |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 261 | |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame^] | 262 | isReady = true; |
| 263 | if (service != null) { |
| 264 | onServiceSipBinded(service); |
| 265 | } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 266 | 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 | |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame^] | 282 | Log.i(TAG, "onCreateLoader"); |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 283 | // 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) { |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame^] | 308 | Log.i(TAG, "onLoadFinished"); |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 309 | // 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 | } |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame^] | 326 | |
| 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 | |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 347 | } |