1 package ro
.ieval
.fonbot
;
3 import static ro
.ieval
.fonbot
.R
.string
.*;
4 import static ro
.ieval
.fonbot
.Utils
.toNonNull
;
6 import java
.net
.SocketTimeoutException
;
7 import java
.util
.Collections
;
8 import java
.util
.EnumSet
;
11 import org
.eclipse
.jdt
.annotation
.Nullable
;
13 import ro
.ieval
.fonbot
.Utils
.OngoingEvent
;
15 import android
.app
.Notification
;
16 import android
.app
.NotificationManager
;
17 import android
.app
.PendingIntent
;
18 import android
.app
.Service
;
19 import android
.content
.Intent
;
20 import android
.content
.IntentFilter
;
21 import android
.net
.ConnectivityManager
;
22 import android
.os
.IBinder
;
23 import android
.preference
.PreferenceManager
;
24 import android
.support
.v4
.app
.NotificationCompat
;
25 import android
.support
.v4
.content
.LocalBroadcastManager
;
26 import android
.util
.Log
;
29 * Copyright © 2013 Marius Gavrilescu
31 * This file is part of FonBot.
33 * FonBot is free software: you can redistribute it and/or modify
34 * it under the terms of the GNU General Public License as published by
35 * the Free Software Foundation, either version 3 of the License, or
36 * (at your option) any later version.
38 * FonBot is distributed in the hope that it will be useful,
39 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 * GNU General Public License for more details.
43 * You should have received a copy of the GNU General Public License
44 * along with FonBot. If not, see <http://www.gnu.org/licenses/>.
50 * @author Marius Gavrilescu
52 public final class FonBotMainService
extends Service
{
54 * Binder for the service. It lets clients get a reference to the service.
56 * @author Marius Gavrilescu <marius@ieval.ro>
58 public final class Binder
extends android
.os
.Binder
{
60 * Get a reference to the {@link FonBotMainService}
61 * @return a reference to the {@link FonBotMainService}
63 public FonBotMainService
getService(){
64 return FonBotMainService
.this;
69 * Runnable that continously long polls the server for commands
71 * @author Marius Gavrilescu <marius@ieval.ro>
73 private final class LongPollRunnable
implements Runnable
{
75 final ConnectivityManager man
=(ConnectivityManager
) getSystemService(CONNECTIVITY_SERVICE
);
76 final HttpCallExecutableRunnable runnable
=new HttpCallExecutableRunnable("/get", null, FonBotMainService
.this, new PollResultCallback(FonBotMainService
.this), false);
78 Log
.d("LongPollRunnable", "Long polling started");
79 while(man
.getActiveNetworkInfo() != null && man
.getActiveNetworkInfo().isConnected())
82 } catch (final SocketTimeoutException e
){
83 Log
.d("LongPollRunnable", "Socket timeout, refreshing connection");
84 } catch (final Exception ex
){
88 Log
.d("LongPollRunnable", "Long polling stopped");
93 * Broadcast action: add an ongoing event
95 public static final String ACTION_PUT_ONGOING
="ro.ieval.fonbot.FonBotMainService.ACTION_PUT_ONGOING";
97 * Broadcast action: remove an ongoing event
99 public static final String ACTION_DELETE_ONGOING
="ro.ieval.fonbot.FonBotMainService.ACTION_DELETE_ONGOING";
102 * Extra: ongoing event id
105 * @see #ACTION_DELETE_ONGOING
106 * @see #ACTION_PUT_ONGOING
108 public static final String EXTRA_ONGOING_ID
="ro.ieval.fonbot.FonBotMainService.EXTRA_ONGOING_ID";
110 * Broadcast sent when the ongoing event list is updated.
112 public static final String ACTION_ONGOING_UPDATE
="ro.ieval.fonbot.FonBotMainService.ACTION_ONGOING_UPDATE";
114 * IntentFilter for events caught by the {@link DynamicEventReceiver}
116 private static final IntentFilter DYNAMIC_BROADCAST_FILTER
=new IntentFilter();
119 DYNAMIC_BROADCAST_FILTER
.addAction(Intent
.ACTION_BATTERY_CHANGED
);
120 DYNAMIC_BROADCAST_FILTER
.addAction(Intent
.ACTION_HEADSET_PLUG
);
124 * The one instance of {@link DynamicEventReceiver}
126 private final DynamicEventReceiver receiver
=new DynamicEventReceiver();
128 * Set of ongoing events.
130 private final Set
<OngoingEvent
> ongoing
=EnumSet
.noneOf(OngoingEvent
.class);
132 /** true if running in foreground, false otherwise */
133 private boolean isForeground
= false;
135 /** Thread that runs a {@link LongPollRunnable} */
136 private Thread longPollThread
;
139 * Get the set of ongoing events.
141 * @return a set of ongoing events
143 public Set
<OngoingEvent
> getOngoingEvents(){
144 return toNonNull(Collections
.unmodifiableSet(ongoing
));
148 public @Nullable IBinder
onBind(final @Nullable Intent intent
) {
153 public void onCreate() {
155 registerReceiver(receiver
, DYNAMIC_BROADCAST_FILTER
);
159 public void onDestroy() {
161 unregisterReceiver(receiver
);
165 public int onStartCommand(final @Nullable Intent intent
, final int flags
, final int startId
) {
166 final boolean showOngoing
=PreferenceManager
.getDefaultSharedPreferences(this).getBoolean("ongoing", false);
167 boolean updateNotification
= false;
168 if(intent
!=null && intent
.getAction()==ACTION_PUT_ONGOING
&& intent
.hasExtra(EXTRA_ONGOING_ID
) && showOngoing
){
169 ongoing
.add(OngoingEvent
.values()[intent
.getIntExtra(EXTRA_ONGOING_ID
, 0)]);
170 LocalBroadcastManager
.getInstance(this).sendBroadcast(new Intent(ACTION_ONGOING_UPDATE
));
171 updateNotification
=true;
173 if(intent
!=null && intent
.getAction()==ACTION_DELETE_ONGOING
&& intent
.hasExtra(EXTRA_ONGOING_ID
) && showOngoing
){
174 ongoing
.remove(OngoingEvent
.values()[intent
.getIntExtra(EXTRA_ONGOING_ID
, 0)]);
175 LocalBroadcastManager
.getInstance(this).sendBroadcast(new Intent(ACTION_ONGOING_UPDATE
));
176 updateNotification
=true;
179 if(longPollThread
== null || !longPollThread
.isAlive()){
180 longPollThread
= new Thread(new LongPollRunnable());
181 longPollThread
.start();
184 ExecutableRunnable
.retryTasks();
186 final boolean runForeground
=PreferenceManager
.getDefaultSharedPreferences(this).getBoolean("foreground", false);
188 stopForeground(true);
190 final NotificationManager man
=(NotificationManager
) getSystemService(NOTIFICATION_SERVICE
);
191 final boolean shouldNotify
=runForeground
||!ongoing
.isEmpty();
192 if(shouldNotify
&& (updateNotification
|| runForeground
!= isForeground
)){
193 final Intent mainIntent
=new Intent(this, FonBotMainActivity
.class);
194 mainIntent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
195 final NotificationCompat
.Builder builder
=new NotificationCompat
.Builder(this).
196 setContentText(getString(foreground_notification_text
)).
197 setContentTitle(getString(foreground_notification_title
)).
198 setSmallIcon(android
.R
.drawable
.stat_notify_sync_noanim
).
199 setPriority(NotificationCompat
.PRIORITY_MIN
).
200 setContentIntent(PendingIntent
.getActivity(this, 0, mainIntent
, 0)).
203 if(showOngoing
&& !ongoing
.isEmpty()) {
204 final NotificationCompat
.InboxStyle inboxBuilder
=new NotificationCompat
.InboxStyle(builder
);
206 for(final OngoingEvent event
: ongoing
)
207 inboxBuilder
.addLine(getString(event
.resource
));
210 final Notification notification
=builder
.build();
213 startForeground(1337, notification
);
215 man
.notify(1337, notification
);
220 isForeground
=runForeground
;