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 | e2839d5 | 2013-10-01 09:37:37 -0400 | [diff] [blame^] | 81 | |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 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()) { |
Alexandre Lision | 6d75d06 | 2013-09-13 14:18:34 -0400 | [diff] [blame] | 177 | Conference confOne = new Conference("-1"); |
| 178 | confOne.getParticipants().add(call); |
| 179 | conferences.add(confOne); |
| 180 | } |
Alexandre Lision | 1a9e3b1 | 2013-09-16 11:06:07 -0400 | [diff] [blame] | 181 | |
Alexandre Lision | ebeb366 | 2013-09-17 16:20:54 -0400 | [diff] [blame] | 182 | calls_adapter.updateDataset(conferences); |
Alexandre Lision | 6d75d06 | 2013-09-13 14:18:34 -0400 | [diff] [blame] | 183 | |
Alexandre Lision | 573045c | 2013-09-11 17:20:25 -0400 | [diff] [blame] | 184 | } |
| 185 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 186 | @Override |
| 187 | public void onDetach() { |
Alexandre Lision | 3c6b710 | 2013-09-16 16:56:46 -0400 | [diff] [blame] | 188 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 189 | super.onDetach(); |
| 190 | mCallbacks = sDummyCallbacks; |
Alexandre Lision | 3c6b710 | 2013-09-16 16:56:46 -0400 | [diff] [blame] | 191 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 192 | } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 193 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 194 | @Override |
| 195 | public void onCreate(Bundle savedInstanceState) { |
| 196 | super.onCreate(savedInstanceState); |
Alexandre Lision | 0c38451 | 2013-09-17 17:15:57 -0400 | [diff] [blame] | 197 | } |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 198 | |
Alexandre Lision | 0c38451 | 2013-09-17 17:15:57 -0400 | [diff] [blame] | 199 | @Override |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 200 | public void onPause() { |
Alexandre Lision | 0c38451 | 2013-09-17 17:15:57 -0400 | [diff] [blame] | 201 | super.onPause(); |
| 202 | mHandler.removeCallbacks(mUpdateTimeTask); |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 203 | } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 204 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 205 | @Override |
| 206 | public void onActivityCreated(Bundle savedInstanceState) { |
Alexandre Lision | ebeb366 | 2013-09-17 16:20:54 -0400 | [diff] [blame] | 207 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 208 | super.onActivityCreated(savedInstanceState); |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 209 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 210 | // Give some text to display if there is no data. In a real |
| 211 | // application this would come from a resource. |
| 212 | // setEmptyText("No phone numbers"); |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 213 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 214 | // We have a menu item to show in action bar. |
| 215 | setHasOptionsMenu(true); |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 216 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 217 | } |
alision | 371b77e | 2013-04-23 14:51:26 -0400 | [diff] [blame] | 218 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 219 | @Override |
| 220 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
Alexandre Lision | 3c6b710 | 2013-09-16 16:56:46 -0400 | [diff] [blame] | 221 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 222 | inflater.inflate(R.menu.call_element_menu, menu); |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 223 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 224 | } |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 225 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 226 | @Override |
| 227 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 228 | Log.i(TAG, "onCreateView"); |
alision | 465ceba | 2013-07-04 09:24:30 -0400 | [diff] [blame] | 229 | View inflatedView = inflater.inflate(R.layout.frag_home, container, false); |
alision | 43a9b36 | 2013-05-01 16:30:15 -0400 | [diff] [blame] | 230 | |
Alexandre Lision | c51ccb1 | 2013-09-11 16:00:30 -0400 | [diff] [blame] | 231 | nb_calls = (TextView) inflatedView.findViewById(R.id.calls_counter); |
| 232 | nb_confs = (TextView) inflatedView.findViewById(R.id.confs_counter); |
Alexandre Lision | 6d75d06 | 2013-09-13 14:18:34 -0400 | [diff] [blame] | 233 | |
| 234 | confs_adapter = new CallListAdapter(getActivity()); |
| 235 | ((ListView) inflatedView.findViewById(R.id.confs_list)).setAdapter(confs_adapter); |
Alexandre Lision | 1a9e3b1 | 2013-09-16 11:06:07 -0400 | [diff] [blame] | 236 | |
Alexandre Lision | 6d75d06 | 2013-09-13 14:18:34 -0400 | [diff] [blame] | 237 | calls_adapter = new CallListAdapter(getActivity()); |
| 238 | ((ListView) inflatedView.findViewById(R.id.calls_list)).setAdapter(calls_adapter); |
Alexandre Lision | 1a9e3b1 | 2013-09-16 11:06:07 -0400 | [diff] [blame] | 239 | ((ListView) inflatedView.findViewById(R.id.calls_list)).setOnItemClickListener(callClickListener); |
| 240 | ((ListView) inflatedView.findViewById(R.id.confs_list)).setOnItemClickListener(callClickListener); |
alision | 43a9b36 | 2013-05-01 16:30:15 -0400 | [diff] [blame] | 241 | |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 242 | ((ListView) inflatedView.findViewById(R.id.calls_list)).setOnItemLongClickListener(mItemLongClickListener); |
| 243 | ((ListView) inflatedView.findViewById(R.id.confs_list)).setOnItemLongClickListener(mItemLongClickListener); |
| 244 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 245 | return inflatedView; |
| 246 | } |
Alexandre Lision | ebeb366 | 2013-09-17 16:20:54 -0400 | [diff] [blame] | 247 | |
| 248 | OnItemClickListener callClickListener = new OnItemClickListener() { |
Alexandre Lision | 1a9e3b1 | 2013-09-16 11:06:07 -0400 | [diff] [blame] | 249 | |
| 250 | @Override |
| 251 | public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) { |
Alexandre Lision | ebeb366 | 2013-09-17 16:20:54 -0400 | [diff] [blame] | 252 | mCallbacks.selectedCall((Conference) v.getTag()); |
Alexandre Lision | 1a9e3b1 | 2013-09-16 11:06:07 -0400 | [diff] [blame] | 253 | } |
| 254 | }; |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 255 | |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 256 | private OnItemLongClickListener mItemLongClickListener = new OnItemLongClickListener() { |
| 257 | |
| 258 | @Override |
| 259 | public boolean onItemLongClick(AdapterView<?> adptv, View view, int pos, long arg3) { |
| 260 | final Vibrator vibe = (Vibrator) view.getContext().getSystemService(Context.VIBRATOR_SERVICE); |
| 261 | vibe.vibrate(80); |
| 262 | Intent i = new Intent(); |
| 263 | Bundle b = new Bundle(); |
| 264 | b.putParcelable("conference", (Conference) adptv.getAdapter().getItem(pos)); |
| 265 | i.putExtra("bconference", b); |
| 266 | |
| 267 | DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view); |
| 268 | ClipData data = ClipData.newIntent("conference", i); |
| 269 | view.startDrag(data, shadowBuilder, view, 0); |
| 270 | return false; |
| 271 | } |
| 272 | |
| 273 | }; |
| 274 | |
Alexandre Lision | 3c6b710 | 2013-09-16 16:56:46 -0400 | [diff] [blame] | 275 | public class CallListAdapter extends BaseAdapter implements Observer { |
Alexandre Lision | 6d75d06 | 2013-09-13 14:18:34 -0400 | [diff] [blame] | 276 | |
| 277 | private ArrayList<Conference> calls; |
| 278 | |
| 279 | private Context mContext; |
| 280 | |
| 281 | public CallListAdapter(Context act) { |
| 282 | super(); |
| 283 | mContext = act; |
| 284 | calls = new ArrayList<Conference>(); |
| 285 | |
| 286 | } |
| 287 | |
Alexandre Lision | 3c6b710 | 2013-09-16 16:56:46 -0400 | [diff] [blame] | 288 | public ArrayList<Conference> getDataset() { |
| 289 | return calls; |
| 290 | } |
| 291 | |
Alexandre Lision | 6d75d06 | 2013-09-13 14:18:34 -0400 | [diff] [blame] | 292 | public void remove(Conference transfer) { |
| 293 | |
| 294 | } |
| 295 | |
Alexandre Lision | ebeb366 | 2013-09-17 16:20:54 -0400 | [diff] [blame] | 296 | public void updateDataset(ArrayList<Conference> list) { |
Alexandre Lision | 6d75d06 | 2013-09-13 14:18:34 -0400 | [diff] [blame] | 297 | calls.clear(); |
| 298 | calls.addAll(list); |
| 299 | notifyDataSetChanged(); |
| 300 | } |
| 301 | |
| 302 | @Override |
| 303 | public int getCount() { |
| 304 | return calls.size(); |
| 305 | } |
| 306 | |
| 307 | @Override |
| 308 | public Conference getItem(int position) { |
| 309 | return calls.get(position); |
| 310 | } |
| 311 | |
| 312 | @Override |
| 313 | public long getItemId(int position) { |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | @Override |
| 318 | public View getView(int position, View convertView, ViewGroup parent) { |
| 319 | if (convertView == null) |
| 320 | convertView = LayoutInflater.from(mContext).inflate(R.layout.item_calllist, null); |
| 321 | |
| 322 | Conference call = calls.get(position); |
| 323 | if (call.getParticipants().size() == 1) { |
| 324 | ((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] | 325 | |
| 326 | long duration = System.currentTimeMillis() / 1000 - (call.getParticipants().get(0).getTimestamp_start()); |
| 327 | |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 328 | ((TextView) convertView.findViewById(R.id.call_time)).setText(String.format("%d:%02d:%02d", duration / 3600, (duration % 3600) / 60, |
| 329 | (duration % 60))); |
Alexandre Lision | 6d75d06 | 2013-09-13 14:18:34 -0400 | [diff] [blame] | 330 | } else { |
Alexandre Lision | 64d6323 | 2013-10-01 09:34:56 -0400 | [diff] [blame] | 331 | // String tmp = "Conference with " + call.getParticipants().size() + " participants"; |
| 332 | ((TextView) convertView.findViewById(R.id.call_title)).setText(getString(R.string.home_conf_item, call.getParticipants().size())); |
Alexandre Lision | 6d75d06 | 2013-09-13 14:18:34 -0400 | [diff] [blame] | 333 | } |
| 334 | // ((TextView) convertView.findViewById(R.id.num_participants)).setText("" + call.getParticipants().size()); |
| 335 | ((TextView) convertView.findViewById(R.id.call_status)).setText(call.getState()); |
| 336 | |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 337 | convertView.setOnDragListener(dragListener); |
Alexandre Lision | 6d75d06 | 2013-09-13 14:18:34 -0400 | [diff] [blame] | 338 | convertView.setTag(call); |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 339 | |
Alexandre Lision | 6d75d06 | 2013-09-13 14:18:34 -0400 | [diff] [blame] | 340 | return convertView; |
| 341 | } |
| 342 | |
Alexandre Lision | 3c6b710 | 2013-09-16 16:56:46 -0400 | [diff] [blame] | 343 | @Override |
| 344 | public void update(Observable observable, Object data) { |
Alexandre Lision | ebeb366 | 2013-09-17 16:20:54 -0400 | [diff] [blame] | 345 | Log.i(TAG, "Updating views..."); |
| 346 | notifyDataSetChanged(); |
Alexandre Lision | 3c6b710 | 2013-09-16 16:56:46 -0400 | [diff] [blame] | 347 | } |
| 348 | |
Alexandre Lision | 6d75d06 | 2013-09-13 14:18:34 -0400 | [diff] [blame] | 349 | } |
| 350 | |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 351 | OnDragListener dragListener = new OnDragListener() { |
| 352 | |
| 353 | @SuppressWarnings("deprecation") |
| 354 | // deprecated in API 16.... |
| 355 | @Override |
| 356 | public boolean onDrag(View v, DragEvent event) { |
| 357 | switch (event.getAction()) { |
| 358 | case DragEvent.ACTION_DRAG_STARTED: |
| 359 | // Do nothing |
Alexandre Lision | e2839d5 | 2013-10-01 09:37:37 -0400 | [diff] [blame^] | 360 | // Log.w(TAG, "ACTION_DRAG_STARTED"); |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 361 | break; |
| 362 | case DragEvent.ACTION_DRAG_ENTERED: |
Alexandre Lision | e2839d5 | 2013-10-01 09:37:37 -0400 | [diff] [blame^] | 363 | // Log.w(TAG, "ACTION_DRAG_ENTERED"); |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 364 | v.setBackgroundColor(Color.GREEN); |
| 365 | break; |
| 366 | case DragEvent.ACTION_DRAG_EXITED: |
Alexandre Lision | e2839d5 | 2013-10-01 09:37:37 -0400 | [diff] [blame^] | 367 | // Log.w(TAG, "ACTION_DRAG_EXITED"); |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 368 | v.setBackgroundDrawable(getResources().getDrawable(R.drawable.item_call_selector)); |
| 369 | break; |
| 370 | case DragEvent.ACTION_DROP: |
Alexandre Lision | e2839d5 | 2013-10-01 09:37:37 -0400 | [diff] [blame^] | 371 | // Log.w(TAG, "ACTION_DROP"); |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 372 | View view = (View) event.getLocalState(); |
| 373 | |
| 374 | Item i = event.getClipData().getItemAt(0); |
| 375 | Intent intent = i.getIntent(); |
| 376 | intent.setExtrasClassLoader(Conference.class.getClassLoader()); |
| 377 | |
| 378 | Conference initial = (Conference) view.getTag(); |
| 379 | Conference target = (Conference) v.getTag(); |
| 380 | |
| 381 | if (initial == target) { |
| 382 | return true; |
| 383 | } |
| 384 | |
| 385 | DropActionsChoice dialog = DropActionsChoice.newInstance(); |
| 386 | Bundle b = new Bundle(); |
| 387 | b.putParcelable("call_initial", initial); |
| 388 | b.putParcelable("call_targeted", target); |
| 389 | dialog.setArguments(b); |
| 390 | dialog.setTargetFragment(HomeFragment.this, 0); |
| 391 | dialog.show(getFragmentManager(), "dialog"); |
| 392 | |
| 393 | // view.setBackgroundColor(Color.WHITE); |
| 394 | // v.setBackgroundColor(Color.BLACK); |
| 395 | break; |
| 396 | case DragEvent.ACTION_DRAG_ENDED: |
Alexandre Lision | e2839d5 | 2013-10-01 09:37:37 -0400 | [diff] [blame^] | 397 | // Log.w(TAG, "ACTION_DRAG_ENDED"); |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 398 | View view1 = (View) event.getLocalState(); |
| 399 | view1.setVisibility(View.VISIBLE); |
| 400 | v.setBackgroundDrawable(getResources().getDrawable(R.drawable.item_call_selector)); |
| 401 | default: |
| 402 | break; |
| 403 | } |
| 404 | return true; |
| 405 | } |
| 406 | |
| 407 | }; |
Alexandre Lision | e2839d5 | 2013-10-01 09:37:37 -0400 | [diff] [blame^] | 408 | |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 409 | @Override |
| 410 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 411 | super.onActivityResult(requestCode, resultCode, data); |
| 412 | Conference transfer = null; |
| 413 | if (requestCode == REQUEST_TRANSFER) { |
| 414 | switch (resultCode) { |
| 415 | case 0: |
| 416 | Conference c = data.getParcelableExtra("target"); |
| 417 | transfer = data.getParcelableExtra("transfer"); |
| 418 | try { |
| 419 | |
| 420 | mCallbacks.getService().attendedTransfer(transfer.getParticipants().get(0).getCallId(), c.getParticipants().get(0).getCallId()); |
| 421 | calls_adapter.remove(transfer); |
| 422 | calls_adapter.remove(c); |
| 423 | calls_adapter.notifyDataSetChanged(); |
| 424 | } catch (RemoteException e) { |
| 425 | // TODO Auto-generated catch block |
| 426 | e.printStackTrace(); |
| 427 | } |
Alexandre Lision | 64d6323 | 2013-10-01 09:34:56 -0400 | [diff] [blame] | 428 | Toast.makeText(getActivity(), getString(R.string.home_transfer_complet), Toast.LENGTH_LONG).show(); |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 429 | break; |
| 430 | |
| 431 | case 1: |
| 432 | String to = data.getStringExtra("to_number"); |
| 433 | transfer = data.getParcelableExtra("transfer"); |
| 434 | try { |
Alexandre Lision | 64d6323 | 2013-10-01 09:34:56 -0400 | [diff] [blame] | 435 | Toast.makeText(getActivity(), getString(R.string.home_transfering,transfer.getParticipants().get(0).getContact().getmDisplayName(),to), |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 436 | Toast.LENGTH_SHORT).show(); |
| 437 | mCallbacks.getService().transfer(transfer.getParticipants().get(0).getCallId(), to); |
| 438 | mCallbacks.getService().hangUp(transfer.getParticipants().get(0).getCallId()); |
| 439 | } catch (RemoteException e) { |
| 440 | // TODO Auto-generated catch block |
| 441 | e.printStackTrace(); |
| 442 | } |
| 443 | break; |
| 444 | |
| 445 | default: |
| 446 | break; |
| 447 | } |
| 448 | } else if (requestCode == REQUEST_CONF) { |
| 449 | switch (resultCode) { |
| 450 | case 0: |
| 451 | Conference call_to_add = data.getParcelableExtra("transfer"); |
| 452 | Conference call_target = data.getParcelableExtra("target"); |
| 453 | |
| 454 | bindCalls(call_to_add, call_target); |
| 455 | break; |
| 456 | |
| 457 | default: |
| 458 | break; |
| 459 | } |
| 460 | } |
| 461 | } |
Alexandre Lision | e2839d5 | 2013-10-01 09:37:37 -0400 | [diff] [blame^] | 462 | |
Alexandre Lision | f1850c0 | 2013-09-23 14:19:34 -0400 | [diff] [blame] | 463 | private void bindCalls(Conference call_to_add, Conference call_target) { |
| 464 | try { |
| 465 | |
| 466 | if (call_target.hasMultipleParticipants() && !call_to_add.hasMultipleParticipants()) { |
| 467 | |
| 468 | mCallbacks.getService().addParticipant(call_to_add.getParticipants().get(0), call_target.getId()); |
| 469 | |
| 470 | } else if (call_target.hasMultipleParticipants() && call_to_add.hasMultipleParticipants()) { |
| 471 | |
| 472 | // We join two conferences |
| 473 | mCallbacks.getService().joinConference(call_to_add.getId(), call_target.getId()); |
| 474 | |
| 475 | } else if (!call_target.hasMultipleParticipants() && call_to_add.hasMultipleParticipants()) { |
| 476 | |
| 477 | mCallbacks.getService().addParticipant(call_target.getParticipants().get(0), call_to_add.getId()); |
| 478 | |
| 479 | } else { |
| 480 | // We join two single calls to create a conf |
| 481 | mCallbacks.getService().joinParticipant(call_to_add.getParticipants().get(0).getCallId(), |
| 482 | call_target.getParticipants().get(0).getCallId()); |
| 483 | } |
| 484 | |
| 485 | } catch (RemoteException e) { |
| 486 | // TODO Auto-generated catch block |
| 487 | e.printStackTrace(); |
| 488 | } |
| 489 | } |
| 490 | |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 491 | } |