blob: 984d9a130832055b6d0e698a9d94a97856595f3e [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
alision85992112013-05-29 12:18:08 -040089
90
alision84813a12013-05-27 17:40:39 -040091 @Override
alision85992112013-05-29 12:18:08 -040092 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) {
alision84813a12013-05-27 17:40:39 -0400102 // TODO Auto-generated method stub
alision85992112013-05-29 12:18:08 -0400103
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
alision84813a12013-05-27 17:40:39 -0400140 }
141 };
142
143 /**
144 * The Activity calling this fragment has to implement this interface
145 *
146 */
147 public interface Callbacks {
alision84813a12013-05-27 17:40:39 -0400148
alision85992112013-05-29 12:18:08 -0400149 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);
alision84813a12013-05-27 17:40:39 -0400166 }
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 }
alision85992112013-05-29 12:18:08 -0400178
alisionfde875f2013-05-28 17:01:54 -0400179 @Override
180 public void onDetach() {
181 super.onDetach();
182 mCallbacks = sDummyCallbacks;
183 }
alision84813a12013-05-27 17:40:39 -0400184
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
alisionfde875f2013-05-28 17:01:54 -0400194 mCall = b.getParcelable("CallInfo");
195 Log.i(TAG, "Starting fragment for call " + mCall.getCallId());
alision84813a12013-05-27 17:40:39 -0400196
alision85992112013-05-29 12:18:08 -0400197 mCall.printCallInfo();
alision84813a12013-05-27 17:40:39 -0400198 String pendingAction = b.getString("action");
199 if (pendingAction != null && pendingAction.contentEquals("call")) {
alisionfde875f2013-05-28 17:01:54 -0400200 callContact(mCall);
alision85992112013-05-29 12:18:08 -0400201 } else if (pendingAction.contentEquals(CallManagerCallBack.INCOMING_CALL)) {
alision84813a12013-05-27 17:40:39 -0400202 callIncoming();
203 }
204
205 return rootView;
206 }
207
alisionfde875f2013-05-28 17:01:54 -0400208 private void callContact(SipCall infos) {
alision84813a12013-05-27 17:40:39 -0400209 // TODO off-thread image loading
210 Bubble contact_bubble;
alisionfde875f2013-05-28 17:01:54 -0400211 if (infos.getContacts().get(0).getPhoto_id() > 0) {
212 Bitmap photo = ContactPictureLoader.loadContactPhoto(getActivity().getContentResolver(), infos.getContacts().get(0).getId());
alision84813a12013-05-27 17:40:39 -0400213 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 ! ");
alision85992112013-05-29 12:18:08 -0400223 mCallbacks.onCallEnded(mCall);
alision84813a12013-05-27 17:40:39 -0400224 }
225 }));
226
alisionfde875f2013-05-28 17:01:54 -0400227 contact_bubble.contact = infos.getContacts().get(0);
alision84813a12013-05-27 17:40:39 -0400228 model.listBubbles.add(contact_bubble);
alisionfde875f2013-05-28 17:01:54 -0400229 contacts.put(infos.getContacts().get(0), contact_bubble);
230
alision85992112013-05-29 12:18:08 -0400231 mCallbacks.callContact(infos);
232
alision84813a12013-05-27 17:40:39 -0400233 }
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) {
alision85992112013-05-29 12:18:08 -0400240 mCallbacks.onCallAccepted(mCall);
alision84813a12013-05-27 17:40:39 -0400241 }
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) {
alision85992112013-05-29 12:18:08 -0400246 mCallbacks.onCallRejected(mCall);
alision84813a12013-05-27 17:40:39 -0400247 }
248 }));
249
250 }
251
alision84813a12013-05-27 17:40:39 -0400252 public void changeCallState(int callState) {
alisionfde875f2013-05-28 17:01:54 -0400253
alision84813a12013-05-27 17:40:39 -0400254 mCall.setCallState(callState);
255 }
256
alision84813a12013-05-27 17:40:39 -0400257}