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
.Address
.Protocol
;
14 import ro
.ieval
.fonbot
.Utils
.OngoingEvent
;
16 import android
.app
.Notification
;
17 import android
.app
.NotificationManager
;
18 import android
.app
.PendingIntent
;
19 import android
.app
.Service
;
20 import android
.content
.Intent
;
21 import android
.content
.IntentFilter
;
22 import android
.net
.ConnectivityManager
;
23 import android
.os
.IBinder
;
24 import android
.preference
.PreferenceManager
;
25 import android
.support
.v4
.app
.NotificationCompat
;
26 import android
.support
.v4
.content
.LocalBroadcastManager
;
27 import android
.util
.Log
;
30 * Copyright © 2013 Marius Gavrilescu
32 * This file is part of FonBot.
34 * FonBot is free software: you can redistribute it and/or modify
35 * it under the terms of the GNU General Public License as published by
36 * the Free Software Foundation, either version 3 of the License, or
37 * (at your option) any later version.
39 * FonBot is distributed in the hope that it will be useful,
40 * but WITHOUT ANY WARRANTY; without even the implied warranty of
41 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 * GNU General Public License for more details.
44 * You should have received a copy of the GNU General Public License
45 * along with FonBot. If not, see <http://www.gnu.org/licenses/>.
51 * @author Marius Gavrilescu
53 public final class FonBotMainService
extends Service
{
55 * Binder for the service. It lets clients get a reference to the service.
57 * @author Marius Gavrilescu <marius@ieval.ro>
59 public final class Binder
extends android
.os
.Binder
{
61 * Get a reference to the {@link FonBotMainService}
62 * @return a reference to the {@link FonBotMainService}
64 public FonBotMainService
getService(){
65 return FonBotMainService
.this;
70 * Runnable that continously long polls the server for commands
72 * @author Marius Gavrilescu <marius@ieval.ro>
74 private final class LongPollRunnable
implements Runnable
{
76 final ConnectivityManager man
=(ConnectivityManager
) getSystemService(CONNECTIVITY_SERVICE
);
77 final HttpCallExecutableRunnable runnable
=new HttpCallExecutableRunnable("/get", null, FonBotMainService
.this, new PollResultCallback(FonBotMainService
.this), false);
79 Log
.d("LongPollRunnable", "Long polling started");
80 while(man
.getActiveNetworkInfo() != null && man
.getActiveNetworkInfo().isConnected())
83 } catch (final SocketTimeoutException e
){
84 Log
.d("LongPollRunnable", "Socket timeout, refreshing connection");
85 } catch (final Exception ex
){
89 Log
.d("LongPollRunnable", "Long polling stopped");
94 * Broadcast action: add an ongoing event
96 public static final String ACTION_PUT_ONGOING
="ro.ieval.fonbot.FonBotMainService.ACTION_PUT_ONGOING";
98 * Broadcast action: remove an ongoing event
100 public static final String ACTION_DELETE_ONGOING
="ro.ieval.fonbot.FonBotMainService.ACTION_DELETE_ONGOING";
103 * Broadcast action: process a command received via SMS
105 public static final String ACTION_PROCESS_COMMAND
="ro.ieval.fonbot.FonBotMainService.ACTION_PROCESS_COMMAND";
108 * Extra: command line
110 public static final String EXTRA_COMMAND_LINE
="ro.ieval.fonbot.FonBotMainService.EXTRA_COMMAND_LINE";
113 * Extra: SMS originating address
115 public static final String EXTRA_SMS_ORIGIN_ADDRESS
="ro.ieval.fonbot.FonBotMainService.EXTRA_SMS_ORIGIN_ADDRESS";
118 * Extra: ongoing event id
121 * @see #ACTION_DELETE_ONGOING
122 * @see #ACTION_PUT_ONGOING
124 public static final String EXTRA_ONGOING_ID
="ro.ieval.fonbot.FonBotMainService.EXTRA_ONGOING_ID";
126 * Broadcast sent when the ongoing event list is updated.
128 public static final String ACTION_ONGOING_UPDATE
="ro.ieval.fonbot.FonBotMainService.ACTION_ONGOING_UPDATE";
130 * IntentFilter for events caught by the {@link DynamicEventReceiver}
132 private static final IntentFilter DYNAMIC_BROADCAST_FILTER
=new IntentFilter();
135 DYNAMIC_BROADCAST_FILTER
.addAction(Intent
.ACTION_BATTERY_CHANGED
);
136 DYNAMIC_BROADCAST_FILTER
.addAction(Intent
.ACTION_HEADSET_PLUG
);
140 * The one instance of {@link DynamicEventReceiver}
142 private final DynamicEventReceiver receiver
=new DynamicEventReceiver();
144 * Set of ongoing events.
146 private final Set
<OngoingEvent
> ongoing
=EnumSet
.noneOf(OngoingEvent
.class);
148 /** true if running in foreground, false otherwise */
149 private boolean isForeground
= false;
151 /** Thread that runs a {@link LongPollRunnable} */
152 private Thread longPollThread
;
155 * Get the set of ongoing events.
157 * @return a set of ongoing events
159 public Set
<OngoingEvent
> getOngoingEvents(){
160 return toNonNull(Collections
.unmodifiableSet(ongoing
));
164 public @Nullable IBinder
onBind(final @Nullable Intent intent
) {
169 public void onCreate() {
171 registerReceiver(receiver
, DYNAMIC_BROADCAST_FILTER
);
175 public void onDestroy() {
177 unregisterReceiver(receiver
);
181 public int onStartCommand(final @Nullable Intent intent
, final int flags
, final int startId
) {
182 final boolean showOngoing
=PreferenceManager
.getDefaultSharedPreferences(this).getBoolean("ongoing", false);
183 boolean updateNotification
= false;
184 if(intent
!=null && intent
.getAction()==ACTION_PUT_ONGOING
&& intent
.hasExtra(EXTRA_ONGOING_ID
) && showOngoing
){
185 ongoing
.add(OngoingEvent
.values()[intent
.getIntExtra(EXTRA_ONGOING_ID
, 0)]);
186 LocalBroadcastManager
.getInstance(this).sendBroadcast(new Intent(ACTION_ONGOING_UPDATE
));
187 updateNotification
=true;
189 if(intent
!=null && intent
.getAction()==ACTION_DELETE_ONGOING
&& intent
.hasExtra(EXTRA_ONGOING_ID
) && showOngoing
){
190 ongoing
.remove(OngoingEvent
.values()[intent
.getIntExtra(EXTRA_ONGOING_ID
, 0)]);
191 LocalBroadcastManager
.getInstance(this).sendBroadcast(new Intent(ACTION_ONGOING_UPDATE
));
192 updateNotification
=true;
194 if(intent
!=null && intent
.getAction()==ACTION_PROCESS_COMMAND
) {
195 final String cmdline
= intent
.getStringExtra(EXTRA_COMMAND_LINE
);
196 final String origin
= intent
.getStringExtra(EXTRA_SMS_ORIGIN_ADDRESS
);
197 final Address address
= new Address(toNonNull(Protocol
.SMS
), origin
);//NOPMD variable depends on originAddress
198 final String
[] words
=Utils
.shellwords(toNonNull(cmdline
));
199 final String
[] args
=new String
[words
.length
-1];//NOPMD variable size depends on words.length
200 System
.arraycopy(words
, 1, args
, 0, args
.length
);
202 Utils
.processCommand(this, toNonNull(words
[0]), args
, toNonNull(address
));
205 if(longPollThread
== null || !longPollThread
.isAlive()){
206 longPollThread
= new Thread(new LongPollRunnable());
207 longPollThread
.start();
210 ExecutableRunnable
.retryTasks();
212 final boolean runForeground
=PreferenceManager
.getDefaultSharedPreferences(this).getBoolean("foreground", false);
214 stopForeground(true);
216 final NotificationManager man
=(NotificationManager
) getSystemService(NOTIFICATION_SERVICE
);
217 final boolean shouldNotify
=runForeground
||!ongoing
.isEmpty();
218 if(shouldNotify
&& (updateNotification
|| runForeground
!= isForeground
)){
219 final Intent mainIntent
=new Intent(this, FonBotMainActivity
.class);
220 mainIntent
.addFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
);
221 final NotificationCompat
.Builder builder
=new NotificationCompat
.Builder(this).
222 setContentText(getString(foreground_notification_text
)).
223 setContentTitle(getString(foreground_notification_title
)).
224 setSmallIcon(android
.R
.drawable
.stat_notify_sync_noanim
).
225 setPriority(NotificationCompat
.PRIORITY_MIN
).
226 setContentIntent(PendingIntent
.getActivity(this, 0, mainIntent
, 0)).
229 if(showOngoing
&& !ongoing
.isEmpty()) {
230 final NotificationCompat
.InboxStyle inboxBuilder
=new NotificationCompat
.InboxStyle(builder
);
232 for(final OngoingEvent event
: ongoing
)
233 inboxBuilder
.addLine(getString(event
.resource
));
236 final Notification notification
=builder
.build();
239 startForeground(1337, notification
);
241 man
.notify(1337, notification
);
246 isForeground
=runForeground
;