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 | |
| 89 | @Override |
| 90 | public ISipService getService() { |
| 91 | // TODO Auto-generated method stub |
| 92 | return null; |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | /** |
| 97 | * The Activity calling this fragment has to implement this interface |
| 98 | * |
| 99 | */ |
| 100 | public interface Callbacks { |
| 101 | public ISipService getService(); |
| 102 | |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void onAttach(Activity activity) { |
| 107 | super.onAttach(activity); |
| 108 | |
| 109 | if (!(activity instanceof Callbacks)) { |
| 110 | throw new IllegalStateException("Activity must implement fragment's callbacks."); |
| 111 | } |
| 112 | |
| 113 | mCallbacks = (Callbacks) activity; |
| 114 | } |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame^] | 115 | |
| 116 | @Override |
| 117 | public void onDetach() { |
| 118 | super.onDetach(); |
| 119 | mCallbacks = sDummyCallbacks; |
| 120 | } |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 121 | |
| 122 | @Override |
| 123 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 124 | ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.frag_call, container, false); |
| 125 | |
| 126 | view = (BubblesView) rootView.findViewById(R.id.main_view); |
| 127 | view.setModel(model); |
| 128 | |
| 129 | Bundle b = getArguments(); |
| 130 | |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame^] | 131 | mCall = b.getParcelable("CallInfo"); |
| 132 | Log.i(TAG, "Starting fragment for call " + mCall.getCallId()); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 133 | |
| 134 | String pendingAction = b.getString("action"); |
| 135 | if (pendingAction != null && pendingAction.contentEquals("call")) { |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame^] | 136 | callContact(mCall); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 137 | } else if (pendingAction.equals(CallManagerCallBack.INCOMING_CALL)) { |
| 138 | callIncoming(); |
| 139 | } |
| 140 | |
| 141 | return rootView; |
| 142 | } |
| 143 | |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame^] | 144 | private void callContact(SipCall infos) { |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 145 | // TODO off-thread image loading |
| 146 | Bubble contact_bubble; |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame^] | 147 | if (infos.getContacts().get(0).getPhoto_id() > 0) { |
| 148 | Bitmap photo = ContactPictureLoader.loadContactPhoto(getActivity().getContentResolver(), infos.getContacts().get(0).getId()); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 149 | contact_bubble = new Bubble(getActivity(), screenCenter.x, screenCenter.y, 150, photo); |
| 150 | } else { |
| 151 | contact_bubble = new Bubble(getActivity(), screenCenter.x, screenCenter.y, 150, R.drawable.ic_contact_picture); |
| 152 | } |
| 153 | |
| 154 | model.attractors.clear(); |
| 155 | model.attractors.add(new Attractor(new PointF(metrics.widthPixels / 2, metrics.heightPixels * .8f), new Attractor.Callback() { |
| 156 | @Override |
| 157 | public void onBubbleSucked(Bubble b) { |
| 158 | Log.w(TAG, "Bubble sucked ! "); |
| 159 | onCallEnded(); |
| 160 | } |
| 161 | })); |
| 162 | |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame^] | 163 | contact_bubble.contact = infos.getContacts().get(0); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 164 | model.listBubbles.add(contact_bubble); |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame^] | 165 | contacts.put(infos.getContacts().get(0), contact_bubble); |
| 166 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 167 | try { |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame^] | 168 | mCallbacks.getService().placeCall(infos); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 169 | } catch (RemoteException e) { |
| 170 | Log.e(TAG, e.toString()); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | private void callIncoming() { |
| 175 | model.attractors.clear(); |
| 176 | model.attractors.add(new Attractor(new PointF(3 * metrics.widthPixels / 4, metrics.heightPixels / 4), new Attractor.Callback() { |
| 177 | @Override |
| 178 | public void onBubbleSucked(Bubble b) { |
| 179 | onCallAccepted(); |
| 180 | } |
| 181 | })); |
| 182 | model.attractors.add(new Attractor(new PointF(metrics.widthPixels / 4, metrics.heightPixels / 4), new Attractor.Callback() { |
| 183 | @Override |
| 184 | public void onBubbleSucked(Bubble b) { |
| 185 | onCallRejected(); |
| 186 | } |
| 187 | })); |
| 188 | |
| 189 | } |
| 190 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 191 | public void onCallAccepted() { |
| 192 | |
| 193 | mCall.notifyServiceAnswer(mCallbacks.getService()); |
| 194 | } |
| 195 | |
| 196 | public void onCallRejected() { |
| 197 | if (mCall.notifyServiceHangup(mCallbacks.getService())) |
| 198 | ; |
| 199 | |
| 200 | } |
| 201 | |
| 202 | public void onCallEnded() { |
| 203 | if (mCall.notifyServiceHangup(mCallbacks.getService())) |
| 204 | ; |
| 205 | |
| 206 | } |
| 207 | |
| 208 | public void onCallSuspended() { |
| 209 | mCall.notifyServiceHold(mCallbacks.getService()); |
| 210 | } |
| 211 | |
| 212 | public void onCallResumed() { |
| 213 | mCall.notifyServiceUnhold(mCallbacks.getService()); |
| 214 | } |
| 215 | |
| 216 | public void onCalltransfered(String to) { |
| 217 | mCall.notifyServiceTransfer(mCallbacks.getService(), to); |
| 218 | |
| 219 | } |
| 220 | |
| 221 | public void onRecordCall() { |
| 222 | mCall.notifyServiceRecord(mCallbacks.getService()); |
| 223 | |
| 224 | } |
| 225 | |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame^] | 226 | // public void onSendMessage(String msg) { |
| 227 | // mCall.notifyServiceSendMsg(mCallbacks.getService(), msg); |
| 228 | // |
| 229 | // } |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 230 | |
| 231 | public void changeCallState(int callState) { |
alision | fde875f | 2013-05-28 17:01:54 -0400 | [diff] [blame^] | 232 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 233 | mCall.setCallState(callState); |
| 234 | } |
| 235 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 236 | } |