blob: 47961cccc4379b238d06f0e45c1e1ce02dee7bdd [file] [log] [blame]
Alexandre Lision1edb1472013-10-24 14:18:05 -04001/**
2 * Copyright (C) 2010-2012 Regis Montoya (aka r3gis - www.r3gis.fr)
Alexandre Lisionc1024c02014-01-06 11:12:53 -05003 * Copyright (C) 2004-2014 Savoir-Faire Linux Inc.
Alexandre Lision1edb1472013-10-24 14:18:05 -04004 *
5 * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
6 * Adrien BĂ©raud <adrien.beraud@gmail.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *
22 * Additional permission under GNU GPL version 3 section 7:
23 *
24 * If you modify this program, or any covered work, by linking or
25 * combining it with the OpenSSL project's OpenSSL library (or a
26 * modified version of that library), containing parts covered by the
27 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
28 * grants you additional permission to convey the resulting work.
29 * Corresponding Source for a non-source form of such a combination
30 * shall include the source code for the parts of OpenSSL used as well
31 * as that of the covered work.
32 */
33
34package org.sflphone.utils;
35
Alexandre Lisionf02190d2013-12-12 17:26:12 -050036import java.util.HashMap;
37import java.util.Random;
Alexandre Lision1edb1472013-10-24 14:18:05 -040038
Alexandre Lision81ef7502013-12-11 17:29:49 -050039import org.sflphone.R;
40import org.sflphone.client.HomeActivity;
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -050041import org.sflphone.model.Conference;
Alexandre Lision1edb1472013-10-24 14:18:05 -040042import org.sflphone.model.SipCall;
43
44import android.app.Notification;
45import android.app.NotificationManager;
Alexandre Lision81ef7502013-12-11 17:29:49 -050046import android.app.PendingIntent;
Alexandre Lision1edb1472013-10-24 14:18:05 -040047import android.content.Context;
Alexandre Lision81ef7502013-12-11 17:29:49 -050048import android.content.Intent;
49import android.graphics.BitmapFactory;
Alexandre Lision1edb1472013-10-24 14:18:05 -040050import android.graphics.Typeface;
51import android.net.sip.SipProfile;
Alexandre Lision81ef7502013-12-11 17:29:49 -050052import android.support.v4.app.NotificationCompat;
Alexandre Lision1edb1472013-10-24 14:18:05 -040053import android.support.v4.app.NotificationCompat.Builder;
54import android.text.Spannable;
55import android.text.SpannableString;
56import android.text.TextUtils;
57import android.text.style.StyleSpan;
Alexandre Lision1edb1472013-10-24 14:18:05 -040058
59public class SipNotifications {
60
61 private final NotificationManager notificationManager;
62 private final Context context;
Alexandre Lision81ef7502013-12-11 17:29:49 -050063
Alexandre Lision1edb1472013-10-24 14:18:05 -040064 public static final String NOTIF_CREATION = "notif_creation";
65 public static final String NOTIF_DELETION = "notif_deletion";
66
Alexandre Lisionf02190d2013-12-12 17:26:12 -050067 private final int NOTIFICATION_ID = new Random().nextInt(1000);
68
Alexandre Lision1edb1472013-10-24 14:18:05 -040069 public static final int REGISTER_NOTIF_ID = 1;
70 public static final int CALL_NOTIF_ID = REGISTER_NOTIF_ID + 1;
71 public static final int CALLLOG_NOTIF_ID = REGISTER_NOTIF_ID + 2;
72 public static final int MESSAGE_NOTIF_ID = REGISTER_NOTIF_ID + 3;
73 public static final int VOICEMAIL_NOTIF_ID = REGISTER_NOTIF_ID + 4;
74
75 private static boolean isInit = false;
76
77 public SipNotifications(Context aContext) {
78 context = aContext;
79 notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
80
81 if (!isInit) {
82 cancelAll();
83 cancelCalls();
84 isInit = true;
85 }
Alexandre Lision1edb1472013-10-24 14:18:05 -040086
Alexandre Lision1edb1472013-10-24 14:18:05 -040087 }
Alexandre Lision1edb1472013-10-24 14:18:05 -040088
Alexandre Lision1edb1472013-10-24 14:18:05 -040089 public void onServiceCreate() {
Alexandre Lisionf02190d2013-12-12 17:26:12 -050090
Alexandre Lision1edb1472013-10-24 14:18:05 -040091 }
92
93 public void onServiceDestroy() {
94 // Make sure our notification is gone.
95 cancelAll();
96 cancelCalls();
97 }
98
Alexandre Lision1edb1472013-10-24 14:18:05 -040099 // Calls
100 public void showNotificationForCall(SipCall callInfo) {
Alexandre Lision81ef7502013-12-11 17:29:49 -0500101 // TODO
Alexandre Lision1edb1472013-10-24 14:18:05 -0400102 }
103
104 public void showNotificationForVoiceMail(SipProfile acc, int numberOfMessages) {
Alexandre Lision81ef7502013-12-11 17:29:49 -0500105 // TODO
Alexandre Lision1edb1472013-10-24 14:18:05 -0400106 }
107
Alexandre Lision1edb1472013-10-24 14:18:05 -0400108 protected static CharSequence buildTickerMessage(Context context, String address, String body) {
109 String displayAddress = address;
110
111 StringBuilder buf = new StringBuilder(displayAddress == null ? "" : displayAddress.replace('\n', ' ').replace('\r', ' '));
112 buf.append(':').append(' ');
113
114 int offset = buf.length();
115
116 if (!TextUtils.isEmpty(body)) {
117 body = body.replace('\n', ' ').replace('\r', ' ');
118 buf.append(body);
119 }
120
121 SpannableString spanText = new SpannableString(buf.toString());
122 spanText.setSpan(new StyleSpan(Typeface.BOLD), 0, offset, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
123
124 return spanText;
125 }
126
Alexandre Lision1edb1472013-10-24 14:18:05 -0400127 public final void cancelCalls() {
128 notificationManager.cancel(CALL_NOTIF_ID);
129 }
130
131 public final void cancelMissedCalls() {
132 notificationManager.cancel(CALLLOG_NOTIF_ID);
133 }
134
135 public final void cancelMessages() {
136 notificationManager.cancel(MESSAGE_NOTIF_ID);
137 }
138
139 public final void cancelVoicemails() {
140 notificationManager.cancel(VOICEMAIL_NOTIF_ID);
141 }
142
143 public final void cancelAll() {
Alexandre Lision1edb1472013-10-24 14:18:05 -0400144 cancelMessages();
145 cancelMissedCalls();
146 cancelVoicemails();
147 }
148
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500149 public void publishMissedCallNotification(Conference missedConf) {
Alexandre Lision81ef7502013-12-11 17:29:49 -0500150
151 CharSequence tickerText = context.getString(R.string.notif_missed_call_title);
152 long when = System.currentTimeMillis();
153
154 Builder nb = new NotificationCompat.Builder(context);
155 nb.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher));
156 nb.setSmallIcon(R.drawable.ic_action_call);
157
158 nb.setTicker(tickerText);
159 nb.setWhen(when);
160 nb.setContentTitle(context.getString(R.string.notif_missed_call_title));
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500161 nb.setContentText(context.getString(R.string.notif_missed_call_content, missedConf.getParticipants().get(0).getContact().getmDisplayName()));
Alexandre Lision81ef7502013-12-11 17:29:49 -0500162 Intent notificationIntent = new Intent(context, HomeActivity.class);
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500163 notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
Alexandre Lision81ef7502013-12-11 17:29:49 -0500164 PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
165
166 // notification.setLatestEventInfo(context, contentTitle,
167 // contentText, contentIntent);
168 nb.setOnlyAlertOnce(true);
169 nb.setContentIntent(contentIntent);
170
171 Notification notification = nb.build();
172 // We have to re-write content view because getNotification setLatestEventInfo implicitly
173 // notification.contentView = contentView;
174
175 // startForegroundCompat(CALL_NOTIF_ID, notification);
176 notificationManager.notify(CALL_NOTIF_ID, notification);
177 }
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500178
179 public void makeNotification(HashMap<String, SipCall> calls) {
180 if (calls.size() == 0) {
181 return;
182 }
183 Intent notificationIntent = new Intent(context, HomeActivity.class);
184 PendingIntent contentIntent = PendingIntent.getActivity(context, 007, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
185
186 NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
187 nm.cancel(NOTIFICATION_ID); // clear previous notifications.
188
189 NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
190
191 builder.setContentIntent(contentIntent).setOngoing(true).setSmallIcon(R.drawable.ic_launcher)
192 .setContentTitle(calls.size() + " ongoing calls").setTicker("Pending calls").setWhen(System.currentTimeMillis()).setAutoCancel(false);
193 builder.setPriority(NotificationCompat.PRIORITY_MAX);
194 Notification n = builder.build();
195
196 nm.notify(NOTIFICATION_ID, n);
197 }
198
199 public void removeNotification() {
200 NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
201 nm.cancel(NOTIFICATION_ID);
202 }
Alexandre Lision1edb1472013-10-24 14:18:05 -0400203}