alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2012 Savoir-Faire Linux Inc. |
| 3 | * |
| 4 | * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | * |
| 20 | * Additional permission under GNU GPL version 3 section 7: |
| 21 | * |
| 22 | * If you modify this program, or any covered work, by linking or |
| 23 | * combining it with the OpenSSL project's OpenSSL library (or a |
| 24 | * modified version of that library), containing parts covered by the |
| 25 | * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc. |
| 26 | * grants you additional permission to convey the resulting work. |
| 27 | * Corresponding Source for a non-source form of such a combination |
| 28 | * shall include the source code for the parts of OpenSSL used as well |
| 29 | * as that of the covered work. |
| 30 | */ |
| 31 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 32 | package com.savoirfairelinux.sflphone.fragments; |
| 33 | |
| 34 | import java.util.HashMap; |
| 35 | |
| 36 | import android.app.Activity; |
| 37 | import android.app.Fragment; |
| 38 | import android.graphics.Bitmap; |
| 39 | import android.graphics.PointF; |
| 40 | import android.os.Bundle; |
| 41 | import android.os.RemoteException; |
| 42 | import android.util.DisplayMetrics; |
| 43 | import android.util.Log; |
| 44 | import android.view.LayoutInflater; |
| 45 | import android.view.View; |
| 46 | import android.view.ViewGroup; |
| 47 | import android.widget.TextView; |
| 48 | |
| 49 | import com.savoirfairelinux.sflphone.R; |
| 50 | import com.savoirfairelinux.sflphone.adapters.ContactPictureLoader; |
| 51 | import com.savoirfairelinux.sflphone.model.Attractor; |
| 52 | import com.savoirfairelinux.sflphone.model.Bubble; |
| 53 | import com.savoirfairelinux.sflphone.model.BubbleModel; |
| 54 | import com.savoirfairelinux.sflphone.model.BubblesView; |
| 55 | import com.savoirfairelinux.sflphone.model.CallContact; |
| 56 | import com.savoirfairelinux.sflphone.model.SipCall; |
| 57 | import com.savoirfairelinux.sflphone.service.CallManagerCallBack; |
| 58 | import com.savoirfairelinux.sflphone.service.ISipService; |
| 59 | |
| 60 | public class CallFragment extends Fragment { |
| 61 | static final String TAG = "CallFragment"; |
| 62 | |
| 63 | private SipCall mCall; |
| 64 | |
| 65 | private BubblesView view; |
| 66 | private BubbleModel model; |
| 67 | private PointF screenCenter; |
| 68 | private DisplayMetrics metrics; |
| 69 | |
| 70 | private Callbacks mCallbacks = sDummyCallbacks; |
| 71 | |
| 72 | private HashMap<CallContact, Bubble> contacts = new HashMap<CallContact, Bubble>(); |
| 73 | |
| 74 | private TextView contact_name_txt; |
| 75 | |
| 76 | @Override |
| 77 | public void onCreate(Bundle savedBundle) { |
| 78 | super.onCreate(savedBundle); |
| 79 | model = new BubbleModel(getResources().getDisplayMetrics().density); |
| 80 | metrics = getResources().getDisplayMetrics(); |
| 81 | screenCenter = new PointF(metrics.widthPixels / 2, metrics.heightPixels / 3); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity. |
| 86 | */ |
| 87 | private static Callbacks sDummyCallbacks = new Callbacks() { |
| 88 | |
alision | 8599211 | 2013-05-29 12:18:08 -0400 | [diff] [blame^] | 89 | |
| 90 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 91 | @Override |
alision | 8599211 | 2013-05-29 12:18:08 -0400 | [diff] [blame^] | 92 | public void onSendMessage(SipCall call, String msg) { |
| 93 | |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void callContact(SipCall call) { |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void onCallAccepted(SipCall call) { |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 102 | // TODO Auto-generated method stub |
alision | 8599211 | 2013-05-29 12:18:08 -0400 | [diff] [blame^] | 103 | |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public void onCallRejected(SipCall call) { |
| 108 | // TODO Auto-generated method stub |
| 109 | |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public void onCallEnded(SipCall call) { |
| 114 | // TODO Auto-generated method stub |
| 115 | |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public void onCallSuspended(SipCall call) { |
| 120 | // TODO Auto-generated method stub |
| 121 | |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void onCallResumed(SipCall call) { |
| 126 | // TODO Auto-generated method stub |
| 127 | |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void onCalltransfered(SipCall call, String to) { |
| 132 | // TODO Auto-generated method stub |
| 133 | |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public void onRecordCall(SipCall call) { |
| 138 | // TODO Auto-generated method stub |
| 139 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 140 | } |
| 141 | }; |
| 142 | |
| 143 | /** |
| 144 | * The Activity calling this fragment has to implement this interface |
| 145 | * |
| 146 | */ |
| 147 | public interface Callbacks { |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 148 | |
alision | 8599211 | 2013-05-29 12:18:08 -0400 | [diff] [blame^] | 149 | public void callContact(SipCall call); |
| 150 | |
| 151 | public void onCallAccepted(SipCall call); |
| 152 | |
| 153 | public void onCallRejected(SipCall call); |
| 154 | |
| 155 | public void onCallEnded(SipCall call); |
| 156 | |
| 157 | public void onCallSuspended(SipCall call); |
| 158 | |
| 159 | public void onCallResumed(SipCall call); |
| 160 | |
| 161 | public void onCalltransfered(SipCall call, String to); |
| 162 | |
| 163 | public void onRecordCall(SipCall call); |
| 164 | |
| 165 | public void onSendMessage(SipCall call, String msg); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public void onAttach(Activity activity) { |
| 170 | super.onAttach(activity); |
| 171 | |
| 172 | if (!(activity instanceof Callbacks)) { |
| 173 | throw new IllegalStateException("Activity must implement fragment's callbacks."); |
| 174 | } |
| 175 | |
| 176 | mCallbacks = (Callbacks) activity; |
| 177 | } |
alision | 8599211 | 2013-05-29 12:18:08 -0400 | [diff] [blame^] | 178 | |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 179 | @Override |
| 180 | public void onDetach() { |
| 181 | super.onDetach(); |
| 182 | mCallbacks = sDummyCallbacks; |
| 183 | } |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 184 | |
| 185 | @Override |
| 186 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 187 | ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.frag_call, container, false); |
| 188 | |
| 189 | view = (BubblesView) rootView.findViewById(R.id.main_view); |
| 190 | view.setModel(model); |
| 191 | |
| 192 | Bundle b = getArguments(); |
| 193 | |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 194 | mCall = b.getParcelable("CallInfo"); |
| 195 | Log.i(TAG, "Starting fragment for call " + mCall.getCallId()); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 196 | |
alision | 8599211 | 2013-05-29 12:18:08 -0400 | [diff] [blame^] | 197 | mCall.printCallInfo(); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 198 | String pendingAction = b.getString("action"); |
| 199 | if (pendingAction != null && pendingAction.contentEquals("call")) { |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 200 | callContact(mCall); |
alision | 8599211 | 2013-05-29 12:18:08 -0400 | [diff] [blame^] | 201 | } else if (pendingAction.contentEquals(CallManagerCallBack.INCOMING_CALL)) { |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 202 | callIncoming(); |
| 203 | } |
| 204 | |
| 205 | return rootView; |
| 206 | } |
| 207 | |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 208 | private void callContact(SipCall infos) { |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 209 | // TODO off-thread image loading |
| 210 | Bubble contact_bubble; |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 211 | if (infos.getContacts().get(0).getPhoto_id() > 0) { |
| 212 | Bitmap photo = ContactPictureLoader.loadContactPhoto(getActivity().getContentResolver(), infos.getContacts().get(0).getId()); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 213 | contact_bubble = new Bubble(getActivity(), screenCenter.x, screenCenter.y, 150, photo); |
| 214 | } else { |
| 215 | contact_bubble = new Bubble(getActivity(), screenCenter.x, screenCenter.y, 150, R.drawable.ic_contact_picture); |
| 216 | } |
| 217 | |
| 218 | model.attractors.clear(); |
| 219 | model.attractors.add(new Attractor(new PointF(metrics.widthPixels / 2, metrics.heightPixels * .8f), new Attractor.Callback() { |
| 220 | @Override |
| 221 | public void onBubbleSucked(Bubble b) { |
| 222 | Log.w(TAG, "Bubble sucked ! "); |
alision | 8599211 | 2013-05-29 12:18:08 -0400 | [diff] [blame^] | 223 | mCallbacks.onCallEnded(mCall); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 224 | } |
| 225 | })); |
| 226 | |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 227 | contact_bubble.contact = infos.getContacts().get(0); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 228 | model.listBubbles.add(contact_bubble); |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 229 | contacts.put(infos.getContacts().get(0), contact_bubble); |
| 230 | |
alision | 8599211 | 2013-05-29 12:18:08 -0400 | [diff] [blame^] | 231 | mCallbacks.callContact(infos); |
| 232 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | private void callIncoming() { |
| 236 | model.attractors.clear(); |
| 237 | model.attractors.add(new Attractor(new PointF(3 * metrics.widthPixels / 4, metrics.heightPixels / 4), new Attractor.Callback() { |
| 238 | @Override |
| 239 | public void onBubbleSucked(Bubble b) { |
alision | 8599211 | 2013-05-29 12:18:08 -0400 | [diff] [blame^] | 240 | mCallbacks.onCallAccepted(mCall); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 241 | } |
| 242 | })); |
| 243 | model.attractors.add(new Attractor(new PointF(metrics.widthPixels / 4, metrics.heightPixels / 4), new Attractor.Callback() { |
| 244 | @Override |
| 245 | public void onBubbleSucked(Bubble b) { |
alision | 8599211 | 2013-05-29 12:18:08 -0400 | [diff] [blame^] | 246 | mCallbacks.onCallRejected(mCall); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 247 | } |
| 248 | })); |
| 249 | |
| 250 | } |
| 251 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 252 | public void changeCallState(int callState) { |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame] | 253 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 254 | mCall.setCallState(callState); |
| 255 | } |
| 256 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 257 | } |