1 package ro
.ieval
.fonbot
;
3 import static ro
.ieval
.fonbot
.R
.string
.*;
4 import static ro
.ieval
.fonbot
.Utils
.toNonNull
;
6 import java
.util
.Collections
;
7 import java
.util
.EnumSet
;
10 import org
.eclipse
.jdt
.annotation
.Nullable
;
12 import ro
.ieval
.fonbot
.Utils
.OngoingEvent
;
14 import android
.app
.Notification
;
15 import android
.app
.NotificationManager
;
16 import android
.app
.PendingIntent
;
17 import android
.app
.Service
;
18 import android
.content
.Intent
;
19 import android
.content
.IntentFilter
;
20 import android
.net
.ConnectivityManager
;
21 import android
.os
.IBinder
;
22 import android
.preference
.PreferenceManager
;
23 import android
.support
.v4
.app
.NotificationCompat
;
24 import android
.support
.v4
.content
.LocalBroadcastManager
;
25 import android
.util
.Log
;
28 * Copyright © 2013 Marius Gavrilescu
30 * This file is part of FonBot.
32 * FonBot is free software: you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation, either version 3 of the License, or
35 * (at your option) any later version.
37 * FonBot is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 * GNU General Public License for more details.
42 * You should have received a copy of the GNU General Public License
43 * along with FonBot. If not, see <http://www.gnu.org/licenses/>.
49 * @author Marius Gavrilescu
51 public final class FonBotMainService
extends Service
{
53 * Binder for the service. It lets clients get a reference to the service.
55 * @author Marius Gavrilescu <marius@ieval.ro>
57 public final class Binder
extends android
.os
.Binder
{
59 * Get a reference to the {@link FonBotMainService}
60 * @return a reference to the {@link FonBotMainService}
62 public FonBotMainService
getService(){
63 return FonBotMainService
.this;
68 * Runnable that continously long polls the server for commands
70 * @author Marius Gavrilescu <marius@ieval.ro>
72 private final class LongPollRunnable
implements Runnable
{
74 final ConnectivityManager man
=(ConnectivityManager
) getSystemService(CONNECTIVITY_SERVICE
);
75 final HttpCallExecutableRunnable runnable
=new HttpCallExecutableRunnable("/get", null, FonBotMainService
.this, new PollResultCallback(FonBotMainService
.this), false);
77 Log
.d("LongPollRunnable", "Long polling started");
78 while(man
.getActiveNetworkInfo() != null && man
.getActiveNetworkInfo().isConnected())
81 } catch (final Exception ex
){
84 Log
.d("LongPollRunnable", "Long polling stopped");
89 * Broadcast action: add an ongoing event
91 public static final String ACTION_PUT_ONGOING
="ro.ieval.fonbot.FonBotMainService.ACTION_PUT_ONGOING";
93 * Broadcast action: remove an ongoing event
95 public static final String ACTION_DELETE_ONGOING
="ro.ieval.fonbot.FonBotMainService.ACTION_DELETE_ONGOING";
96 /** Broadcast action: trigger a server poll */
97 public static final String ACTION_TRIGGER_POLL
="ro.ieval.fonbot.FonBotMainService.ACTION_TRIGGER_POLL";
99 * Extra: ongoing event id
102 * @see #ACTION_DELETE_ONGOING
103 * @see #ACTION_PUT_ONGOING
105 public static final String EXTRA_ONGOING_ID
="ro.ieval.fonbot.FonBotMainService.EXTRA_ONGOING_ID";
107 * Broadcast sent when the ongoing event list is updated.
109 public static final String ACTION_ONGOING_UPDATE
="ro.ieval.fonbot.FonBotMainService.ACTION_ONGOING_UPDATE";
111 * IntentFilter for events caught by the {@link DynamicEventReceiver}
113 private static final IntentFilter DYNAMIC_BROADCAST_FILTER
=new IntentFilter();
116 DYNAMIC_BROADCAST_FILTER
.addAction(Intent
.ACTION_BATTERY_CHANGED
);
117 DYNAMIC_BROADCAST_FILTER
.addAction(Intent
.ACTION_HEADSET_PLUG
);
121 * The one instance of {@link DynamicEventReceiver}
123 private final DynamicEventReceiver receiver
=new DynamicEventReceiver();
125 * Set of ongoing events.
127 private final Set
<OngoingEvent
> ongoing
=EnumSet
.noneOf(OngoingEvent
.class);
129 /** true if running in foreground, false otherwise */
130 private boolean isForeground
= false;
132 /** Thread that runs a {@link LongPollRunnable} */
133 private Thread longPollThread
;
136 * Get the set of ongoing events.
138 * @return a set of ongoing events
140 public Set
<OngoingEvent
> getOngoingEvents(){
141 return toNonNull(Collections
.unmodifiableSet(ongoing
));
145 public @Nullable IBinder
onBind(final @Nullable Intent intent
) {
150 public void onCreate() {
152 registerReceiver(receiver
, DYNAMIC_BROADCAST_FILTER
);
156 public void onDestroy() {
158 unregisterReceiver(receiver
);
162 public int onStartCommand(final @Nullable Intent intent
, final int flags
, final int startId
) {
163 boolean updateNotification
= false;
164 if(intent
!=null && intent
.getAction()==ACTION_PUT_ONGOING
&& intent
.hasExtra(EXTRA_ONGOING_ID
)){
165 ongoing
.add(OngoingEvent
.values()[intent
.getIntExtra(EXTRA_ONGOING_ID
, 0)]);
166 LocalBroadcastManager
.getInstance(this).sendBroadcast(new Intent(ACTION_ONGOING_UPDATE
));
167 updateNotification
=true;
169 if(intent
!=null && intent
.getAction()==ACTION_DELETE_ONGOING
&& intent
.hasExtra(EXTRA_ONGOING_ID
)){
170 ongoing
.remove(OngoingEvent
.values()[intent
.getIntExtra(EXTRA_ONGOING_ID
, 0)]);
171 LocalBroadcastManager
.getInstance(this).sendBroadcast(new Intent(ACTION_ONGOING_UPDATE
));
172 updateNotification
=true;
175 if(longPollThread
== null || !longPollThread
.isAlive()){
176 longPollThread
= new Thread(new LongPollRunnable());
177 longPollThread
.start();
180 final boolean runForeground
=PreferenceManager
.getDefaultSharedPreferences(this).getBoolean("foreground", false);
182 stopForeground(true);
184 final NotificationManager man
=(NotificationManager
) getSystemService(NOTIFICATION_SERVICE
);
185 final boolean shouldNotify
=runForeground
||!ongoing
.isEmpty();
186 if(shouldNotify
&& (updateNotification
|| runForeground
!= isForeground
)){
187 final Intent mainIntent
=new Intent(this, FonBotMainActivity
.class);
188 mainIntent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
189 final NotificationCompat
.Builder builder
=new NotificationCompat
.Builder(this).
190 setContentText(getString(foreground_notification_text
)).
191 setContentTitle(getString(foreground_notification_title
)).
192 setSmallIcon(android
.R
.drawable
.stat_notify_sync_noanim
).
193 setPriority(NotificationCompat
.PRIORITY_MIN
).
194 setContentIntent(PendingIntent
.getActivity(this, 0, mainIntent
, 0)).
197 if(!ongoing
.isEmpty()) {
198 final NotificationCompat
.InboxStyle inboxBuilder
=new NotificationCompat
.InboxStyle(builder
);
200 for(final OngoingEvent event
: ongoing
)
201 inboxBuilder
.addLine(getString(event
.resource
));
204 final Notification notification
=builder
.build();
207 startForeground(1337, notification
);
209 man
.notify(1337, notification
);
214 isForeground
=runForeground
;