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
.os
.SystemClock
;
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 * Minimum time between two runs of the long polling service, in milliseconds
77 * This ensures the long polling service won't use up all your mobile data.
79 private static final int MIN_MILLIS_BETWEEN_RUNS
= 10000;
82 final ConnectivityManager man
=(ConnectivityManager
) getSystemService(CONNECTIVITY_SERVICE
);
83 final HttpCallExecutableRunnable runnable
=new HttpCallExecutableRunnable("/get", null, FonBotMainService
.this, new PollResultCallback(FonBotMainService
.this), false);
85 Log
.d("LongPollRunnable", "Long polling started");
87 while(man
.getActiveNetworkInfo() != null && man
.getActiveNetworkInfo().isConnected())
89 if(lastRun
+ MIN_MILLIS_BETWEEN_RUNS
> System
.currentTimeMillis())
90 SystemClock
.sleep(lastRun
+ MIN_MILLIS_BETWEEN_RUNS
- System
.currentTimeMillis());
93 } catch (final Exception ex
){
97 Log
.d("LongPollRunnable", "Long polling stopped");
102 * Broadcast action: add an ongoing event
104 public static final String ACTION_PUT_ONGOING
="ro.ieval.fonbot.FonBotMainService.ACTION_PUT_ONGOING";
106 * Broadcast action: remove an ongoing event
108 public static final String ACTION_DELETE_ONGOING
="ro.ieval.fonbot.FonBotMainService.ACTION_DELETE_ONGOING";
111 * Extra: ongoing event id
114 * @see #ACTION_DELETE_ONGOING
115 * @see #ACTION_PUT_ONGOING
117 public static final String EXTRA_ONGOING_ID
="ro.ieval.fonbot.FonBotMainService.EXTRA_ONGOING_ID";
119 * Broadcast sent when the ongoing event list is updated.
121 public static final String ACTION_ONGOING_UPDATE
="ro.ieval.fonbot.FonBotMainService.ACTION_ONGOING_UPDATE";
123 * IntentFilter for events caught by the {@link DynamicEventReceiver}
125 private static final IntentFilter DYNAMIC_BROADCAST_FILTER
=new IntentFilter();
128 DYNAMIC_BROADCAST_FILTER
.addAction(Intent
.ACTION_BATTERY_CHANGED
);
129 DYNAMIC_BROADCAST_FILTER
.addAction(Intent
.ACTION_HEADSET_PLUG
);
133 * The one instance of {@link DynamicEventReceiver}
135 private final DynamicEventReceiver receiver
=new DynamicEventReceiver();
137 * Set of ongoing events.
139 private final Set
<OngoingEvent
> ongoing
=EnumSet
.noneOf(OngoingEvent
.class);
141 /** true if running in foreground, false otherwise */
142 private boolean isForeground
= false;
144 /** Thread that runs a {@link LongPollRunnable} */
145 private Thread longPollThread
;
148 * Get the set of ongoing events.
150 * @return a set of ongoing events
152 public Set
<OngoingEvent
> getOngoingEvents(){
153 return toNonNull(Collections
.unmodifiableSet(ongoing
));
157 public @Nullable IBinder
onBind(final @Nullable Intent intent
) {
162 public void onCreate() {
164 registerReceiver(receiver
, DYNAMIC_BROADCAST_FILTER
);
168 public void onDestroy() {
170 unregisterReceiver(receiver
);
174 public int onStartCommand(final @Nullable Intent intent
, final int flags
, final int startId
) {
175 final boolean showOngoing
=PreferenceManager
.getDefaultSharedPreferences(this).getBoolean("ongoing", false);
176 boolean updateNotification
= false;
177 if(intent
!=null && intent
.getAction()==ACTION_PUT_ONGOING
&& intent
.hasExtra(EXTRA_ONGOING_ID
) && showOngoing
){
178 ongoing
.add(OngoingEvent
.values()[intent
.getIntExtra(EXTRA_ONGOING_ID
, 0)]);
179 LocalBroadcastManager
.getInstance(this).sendBroadcast(new Intent(ACTION_ONGOING_UPDATE
));
180 updateNotification
=true;
182 if(intent
!=null && intent
.getAction()==ACTION_DELETE_ONGOING
&& intent
.hasExtra(EXTRA_ONGOING_ID
) && showOngoing
){
183 ongoing
.remove(OngoingEvent
.values()[intent
.getIntExtra(EXTRA_ONGOING_ID
, 0)]);
184 LocalBroadcastManager
.getInstance(this).sendBroadcast(new Intent(ACTION_ONGOING_UPDATE
));
185 updateNotification
=true;
188 if(longPollThread
== null || !longPollThread
.isAlive()){
189 longPollThread
= new Thread(new LongPollRunnable());
190 longPollThread
.start();
193 final boolean runForeground
=PreferenceManager
.getDefaultSharedPreferences(this).getBoolean("foreground", false);
195 stopForeground(true);
197 final NotificationManager man
=(NotificationManager
) getSystemService(NOTIFICATION_SERVICE
);
198 final boolean shouldNotify
=runForeground
||!ongoing
.isEmpty();
199 if(shouldNotify
&& (updateNotification
|| runForeground
!= isForeground
)){
200 final Intent mainIntent
=new Intent(this, FonBotMainActivity
.class);
201 mainIntent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
202 final NotificationCompat
.Builder builder
=new NotificationCompat
.Builder(this).
203 setContentText(getString(foreground_notification_text
)).
204 setContentTitle(getString(foreground_notification_title
)).
205 setSmallIcon(android
.R
.drawable
.stat_notify_sync_noanim
).
206 setPriority(NotificationCompat
.PRIORITY_MIN
).
207 setContentIntent(PendingIntent
.getActivity(this, 0, mainIntent
, 0)).
210 if(showOngoing
&& !ongoing
.isEmpty()) {
211 final NotificationCompat
.InboxStyle inboxBuilder
=new NotificationCompat
.InboxStyle(builder
);
213 for(final OngoingEvent event
: ongoing
)
214 inboxBuilder
.addLine(getString(event
.resource
));
217 final Notification notification
=builder
.build();
220 startForeground(1337, notification
);
222 man
.notify(1337, notification
);
227 isForeground
=runForeground
;