blob: 903c673950656d24c366a1c12e44d74d6dec9b73 [file] [log] [blame]
alisionfde875f2013-05-28 17:01:54 -04001/*
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
alision84813a12013-05-27 17:40:39 -040032package com.savoirfairelinux.sflphone.fragments;
33
34import java.util.HashMap;
35
36import android.app.Activity;
37import android.app.Fragment;
38import android.graphics.Bitmap;
39import android.graphics.PointF;
40import android.os.Bundle;
41import android.os.RemoteException;
42import android.util.DisplayMetrics;
43import android.util.Log;
44import android.view.LayoutInflater;
45import android.view.View;
46import android.view.ViewGroup;
47import android.widget.TextView;
48
49import com.savoirfairelinux.sflphone.R;
50import com.savoirfairelinux.sflphone.adapters.ContactPictureLoader;
51import com.savoirfairelinux.sflphone.model.Attractor;
52import com.savoirfairelinux.sflphone.model.Bubble;
53import com.savoirfairelinux.sflphone.model.BubbleModel;
54import com.savoirfairelinux.sflphone.model.BubblesView;
55import com.savoirfairelinux.sflphone.model.CallContact;
56import com.savoirfairelinux.sflphone.model.SipCall;
57import com.savoirfairelinux.sflphone.service.CallManagerCallBack;
58import com.savoirfairelinux.sflphone.service.ISipService;
59
60public 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 }
alisionfde875f2013-05-28 17:01:54 -0400115
116 @Override
117 public void onDetach() {
118 super.onDetach();
119 mCallbacks = sDummyCallbacks;
120 }
alision84813a12013-05-27 17:40:39 -0400121
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
alisionfde875f2013-05-28 17:01:54 -0400131 mCall = b.getParcelable("CallInfo");
132 Log.i(TAG, "Starting fragment for call " + mCall.getCallId());
alision84813a12013-05-27 17:40:39 -0400133
134 String pendingAction = b.getString("action");
135 if (pendingAction != null && pendingAction.contentEquals("call")) {
alisionfde875f2013-05-28 17:01:54 -0400136 callContact(mCall);
alision84813a12013-05-27 17:40:39 -0400137 } else if (pendingAction.equals(CallManagerCallBack.INCOMING_CALL)) {
138 callIncoming();
139 }
140
141 return rootView;
142 }
143
alisionfde875f2013-05-28 17:01:54 -0400144 private void callContact(SipCall infos) {
alision84813a12013-05-27 17:40:39 -0400145 // TODO off-thread image loading
146 Bubble contact_bubble;
alisionfde875f2013-05-28 17:01:54 -0400147 if (infos.getContacts().get(0).getPhoto_id() > 0) {
148 Bitmap photo = ContactPictureLoader.loadContactPhoto(getActivity().getContentResolver(), infos.getContacts().get(0).getId());
alision84813a12013-05-27 17:40:39 -0400149 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
alisionfde875f2013-05-28 17:01:54 -0400163 contact_bubble.contact = infos.getContacts().get(0);
alision84813a12013-05-27 17:40:39 -0400164 model.listBubbles.add(contact_bubble);
alisionfde875f2013-05-28 17:01:54 -0400165 contacts.put(infos.getContacts().get(0), contact_bubble);
166
alision84813a12013-05-27 17:40:39 -0400167 try {
alisionfde875f2013-05-28 17:01:54 -0400168 mCallbacks.getService().placeCall(infos);
alision84813a12013-05-27 17:40:39 -0400169 } 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
alision84813a12013-05-27 17:40:39 -0400191 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
alisionfde875f2013-05-28 17:01:54 -0400226 // public void onSendMessage(String msg) {
227 // mCall.notifyServiceSendMsg(mCallbacks.getService(), msg);
228 //
229 // }
alision84813a12013-05-27 17:40:39 -0400230
231 public void changeCallState(int callState) {
alisionfde875f2013-05-28 17:01:54 -0400232
alision84813a12013-05-27 17:40:39 -0400233 mCall.setCallState(callState);
234 }
235
alision84813a12013-05-27 17:40:39 -0400236}