alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 1 | /* |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 2 | * Copyright (C) 2004-2013 Savoir-Faire Linux Inc. |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 3 | * |
| 4 | * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.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 | |
| 32 | package com.savoirfairelinux.sflphone.fragments; |
| 33 | |
| 34 | import java.util.ArrayList; |
| 35 | import java.util.HashMap; |
| 36 | |
| 37 | import android.app.Activity; |
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 38 | import android.app.AlertDialog; |
| 39 | import android.app.Dialog; |
| 40 | import android.app.DialogFragment; |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 41 | import android.app.Fragment; |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 42 | import android.app.FragmentManager; |
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 43 | import android.content.ClipData; |
| 44 | import android.content.ClipData.Item; |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 45 | import android.content.Context; |
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 46 | import android.content.DialogInterface; |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 47 | import android.content.Intent; |
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 48 | import android.graphics.Color; |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 49 | import android.os.Bundle; |
| 50 | import android.os.RemoteException; |
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 51 | import android.os.Vibrator; |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 52 | import android.util.Log; |
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 53 | import android.view.DragEvent; |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 54 | import android.view.LayoutInflater; |
| 55 | import android.view.View; |
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 56 | import android.view.View.DragShadowBuilder; |
| 57 | import android.view.View.OnDragListener; |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 58 | import android.view.ViewGroup; |
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 59 | import android.widget.AdapterView; |
| 60 | import android.widget.AdapterView.OnItemClickListener; |
| 61 | import android.widget.AdapterView.OnItemLongClickListener; |
| 62 | import android.widget.ArrayAdapter; |
| 63 | import android.widget.BaseAdapter; |
| 64 | import android.widget.ListAdapter; |
| 65 | import android.widget.ListView; |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 66 | import android.widget.TextView; |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 67 | import android.widget.Toast; |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 68 | |
| 69 | import com.savoirfairelinux.sflphone.R; |
alision | 806e18e | 2013-06-21 15:30:17 -0400 | [diff] [blame] | 70 | import com.savoirfairelinux.sflphone.model.Conference; |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 71 | import com.savoirfairelinux.sflphone.model.SipCall; |
| 72 | import com.savoirfairelinux.sflphone.service.ISipService; |
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 73 | import com.savoirfairelinux.sflphone.views.SwipeListViewTouchListener; |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 74 | |
| 75 | public class CallListFragment extends Fragment { |
alision | 806e18e | 2013-06-21 15:30:17 -0400 | [diff] [blame] | 76 | static final String TAG = CallListFragment.class.getSimpleName(); |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 77 | |
| 78 | private Callbacks mCallbacks = sDummyCallbacks; |
| 79 | |
| 80 | CallListAdapter mAdapter; |
| 81 | |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 82 | public static final int REQUEST_TRANSFER = 10; |
| 83 | public static final int REQUEST_CONF = 20; |
| 84 | |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 85 | @Override |
| 86 | public void onCreate(Bundle savedBundle) { |
| 87 | super.onCreate(savedBundle); |
| 88 | |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 89 | mAdapter = new CallListAdapter(getActivity()); |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 90 | |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity. |
| 95 | */ |
| 96 | private static Callbacks sDummyCallbacks = new Callbacks() { |
| 97 | |
| 98 | @Override |
| 99 | public ISipService getService() { |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 100 | return null; |
| 101 | } |
alision | 8599211 | 2013-05-29 12:18:08 -0400 | [diff] [blame] | 102 | |
| 103 | @Override |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 104 | public void onCallSelected(Conference conf) { |
alision | 806e18e | 2013-06-21 15:30:17 -0400 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void onCallsTerminated() { |
alision | 8599211 | 2013-05-29 12:18:08 -0400 | [diff] [blame] | 109 | } |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | /** |
| 113 | * The Activity calling this fragment has to implement this interface |
| 114 | * |
| 115 | */ |
| 116 | public interface Callbacks { |
| 117 | public ISipService getService(); |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 118 | |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 119 | public void onCallSelected(Conference conf); |
alision | 806e18e | 2013-06-21 15:30:17 -0400 | [diff] [blame] | 120 | |
| 121 | public void onCallsTerminated(); |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 122 | |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void onAttach(Activity activity) { |
| 127 | super.onAttach(activity); |
| 128 | |
| 129 | if (!(activity instanceof Callbacks)) { |
| 130 | throw new IllegalStateException("Activity must implement fragment's callbacks."); |
| 131 | } |
| 132 | |
| 133 | mCallbacks = (Callbacks) activity; |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public void onDetach() { |
| 138 | super.onDetach(); |
| 139 | mCallbacks = sDummyCallbacks; |
| 140 | } |
| 141 | |
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 142 | ListView list; |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 143 | |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 144 | @Override |
| 145 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 146 | ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.frag_call_list, container, false); |
| 147 | |
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 148 | list = (ListView) rootView.findViewById(R.id.call_list); |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 149 | |
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 150 | list.setDivider(getResources().getDrawable(android.R.drawable.divider_horizontal_dark)); |
| 151 | list.setDividerHeight(10); |
| 152 | list.setAdapter(mAdapter); |
| 153 | list.setOnItemClickListener(mItemClickListener); |
| 154 | list.setOnTouchListener(new SwipeListViewTouchListener(list, new SwipeListViewTouchListener.OnSwipeCallback() { |
| 155 | @Override |
| 156 | public void onSwipeLeft(ListView listView, int[] reverseSortedPositions) { |
| 157 | // Log.i(this.getClass().getName(), "swipe left : pos="+reverseSortedPositions[0]); |
| 158 | // TODO : YOUR CODE HERE FOR LEFT ACTION |
alision | f2ae436 | 2013-07-05 16:16:12 -0400 | [diff] [blame] | 159 | Conference tmp = mAdapter.getItem(reverseSortedPositions[0]); |
| 160 | try { |
| 161 | if (tmp.hasMultipleParticipants()) { |
| 162 | mCallbacks.getService().hangUpConference(tmp.getId()); |
| 163 | } else { |
| 164 | mCallbacks.getService().hangUp(tmp.getParticipants().get(0).getCallId()); |
| 165 | } |
| 166 | } catch (RemoteException e) { |
| 167 | // TODO Auto-generated catch block |
| 168 | e.printStackTrace(); |
| 169 | } |
| 170 | |
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public void onSwipeRight(ListView listView, int[] reverseSortedPositions) { |
| 175 | // Log.i(ProfileMenuActivity.class.getClass().getName(), "swipe right : pos="+reverseSortedPositions[0]); |
| 176 | // TODO : YOUR CODE HERE FOR RIGHT ACTION |
alision | f2ae436 | 2013-07-05 16:16:12 -0400 | [diff] [blame] | 177 | |
| 178 | Conference tmp = mAdapter.getItem(reverseSortedPositions[0]); |
| 179 | try { |
| 180 | if (tmp.hasMultipleParticipants()) { |
| 181 | if (tmp.isOnHold()) { |
| 182 | mCallbacks.getService().unholdConference(tmp.getId()); |
| 183 | } else { |
| 184 | mCallbacks.getService().holdConference(tmp.getId()); |
| 185 | } |
| 186 | } else { |
| 187 | if (tmp.isOnHold()) { |
| 188 | Toast.makeText(getActivity(), "call is on hold, unholding", Toast.LENGTH_SHORT).show(); |
| 189 | mCallbacks.getService().unhold(tmp.getParticipants().get(0).getCallId()); |
| 190 | } else { |
| 191 | Toast.makeText(getActivity(), "call is current, holding", Toast.LENGTH_SHORT).show(); |
| 192 | mCallbacks.getService().hold(tmp.getParticipants().get(0).getCallId()); |
| 193 | } |
| 194 | } |
| 195 | } catch (RemoteException e) { |
| 196 | // TODO Auto-generated catch block |
| 197 | e.printStackTrace(); |
| 198 | } |
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 199 | } |
| 200 | }, true, // example : left action = dismiss |
| 201 | false)); // example : right action without dismiss animation); |
| 202 | list.setOnItemLongClickListener(mItemLongClickListener); |
| 203 | |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 204 | return rootView; |
| 205 | } |
| 206 | |
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 207 | OnDragListener dragListener = new OnDragListener() { |
| 208 | |
| 209 | @Override |
| 210 | public boolean onDrag(View v, DragEvent event) { |
| 211 | switch (event.getAction()) { |
| 212 | case DragEvent.ACTION_DRAG_STARTED: |
| 213 | // Do nothing |
| 214 | Log.w(TAG, "ACTION_DRAG_STARTED"); |
| 215 | break; |
| 216 | case DragEvent.ACTION_DRAG_ENTERED: |
| 217 | Log.w(TAG, "ACTION_DRAG_ENTERED"); |
| 218 | v.setBackgroundColor(Color.GREEN); |
| 219 | break; |
| 220 | case DragEvent.ACTION_DRAG_EXITED: |
| 221 | Log.w(TAG, "ACTION_DRAG_EXITED"); |
| 222 | v.setBackgroundDrawable(getResources().getDrawable(R.drawable.item_call_selector)); |
| 223 | break; |
| 224 | case DragEvent.ACTION_DROP: |
| 225 | Log.w(TAG, "ACTION_DROP"); |
| 226 | View view = (View) event.getLocalState(); |
| 227 | |
| 228 | Item i = event.getClipData().getItemAt(0); |
| 229 | Intent intent = i.getIntent(); |
| 230 | intent.setExtrasClassLoader(Conference.class.getClassLoader()); |
| 231 | |
| 232 | Conference initial = (Conference) view.getTag(); |
| 233 | Conference target = (Conference) v.getTag(); |
alision | f2ae436 | 2013-07-05 16:16:12 -0400 | [diff] [blame] | 234 | |
| 235 | if (initial == target) { |
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 236 | return true; |
| 237 | } |
| 238 | |
| 239 | DropActionsChoice dialog = DropActionsChoice.newInstance(); |
| 240 | Bundle b = new Bundle(); |
| 241 | b.putParcelable("call_initial", initial); |
| 242 | b.putParcelable("call_targeted", target); |
| 243 | dialog.setArguments(b); |
| 244 | dialog.setTargetFragment(CallListFragment.this, 0); |
| 245 | dialog.show(getFragmentManager(), "dialog"); |
| 246 | |
| 247 | Toast.makeText( |
| 248 | getActivity(), |
| 249 | "Dropped " + initial.getParticipants().get(0).getContact().getmDisplayName() + " on " |
| 250 | + target.getParticipants().get(0).getContact().getmDisplayName(), Toast.LENGTH_SHORT).show(); |
| 251 | // view.setBackgroundColor(Color.WHITE); |
| 252 | // v.setBackgroundColor(Color.BLACK); |
| 253 | break; |
| 254 | case DragEvent.ACTION_DRAG_ENDED: |
| 255 | Log.w(TAG, "ACTION_DRAG_ENDED"); |
| 256 | View view1 = (View) event.getLocalState(); |
| 257 | view1.setVisibility(View.VISIBLE); |
| 258 | v.setBackgroundDrawable(getResources().getDrawable(R.drawable.item_call_selector)); |
| 259 | default: |
| 260 | break; |
| 261 | } |
| 262 | return true; |
| 263 | } |
| 264 | |
| 265 | }; |
| 266 | |
| 267 | private OnItemLongClickListener mItemLongClickListener = new OnItemLongClickListener() { |
| 268 | |
| 269 | @Override |
| 270 | public boolean onItemLongClick(AdapterView<?> arg0, View view, int pos, long arg3) { |
| 271 | final Vibrator vibe = (Vibrator) view.getContext().getSystemService(Context.VIBRATOR_SERVICE); |
| 272 | vibe.vibrate(80); |
| 273 | Intent i = new Intent(); |
| 274 | Bundle b = new Bundle(); |
| 275 | b.putParcelable("conference", mAdapter.getItem(pos)); |
| 276 | i.putExtra("bconference", b); |
alision | f2ae436 | 2013-07-05 16:16:12 -0400 | [diff] [blame] | 277 | |
| 278 | DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view); |
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 279 | ClipData data = ClipData.newIntent("conference", i); |
| 280 | view.startDrag(data, shadowBuilder, view, 0); |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | }; |
| 285 | |
| 286 | private OnItemClickListener mItemClickListener = new OnItemClickListener() { |
| 287 | |
| 288 | @Override |
| 289 | public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) { |
| 290 | mCallbacks.onCallSelected(mAdapter.getItem(pos)); |
| 291 | |
| 292 | } |
| 293 | }; |
| 294 | |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 295 | public void update() { |
| 296 | try { |
alision | 806e18e | 2013-06-21 15:30:17 -0400 | [diff] [blame] | 297 | Log.w(TAG, "Updating"); |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 298 | HashMap<String, SipCall> list = (HashMap<String, SipCall>) mCallbacks.getService().getCallList(); |
alision | 806e18e | 2013-06-21 15:30:17 -0400 | [diff] [blame] | 299 | |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 300 | // Toast.makeText(getActivity(), "Calls: " + list.size(), Toast.LENGTH_SHORT).show(); |
alision | 806e18e | 2013-06-21 15:30:17 -0400 | [diff] [blame] | 301 | ArrayList<Conference> conferences = new ArrayList<Conference>(); |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 302 | HashMap<String, Conference> tmp = (HashMap<String, Conference>) mCallbacks.getService().getConferenceList(); |
| 303 | conferences.addAll(tmp.values()); |
alision | 806e18e | 2013-06-21 15:30:17 -0400 | [diff] [blame] | 304 | |
| 305 | ArrayList<SipCall> simple_calls = new ArrayList<SipCall>(list.values()); |
| 306 | for (SipCall call : simple_calls) { |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 307 | Log.w(TAG, "SimpleCall:" + call.getCallId()); |
alision | 806e18e | 2013-06-21 15:30:17 -0400 | [diff] [blame] | 308 | Conference confOne = new Conference("-1"); |
| 309 | confOne.getParticipants().add(call); |
| 310 | conferences.add(confOne); |
| 311 | } |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 312 | |
| 313 | if (conferences.isEmpty()) { |
alision | 806e18e | 2013-06-21 15:30:17 -0400 | [diff] [blame] | 314 | mCallbacks.onCallsTerminated(); |
| 315 | } |
| 316 | |
| 317 | mAdapter.update(conferences); |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 318 | } catch (RemoteException e) { |
| 319 | Log.e(TAG, e.toString()); |
| 320 | } |
| 321 | |
| 322 | } |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 323 | |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 324 | private void makeTransferDialog(int groupPosition) { |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 325 | FragmentManager fm = getFragmentManager(); |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 326 | TransferDFragment editNameDialog = new TransferDFragment(); |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 327 | |
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 328 | if (!mAdapter.getItem(groupPosition).hasMultipleParticipants()) { |
alision | 806e18e | 2013-06-21 15:30:17 -0400 | [diff] [blame] | 329 | Bundle b = new Bundle(); |
| 330 | b.putParcelableArrayList("calls", mAdapter.getConcurrentCalls(groupPosition)); |
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 331 | b.putParcelable("call_selected", mAdapter.getItem(groupPosition)); |
alision | 806e18e | 2013-06-21 15:30:17 -0400 | [diff] [blame] | 332 | editNameDialog.setArguments(b); |
| 333 | editNameDialog.setTargetFragment(this, REQUEST_TRANSFER); |
| 334 | editNameDialog.show(fm, "dialog"); |
| 335 | } else { |
| 336 | Toast.makeText(getActivity(), "Transfer a Conference ?", Toast.LENGTH_SHORT).show(); |
| 337 | } |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 338 | |
| 339 | } |
| 340 | |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 341 | private void makeConferenceDialog(int groupPosition) { |
| 342 | FragmentManager fm = getFragmentManager(); |
alision | 806e18e | 2013-06-21 15:30:17 -0400 | [diff] [blame] | 343 | ConferenceDFragment confDialog = ConferenceDFragment.newInstance(); |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 344 | |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 345 | Bundle b = new Bundle(); |
| 346 | b.putParcelableArrayList("calls", mAdapter.getConcurrentCalls(groupPosition)); |
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 347 | b.putParcelable("call_selected", mAdapter.getItem(groupPosition)); |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 348 | confDialog.setArguments(b); |
| 349 | confDialog.setTargetFragment(this, REQUEST_CONF); |
| 350 | confDialog.show(fm, "dialog"); |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 351 | |
| 352 | } |
| 353 | |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 354 | @Override |
| 355 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 356 | super.onActivityResult(requestCode, resultCode, data); |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 357 | Conference transfer = null; |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 358 | if (requestCode == REQUEST_TRANSFER) { |
| 359 | switch (resultCode) { |
| 360 | case 0: |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 361 | Conference c = data.getParcelableExtra("target"); |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 362 | transfer = data.getParcelableExtra("transfer"); |
| 363 | try { |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 364 | |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 365 | mCallbacks.getService().attendedTransfer(transfer.getParticipants().get(0).getCallId(), c.getParticipants().get(0).getCallId()); |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 366 | mAdapter.remove(transfer); |
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 367 | mAdapter.remove(c); |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 368 | mAdapter.notifyDataSetChanged(); |
| 369 | } catch (RemoteException e) { |
| 370 | // TODO Auto-generated catch block |
| 371 | e.printStackTrace(); |
| 372 | } |
| 373 | Toast.makeText(getActivity(), "Transfer complete", Toast.LENGTH_LONG).show(); |
| 374 | break; |
| 375 | |
| 376 | case 1: |
| 377 | String to = data.getStringExtra("to_number"); |
| 378 | transfer = data.getParcelableExtra("transfer"); |
| 379 | try { |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 380 | Toast.makeText(getActivity(), "Transferring " + transfer.getParticipants().get(0).getContact().getmDisplayName() + " to " + to, |
| 381 | Toast.LENGTH_SHORT).show(); |
| 382 | mCallbacks.getService().transfer(transfer.getParticipants().get(0).getCallId(), to); |
| 383 | mCallbacks.getService().hangUp(transfer.getParticipants().get(0).getCallId()); |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 384 | } catch (RemoteException e) { |
| 385 | // TODO Auto-generated catch block |
| 386 | e.printStackTrace(); |
| 387 | } |
| 388 | break; |
| 389 | |
| 390 | default: |
| 391 | break; |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 392 | } |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 393 | } else if (requestCode == REQUEST_CONF) { |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 394 | switch (resultCode) { |
| 395 | case 0: |
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 396 | Conference call_to_add = data.getParcelableExtra("transfer"); |
| 397 | Conference call_target = data.getParcelableExtra("target"); |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 398 | |
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 399 | bindCalls(call_to_add, call_target); |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 400 | break; |
| 401 | |
| 402 | default: |
| 403 | break; |
| 404 | } |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 408 | private void bindCalls(Conference call_to_add, Conference call_target) { |
| 409 | try { |
| 410 | |
| 411 | if (call_target.hasMultipleParticipants() && !call_to_add.hasMultipleParticipants()) { |
| 412 | |
| 413 | mCallbacks.getService().addParticipant(call_to_add.getParticipants().get(0), call_target.getId()); |
| 414 | |
| 415 | } else if (call_target.hasMultipleParticipants() && call_to_add.hasMultipleParticipants()) { |
| 416 | |
| 417 | // We join two conferences |
| 418 | mCallbacks.getService().joinConference(call_to_add.getId(), call_target.getId()); |
| 419 | |
| 420 | } else if (!call_target.hasMultipleParticipants() && call_to_add.hasMultipleParticipants()) { |
| 421 | |
| 422 | mCallbacks.getService().addParticipant(call_target.getParticipants().get(0), call_to_add.getId()); |
| 423 | |
| 424 | } else { |
| 425 | // We join two single calls to create a conf |
| 426 | mCallbacks.getService().joinParticipant(call_to_add.getParticipants().get(0).getCallId(), |
| 427 | call_target.getParticipants().get(0).getCallId()); |
| 428 | } |
| 429 | |
| 430 | } catch (RemoteException e) { |
| 431 | // TODO Auto-generated catch block |
| 432 | e.printStackTrace(); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | public class CallListAdapter extends BaseAdapter { |
alision | 806e18e | 2013-06-21 15:30:17 -0400 | [diff] [blame] | 437 | |
| 438 | private ArrayList<Conference> calls; |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 439 | |
| 440 | private Context mContext; |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 441 | |
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 442 | public CallListAdapter(Context act) { |
| 443 | super(); |
| 444 | mContext = act; |
alision | 806e18e | 2013-06-21 15:30:17 -0400 | [diff] [blame] | 445 | calls = new ArrayList<Conference>(); |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 446 | |
| 447 | } |
| 448 | |
alision | 806e18e | 2013-06-21 15:30:17 -0400 | [diff] [blame] | 449 | public ArrayList<Conference> getConcurrentCalls(int position) { |
| 450 | ArrayList<Conference> toReturn = new ArrayList<Conference>(); |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 451 | for (int i = 0; i < calls.size(); ++i) { |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 452 | if (position != i) |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 453 | toReturn.add(calls.get(i)); |
| 454 | } |
| 455 | return toReturn; |
| 456 | } |
| 457 | |
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 458 | public void remove(Conference transfer) { |
alision | 8599211 | 2013-05-29 12:18:08 -0400 | [diff] [blame] | 459 | |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 460 | } |
| 461 | |
alision | 806e18e | 2013-06-21 15:30:17 -0400 | [diff] [blame] | 462 | public void update(ArrayList<Conference> list) { |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 463 | calls.clear(); |
alision | 806e18e | 2013-06-21 15:30:17 -0400 | [diff] [blame] | 464 | calls.addAll(list); |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 465 | notifyDataSetChanged(); |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 466 | } |
| 467 | |
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 468 | @Override |
| 469 | public int getCount() { |
| 470 | return calls.size(); |
| 471 | } |
| 472 | |
| 473 | @Override |
| 474 | public Conference getItem(int position) { |
| 475 | return calls.get(position); |
| 476 | } |
| 477 | |
| 478 | @Override |
| 479 | public long getItemId(int position) { |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | @Override |
| 484 | public View getView(int position, View convertView, ViewGroup parent) { |
| 485 | if (convertView == null) |
| 486 | convertView = LayoutInflater.from(mContext).inflate(R.layout.item_calllist, null); |
| 487 | |
| 488 | Conference call = calls.get(position); |
| 489 | if (call.getParticipants().size() == 1) { |
| 490 | ((TextView) convertView.findViewById(R.id.call_title)).setText(call.getParticipants().get(0).getContact().getmDisplayName()); |
| 491 | } else { |
alision | f2ae436 | 2013-07-05 16:16:12 -0400 | [diff] [blame] | 492 | String tmp = "Conference with "+ call.getParticipants().size()+" participants"; |
| 493 | // for (SipCall c : call.getParticipants()) { |
| 494 | // tmp += c.getContact().getmDisplayName() + " "; |
| 495 | // } |
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 496 | ((TextView) convertView.findViewById(R.id.call_title)).setText(tmp); |
| 497 | } |
alision | f2ae436 | 2013-07-05 16:16:12 -0400 | [diff] [blame] | 498 | // ((TextView) convertView.findViewById(R.id.num_participants)).setText("" + call.getParticipants().size()); |
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 499 | ((TextView) convertView.findViewById(R.id.call_status)).setText(call.getState()); |
| 500 | convertView.setOnDragListener(dragListener); |
| 501 | |
| 502 | convertView.setTag(call); |
| 503 | return convertView; |
| 504 | } |
| 505 | |
| 506 | } |
| 507 | |
| 508 | public static class DropActionsChoice extends DialogFragment { |
| 509 | |
| 510 | ListAdapter mAdapter; |
| 511 | private Bundle args; |
| 512 | |
| 513 | /** |
| 514 | * Create a new instance of CallActionsDFragment |
| 515 | */ |
| 516 | public static DropActionsChoice newInstance() { |
| 517 | DropActionsChoice f = new DropActionsChoice(); |
| 518 | return f; |
| 519 | } |
| 520 | |
| 521 | @Override |
| 522 | public void onCreate(Bundle savedInstanceState) { |
| 523 | super.onCreate(savedInstanceState); |
| 524 | |
| 525 | // Pick a style based on the num. |
| 526 | int style = DialogFragment.STYLE_NORMAL, theme = 0; |
| 527 | setStyle(style, theme); |
| 528 | } |
| 529 | |
| 530 | @Override |
| 531 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 532 | ListView rootView = new ListView(getActivity()); |
| 533 | |
| 534 | args = getArguments(); |
| 535 | mAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, getResources().getStringArray( |
| 536 | R.array.drop_actions)); |
| 537 | |
| 538 | // ListView list = (ListView) rootView.findViewById(R.id.concurrent_calls); |
| 539 | rootView.setAdapter(mAdapter); |
| 540 | rootView.setOnItemClickListener(new OnItemClickListener() { |
| 541 | |
| 542 | @Override |
| 543 | public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) { |
| 544 | Intent in = new Intent(); |
| 545 | |
| 546 | in.putExtra("transfer", args.getParcelable("call_initial")); |
| 547 | in.putExtra("target", args.getParcelable("call_targeted")); |
| 548 | |
| 549 | switch (pos) { |
| 550 | case 0: // Transfer |
| 551 | getTargetFragment().onActivityResult(REQUEST_TRANSFER, 0, in); |
| 552 | break; |
| 553 | case 1: // Conference |
| 554 | getTargetFragment().onActivityResult(REQUEST_CONF, 0, in); |
| 555 | break; |
| 556 | } |
| 557 | dismiss(); |
| 558 | |
| 559 | } |
| 560 | }); |
| 561 | |
| 562 | final AlertDialog a = new AlertDialog.Builder(getActivity()).setView(rootView).setTitle("Choose Action") |
| 563 | .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { |
| 564 | public void onClick(DialogInterface dialog, int whichButton) { |
| 565 | dismiss(); |
| 566 | } |
| 567 | }).create(); |
| 568 | |
| 569 | return a; |
| 570 | } |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 571 | } |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 572 | |
| 573 | } |