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