alision | f76de3b | 2013-04-16 15:35:22 -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 | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 3 | * |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 4 | * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com> |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 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 | |
Alexandre Lision | 6d75d06 | 2013-09-13 14:18:34 -0400 | [diff] [blame] | 33 | import java.util.ArrayList; |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 34 | import java.util.HashMap; |
Alexandre Lision | 3c6b710 | 2013-09-16 16:56:46 -0400 | [diff] [blame] | 35 | import java.util.Observable; |
| 36 | import java.util.Observer; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 37 | |
| 38 | import android.app.Activity; |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 39 | import android.app.Fragment; |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 40 | import android.content.ClipData; |
Alexandre Lision | 6d75d06 | 2013-09-13 14:18:34 -0400 | [diff] [blame] | 41 | import android.content.Context; |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 42 | import android.content.Intent; |
| 43 | import android.content.ClipData.Item; |
| 44 | import android.graphics.Color; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 45 | import android.os.Bundle; |
Alexandre Lision | ebeb366 | 2013-09-17 16:20:54 -0400 | [diff] [blame] | 46 | import android.os.Handler; |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 47 | import android.os.RemoteException; |
Alexandre Lision | ebeb366 | 2013-09-17 16:20:54 -0400 | [diff] [blame] | 48 | import android.os.SystemClock; |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 49 | import android.os.Vibrator; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 50 | import android.util.Log; |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 51 | import android.view.DragEvent; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 52 | import android.view.LayoutInflater; |
alision | d8c8388 | 2013-05-17 17:00:42 -0400 | [diff] [blame] | 53 | import android.view.Menu; |
| 54 | import android.view.MenuInflater; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 55 | import android.view.View; |
alision | 43a9b36 | 2013-05-01 16:30:15 -0400 | [diff] [blame] | 56 | import android.view.ViewGroup; |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 57 | import android.view.View.DragShadowBuilder; |
| 58 | import android.view.View.OnDragListener; |
Alexandre Lision | 1a9e3b1 | 2013-09-16 11:06:07 -0400 | [diff] [blame] | 59 | import android.widget.AdapterView; |
| 60 | import android.widget.AdapterView.OnItemClickListener; |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 61 | import android.widget.AdapterView.OnItemLongClickListener; |
Alexandre Lision | 6d75d06 | 2013-09-13 14:18:34 -0400 | [diff] [blame] | 62 | import android.widget.BaseAdapter; |
Alexandre Lision | 573045c | 2013-09-11 17:20:25 -0400 | [diff] [blame] | 63 | import android.widget.ListView; |
Alexandre Lision | c51ccb1 | 2013-09-11 16:00:30 -0400 | [diff] [blame] | 64 | import android.widget.TextView; |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 65 | import android.widget.Toast; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 66 | |
| 67 | import com.savoirfairelinux.sflphone.R; |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 68 | import com.savoirfairelinux.sflphone.fragments.CallListFragment.DropActionsChoice; |
Alexandre Lision | 3c6b710 | 2013-09-16 16:56:46 -0400 | [diff] [blame] | 69 | import com.savoirfairelinux.sflphone.model.CallTimer; |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 70 | import com.savoirfairelinux.sflphone.model.Conference; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 71 | import com.savoirfairelinux.sflphone.model.SipCall; |
| 72 | import com.savoirfairelinux.sflphone.service.ISipService; |
| 73 | |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 74 | public class HomeFragment extends Fragment { |
alision | 55c36cb | 2013-06-14 14:57:38 -0400 | [diff] [blame] | 75 | private static final String TAG = HomeFragment.class.getSimpleName(); |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 76 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 77 | private Callbacks mCallbacks = sDummyCallbacks; |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 78 | private TextView nb_calls, nb_confs; |
| 79 | CallListAdapter confs_adapter, calls_adapter; |
Alexandre Lision | ebeb366 | 2013-09-17 16:20:54 -0400 | [diff] [blame] | 80 | CallTimer timer; |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 81 | |
| 82 | public static final int REQUEST_TRANSFER = 10; |
| 83 | public static final int REQUEST_CONF = 20; |
alision | 43a9b36 | 2013-05-01 16:30:15 -0400 | [diff] [blame] | 84 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 85 | /** |
| 86 | * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity. |
| 87 | */ |
| 88 | private static Callbacks sDummyCallbacks = new Callbacks() { |
alision | 371b77e | 2013-04-23 14:51:26 -0400 | [diff] [blame] | 89 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 90 | @Override |
| 91 | public ISipService getService() { |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 92 | Log.i(TAG, "I'm a dummy"); |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 93 | return null; |
| 94 | } |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 95 | |
| 96 | @Override |
Alexandre Lision | 1a9e3b1 | 2013-09-16 11:06:07 -0400 | [diff] [blame] | 97 | public void selectedCall(Conference c) { |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 98 | } |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 99 | }; |
alision | 371b77e | 2013-04-23 14:51:26 -0400 | [diff] [blame] | 100 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 101 | /** |
| 102 | * The Activity calling this fragment has to implement this interface |
| 103 | * |
| 104 | */ |
| 105 | public interface Callbacks { |
alision | 371b77e | 2013-04-23 14:51:26 -0400 | [diff] [blame] | 106 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 107 | public ISipService getService(); |
alision | 43a9b36 | 2013-05-01 16:30:15 -0400 | [diff] [blame] | 108 | |
Alexandre Lision | 1a9e3b1 | 2013-09-16 11:06:07 -0400 | [diff] [blame] | 109 | public void selectedCall(Conference c); |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 110 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 111 | } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 112 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 113 | @Override |
| 114 | public void onAttach(Activity activity) { |
| 115 | super.onAttach(activity); |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 116 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 117 | if (!(activity instanceof Callbacks)) { |
| 118 | throw new IllegalStateException("Activity must implement fragment's callbacks."); |
| 119 | } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 120 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 121 | mCallbacks = (Callbacks) activity; |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 122 | |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 123 | } |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 124 | |
Alexandre Lision | ebeb366 | 2013-09-17 16:20:54 -0400 | [diff] [blame] | 125 | private Runnable mUpdateTimeTask = new Runnable() { |
| 126 | public void run() { |
| 127 | final long start = SystemClock.uptimeMillis(); |
| 128 | long millis = SystemClock.uptimeMillis() - start; |
| 129 | int seconds = (int) (millis / 1000); |
| 130 | int minutes = seconds / 60; |
| 131 | seconds = seconds % 60; |
| 132 | |
| 133 | calls_adapter.notifyDataSetChanged(); |
Alexandre Lision | 0c38451 | 2013-09-17 17:15:57 -0400 | [diff] [blame] | 134 | confs_adapter.notifyDataSetChanged(); |
Alexandre Lision | ebeb366 | 2013-09-17 16:20:54 -0400 | [diff] [blame] | 135 | mHandler.postAtTime(this, start + (((minutes * 60) + seconds + 1) * 1000)); |
| 136 | } |
| 137 | }; |
| 138 | |
| 139 | private Handler mHandler = new Handler(); |
| 140 | |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 141 | @Override |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 142 | public void onResume() { |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 143 | super.onResume(); |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 144 | if (mCallbacks.getService() != null) { |
| 145 | try { |
Alexandre Lision | ebeb366 | 2013-09-17 16:20:54 -0400 | [diff] [blame] | 146 | updateLists(); |
Alexandre Lision | ebeb366 | 2013-09-17 16:20:54 -0400 | [diff] [blame] | 147 | if (!calls_adapter.isEmpty() || !confs_adapter.isEmpty()) { |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 148 | mHandler.postDelayed(mUpdateTimeTask, 0); |
Alexandre Lision | 3c6b710 | 2013-09-16 16:56:46 -0400 | [diff] [blame] | 149 | } |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 150 | |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 151 | } catch (RemoteException e) { |
| 152 | Log.e(TAG, e.toString()); |
| 153 | } |
| 154 | } |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 155 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 156 | } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 157 | |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 158 | @SuppressWarnings("unchecked") |
| 159 | // No proper solution with HashMap runtime cast |
Alexandre Lision | 1a9e3b1 | 2013-09-16 11:06:07 -0400 | [diff] [blame] | 160 | public void updateLists() throws RemoteException { |
| 161 | HashMap<String, SipCall> calls = (HashMap<String, SipCall>) mCallbacks.getService().getCallList(); |
| 162 | HashMap<String, Conference> confs = (HashMap<String, Conference>) mCallbacks.getService().getConferenceList(); |
| 163 | |
| 164 | updateCallList(calls); |
| 165 | updateConferenceList(confs); |
| 166 | } |
| 167 | |
Alexandre Lision | 573045c | 2013-09-11 17:20:25 -0400 | [diff] [blame] | 168 | private void updateConferenceList(HashMap<String, Conference> confs) { |
Alexandre Lision | 6d75d06 | 2013-09-13 14:18:34 -0400 | [diff] [blame] | 169 | nb_confs.setText("" + confs.size()); |
Alexandre Lision | ebeb366 | 2013-09-17 16:20:54 -0400 | [diff] [blame] | 170 | confs_adapter.updateDataset(new ArrayList<Conference>(confs.values())); |
Alexandre Lision | 573045c | 2013-09-11 17:20:25 -0400 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | private void updateCallList(HashMap<String, SipCall> calls) { |
Alexandre Lision | 6d75d06 | 2013-09-13 14:18:34 -0400 | [diff] [blame] | 174 | nb_calls.setText("" + calls.size()); |
| 175 | ArrayList<Conference> conferences = new ArrayList<Conference>(); |
| 176 | for (SipCall call : calls.values()) { |
| 177 | Log.w(TAG, "SimpleCall:" + call.getCallId()); |
| 178 | Conference confOne = new Conference("-1"); |
| 179 | confOne.getParticipants().add(call); |
| 180 | conferences.add(confOne); |
| 181 | } |
Alexandre Lision | 1a9e3b1 | 2013-09-16 11:06:07 -0400 | [diff] [blame] | 182 | |
Alexandre Lision | ebeb366 | 2013-09-17 16:20:54 -0400 | [diff] [blame] | 183 | calls_adapter.updateDataset(conferences); |
Alexandre Lision | 6d75d06 | 2013-09-13 14:18:34 -0400 | [diff] [blame] | 184 | |
Alexandre Lision | 573045c | 2013-09-11 17:20:25 -0400 | [diff] [blame] | 185 | } |
| 186 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 187 | @Override |
| 188 | public void onDetach() { |
Alexandre Lision | 3c6b710 | 2013-09-16 16:56:46 -0400 | [diff] [blame] | 189 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 190 | super.onDetach(); |
| 191 | mCallbacks = sDummyCallbacks; |
Alexandre Lision | 3c6b710 | 2013-09-16 16:56:46 -0400 | [diff] [blame] | 192 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 193 | } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 194 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 195 | @Override |
| 196 | public void onCreate(Bundle savedInstanceState) { |
| 197 | super.onCreate(savedInstanceState); |
Alexandre Lision | 0c38451 | 2013-09-17 17:15:57 -0400 | [diff] [blame] | 198 | } |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 199 | |
Alexandre Lision | 0c38451 | 2013-09-17 17:15:57 -0400 | [diff] [blame] | 200 | @Override |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 201 | public void onPause() { |
Alexandre Lision | 0c38451 | 2013-09-17 17:15:57 -0400 | [diff] [blame] | 202 | super.onPause(); |
| 203 | mHandler.removeCallbacks(mUpdateTimeTask); |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 204 | } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 205 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 206 | @Override |
| 207 | public void onActivityCreated(Bundle savedInstanceState) { |
Alexandre Lision | ebeb366 | 2013-09-17 16:20:54 -0400 | [diff] [blame] | 208 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 209 | super.onActivityCreated(savedInstanceState); |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 210 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 211 | // Give some text to display if there is no data. In a real |
| 212 | // application this would come from a resource. |
| 213 | // setEmptyText("No phone numbers"); |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 214 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 215 | // We have a menu item to show in action bar. |
| 216 | setHasOptionsMenu(true); |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 217 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 218 | } |
alision | 371b77e | 2013-04-23 14:51:26 -0400 | [diff] [blame] | 219 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 220 | @Override |
| 221 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
Alexandre Lision | 3c6b710 | 2013-09-16 16:56:46 -0400 | [diff] [blame] | 222 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 223 | inflater.inflate(R.menu.call_element_menu, menu); |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 224 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 225 | } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 226 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 227 | @Override |
| 228 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 229 | Log.i(TAG, "onCreateView"); |
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 230 | View inflatedView = inflater.inflate(R.layout.frag_home, container, false); |
alision | 43a9b36 | 2013-05-01 16:30:15 -0400 | [diff] [blame] | 231 | |
Alexandre Lision | c51ccb1 | 2013-09-11 16:00:30 -0400 | [diff] [blame] | 232 | nb_calls = (TextView) inflatedView.findViewById(R.id.calls_counter); |
| 233 | nb_confs = (TextView) inflatedView.findViewById(R.id.confs_counter); |
Alexandre Lision | 6d75d06 | 2013-09-13 14:18:34 -0400 | [diff] [blame] | 234 | |
| 235 | confs_adapter = new CallListAdapter(getActivity()); |
| 236 | ((ListView) inflatedView.findViewById(R.id.confs_list)).setAdapter(confs_adapter); |
Alexandre Lision | 1a9e3b1 | 2013-09-16 11:06:07 -0400 | [diff] [blame] | 237 | |
Alexandre Lision | 6d75d06 | 2013-09-13 14:18:34 -0400 | [diff] [blame] | 238 | calls_adapter = new CallListAdapter(getActivity()); |
| 239 | ((ListView) inflatedView.findViewById(R.id.calls_list)).setAdapter(calls_adapter); |
Alexandre Lision | 1a9e3b1 | 2013-09-16 11:06:07 -0400 | [diff] [blame] | 240 | ((ListView) inflatedView.findViewById(R.id.calls_list)).setOnItemClickListener(callClickListener); |
| 241 | ((ListView) inflatedView.findViewById(R.id.confs_list)).setOnItemClickListener(callClickListener); |
alision | 43a9b36 | 2013-05-01 16:30:15 -0400 | [diff] [blame] | 242 | |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 243 | ((ListView) inflatedView.findViewById(R.id.calls_list)).setOnItemLongClickListener(mItemLongClickListener); |
| 244 | ((ListView) inflatedView.findViewById(R.id.confs_list)).setOnItemLongClickListener(mItemLongClickListener); |
| 245 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 246 | return inflatedView; |
| 247 | } |
Alexandre Lision | ebeb366 | 2013-09-17 16:20:54 -0400 | [diff] [blame] | 248 | |
| 249 | OnItemClickListener callClickListener = new OnItemClickListener() { |
Alexandre Lision | 1a9e3b1 | 2013-09-16 11:06:07 -0400 | [diff] [blame] | 250 | |
| 251 | @Override |
| 252 | public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) { |
Alexandre Lision | ebeb366 | 2013-09-17 16:20:54 -0400 | [diff] [blame] | 253 | mCallbacks.selectedCall((Conference) v.getTag()); |
Alexandre Lision | 1a9e3b1 | 2013-09-16 11:06:07 -0400 | [diff] [blame] | 254 | } |
| 255 | }; |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 256 | |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 257 | private OnItemLongClickListener mItemLongClickListener = new OnItemLongClickListener() { |
| 258 | |
| 259 | @Override |
| 260 | public boolean onItemLongClick(AdapterView<?> adptv, View view, int pos, long arg3) { |
| 261 | final Vibrator vibe = (Vibrator) view.getContext().getSystemService(Context.VIBRATOR_SERVICE); |
| 262 | vibe.vibrate(80); |
| 263 | Intent i = new Intent(); |
| 264 | Bundle b = new Bundle(); |
| 265 | b.putParcelable("conference", (Conference) adptv.getAdapter().getItem(pos)); |
| 266 | i.putExtra("bconference", b); |
| 267 | |
| 268 | DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view); |
| 269 | ClipData data = ClipData.newIntent("conference", i); |
| 270 | view.startDrag(data, shadowBuilder, view, 0); |
| 271 | return false; |
| 272 | } |
| 273 | |
| 274 | }; |
| 275 | |
Alexandre Lision | 3c6b710 | 2013-09-16 16:56:46 -0400 | [diff] [blame] | 276 | public class CallListAdapter extends BaseAdapter implements Observer { |
Alexandre Lision | 6d75d06 | 2013-09-13 14:18:34 -0400 | [diff] [blame] | 277 | |
| 278 | private ArrayList<Conference> calls; |
| 279 | |
| 280 | private Context mContext; |
| 281 | |
| 282 | public CallListAdapter(Context act) { |
| 283 | super(); |
| 284 | mContext = act; |
| 285 | calls = new ArrayList<Conference>(); |
| 286 | |
| 287 | } |
| 288 | |
Alexandre Lision | 3c6b710 | 2013-09-16 16:56:46 -0400 | [diff] [blame] | 289 | public ArrayList<Conference> getDataset() { |
| 290 | return calls; |
| 291 | } |
| 292 | |
Alexandre Lision | 6d75d06 | 2013-09-13 14:18:34 -0400 | [diff] [blame] | 293 | public void remove(Conference transfer) { |
| 294 | |
| 295 | } |
| 296 | |
Alexandre Lision | ebeb366 | 2013-09-17 16:20:54 -0400 | [diff] [blame] | 297 | public void updateDataset(ArrayList<Conference> list) { |
Alexandre Lision | 6d75d06 | 2013-09-13 14:18:34 -0400 | [diff] [blame] | 298 | calls.clear(); |
| 299 | calls.addAll(list); |
| 300 | notifyDataSetChanged(); |
| 301 | } |
| 302 | |
| 303 | @Override |
| 304 | public int getCount() { |
| 305 | return calls.size(); |
| 306 | } |
| 307 | |
| 308 | @Override |
| 309 | public Conference getItem(int position) { |
| 310 | return calls.get(position); |
| 311 | } |
| 312 | |
| 313 | @Override |
| 314 | public long getItemId(int position) { |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | @Override |
| 319 | public View getView(int position, View convertView, ViewGroup parent) { |
| 320 | if (convertView == null) |
| 321 | convertView = LayoutInflater.from(mContext).inflate(R.layout.item_calllist, null); |
| 322 | |
| 323 | Conference call = calls.get(position); |
| 324 | if (call.getParticipants().size() == 1) { |
| 325 | ((TextView) convertView.findViewById(R.id.call_title)).setText(call.getParticipants().get(0).getContact().getmDisplayName()); |
Alexandre Lision | ebeb366 | 2013-09-17 16:20:54 -0400 | [diff] [blame] | 326 | |
| 327 | long duration = System.currentTimeMillis() / 1000 - (call.getParticipants().get(0).getTimestamp_start()); |
| 328 | |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 329 | ((TextView) convertView.findViewById(R.id.call_time)).setText(String.format("%d:%02d:%02d", duration / 3600, (duration % 3600) / 60, |
| 330 | (duration % 60))); |
Alexandre Lision | 6d75d06 | 2013-09-13 14:18:34 -0400 | [diff] [blame] | 331 | } else { |
| 332 | String tmp = "Conference with " + call.getParticipants().size() + " participants"; |
Alexandre Lision | 6d75d06 | 2013-09-13 14:18:34 -0400 | [diff] [blame] | 333 | ((TextView) convertView.findViewById(R.id.call_title)).setText(tmp); |
| 334 | } |
| 335 | // ((TextView) convertView.findViewById(R.id.num_participants)).setText("" + call.getParticipants().size()); |
| 336 | ((TextView) convertView.findViewById(R.id.call_status)).setText(call.getState()); |
| 337 | |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 338 | convertView.setOnDragListener(dragListener); |
Alexandre Lision | 6d75d06 | 2013-09-13 14:18:34 -0400 | [diff] [blame] | 339 | convertView.setTag(call); |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 340 | |
Alexandre Lision | 6d75d06 | 2013-09-13 14:18:34 -0400 | [diff] [blame] | 341 | return convertView; |
| 342 | } |
| 343 | |
Alexandre Lision | 3c6b710 | 2013-09-16 16:56:46 -0400 | [diff] [blame] | 344 | @Override |
| 345 | public void update(Observable observable, Object data) { |
Alexandre Lision | ebeb366 | 2013-09-17 16:20:54 -0400 | [diff] [blame] | 346 | Log.i(TAG, "Updating views..."); |
| 347 | notifyDataSetChanged(); |
Alexandre Lision | 3c6b710 | 2013-09-16 16:56:46 -0400 | [diff] [blame] | 348 | } |
| 349 | |
Alexandre Lision | 6d75d06 | 2013-09-13 14:18:34 -0400 | [diff] [blame] | 350 | } |
| 351 | |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 352 | OnDragListener dragListener = new OnDragListener() { |
| 353 | |
| 354 | @SuppressWarnings("deprecation") |
| 355 | // deprecated in API 16.... |
| 356 | @Override |
| 357 | public boolean onDrag(View v, DragEvent event) { |
| 358 | switch (event.getAction()) { |
| 359 | case DragEvent.ACTION_DRAG_STARTED: |
| 360 | // Do nothing |
| 361 | Log.w(TAG, "ACTION_DRAG_STARTED"); |
| 362 | break; |
| 363 | case DragEvent.ACTION_DRAG_ENTERED: |
| 364 | Log.w(TAG, "ACTION_DRAG_ENTERED"); |
| 365 | v.setBackgroundColor(Color.GREEN); |
| 366 | break; |
| 367 | case DragEvent.ACTION_DRAG_EXITED: |
| 368 | Log.w(TAG, "ACTION_DRAG_EXITED"); |
| 369 | v.setBackgroundDrawable(getResources().getDrawable(R.drawable.item_call_selector)); |
| 370 | break; |
| 371 | case DragEvent.ACTION_DROP: |
| 372 | Log.w(TAG, "ACTION_DROP"); |
| 373 | View view = (View) event.getLocalState(); |
| 374 | |
| 375 | Item i = event.getClipData().getItemAt(0); |
| 376 | Intent intent = i.getIntent(); |
| 377 | intent.setExtrasClassLoader(Conference.class.getClassLoader()); |
| 378 | |
| 379 | Conference initial = (Conference) view.getTag(); |
| 380 | Conference target = (Conference) v.getTag(); |
| 381 | |
| 382 | if (initial == target) { |
| 383 | return true; |
| 384 | } |
| 385 | |
| 386 | DropActionsChoice dialog = DropActionsChoice.newInstance(); |
| 387 | Bundle b = new Bundle(); |
| 388 | b.putParcelable("call_initial", initial); |
| 389 | b.putParcelable("call_targeted", target); |
| 390 | dialog.setArguments(b); |
| 391 | dialog.setTargetFragment(HomeFragment.this, 0); |
| 392 | dialog.show(getFragmentManager(), "dialog"); |
| 393 | |
| 394 | // view.setBackgroundColor(Color.WHITE); |
| 395 | // v.setBackgroundColor(Color.BLACK); |
| 396 | break; |
| 397 | case DragEvent.ACTION_DRAG_ENDED: |
| 398 | Log.w(TAG, "ACTION_DRAG_ENDED"); |
| 399 | View view1 = (View) event.getLocalState(); |
| 400 | view1.setVisibility(View.VISIBLE); |
| 401 | v.setBackgroundDrawable(getResources().getDrawable(R.drawable.item_call_selector)); |
| 402 | default: |
| 403 | break; |
| 404 | } |
| 405 | return true; |
| 406 | } |
| 407 | |
| 408 | }; |
| 409 | |
| 410 | @Override |
| 411 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 412 | super.onActivityResult(requestCode, resultCode, data); |
| 413 | Conference transfer = null; |
| 414 | if (requestCode == REQUEST_TRANSFER) { |
| 415 | switch (resultCode) { |
| 416 | case 0: |
| 417 | Conference c = data.getParcelableExtra("target"); |
| 418 | transfer = data.getParcelableExtra("transfer"); |
| 419 | try { |
| 420 | |
| 421 | mCallbacks.getService().attendedTransfer(transfer.getParticipants().get(0).getCallId(), c.getParticipants().get(0).getCallId()); |
| 422 | calls_adapter.remove(transfer); |
| 423 | calls_adapter.remove(c); |
| 424 | calls_adapter.notifyDataSetChanged(); |
| 425 | } catch (RemoteException e) { |
| 426 | // TODO Auto-generated catch block |
| 427 | e.printStackTrace(); |
| 428 | } |
| 429 | Toast.makeText(getActivity(), "Transfer complete", Toast.LENGTH_LONG).show(); |
| 430 | break; |
| 431 | |
| 432 | case 1: |
| 433 | String to = data.getStringExtra("to_number"); |
| 434 | transfer = data.getParcelableExtra("transfer"); |
| 435 | try { |
| 436 | Toast.makeText(getActivity(), "Transferring " + transfer.getParticipants().get(0).getContact().getmDisplayName() + " to " + to, |
| 437 | Toast.LENGTH_SHORT).show(); |
| 438 | mCallbacks.getService().transfer(transfer.getParticipants().get(0).getCallId(), to); |
| 439 | mCallbacks.getService().hangUp(transfer.getParticipants().get(0).getCallId()); |
| 440 | } catch (RemoteException e) { |
| 441 | // TODO Auto-generated catch block |
| 442 | e.printStackTrace(); |
| 443 | } |
| 444 | break; |
| 445 | |
| 446 | default: |
| 447 | break; |
| 448 | } |
| 449 | } else if (requestCode == REQUEST_CONF) { |
| 450 | switch (resultCode) { |
| 451 | case 0: |
| 452 | Conference call_to_add = data.getParcelableExtra("transfer"); |
| 453 | Conference call_target = data.getParcelableExtra("target"); |
| 454 | |
| 455 | bindCalls(call_to_add, call_target); |
| 456 | break; |
| 457 | |
| 458 | default: |
| 459 | break; |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | private void bindCalls(Conference call_to_add, Conference call_target) { |
| 465 | try { |
| 466 | |
| 467 | if (call_target.hasMultipleParticipants() && !call_to_add.hasMultipleParticipants()) { |
| 468 | |
| 469 | mCallbacks.getService().addParticipant(call_to_add.getParticipants().get(0), call_target.getId()); |
| 470 | |
| 471 | } else if (call_target.hasMultipleParticipants() && call_to_add.hasMultipleParticipants()) { |
| 472 | |
| 473 | // We join two conferences |
| 474 | mCallbacks.getService().joinConference(call_to_add.getId(), call_target.getId()); |
| 475 | |
| 476 | } else if (!call_target.hasMultipleParticipants() && call_to_add.hasMultipleParticipants()) { |
| 477 | |
| 478 | mCallbacks.getService().addParticipant(call_target.getParticipants().get(0), call_to_add.getId()); |
| 479 | |
| 480 | } else { |
| 481 | // We join two single calls to create a conf |
| 482 | mCallbacks.getService().joinParticipant(call_to_add.getParticipants().get(0).getCallId(), |
| 483 | call_target.getParticipants().get(0).getCallId()); |
| 484 | } |
| 485 | |
| 486 | } catch (RemoteException e) { |
| 487 | // TODO Auto-generated catch block |
| 488 | e.printStackTrace(); |
| 489 | } |
| 490 | } |
| 491 | |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 492 | } |